blob: ca19b30da729c65629789e8cafdaa4d659239495 [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;
Irina98873262025-04-28 19:19:2458class IwaBundleCacheManager;
Glen Robertson38c4f7db2023-07-25 00:30:1259#endif
60
Alan Cutter30aacee52022-08-17 05:36:4861// WebAppProvider is the heart of Chrome web app code.
62//
Nigel Tao192c3302018-07-19 15:17:2863// Connects Web App features, such as the installation of default and
64// policy-managed web apps, with Profiles (as WebAppProvider is a
65// Profile-linked KeyedService) and their associated PrefService.
Christopher Lama45ea6d2019-07-08 07:22:0566//
67// Lifecycle notes:
Alan Cutter30aacee52022-08-17 05:36:4868// - WebAppProvider and its sub-managers are not ready for use until the
69// on_registry_ready() event has fired. Its database must be loaded from
70// disk before it can be interacted with.
71// Example of waiting for on_registry_ready():
72// WebAppProvider* provider = WebAppProvider::GetForWebApps(profile);
73// provider->on_registry_ready().Post(
74// FROM_HERE,
75// base::BindOnce([](WebAppProvider& provider) {
76// ...
Simon Hanglbe287e9102025-03-17 10:13:5877// }, std::ref(*provider)));
Alan Cutter30aacee52022-08-17 05:36:4878// - All subsystems are constructed independently of each other in the
79// WebAppProvider constructor.
80// - Subsystem construction should have no side effects and start no tasks.
81// - Tests can replace any of the subsystems before Start() is called.
82// - Similarly, in destruction, subsystems should not refer to each other.
Song Fangzhen59c5ae282021-07-13 07:36:2783class WebAppProvider : public KeyedService {
Nigel Tao192c3302018-07-19 15:17:2884 public:
Alexey Baskakov50792c12022-05-26 04:34:1385 // Deprecated: Use GetForWebApps instead.
Eric Willigers607ca1052021-09-01 05:23:3286 static WebAppProvider* GetDeprecated(Profile* profile);
Jiewei Qianb6cfe542021-05-31 07:45:4587
Daniel Murphy9280ab222025-01-16 01:23:0588 // This returns a WebAppProvider for the given `profile`, or `nullptr` if
89 // installed web apps are not supported on the given `profile`. Use
90 // `web_app::AreWebAppsEnabled` to determine if web apps are supported on a
91 // profile.
92 // Note: On ChromeOS, to support the system web app implementation, this also
93 // considers the `profile`'s 'original' profile, if `AreWebAppsEnabled`
94 // returns `false` for `profile`.
Daniel Murphy42038622024-12-18 20:00:3295 // TODO(https://crbug.com/384063076): Stop returning the WebAppProvider for
Daniel Murphy9280ab222025-01-16 01:23:0596 // profiles where `AreWebAppsEnabled` returns `false` to support CrOS system
97 // web apps.
Jiewei Qianb6cfe542021-05-31 07:45:4598 static WebAppProvider* GetForWebApps(Profile* profile);
99
Georg Neis251cf6f2025-02-19 05:23:19100 // Returns the WebAppProvider for the current process.
Raymes Khoury588aef42021-08-24 05:20:28101 //
Alexey Baskakov50792c12022-05-26 04:34:13102 // Avoid using this function where possible and prefer GetForWebApps which
Georg Neis251cf6f2025-02-19 05:23:19103 // provides a guarantee they are being called from the correct process.
Daniel Murphy42038622024-12-18 20:00:32104 // TODO(https://crbug.com/384063076): Stop returning the WebAppProvider for
Daniel Murphy9280ab222025-01-16 01:23:05105 // profiles where `AreWebAppsEnabled` returns `false` to support CrOS system
106 // web apps.
Raymes Khoury588aef42021-08-24 05:20:28107 static WebAppProvider* GetForLocalAppsUnchecked(Profile* profile);
Eric Willigersf4283782021-08-07 04:05:55108
Georg Neis251cf6f2025-02-19 05:23:19109 // Return the WebAppProvider for tests. Blocks if the web app registry is not
110 // yet ready.
Daniel Murphy9280ab222025-01-16 01:23:05111 // This returns `nullptr` if installed web apps are not supported on the
112 // given `profile`. Use `web_app::AreWebAppsEnabled` to determine if web apps
113 // are supported on a profile.
114 // Note: On ChromeOS, to support the system web app implementation, this also
115 // considers the `profile`'s 'original' profile, if `AreWebAppsEnabled`
116 // returns `false` for `profile`.
Daniel Murphy42038622024-12-18 20:00:32117 // TODO(https://crbug.com/384063076): Stop returning the WebAppProvider for
Daniel Murphy9280ab222025-01-16 01:23:05118 // profiles where `AreWebAppsEnabled` returns `false` to support CrOS system
119 // web apps.
Raymes Khoury0be37da52021-08-19 07:15:47120 static WebAppProvider* GetForTest(Profile* profile);
121
Daniel Murphy9280ab222025-01-16 01:23:05122 // See `GetForWebApps` above for when this returns `nullptr`.
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41123 static WebAppProvider* GetForWebContents(content::WebContents* web_contents);
Nigel Tao192c3302018-07-19 15:17:28124
Alan Cutterf3d9aba72020-12-03 10:19:31125 using OsIntegrationManagerFactory =
126 std::unique_ptr<OsIntegrationManager> (*)(Profile*);
Alan Cutterf3d9aba72020-12-03 10:19:31127
Giovanni Ortuño Urquidi8ea2f1e2018-08-06 01:13:05128 explicit WebAppProvider(Profile* profile);
Haben Fotoe3d073b2020-10-06 01:22:58129 WebAppProvider(const WebAppProvider&) = delete;
130 WebAppProvider& operator=(const WebAppProvider&) = delete;
Alexey Baskakovd0392752019-01-10 08:38:28131 ~WebAppProvider() override;
132
Christopher Lam4ce340fd2019-07-23 08:07:32133 // Start the Web App system. This will run subsystem startup tasks.
134 void Start();
Nigel Tao192c3302018-07-19 15:17:28135
Phillis Tang549802e2022-09-30 19:40:31136 // Read/write to web app system should use `scheduler()` to guarantee safe
137 // access. This is safe to access even if the `WebAppProvider` is not ready.
138 WebAppCommandScheduler& scheduler();
Daniel Murphy8e0d8892022-11-08 13:30:04139 // This is safe to access even if the `WebAppProvider` is not ready.
140 WebAppCommandManager& command_manager();
Phillis Tang549802e2022-09-30 19:40:31141
142 // Web App sub components. These should only be accessed after
143 // `on_registry_ready()` is signaled.
144
Camden King7f851df2022-12-07 19:29:27145 // Unsafe access to the app registry model. For safe access use locks (see
146 // chrome/browser/web_applications/locks/ for more info).
147 WebAppRegistrar& registrar_unsafe();
148 const WebAppRegistrar& registrar_unsafe() const;
Glen Robertson7f21d6d82023-07-04 01:54:43149 // Must be exclusively accessed by WebAppSyncBridge.
Alan Cutterbd7803c2023-06-29 07:38:43150 WebAppRegistrarMutable& registrar_mutable(base::PassKey<WebAppSyncBridge>);
Camden King9bf43ed2022-12-21 22:38:25151 // Unsafe access to the WebAppSyncBridge. Reading or data from here should be
152 // considered an 'uncommitted read', and writing data is unsafe and could
153 // interfere with other operations. For safe access use locks to ensure no
154 // operations (like install/update/uninstall/etc) are currently running. See
155 // chrome/browser/web_applications/locks/ for more info.
Camden King7f851df2022-12-07 19:29:27156 WebAppSyncBridge& sync_bridge_unsafe();
Song Fangzhen1182ac9c2021-07-28 18:05:54157 // UIs can use WebAppInstallManager for user-initiated Web Apps install.
158 WebAppInstallManager& install_manager();
Song Fangzhen59c5ae282021-07-13 07:36:27159 // Implements persistence for Web Apps install.
Song Fangzhenb60b44f42021-08-11 08:49:23160 WebAppInstallFinalizer& install_finalizer();
Song Fangzhen59c5ae282021-07-13 07:36:27161 // Keeps app metadata up to date with site manifests.
162 ManifestUpdateManager& manifest_update_manager();
163 // Clients can use ExternallyManagedAppManager to install, uninstall, and
164 // update Web Apps.
165 ExternallyManagedAppManager& externally_managed_app_manager();
166 // Clients can use WebAppPolicyManager to request updates of policy installed
167 // Web Apps.
168 WebAppPolicyManager& policy_manager();
Zelin Liu51615d12023-08-29 18:13:39169 // `IsolatedWebAppInstallationManager` is the entry point for Isolated Web App
170 // installation.
171 IsolatedWebAppInstallationManager& isolated_web_app_installation_manager();
Christian Flache17ea922023-07-04 10:03:49172 // Keeps Isolated Web Apps up to date by regularly checking for updates,
173 // downloading them, and applying them.
Christian Flache17ea922023-07-04 10:03:49174 IsolatedWebAppUpdateManager& iwa_update_manager();
Sebastian Fiße8beb0442023-08-23 14:13:18175
Christian Flach42b091fd2023-10-11 12:16:15176#if BUILDFLAG(IS_CHROMEOS)
Sebastian Fiße8beb0442023-08-23 14:13:18177 // Runs web apps on OS login.
178 WebAppRunOnOsLoginManager& run_on_os_login_manager();
Irina98873262025-04-28 19:19:24179
180 // Isolated Web App bundle cache manager.
181 IwaBundleCacheManager& iwa_cache_manager();
Christian Flache17ea922023-07-04 10:03:49182#endif
Song Fangzhen59c5ae282021-07-13 07:36:27183
Simon Hanglc0197322024-11-27 19:03:44184 IsolatedWebAppPolicyManager& iwa_policy_manager();
185
Song Fangzhen59c5ae282021-07-13 07:36:27186 WebAppUiManager& ui_manager();
187
188 WebAppAudioFocusIdMap& audio_focus_id_map();
189
Glen Robertson7f21d6d82023-07-04 01:54:43190 // Interface for file access, allowing mocking for tests. `scoped_refptr` for
191 // thread safety as this is used on other task runners.
192 scoped_refptr<FileUtilsWrapper> file_utils();
193
Song Fangzhen59c5ae282021-07-13 07:36:27194 // Implements fetching of app icons.
Song Fangzhencffb8852021-08-05 03:51:18195 WebAppIconManager& icon_manager();
Song Fangzhen59c5ae282021-07-13 07:36:27196
Louise Brett9a07d832022-02-10 05:52:33197 WebAppTranslationManager& translation_manager();
198
Song Fangzhen59c5ae282021-07-13 07:36:27199 // Manage all OS hooks that need to be deployed during Web Apps install
200 OsIntegrationManager& os_integration_manager();
Daniel d'Andradad8ed51a2022-01-11 09:49:51201 const OsIntegrationManager& os_integration_manager() const;
Alexey Baskakov5f15de12019-07-10 00:36:15202
Lu Huang9cbbdd92023-04-18 19:14:56203 WebAppOriginAssociationManager& origin_association_manager();
204
Daniel Murphy22f66f12023-05-26 20:35:02205 WebContentsManager& web_contents_manager();
206
Alan Cutterbd7803c2023-06-29 07:38:43207 PreinstalledWebAppManager& preinstalled_web_app_manager();
208
Zelin Liu5cc0e1e2023-06-30 20:26:49209 ExtensionsManager& extensions_manager();
210
Alan Cutter56f3cd32023-09-20 00:29:36211 GeneratedIconFixManager& generated_icon_fix_manager();
212
Alan Cutterbd7803c2023-06-29 07:38:43213 AbstractWebAppDatabaseFactory& database_factory();
214
Daniel Murphy2e5932d2024-09-18 16:23:39215 VisitedManifestManager& visited_manifest_manager();
216
Daniel Murphy9b630c52024-09-19 01:21:10217 NavigationCapturingLog& navigation_capturing_log();
218
Christopher Lam342a6f82019-05-17 05:48:35219 // KeyedService:
220 void Shutdown() override;
221
Eric Willigers2cd31922019-06-26 00:53:34222 // Signals when app registry becomes ready.
223 const base::OneShotEvent& on_registry_ready() const {
224 return on_registry_ready_;
225 }
Alexey Baskakov7f4bffa2019-01-08 02:12:38226
Daniel Murphy8e0d8892022-11-08 13:30:04227 // Signals when external app managers have finished calling
228 // `SynchronizeInstalledApps`, which means that all installs or uninstalls for
229 // external managers have been scheduled. Specifically these calls are
230 // triggered from the PreinstalledWebAppManager and the WebAppPolicyManager.
231 // Note: This does not include the call from the ChromeOS SystemWebAppManager,
232 // which is a separate keyed service.
233 const base::OneShotEvent& on_external_managers_synchronized() const {
234 return on_external_managers_synchronized_;
235 }
236
Daseul Leef3b62d52022-02-11 22:46:12237 // Returns whether the app registry is ready.
Eric Willigers94f8550b2022-02-28 00:18:31238 bool is_registry_ready() const { return is_registry_ready_; }
Daseul Leef3b62d52022-02-11 22:46:12239
Dibyajyoti Palc8f07802023-07-21 22:37:33240 base::WeakPtr<WebAppProvider> AsWeakPtr();
241
Daniel Murphy6572be02024-01-17 17:35:03242 // Returns a nullptr in the default implementation
243 virtual FakeWebAppProvider* AsFakeWebAppProviderForTesting();
244
yuncong zhuang89759a7b2025-05-09 02:31:54245#if BUILDFLAG(IS_MAC)
246 void DoDelayedPostStartupWork();
247#endif
248
Alexey Baskakovd0392752019-01-10 08:38:28249 protected:
Christopher Lam4ce340fd2019-07-23 08:07:32250 virtual void StartImpl();
251
Alexey Baskakov84539e9c2021-09-03 07:51:49252 void CreateSubsystems(Profile* profile);
Alexey Baskakovecee29442020-11-05 07:30:20253
Christopher Lam4ce340fd2019-07-23 08:07:32254 // Wire together subsystems but do not start them (yet).
Christopher Lama45ea6d2019-07-08 07:22:05255 void ConnectSubsystems();
256
Song Fangzhend5217842021-08-24 11:18:22257 // Start sync bridge. All other subsystems depend on it.
258 void StartSyncBridge();
259 void OnSyncBridgeReady();
Alexey Baskakov7f4bffa2019-01-08 02:12:38260
Alexey Baskakov0818deee2019-08-06 18:33:33261 void CheckIsConnected() const;
262
Glen Robertsonaaddfad2022-03-24 03:37:46263 std::unique_ptr<AbstractWebAppDatabaseFactory> database_factory_;
Alan Cutterbd7803c2023-06-29 07:38:43264 std::unique_ptr<WebAppRegistrarMutable> registrar_;
Song Fangzhend5217842021-08-24 11:18:22265 std::unique_ptr<WebAppSyncBridge> sync_bridge_;
Daniel Murphy3657906d2021-04-13 20:33:12266 std::unique_ptr<PreinstalledWebAppManager> preinstalled_web_app_manager_;
Song Fangzhencffb8852021-08-05 03:51:18267 std::unique_ptr<WebAppIconManager> icon_manager_;
Louise Brett9a07d832022-02-10 05:52:33268 std::unique_ptr<WebAppTranslationManager> translation_manager_;
Song Fangzhenb60b44f42021-08-11 08:49:23269 std::unique_ptr<WebAppInstallFinalizer> install_finalizer_;
Alan Cutterf2d2485f2019-09-05 07:11:13270 std::unique_ptr<ManifestUpdateManager> manifest_update_manager_;
Daniel Murphy6294817d2021-04-13 22:28:13271 std::unique_ptr<ExternallyManagedAppManager> externally_managed_app_manager_;
Alan Cutter6e07e3b2019-08-09 05:11:58272 std::unique_ptr<WebAppAudioFocusIdMap> audio_focus_id_map_;
273 std::unique_ptr<WebAppInstallManager> install_manager_;
Nigel Tao192c3302018-07-19 15:17:28274 std::unique_ptr<WebAppPolicyManager> web_app_policy_manager_;
Zelin Liu51615d12023-08-29 18:13:39275 std::unique_ptr<IsolatedWebAppInstallationManager>
276 isolated_web_app_installation_manager_;
Christian Flache17ea922023-07-04 10:03:49277 std::unique_ptr<IsolatedWebAppUpdateManager> iwa_update_manager_;
Simon Hanglc0197322024-11-27 19:03:44278 std::unique_ptr<IsolatedWebAppPolicyManager> isolated_web_app_policy_manager_;
Christian Flach42b091fd2023-10-11 12:16:15279#if BUILDFLAG(IS_CHROMEOS)
Sebastian Fiß325719592023-03-07 07:57:58280 std::unique_ptr<WebAppRunOnOsLoginManager> web_app_run_on_os_login_manager_;
Irina98873262025-04-28 19:19:24281 std::unique_ptr<IwaBundleCacheManager> iwa_cache_manager_;
Sebastian Fiß325719592023-03-07 07:57:58282#endif // BUILDFLAG(IS_CHROMEOS)
Alan Cutter6e07e3b2019-08-09 05:11:58283 std::unique_ptr<WebAppUiManager> ui_manager_;
phillis550e3712020-06-19 02:17:27284 std::unique_ptr<OsIntegrationManager> os_integration_manager_;
Phillis Tang52b1ec4b2022-04-13 01:14:31285 std::unique_ptr<WebAppCommandManager> command_manager_;
Phillis Tang549802e2022-09-30 19:40:31286 std::unique_ptr<WebAppCommandScheduler> command_scheduler_;
Lu Huang9cbbdd92023-04-18 19:14:56287 std::unique_ptr<WebAppOriginAssociationManager> origin_association_manager_;
Daniel Murphy22f66f12023-05-26 20:35:02288 std::unique_ptr<WebContentsManager> web_contents_manager_;
Zelin Liu5cc0e1e2023-06-30 20:26:49289 std::unique_ptr<ExtensionsManager> extensions_manager_;
Alan Cutter56f3cd32023-09-20 00:29:36290 std::unique_ptr<GeneratedIconFixManager> generated_icon_fix_manager_;
Glen Robertson7f21d6d82023-07-04 01:54:43291 scoped_refptr<FileUtilsWrapper> file_utils_;
Daniel Murphy2e5932d2024-09-18 16:23:39292 std::unique_ptr<VisitedManifestManager> visited_manifest_manager_;
Daniel Murphy9b630c52024-09-19 01:21:10293 std::unique_ptr<NavigationCapturingLog> navigation_capturing_log_;
Dibyajyoti Pal2ff5a9262024-12-07 01:08:35294 std::unique_ptr<WebAppProfileDeletionManager> profile_deletion_manager_;
Nigel Tao192c3302018-07-19 15:17:28295
Eric Willigers2cd31922019-06-26 00:53:34296 base::OneShotEvent on_registry_ready_;
Daniel Murphy8e0d8892022-11-08 13:30:04297 base::OneShotEvent on_external_managers_synchronized_;
Alexey Baskakov7f4bffa2019-01-08 02:12:38298
Keishi Hattori0e45c022021-11-27 09:25:52299 const raw_ptr<Profile> profile_;
Alexey Baskakovda220372019-01-08 00:21:16300
Christopher Lam4ce340fd2019-07-23 08:07:32301 // Ensures that ConnectSubsystems() is not called after Start().
Christopher Lama45ea6d2019-07-08 07:22:05302 bool started_ = false;
Christopher Lam4ce340fd2019-07-23 08:07:32303 bool connected_ = false;
Daseul Leef3b62d52022-02-11 22:46:12304 bool is_registry_ready_ = false;
Christopher Lama45ea6d2019-07-08 07:22:05305
Dominick Ng7b4ad0b82018-08-03 15:29:10306 base::WeakPtrFactory<WebAppProvider> weak_ptr_factory_{this};
Nigel Tao192c3302018-07-19 15:17:28307};
308
309} // namespace web_app
310
311#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_