#include "android_webview/nonembedded/component_updater/registration.h"
#include <memory>
#include "android_webview/common/aw_switches.h"
#include "android_webview/nonembedded/component_updater/aw_component_installer_policy_shim.h"
#include "android_webview/nonembedded/component_updater/installer_policies/aw_package_names_allowlist_component_installer_policy.h"
#include "base/barrier_closure.h"
#include "base/command_line.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "components/component_updater/component_installer.h"
#include "components/component_updater/component_updater_service.h"
#include "components/component_updater/installer_policies/origin_trials_component_installer.h"
#include "components/component_updater/installer_policies/trust_token_key_commitments_component_installer_policy.h"
#include "components/update_client/update_client.h"
namespace android_webview {
namespace {
constexpr int kNumWebViewComponents = 2;
void RegisterComponentInstallerPolicyShim(
std::unique_ptr<component_updater::ComponentInstallerPolicy> policy,
base::OnceCallback<bool(const component_updater::ComponentRegistration&)>
register_callback,
base::OnceClosure registration_finished) {
base::MakeRefCounted<component_updater::ComponentInstaller>(
std::make_unique<AwComponentInstallerPolicyShim>(std::move(policy)))
->Register(std::move(register_callback),
std::move(registration_finished));
}
}
void RegisterComponentsForUpdate(
base::RepeatingCallback<bool(
const component_updater::ComponentRegistration&)> register_callback,
base::OnceClosure on_finished) {
int num_webview_components = kNumWebViewComponents;
bool trust_tokens_component_enabled =
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kWebViewEnableTrustTokensComponent);
if (trust_tokens_component_enabled) {
num_webview_components++;
}
base::RepeatingClosure barrier_closure =
base::BarrierClosure(num_webview_components, std::move(on_finished));
RegisterComponentInstallerPolicyShim(
std::make_unique<
component_updater::OriginTrialsComponentInstallerPolicy>(),
register_callback, barrier_closure);
if (trust_tokens_component_enabled) {
RegisterComponentInstallerPolicyShim(
std::make_unique<component_updater::
TrustTokenKeyCommitmentsComponentInstallerPolicy>(
base::BindRepeating(
[](const std::string& raw_commitments) { NOTREACHED(); })),
register_callback, barrier_closure);
}
RegisterWebViewAppsPackageNamesAllowlistComponent(register_callback,
barrier_closure);
}
}