1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/web_applications/test/test_web_app_provider.h"
6 
7 #include <utility>
8 
9 #include "base/bind.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/web_applications/components/app_registrar.h"
12 #include "chrome/browser/web_applications/components/install_finalizer.h"
13 #include "chrome/browser/web_applications/components/os_integration_manager.h"
14 #include "chrome/browser/web_applications/components/pending_app_manager.h"
15 #include "chrome/browser/web_applications/components/policy/web_app_policy_manager.h"
16 #include "chrome/browser/web_applications/components/web_app_ui_manager.h"
17 #include "chrome/browser/web_applications/components/web_app_utils.h"
18 #include "chrome/browser/web_applications/system_web_app_manager.h"
19 #include "chrome/browser/web_applications/test/test_system_web_app_manager.h"
20 #include "chrome/browser/web_applications/web_app_install_manager.h"
21 #include "chrome/browser/web_applications/web_app_provider_factory.h"
22 
23 namespace web_app {
24 
25 // static
BuildDefault(content::BrowserContext * context)26 std::unique_ptr<KeyedService> TestWebAppProvider::BuildDefault(
27     content::BrowserContext* context) {
28   auto provider = std::make_unique<TestWebAppProvider>(
29       Profile::FromBrowserContext(context));
30 
31   // Do not call default production StartImpl if in TestingProfile.
32   provider->SetRunSubsystemStartupTasks(false);
33 
34   // TODO(crbug.com/973324): Replace core subsystems with fakes by default.
35   provider->ConnectSubsystems();
36 
37   return provider;
38 }
39 
40 // static
Get(Profile * profile)41 TestWebAppProvider* TestWebAppProvider::Get(Profile* profile) {
42   CHECK(profile->AsTestingProfile());
43   auto* test_provider =
44       static_cast<TestWebAppProvider*>(WebAppProvider::Get(profile));
45   CHECK(!test_provider->started_);
46 
47   // Disconnect so that clients are forced to call Start() before accessing any
48   // subsystems.
49   test_provider->connected_ = false;
50 
51   return test_provider;
52 }
53 
TestWebAppProvider(Profile * profile)54 TestWebAppProvider::TestWebAppProvider(Profile* profile)
55     : WebAppProvider(profile) {}
56 
57 TestWebAppProvider::~TestWebAppProvider() = default;
58 
SetRunSubsystemStartupTasks(bool run_subsystem_startup_tasks)59 void TestWebAppProvider::SetRunSubsystemStartupTasks(
60     bool run_subsystem_startup_tasks) {
61   run_subsystem_startup_tasks_ = run_subsystem_startup_tasks;
62 }
63 
SetRegistrar(std::unique_ptr<AppRegistrar> registrar)64 void TestWebAppProvider::SetRegistrar(std::unique_ptr<AppRegistrar> registrar) {
65   CheckNotStarted();
66   registrar_ = std::move(registrar);
67 }
68 
SetRegistryController(std::unique_ptr<AppRegistryController> controller)69 void TestWebAppProvider::SetRegistryController(
70     std::unique_ptr<AppRegistryController> controller) {
71   CheckNotStarted();
72   registry_controller_ = std::move(controller);
73 }
74 
SetInstallManager(std::unique_ptr<WebAppInstallManager> install_manager)75 void TestWebAppProvider::SetInstallManager(
76     std::unique_ptr<WebAppInstallManager> install_manager) {
77   CheckNotStarted();
78   install_manager_ = std::move(install_manager);
79 }
80 
SetInstallFinalizer(std::unique_ptr<InstallFinalizer> install_finalizer)81 void TestWebAppProvider::SetInstallFinalizer(
82     std::unique_ptr<InstallFinalizer> install_finalizer) {
83   CheckNotStarted();
84   install_finalizer_ = std::move(install_finalizer);
85 }
86 
SetPendingAppManager(std::unique_ptr<PendingAppManager> pending_app_manager)87 void TestWebAppProvider::SetPendingAppManager(
88     std::unique_ptr<PendingAppManager> pending_app_manager) {
89   CheckNotStarted();
90   pending_app_manager_ = std::move(pending_app_manager);
91 }
92 
SetWebAppUiManager(std::unique_ptr<WebAppUiManager> ui_manager)93 void TestWebAppProvider::SetWebAppUiManager(
94     std::unique_ptr<WebAppUiManager> ui_manager) {
95   CheckNotStarted();
96   ui_manager_ = std::move(ui_manager);
97 }
98 
SetSystemWebAppManager(std::unique_ptr<SystemWebAppManager> system_web_app_manager)99 void TestWebAppProvider::SetSystemWebAppManager(
100     std::unique_ptr<SystemWebAppManager> system_web_app_manager) {
101   CheckNotStarted();
102   system_web_app_manager_ = std::move(system_web_app_manager);
103 }
104 
SetWebAppPolicyManager(std::unique_ptr<WebAppPolicyManager> web_app_policy_manager)105 void TestWebAppProvider::SetWebAppPolicyManager(
106     std::unique_ptr<WebAppPolicyManager> web_app_policy_manager) {
107   CheckNotStarted();
108   web_app_policy_manager_ = std::move(web_app_policy_manager);
109 }
110 
SetOsIntegrationManager(std::unique_ptr<OsIntegrationManager> os_integration_manager)111 void TestWebAppProvider::SetOsIntegrationManager(
112     std::unique_ptr<OsIntegrationManager> os_integration_manager) {
113   CheckNotStarted();
114   os_integration_manager_ = std::move(os_integration_manager);
115 }
116 
CheckNotStarted() const117 void TestWebAppProvider::CheckNotStarted() const {
118   CHECK(!started_) << "Attempted to set a WebAppProvider subsystem after "
119                       "Start() was called.";
120 }
121 
StartImpl()122 void TestWebAppProvider::StartImpl() {
123   if (run_subsystem_startup_tasks_)
124     WebAppProvider::StartImpl();
125   else
126     on_registry_ready_.Signal();
127 }
128 
TestWebAppProviderCreator(CreateWebAppProviderCallback callback)129 TestWebAppProviderCreator::TestWebAppProviderCreator(
130     CreateWebAppProviderCallback callback)
131     : callback_(std::move(callback)) {
132   create_services_subscription_ =
133       BrowserContextDependencyManager::GetInstance()
134           ->RegisterCreateServicesCallbackForTesting(base::BindRepeating(
135               &TestWebAppProviderCreator::OnWillCreateBrowserContextServices,
136               base::Unretained(this)));
137 }
138 
139 TestWebAppProviderCreator::~TestWebAppProviderCreator() = default;
140 
OnWillCreateBrowserContextServices(content::BrowserContext * context)141 void TestWebAppProviderCreator::OnWillCreateBrowserContextServices(
142     content::BrowserContext* context) {
143   WebAppProviderFactory::GetInstance()->SetTestingFactory(
144       context,
145       base::BindRepeating(&TestWebAppProviderCreator::CreateWebAppProvider,
146                           base::Unretained(this)));
147 }
148 
CreateWebAppProvider(content::BrowserContext * context)149 std::unique_ptr<KeyedService> TestWebAppProviderCreator::CreateWebAppProvider(
150     content::BrowserContext* context) {
151   Profile* profile = Profile::FromBrowserContext(context);
152   if (!AreWebAppsEnabled(profile) || !callback_)
153     return nullptr;
154   return callback_.Run(profile);
155 }
156 
157 }  // namespace web_app
158