blob: 4ad2c2546ac3a96790906a3ea09d11e0b49275ca [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2018 The Chromium Authors
Nigel Tao192c3302018-07-19 15:17:282// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_
6#define CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_
7
8#include <memory>
9
Keishi Hattori0e45c022021-11-27 09:25:5210#include "base/memory/raw_ptr.h"
Glen Robertson7f21d6d82023-07-04 01:54:4311#include "base/memory/scoped_refptr.h"
Dominick Ng7b4ad0b82018-08-03 15:29:1012#include "base/memory/weak_ptr.h"
Eric Willigers2cd31922019-06-26 00:53:3413#include "base/one_shot_event.h"
Glen Robertson38c4f7db2023-07-25 00:30:1214#include "base/types/pass_key.h"
15#include "build/build_config.h"
Song Fangzhen59c5ae282021-07-13 07:36:2716#include "components/keyed_service/core/keyed_service.h"
Nigel Tao192c3302018-07-19 15:17:2817
18class Profile;
19
Alexey Baskakovfd3894e2018-10-16 06:09:5820namespace content {
21class WebContents;
22}
23
Nigel Tao192c3302018-07-19 15:17:2824namespace web_app {
25
Glen Robertsonaaddfad2022-03-24 03:37:4626class AbstractWebAppDatabaseFactory;
Zelin Liu5cc0e1e2023-06-30 20:26:4927class ExtensionsManager;
Glen Robertson38c4f7db2023-07-25 00:30:1228class ExternallyManagedAppManager;
Daniel Murphy6572be02024-01-17 17:35:0329class FakeWebAppProvider;
Glen Robertson38c4f7db2023-07-25 00:30:1230class FileUtilsWrapper;
Alan Cutter56f3cd32023-09-20 00:29:3631class GeneratedIconFixManager;
Zelin Liu51615d12023-08-29 18:13:3932class IsolatedWebAppInstallationManager;
Simon Hanglc0197322024-11-27 19:03:4433class IsolatedWebAppPolicyManager;
Christian Flach42b091fd2023-10-11 12:16:1534class IsolatedWebAppUpdateManager;
Alan Cutterf2d2485f2019-09-05 07:11:1335class ManifestUpdateManager;
Daniel Murphy9b630c52024-09-19 01:21:1036class NavigationCapturingLog;
phillis550e3712020-06-19 02:17:2737class OsIntegrationManager;
Daniel Murphy22f66f12023-05-26 20:35:0238class PreinstalledWebAppManager;
Daniel Murphy2e5932d2024-09-18 16:23:3939class VisitedManifestManager;
Daniel Murphy22f66f12023-05-26 20:35:0240class WebAppAudioFocusIdMap;
Phillis Tang52b1ec4b2022-04-13 01:14:3141class WebAppCommandManager;
Phillis Tang549802e2022-09-30 19:40:3142class WebAppCommandScheduler;
Daniel Murphy22f66f12023-05-26 20:35:0243class WebAppIconManager;
44class WebAppInstallFinalizer;
45class WebAppInstallManager;
Lu Huang9cbbdd92023-04-18 19:14:5646class WebAppOriginAssociationManager;
Daniel Murphy22f66f12023-05-26 20:35:0247class WebAppPolicyManager;
Glen Robertson38c4f7db2023-07-25 00:30:1248class WebAppRegistrar;
49class WebAppRegistrarMutable;
Daniel Murphy22f66f12023-05-26 20:35:0250class WebAppSyncBridge;
51class WebAppTranslationManager;
52class WebAppUiManager;
53class WebContentsManager;
Dibyajyoti Pal2ff5a9262024-12-07 01:08:3554class WebAppProfileDeletionManager;
Alexey Baskakovfd3894e2018-10-16 06:09:5855
Glen Robertson38c4f7db2023-07-25 00:30:1256#if BUILDFLAG(IS_CHROMEOS)
Glen Robertson38c4f7db2023-07-25 00:30:1257class WebAppRunOnOsLoginManager;
58#endif
59
Alan Cutter30aacee52022-08-17 05:36:4860// WebAppProvider is the heart of Chrome web app code.
61//
Nigel Tao192c3302018-07-19 15:17:2862// Connects Web App features, such as the installation of default and
63// policy-managed web apps, with Profiles (as WebAppProvider is a
64// Profile-linked KeyedService) and their associated PrefService.
Christopher Lama45ea6d2019-07-08 07:22:0565//
66// Lifecycle notes:
Alan Cutter30aacee52022-08-17 05:36:4867// - WebAppProvider and its sub-managers are not ready for use until the
68// on_registry_ready() event has fired. Its database must be loaded from
69// disk before it can be interacted with.
70// Example of waiting for on_registry_ready():
71// WebAppProvider* provider = WebAppProvider::GetForWebApps(profile);
72// provider->on_registry_ready().Post(
73// FROM_HERE,
74// base::BindOnce([](WebAppProvider& provider) {
75// ...
76// }, std::ref(*provider));
77// - All subsystems are constructed independently of each other in the
78// WebAppProvider constructor.
79// - Subsystem construction should have no side effects and start no tasks.
80// - Tests can replace any of the subsystems before Start() is called.
81// - Similarly, in destruction, subsystems should not refer to each other.
Song Fangzhen59c5ae282021-07-13 07:36:2782class WebAppProvider : public KeyedService {
Nigel Tao192c3302018-07-19 15:17:2883 public:
Alexey Baskakov50792c12022-05-26 04:34:1384 // Deprecated: Use GetForWebApps instead.
Eric Willigers607ca1052021-09-01 05:23:3285 static WebAppProvider* GetDeprecated(Profile* profile);
Jiewei Qianb6cfe542021-05-31 07:45:4586
Daniel Murphy9280ab222025-01-16 01:23:0587 // This returns a WebAppProvider for the given `profile`, or `nullptr` if
88 // installed web apps are not supported on the given `profile`. Use
89 // `web_app::AreWebAppsEnabled` to determine if web apps are supported on a
90 // profile.
91 // Note: On ChromeOS, to support the system web app implementation, this also
92 // considers the `profile`'s 'original' profile, if `AreWebAppsEnabled`
93 // returns `false` for `profile`.
Daniel Murphy42038622024-12-18 20:00:3294 // TODO(https://crbug.com/384063076): Stop returning the WebAppProvider for
Daniel Murphy9280ab222025-01-16 01:23:0595 // profiles where `AreWebAppsEnabled` returns `false` to support CrOS system
96 // web apps.
Jiewei Qianb6cfe542021-05-31 07:45:4597 static WebAppProvider* GetForWebApps(Profile* profile);
98
Alexey Baskakov50792c12022-05-26 04:34:1399 // Returns the WebAppProvider for the current process. In particular:
Eric Willigersf4283782021-08-07 04:05:55100 // In Ash: Returns the WebAppProvider that hosts System Web Apps.
Raymes Khoury588aef42021-08-24 05:20:28101 // In Lacros and other platforms: Returns the WebAppProvider that hosts
Eric Willigersf4283782021-08-07 04:05:55102 // non-system Web Apps.
Raymes Khoury588aef42021-08-24 05:20:28103 //
Alexey Baskakov50792c12022-05-26 04:34:13104 // Avoid using this function where possible and prefer GetForWebApps which
105 // provides a guarantee they are being called from the correct process. Only
106 // use this if the calling code is shared between Ash and Lacros and expects
107 // the PWA WebAppProvider in Lacros and the SWA WebAppProvider in Ash.
Daniel Murphy42038622024-12-18 20:00:32108 // TODO(https://crbug.com/384063076): Stop returning the WebAppProvider for
Daniel Murphy9280ab222025-01-16 01:23:05109 // profiles where `AreWebAppsEnabled` returns `false` to support CrOS system
110 // web apps.
Raymes Khoury588aef42021-08-24 05:20:28111 static WebAppProvider* GetForLocalAppsUnchecked(Profile* profile);
Eric Willigersf4283782021-08-07 04:05:55112
Raymes Khoury0be37da52021-08-19 07:15:47113 // Return the WebAppProvider for tests, regardless of whether this is running
Eric Willigers5484b492021-11-10 04:32:28114 // in Lacros/Ash. Blocks if the web app registry is not yet ready.
Daniel Murphy9280ab222025-01-16 01:23:05115 // This returns `nullptr` if installed web apps are not supported on the
116 // given `profile`. Use `web_app::AreWebAppsEnabled` to determine if web apps
117 // are supported on a profile.
118 // Note: On ChromeOS, to support the system web app implementation, this also
119 // considers the `profile`'s 'original' profile, if `AreWebAppsEnabled`
120 // returns `false` for `profile`.
Daniel Murphy42038622024-12-18 20:00:32121 // TODO(https://crbug.com/384063076): Stop returning the WebAppProvider for
Daniel Murphy9280ab222025-01-16 01:23:05122 // profiles where `AreWebAppsEnabled` returns `false` to support CrOS system
123 // web apps.
Raymes Khoury0be37da52021-08-19 07:15:47124 static WebAppProvider* GetForTest(Profile* profile);
125
Daniel Murphy9280ab222025-01-16 01:23:05126 // See `GetForWebApps` above for when this returns `nullptr`.
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41127 static WebAppProvider* GetForWebContents(content::WebContents* web_contents);
Nigel Tao192c3302018-07-19 15:17:28128
Alan Cutterf3d9aba72020-12-03 10:19:31129 using OsIntegrationManagerFactory =
130 std::unique_ptr<OsIntegrationManager> (*)(Profile*);
Alan Cutterf3d9aba72020-12-03 10:19:31131
Giovanni Ortuño Urquidi8ea2f1e2018-08-06 01:13:05132 explicit WebAppProvider(Profile* profile);
Haben Fotoe3d073b2020-10-06 01:22:58133 WebAppProvider(const WebAppProvider&) = delete;
134 WebAppProvider& operator=(const WebAppProvider&) = delete;
Alexey Baskakovd0392752019-01-10 08:38:28135 ~WebAppProvider() override;
136
Christopher Lam4ce340fd2019-07-23 08:07:32137 // Start the Web App system. This will run subsystem startup tasks.
138 void Start();
Nigel Tao192c3302018-07-19 15:17:28139
Phillis Tang549802e2022-09-30 19:40:31140 // Read/write to web app system should use `scheduler()` to guarantee safe
141 // access. This is safe to access even if the `WebAppProvider` is not ready.
142 WebAppCommandScheduler& scheduler();
Daniel Murphy8e0d8892022-11-08 13:30:04143 // This is safe to access even if the `WebAppProvider` is not ready.
144 WebAppCommandManager& command_manager();
Phillis Tang549802e2022-09-30 19:40:31145
146 // Web App sub components. These should only be accessed after
147 // `on_registry_ready()` is signaled.
148
Camden King7f851df2022-12-07 19:29:27149 // Unsafe access to the app registry model. For safe access use locks (see
150 // chrome/browser/web_applications/locks/ for more info).
151 WebAppRegistrar& registrar_unsafe();
152 const WebAppRegistrar& registrar_unsafe() const;
Glen Robertson7f21d6d82023-07-04 01:54:43153 // Must be exclusively accessed by WebAppSyncBridge.
Alan Cutterbd7803c2023-06-29 07:38:43154 WebAppRegistrarMutable& registrar_mutable(base::PassKey<WebAppSyncBridge>);
Camden King9bf43ed2022-12-21 22:38:25155 // Unsafe access to the WebAppSyncBridge. Reading or data from here should be
156 // considered an 'uncommitted read', and writing data is unsafe and could
157 // interfere with other operations. For safe access use locks to ensure no
158 // operations (like install/update/uninstall/etc) are currently running. See
159 // chrome/browser/web_applications/locks/ for more info.
Camden King7f851df2022-12-07 19:29:27160 WebAppSyncBridge& sync_bridge_unsafe();
Song Fangzhen1182ac9c2021-07-28 18:05:54161 // UIs can use WebAppInstallManager for user-initiated Web Apps install.
162 WebAppInstallManager& install_manager();
Song Fangzhen59c5ae282021-07-13 07:36:27163 // Implements persistence for Web Apps install.
Song Fangzhenb60b44f42021-08-11 08:49:23164 WebAppInstallFinalizer& install_finalizer();
Song Fangzhen59c5ae282021-07-13 07:36:27165 // Keeps app metadata up to date with site manifests.
166 ManifestUpdateManager& manifest_update_manager();
167 // Clients can use ExternallyManagedAppManager to install, uninstall, and
168 // update Web Apps.
169 ExternallyManagedAppManager& externally_managed_app_manager();
170 // Clients can use WebAppPolicyManager to request updates of policy installed
171 // Web Apps.
172 WebAppPolicyManager& policy_manager();
Zelin Liu51615d12023-08-29 18:13:39173 // `IsolatedWebAppInstallationManager` is the entry point for Isolated Web App
174 // installation.
175 IsolatedWebAppInstallationManager& isolated_web_app_installation_manager();
Christian Flache17ea922023-07-04 10:03:49176 // Keeps Isolated Web Apps up to date by regularly checking for updates,
177 // downloading them, and applying them.
Christian Flache17ea922023-07-04 10:03:49178 IsolatedWebAppUpdateManager& iwa_update_manager();
Sebastian Fiße8beb0442023-08-23 14:13:18179
Christian Flach42b091fd2023-10-11 12:16:15180#if BUILDFLAG(IS_CHROMEOS)
Sebastian Fiße8beb0442023-08-23 14:13:18181 // Runs web apps on OS login.
182 WebAppRunOnOsLoginManager& run_on_os_login_manager();
Christian Flache17ea922023-07-04 10:03:49183#endif
Song Fangzhen59c5ae282021-07-13 07:36:27184
Simon Hanglc0197322024-11-27 19:03:44185 IsolatedWebAppPolicyManager& iwa_policy_manager();
186
Song Fangzhen59c5ae282021-07-13 07:36:27187 WebAppUiManager& ui_manager();
188
189 WebAppAudioFocusIdMap& audio_focus_id_map();
190
Glen Robertson7f21d6d82023-07-04 01:54:43191 // Interface for file access, allowing mocking for tests. `scoped_refptr` for
192 // thread safety as this is used on other task runners.
193 scoped_refptr<FileUtilsWrapper> file_utils();
194
Song Fangzhen59c5ae282021-07-13 07:36:27195 // Implements fetching of app icons.
Song Fangzhencffb8852021-08-05 03:51:18196 WebAppIconManager& icon_manager();
Song Fangzhen59c5ae282021-07-13 07:36:27197
Louise Brett9a07d832022-02-10 05:52:33198 WebAppTranslationManager& translation_manager();
199
Song Fangzhen59c5ae282021-07-13 07:36:27200 // Manage all OS hooks that need to be deployed during Web Apps install
201 OsIntegrationManager& os_integration_manager();
Daniel d'Andradad8ed51a2022-01-11 09:49:51202 const OsIntegrationManager& os_integration_manager() const;
Alexey Baskakov5f15de12019-07-10 00:36:15203
Lu Huang9cbbdd92023-04-18 19:14:56204 WebAppOriginAssociationManager& origin_association_manager();
205
Daniel Murphy22f66f12023-05-26 20:35:02206 WebContentsManager& web_contents_manager();
207
Alan Cutterbd7803c2023-06-29 07:38:43208 PreinstalledWebAppManager& preinstalled_web_app_manager();
209
Zelin Liu5cc0e1e2023-06-30 20:26:49210 ExtensionsManager& extensions_manager();
211
Alan Cutter56f3cd32023-09-20 00:29:36212 GeneratedIconFixManager& generated_icon_fix_manager();
213
Alan Cutterbd7803c2023-06-29 07:38:43214 AbstractWebAppDatabaseFactory& database_factory();
215
Daniel Murphy2e5932d2024-09-18 16:23:39216 VisitedManifestManager& visited_manifest_manager();
217
Daniel Murphy9b630c52024-09-19 01:21:10218 NavigationCapturingLog& navigation_capturing_log();
219
Christopher Lam342a6f82019-05-17 05:48:35220 // KeyedService:
221 void Shutdown() override;
222
Eric Willigers2cd31922019-06-26 00:53:34223 // Signals when app registry becomes ready.
224 const base::OneShotEvent& on_registry_ready() const {
225 return on_registry_ready_;
226 }
Alexey Baskakov7f4bffa2019-01-08 02:12:38227
Daniel Murphy8e0d8892022-11-08 13:30:04228 // Signals when external app managers have finished calling
229 // `SynchronizeInstalledApps`, which means that all installs or uninstalls for
230 // external managers have been scheduled. Specifically these calls are
231 // triggered from the PreinstalledWebAppManager and the WebAppPolicyManager.
232 // Note: This does not include the call from the ChromeOS SystemWebAppManager,
233 // which is a separate keyed service.
234 const base::OneShotEvent& on_external_managers_synchronized() const {
235 return on_external_managers_synchronized_;
236 }
237
Daseul Leef3b62d52022-02-11 22:46:12238 // Returns whether the app registry is ready.
Eric Willigers94f8550b2022-02-28 00:18:31239 bool is_registry_ready() const { return is_registry_ready_; }
Daseul Leef3b62d52022-02-11 22:46:12240
Dibyajyoti Palc8f07802023-07-21 22:37:33241 base::WeakPtr<WebAppProvider> AsWeakPtr();
242
Daniel Murphy6572be02024-01-17 17:35:03243 // Returns a nullptr in the default implementation
244 virtual FakeWebAppProvider* AsFakeWebAppProviderForTesting();
245
Alexey Baskakovd0392752019-01-10 08:38:28246 protected:
Christopher Lam4ce340fd2019-07-23 08:07:32247 virtual void StartImpl();
248
Alexey Baskakov84539e9c2021-09-03 07:51:49249 void CreateSubsystems(Profile* profile);
Alexey Baskakovecee29442020-11-05 07:30:20250
Christopher Lam4ce340fd2019-07-23 08:07:32251 // Wire together subsystems but do not start them (yet).
Christopher Lama45ea6d2019-07-08 07:22:05252 void ConnectSubsystems();
253
Song Fangzhend5217842021-08-24 11:18:22254 // Start sync bridge. All other subsystems depend on it.
255 void StartSyncBridge();
256 void OnSyncBridgeReady();
Alexey Baskakov7f4bffa2019-01-08 02:12:38257
Alexey Baskakov0818deee2019-08-06 18:33:33258 void CheckIsConnected() const;
259
Glen Robertsonaaddfad2022-03-24 03:37:46260 std::unique_ptr<AbstractWebAppDatabaseFactory> database_factory_;
Alan Cutterbd7803c2023-06-29 07:38:43261 std::unique_ptr<WebAppRegistrarMutable> registrar_;
Song Fangzhend5217842021-08-24 11:18:22262 std::unique_ptr<WebAppSyncBridge> sync_bridge_;
Daniel Murphy3657906d2021-04-13 20:33:12263 std::unique_ptr<PreinstalledWebAppManager> preinstalled_web_app_manager_;
Song Fangzhencffb8852021-08-05 03:51:18264 std::unique_ptr<WebAppIconManager> icon_manager_;
Louise Brett9a07d832022-02-10 05:52:33265 std::unique_ptr<WebAppTranslationManager> translation_manager_;
Song Fangzhenb60b44f42021-08-11 08:49:23266 std::unique_ptr<WebAppInstallFinalizer> install_finalizer_;
Alan Cutterf2d2485f2019-09-05 07:11:13267 std::unique_ptr<ManifestUpdateManager> manifest_update_manager_;
Daniel Murphy6294817d2021-04-13 22:28:13268 std::unique_ptr<ExternallyManagedAppManager> externally_managed_app_manager_;
Alan Cutter6e07e3b2019-08-09 05:11:58269 std::unique_ptr<WebAppAudioFocusIdMap> audio_focus_id_map_;
270 std::unique_ptr<WebAppInstallManager> install_manager_;
Nigel Tao192c3302018-07-19 15:17:28271 std::unique_ptr<WebAppPolicyManager> web_app_policy_manager_;
Zelin Liu51615d12023-08-29 18:13:39272 std::unique_ptr<IsolatedWebAppInstallationManager>
273 isolated_web_app_installation_manager_;
Christian Flache17ea922023-07-04 10:03:49274 std::unique_ptr<IsolatedWebAppUpdateManager> iwa_update_manager_;
Simon Hanglc0197322024-11-27 19:03:44275 std::unique_ptr<IsolatedWebAppPolicyManager> isolated_web_app_policy_manager_;
Christian Flach42b091fd2023-10-11 12:16:15276#if BUILDFLAG(IS_CHROMEOS)
Sebastian Fiß325719592023-03-07 07:57:58277 std::unique_ptr<WebAppRunOnOsLoginManager> web_app_run_on_os_login_manager_;
278#endif // BUILDFLAG(IS_CHROMEOS)
Alan Cutter6e07e3b2019-08-09 05:11:58279 std::unique_ptr<WebAppUiManager> ui_manager_;
phillis550e3712020-06-19 02:17:27280 std::unique_ptr<OsIntegrationManager> os_integration_manager_;
Phillis Tang52b1ec4b2022-04-13 01:14:31281 std::unique_ptr<WebAppCommandManager> command_manager_;
Phillis Tang549802e2022-09-30 19:40:31282 std::unique_ptr<WebAppCommandScheduler> command_scheduler_;
Lu Huang9cbbdd92023-04-18 19:14:56283 std::unique_ptr<WebAppOriginAssociationManager> origin_association_manager_;
Daniel Murphy22f66f12023-05-26 20:35:02284 std::unique_ptr<WebContentsManager> web_contents_manager_;
Zelin Liu5cc0e1e2023-06-30 20:26:49285 std::unique_ptr<ExtensionsManager> extensions_manager_;
Alan Cutter56f3cd32023-09-20 00:29:36286 std::unique_ptr<GeneratedIconFixManager> generated_icon_fix_manager_;
Glen Robertson7f21d6d82023-07-04 01:54:43287 scoped_refptr<FileUtilsWrapper> file_utils_;
Daniel Murphy2e5932d2024-09-18 16:23:39288 std::unique_ptr<VisitedManifestManager> visited_manifest_manager_;
Daniel Murphy9b630c52024-09-19 01:21:10289 std::unique_ptr<NavigationCapturingLog> navigation_capturing_log_;
Dibyajyoti Pal2ff5a9262024-12-07 01:08:35290 std::unique_ptr<WebAppProfileDeletionManager> profile_deletion_manager_;
Nigel Tao192c3302018-07-19 15:17:28291
Eric Willigers2cd31922019-06-26 00:53:34292 base::OneShotEvent on_registry_ready_;
Daniel Murphy8e0d8892022-11-08 13:30:04293 base::OneShotEvent on_external_managers_synchronized_;
Alexey Baskakov7f4bffa2019-01-08 02:12:38294
Keishi Hattori0e45c022021-11-27 09:25:52295 const raw_ptr<Profile> profile_;
Alexey Baskakovda220372019-01-08 00:21:16296
Christopher Lam4ce340fd2019-07-23 08:07:32297 // Ensures that ConnectSubsystems() is not called after Start().
Christopher Lama45ea6d2019-07-08 07:22:05298 bool started_ = false;
Christopher Lam4ce340fd2019-07-23 08:07:32299 bool connected_ = false;
Daseul Leef3b62d52022-02-11 22:46:12300 bool is_registry_ready_ = false;
Christopher Lama45ea6d2019-07-08 07:22:05301
Dominick Ng7b4ad0b82018-08-03 15:29:10302 base::WeakPtrFactory<WebAppProvider> weak_ptr_factory_{this};
Nigel Tao192c3302018-07-19 15:17:28303};
304
305} // namespace web_app
306
307#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_