Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 1 | // 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 Ng | 7b4ad0b8 | 2018-08-03 15:29:10 | [diff] [blame] | 9 | #include <vector> |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 10 | |
Dominick Ng | 7b4ad0b8 | 2018-08-03 15:29:10 | [diff] [blame] | 11 | #include "base/memory/weak_ptr.h" |
Eric Willigers | 2cd3192 | 2019-06-26 00:53:34 | [diff] [blame] | 12 | #include "base/one_shot_event.h" |
Daniel Murphy | 6294817d | 2021-04-13 22:28:13 | [diff] [blame] | 13 | #include "chrome/browser/web_applications/components/externally_managed_app_manager.h" |
Glen Robertson | 271d133 | 2020-01-28 00:16:02 | [diff] [blame] | 14 | #include "chrome/browser/web_applications/components/web_app_id.h" |
Song Fangzhen | ed83c14 | 2021-07-13 03:21:44 | [diff] [blame] | 15 | #include "chrome/browser/web_applications/web_app_registrar.h" |
Song Fangzhen | 59c5ae28 | 2021-07-13 07:36:27 | [diff] [blame] | 16 | #include "components/keyed_service/core/keyed_service.h" |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 17 | |
| 18 | class Profile; |
| 19 | |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 20 | namespace content { |
| 21 | class WebContents; |
| 22 | } |
| 23 | |
Alexey Baskakov | 396edb9 | 2018-08-02 05:11:42 | [diff] [blame] | 24 | namespace user_prefs { |
| 25 | class PrefRegistrySyncable; |
| 26 | } |
| 27 | |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 28 | namespace web_app { |
| 29 | |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 30 | // Forward declarations of generalized interfaces. |
Alexey Baskakov | 27f14d4 | 2019-09-20 07:09:48 | [diff] [blame] | 31 | class AppRegistryController; |
Song Fangzhen | cffb885 | 2021-08-05 03:51:18 | [diff] [blame^] | 32 | class WebAppIconManager; |
Daniel Murphy | 3657906d | 2021-04-13 20:33:12 | [diff] [blame] | 33 | class PreinstalledWebAppManager; |
Alexey Baskakov | 4859218b | 2019-04-03 22:19:57 | [diff] [blame] | 34 | class InstallFinalizer; |
Alan Cutter | f2d2485f | 2019-09-05 07:11:13 | [diff] [blame] | 35 | class ManifestUpdateManager; |
Alexey Baskakov | 6cf2c15 | 2019-07-26 07:56:49 | [diff] [blame] | 36 | class SystemWebAppManager; |
Becca Hughes | 6f49429 | 2018-12-19 10:57:21 | [diff] [blame] | 37 | class WebAppAudioFocusIdMap; |
Alexey Baskakov | aca7a0c | 2019-07-29 07:34:05 | [diff] [blame] | 38 | class WebAppInstallManager; |
Alan Cutter | 6e07e3b | 2019-08-09 05:11:58 | [diff] [blame] | 39 | class WebAppPolicyManager; |
Alan Cutter | 31b206b | 2019-07-17 16:57:35 | [diff] [blame] | 40 | class WebAppUiManager; |
phillis | 550e371 | 2020-06-19 02:17:27 | [diff] [blame] | 41 | class OsIntegrationManager; |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 42 | |
| 43 | // Forward declarations for new extension-independent subsystems. |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 44 | class WebAppDatabaseFactory; |
Daniel Murphy | bd8993a5a0 | 2020-12-17 18:10:10 | [diff] [blame] | 45 | class WebAppMover; |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 46 | |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 47 | // 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 Lam | a45ea6d | 2019-07-08 07:22:05 | [diff] [blame] | 50 | // |
| 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 Fangzhen | 59c5ae28 | 2021-07-13 07:36:27 | [diff] [blame] | 57 | class WebAppProvider : public KeyedService { |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 58 | public: |
Jiewei Qian | b6cfe54 | 2021-05-31 07:45:45 | [diff] [blame] | 59 | // Deprecated: Use GetForWebApps or GetForSystemWebApps instead. |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 60 | static WebAppProvider* Get(Profile* profile); |
Jiewei Qian | b6cfe54 | 2021-05-31 07:45:45 | [diff] [blame] | 61 | |
| 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 Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 80 | static WebAppProvider* GetForWebContents(content::WebContents* web_contents); |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 81 | |
Alan Cutter | f3d9aba7 | 2020-12-03 10:19:31 | [diff] [blame] | 82 | using OsIntegrationManagerFactory = |
| 83 | std::unique_ptr<OsIntegrationManager> (*)(Profile*); |
| 84 | static void SetOsIntegrationManagerFactoryForTesting( |
| 85 | OsIntegrationManagerFactory factory); |
| 86 | |
Giovanni Ortuño Urquidi | 8ea2f1e | 2018-08-06 01:13:05 | [diff] [blame] | 87 | explicit WebAppProvider(Profile* profile); |
Haben Foto | e3d073b | 2020-10-06 01:22:58 | [diff] [blame] | 88 | WebAppProvider(const WebAppProvider&) = delete; |
| 89 | WebAppProvider& operator=(const WebAppProvider&) = delete; |
Alexey Baskakov | d039275 | 2019-01-10 08:38:28 | [diff] [blame] | 90 | ~WebAppProvider() override; |
| 91 | |
Christopher Lam | 4ce340fd | 2019-07-23 08:07:32 | [diff] [blame] | 92 | // Start the Web App system. This will run subsystem startup tasks. |
| 93 | void Start(); |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 94 | |
Song Fangzhen | 59c5ae28 | 2021-07-13 07:36:27 | [diff] [blame] | 95 | // The app registry model. |
| 96 | WebAppRegistrar& registrar(); |
| 97 | // The app registry controller. |
| 98 | AppRegistryController& registry_controller(); |
Song Fangzhen | 1182ac9c | 2021-07-28 18:05:54 | [diff] [blame] | 99 | // UIs can use WebAppInstallManager for user-initiated Web Apps install. |
| 100 | WebAppInstallManager& install_manager(); |
Song Fangzhen | 59c5ae28 | 2021-07-13 07:36:27 | [diff] [blame] | 101 | // 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 Fangzhen | cffb885 | 2021-08-05 03:51:18 | [diff] [blame^] | 117 | WebAppIconManager& icon_manager(); |
Song Fangzhen | 59c5ae28 | 2021-07-13 07:36:27 | [diff] [blame] | 118 | |
| 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 Baskakov | 5f15de1 | 2019-07-10 00:36:15 | [diff] [blame] | 123 | |
Christopher Lam | 342a6f8 | 2019-05-17 05:48:35 | [diff] [blame] | 124 | // KeyedService: |
| 125 | void Shutdown() override; |
| 126 | |
Alexey Baskakov | 396edb9 | 2018-08-02 05:11:42 | [diff] [blame] | 127 | static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
Alexey Baskakov | 396edb9 | 2018-08-02 05:11:42 | [diff] [blame] | 128 | |
Eric Willigers | 2cd3192 | 2019-06-26 00:53:34 | [diff] [blame] | 129 | // Signals when app registry becomes ready. |
| 130 | const base::OneShotEvent& on_registry_ready() const { |
| 131 | return on_registry_ready_; |
| 132 | } |
Alexey Baskakov | 7f4bffa | 2019-01-08 02:12:38 | [diff] [blame] | 133 | |
Daniel Murphy | 3657906d | 2021-04-13 20:33:12 | [diff] [blame] | 134 | PreinstalledWebAppManager& preinstalled_web_app_manager() { |
| 135 | return *preinstalled_web_app_manager_; |
Alan Cutter | 440d8bdc | 2020-07-21 04:06:50 | [diff] [blame] | 136 | } |
| 137 | |
Alexey Baskakov | d039275 | 2019-01-10 08:38:28 | [diff] [blame] | 138 | protected: |
Christopher Lam | 4ce340fd | 2019-07-23 08:07:32 | [diff] [blame] | 139 | virtual void StartImpl(); |
Song Fangzhen | 064194e | 2021-05-07 05:24:52 | [diff] [blame] | 140 | void WaitForExtensionSystemReady(); |
| 141 | void OnExtensionSystemReady(); |
Christopher Lam | 4ce340fd | 2019-07-23 08:07:32 | [diff] [blame] | 142 | |
Alan Cutter | 193f9d8 | 2019-08-01 03:19:50 | [diff] [blame] | 143 | // Create subsystems that work with either BMO and Extension backends. |
| 144 | void CreateCommonSubsystems(Profile* profile); |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 145 | // Create extension-independent subsystems. |
| 146 | void CreateWebAppsSubsystems(Profile* profile); |
Alexey Baskakov | ecee2944 | 2020-11-05 07:30:20 | [diff] [blame] | 147 | |
Christopher Lam | 4ce340fd | 2019-07-23 08:07:32 | [diff] [blame] | 148 | // Wire together subsystems but do not start them (yet). |
Christopher Lam | a45ea6d | 2019-07-08 07:22:05 | [diff] [blame] | 149 | void ConnectSubsystems(); |
| 150 | |
Alexey Baskakov | 27f14d4 | 2019-09-20 07:09:48 | [diff] [blame] | 151 | // Start registry controller. All other subsystems depend on it. |
| 152 | void StartRegistryController(); |
| 153 | void OnRegistryControllerReady(); |
Alexey Baskakov | 7f4bffa | 2019-01-08 02:12:38 | [diff] [blame] | 154 | |
Alexey Baskakov | 0818deee | 2019-08-06 18:33:33 | [diff] [blame] | 155 | void CheckIsConnected() const; |
| 156 | |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 157 | // New extension-independent subsystems: |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 158 | std::unique_ptr<WebAppDatabaseFactory> database_factory_; |
Daniel Murphy | bd8993a5a0 | 2020-12-17 18:10:10 | [diff] [blame] | 159 | std::unique_ptr<WebAppMover> web_app_mover_; |
Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame] | 160 | |
Alan Cutter | 6e07e3b | 2019-08-09 05:11:58 | [diff] [blame] | 161 | // Generalized subsystems: |
Song Fangzhen | ed83c14 | 2021-07-13 03:21:44 | [diff] [blame] | 162 | std::unique_ptr<WebAppRegistrar> registrar_; |
Alexey Baskakov | 27f14d4 | 2019-09-20 07:09:48 | [diff] [blame] | 163 | std::unique_ptr<AppRegistryController> registry_controller_; |
Daniel Murphy | 3657906d | 2021-04-13 20:33:12 | [diff] [blame] | 164 | std::unique_ptr<PreinstalledWebAppManager> preinstalled_web_app_manager_; |
Song Fangzhen | cffb885 | 2021-08-05 03:51:18 | [diff] [blame^] | 165 | std::unique_ptr<WebAppIconManager> icon_manager_; |
Alan Cutter | 6e07e3b | 2019-08-09 05:11:58 | [diff] [blame] | 166 | std::unique_ptr<InstallFinalizer> install_finalizer_; |
Alan Cutter | f2d2485f | 2019-09-05 07:11:13 | [diff] [blame] | 167 | std::unique_ptr<ManifestUpdateManager> manifest_update_manager_; |
Daniel Murphy | 6294817d | 2021-04-13 22:28:13 | [diff] [blame] | 168 | std::unique_ptr<ExternallyManagedAppManager> externally_managed_app_manager_; |
Christopher Lam | dc309351 | 2019-02-26 04:17:44 | [diff] [blame] | 169 | std::unique_ptr<SystemWebAppManager> system_web_app_manager_; |
Alan Cutter | 6e07e3b | 2019-08-09 05:11:58 | [diff] [blame] | 170 | std::unique_ptr<WebAppAudioFocusIdMap> audio_focus_id_map_; |
| 171 | std::unique_ptr<WebAppInstallManager> install_manager_; |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 172 | std::unique_ptr<WebAppPolicyManager> web_app_policy_manager_; |
Alan Cutter | 6e07e3b | 2019-08-09 05:11:58 | [diff] [blame] | 173 | std::unique_ptr<WebAppUiManager> ui_manager_; |
phillis | 550e371 | 2020-06-19 02:17:27 | [diff] [blame] | 174 | std::unique_ptr<OsIntegrationManager> os_integration_manager_; |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 175 | |
Eric Willigers | 2cd3192 | 2019-06-26 00:53:34 | [diff] [blame] | 176 | base::OneShotEvent on_registry_ready_; |
Alexey Baskakov | 7f4bffa | 2019-01-08 02:12:38 | [diff] [blame] | 177 | |
Eric Willigers | 5a71d19 | 2019-10-08 02:36:40 | [diff] [blame] | 178 | Profile* const profile_; |
Alexey Baskakov | da22037 | 2019-01-08 00:21:16 | [diff] [blame] | 179 | |
Christopher Lam | 4ce340fd | 2019-07-23 08:07:32 | [diff] [blame] | 180 | // Ensures that ConnectSubsystems() is not called after Start(). |
Christopher Lam | a45ea6d | 2019-07-08 07:22:05 | [diff] [blame] | 181 | bool started_ = false; |
Christopher Lam | 4ce340fd | 2019-07-23 08:07:32 | [diff] [blame] | 182 | bool connected_ = false; |
Christopher Lam | a45ea6d | 2019-07-08 07:22:05 | [diff] [blame] | 183 | |
Song Fangzhen | 064194e | 2021-05-07 05:24:52 | [diff] [blame] | 184 | bool skip_awaiting_extension_system_ = false; |
| 185 | |
Dominick Ng | 7b4ad0b8 | 2018-08-03 15:29:10 | [diff] [blame] | 186 | base::WeakPtrFactory<WebAppProvider> weak_ptr_factory_{this}; |
Nigel Tao | 192c330 | 2018-07-19 15:17:28 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | } // namespace web_app |
| 190 | |
| 191 | #endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_ |