blob: 94f06b0755549dbc62cbafb0aaa0808e4e7175e7 [file] [log] [blame]
Nigel Tao192c3302018-07-19 15:17:281// Copyright 2018 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#ifndef CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_
6#define CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_
7
8#include <memory>
Dominick Ng7b4ad0b82018-08-03 15:29:109#include <vector>
Nigel Tao192c3302018-07-19 15:17:2810
Dominick Ng7b4ad0b82018-08-03 15:29:1011#include "base/memory/weak_ptr.h"
Eric Willigers2cd31922019-06-26 00:53:3412#include "base/one_shot_event.h"
Daniel Murphy6294817d2021-04-13 22:28:1313#include "chrome/browser/web_applications/components/externally_managed_app_manager.h"
Glen Robertson271d1332020-01-28 00:16:0214#include "chrome/browser/web_applications/components/web_app_id.h"
Song Fangzhened83c142021-07-13 03:21:4415#include "chrome/browser/web_applications/web_app_registrar.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
Alexey Baskakov396edb92018-08-02 05:11:4224namespace user_prefs {
25class PrefRegistrySyncable;
26}
27
Nigel Tao192c3302018-07-19 15:17:2828namespace web_app {
29
Alexey Baskakovfd3894e2018-10-16 06:09:5830// Forward declarations of generalized interfaces.
Alexey Baskakov27f14d42019-09-20 07:09:4831class AppRegistryController;
Song Fangzhencffb8852021-08-05 03:51:1832class WebAppIconManager;
Daniel Murphy3657906d2021-04-13 20:33:1233class PreinstalledWebAppManager;
Alexey Baskakov4859218b2019-04-03 22:19:5734class InstallFinalizer;
Alan Cutterf2d2485f2019-09-05 07:11:1335class ManifestUpdateManager;
Alexey Baskakov6cf2c152019-07-26 07:56:4936class SystemWebAppManager;
Becca Hughes6f494292018-12-19 10:57:2137class WebAppAudioFocusIdMap;
Alexey Baskakovaca7a0c2019-07-29 07:34:0538class WebAppInstallManager;
Alan Cutter6e07e3b2019-08-09 05:11:5839class WebAppPolicyManager;
Alan Cutter31b206b2019-07-17 16:57:3540class WebAppUiManager;
phillis550e3712020-06-19 02:17:2741class OsIntegrationManager;
Alexey Baskakovfd3894e2018-10-16 06:09:5842
43// Forward declarations for new extension-independent subsystems.
Alexey Baskakovac8c4b02018-11-07 06:10:0244class WebAppDatabaseFactory;
Daniel Murphybd8993a5a02020-12-17 18:10:1045class WebAppMover;
Alexey Baskakovfd3894e2018-10-16 06:09:5846
Nigel Tao192c3302018-07-19 15:17:2847// Connects Web App features, such as the installation of default and
48// policy-managed web apps, with Profiles (as WebAppProvider is a
49// Profile-linked KeyedService) and their associated PrefService.
Christopher Lama45ea6d2019-07-08 07:22:0550//
51// Lifecycle notes:
52// All subsystems are constructed independently of each other in the
53// WebAppProvider constructor.
54// Subsystem construction should have no side effects and start no tasks.
55// Tests can replace any of the subsystems before Start() is called.
56// Similarly, in destruction, subsystems should not refer to each other.
Song Fangzhen59c5ae282021-07-13 07:36:2757class WebAppProvider : public KeyedService {
Nigel Tao192c3302018-07-19 15:17:2858 public:
Jiewei Qianb6cfe542021-05-31 07:45:4559 // Deprecated: Use GetForWebApps or GetForSystemWebApps instead.
Nigel Tao192c3302018-07-19 15:17:2860 static WebAppProvider* Get(Profile* profile);
Jiewei Qianb6cfe542021-05-31 07:45:4561
62 // On Chrome OS: if Lacros Web App (WebAppsCrosapi) is enabled, returns
63 // WebAppProvider in Lacros and nullptr in Ash. Otherwise does the reverse
64 // (nullptr in Lacros, WebAppProvider in Ash). On other platforms, always
65 // returns a WebAppProvider.
66 static WebAppProvider* GetForWebApps(Profile* profile);
67
68 // On Chrome OS: returns the WebAppProvider that hosts System Web Apps in Ash;
69 // In Lacros, returns nullptr (unless EnableSystemWebAppInLacrosForTesting).
70 // On other platforms, always returns a WebAppProvider.
71 static WebAppProvider* GetForSystemWebApps(Profile* profile);
72
73#if BUILDFLAG(IS_CHROMEOS_LACROS)
74 // Enables System Web Apps WebAppProvider so we can test SWA features in
75 // Lacros, even we don't have actual SWAs in Lacros. After calling this,
76 // GetForSystemWebApps will return a valid WebAppProvider in Lacros.
77 static void EnableSystemWebAppsInLacrosForTesting();
78#endif
79
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:4180 static WebAppProvider* GetForWebContents(content::WebContents* web_contents);
Nigel Tao192c3302018-07-19 15:17:2881
Alan Cutterf3d9aba72020-12-03 10:19:3182 using OsIntegrationManagerFactory =
83 std::unique_ptr<OsIntegrationManager> (*)(Profile*);
84 static void SetOsIntegrationManagerFactoryForTesting(
85 OsIntegrationManagerFactory factory);
86
Giovanni Ortuño Urquidi8ea2f1e2018-08-06 01:13:0587 explicit WebAppProvider(Profile* profile);
Haben Fotoe3d073b2020-10-06 01:22:5888 WebAppProvider(const WebAppProvider&) = delete;
89 WebAppProvider& operator=(const WebAppProvider&) = delete;
Alexey Baskakovd0392752019-01-10 08:38:2890 ~WebAppProvider() override;
91
Christopher Lam4ce340fd2019-07-23 08:07:3292 // Start the Web App system. This will run subsystem startup tasks.
93 void Start();
Nigel Tao192c3302018-07-19 15:17:2894
Song Fangzhen59c5ae282021-07-13 07:36:2795 // The app registry model.
96 WebAppRegistrar& registrar();
97 // The app registry controller.
98 AppRegistryController& registry_controller();
Song Fangzhen1182ac9c2021-07-28 18:05:5499 // UIs can use WebAppInstallManager for user-initiated Web Apps install.
100 WebAppInstallManager& install_manager();
Song Fangzhen59c5ae282021-07-13 07:36:27101 // Implements persistence for Web Apps install.
102 InstallFinalizer& install_finalizer();
103 // Keeps app metadata up to date with site manifests.
104 ManifestUpdateManager& manifest_update_manager();
105 // Clients can use ExternallyManagedAppManager to install, uninstall, and
106 // update Web Apps.
107 ExternallyManagedAppManager& externally_managed_app_manager();
108 // Clients can use WebAppPolicyManager to request updates of policy installed
109 // Web Apps.
110 WebAppPolicyManager& policy_manager();
111
112 WebAppUiManager& ui_manager();
113
114 WebAppAudioFocusIdMap& audio_focus_id_map();
115
116 // Implements fetching of app icons.
Song Fangzhencffb8852021-08-05 03:51:18117 WebAppIconManager& icon_manager();
Song Fangzhen59c5ae282021-07-13 07:36:27118
119 SystemWebAppManager& system_web_app_manager();
120
121 // Manage all OS hooks that need to be deployed during Web Apps install
122 OsIntegrationManager& os_integration_manager();
Alexey Baskakov5f15de12019-07-10 00:36:15123
Christopher Lam342a6f82019-05-17 05:48:35124 // KeyedService:
125 void Shutdown() override;
126
Alexey Baskakov396edb92018-08-02 05:11:42127 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
Alexey Baskakov396edb92018-08-02 05:11:42128
Eric Willigers2cd31922019-06-26 00:53:34129 // Signals when app registry becomes ready.
130 const base::OneShotEvent& on_registry_ready() const {
131 return on_registry_ready_;
132 }
Alexey Baskakov7f4bffa2019-01-08 02:12:38133
Daniel Murphy3657906d2021-04-13 20:33:12134 PreinstalledWebAppManager& preinstalled_web_app_manager() {
135 return *preinstalled_web_app_manager_;
Alan Cutter440d8bdc2020-07-21 04:06:50136 }
137
Alexey Baskakovd0392752019-01-10 08:38:28138 protected:
Christopher Lam4ce340fd2019-07-23 08:07:32139 virtual void StartImpl();
Song Fangzhen064194e2021-05-07 05:24:52140 void WaitForExtensionSystemReady();
141 void OnExtensionSystemReady();
Christopher Lam4ce340fd2019-07-23 08:07:32142
Alan Cutter193f9d82019-08-01 03:19:50143 // Create subsystems that work with either BMO and Extension backends.
144 void CreateCommonSubsystems(Profile* profile);
Alexey Baskakovfd3894e2018-10-16 06:09:58145 // Create extension-independent subsystems.
146 void CreateWebAppsSubsystems(Profile* profile);
Alexey Baskakovecee29442020-11-05 07:30:20147
Christopher Lam4ce340fd2019-07-23 08:07:32148 // Wire together subsystems but do not start them (yet).
Christopher Lama45ea6d2019-07-08 07:22:05149 void ConnectSubsystems();
150
Alexey Baskakov27f14d42019-09-20 07:09:48151 // Start registry controller. All other subsystems depend on it.
152 void StartRegistryController();
153 void OnRegistryControllerReady();
Alexey Baskakov7f4bffa2019-01-08 02:12:38154
Alexey Baskakov0818deee2019-08-06 18:33:33155 void CheckIsConnected() const;
156
Alexey Baskakovfd3894e2018-10-16 06:09:58157 // New extension-independent subsystems:
Alexey Baskakovac8c4b02018-11-07 06:10:02158 std::unique_ptr<WebAppDatabaseFactory> database_factory_;
Daniel Murphybd8993a5a02020-12-17 18:10:10159 std::unique_ptr<WebAppMover> web_app_mover_;
Alexey Baskakovfd3894e2018-10-16 06:09:58160
Alan Cutter6e07e3b2019-08-09 05:11:58161 // Generalized subsystems:
Song Fangzhened83c142021-07-13 03:21:44162 std::unique_ptr<WebAppRegistrar> registrar_;
Alexey Baskakov27f14d42019-09-20 07:09:48163 std::unique_ptr<AppRegistryController> registry_controller_;
Daniel Murphy3657906d2021-04-13 20:33:12164 std::unique_ptr<PreinstalledWebAppManager> preinstalled_web_app_manager_;
Song Fangzhencffb8852021-08-05 03:51:18165 std::unique_ptr<WebAppIconManager> icon_manager_;
Alan Cutter6e07e3b2019-08-09 05:11:58166 std::unique_ptr<InstallFinalizer> install_finalizer_;
Alan Cutterf2d2485f2019-09-05 07:11:13167 std::unique_ptr<ManifestUpdateManager> manifest_update_manager_;
Daniel Murphy6294817d2021-04-13 22:28:13168 std::unique_ptr<ExternallyManagedAppManager> externally_managed_app_manager_;
Christopher Lamdc3093512019-02-26 04:17:44169 std::unique_ptr<SystemWebAppManager> system_web_app_manager_;
Alan Cutter6e07e3b2019-08-09 05:11:58170 std::unique_ptr<WebAppAudioFocusIdMap> audio_focus_id_map_;
171 std::unique_ptr<WebAppInstallManager> install_manager_;
Nigel Tao192c3302018-07-19 15:17:28172 std::unique_ptr<WebAppPolicyManager> web_app_policy_manager_;
Alan Cutter6e07e3b2019-08-09 05:11:58173 std::unique_ptr<WebAppUiManager> ui_manager_;
phillis550e3712020-06-19 02:17:27174 std::unique_ptr<OsIntegrationManager> os_integration_manager_;
Nigel Tao192c3302018-07-19 15:17:28175
Eric Willigers2cd31922019-06-26 00:53:34176 base::OneShotEvent on_registry_ready_;
Alexey Baskakov7f4bffa2019-01-08 02:12:38177
Eric Willigers5a71d192019-10-08 02:36:40178 Profile* const profile_;
Alexey Baskakovda220372019-01-08 00:21:16179
Christopher Lam4ce340fd2019-07-23 08:07:32180 // Ensures that ConnectSubsystems() is not called after Start().
Christopher Lama45ea6d2019-07-08 07:22:05181 bool started_ = false;
Christopher Lam4ce340fd2019-07-23 08:07:32182 bool connected_ = false;
Christopher Lama45ea6d2019-07-08 07:22:05183
Song Fangzhen064194e2021-05-07 05:24:52184 bool skip_awaiting_extension_system_ = false;
185
Dominick Ng7b4ad0b82018-08-03 15:29:10186 base::WeakPtrFactory<WebAppProvider> weak_ptr_factory_{this};
Nigel Tao192c3302018-07-19 15:17:28187};
188
189} // namespace web_app
190
191#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_