1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_
#include <memory>
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/one_shot_event.h"
#include "base/types/pass_key.h"
#include "build/build_config.h"
#include "components/keyed_service/core/keyed_service.h"
class Profile;
namespace content {
class WebContents;
}
namespace web_app {
class AbstractWebAppDatabaseFactory;
class ExtensionsManager;
class ExternallyManagedAppManager;
class FakeWebAppProvider;
class FileUtilsWrapper;
class GeneratedIconFixManager;
class IsolatedWebAppInstallationManager;
class IsolatedWebAppPolicyManager;
class IsolatedWebAppUpdateManager;
class ManifestUpdateManager;
class NavigationCapturingLog;
class OsIntegrationManager;
class PreinstalledWebAppManager;
class VisitedManifestManager;
class WebAppAudioFocusIdMap;
class WebAppCommandManager;
class WebAppCommandScheduler;
class WebAppIconManager;
class WebAppInstallFinalizer;
class WebAppInstallManager;
class WebAppOriginAssociationManager;
class WebAppPolicyManager;
class WebAppRegistrar;
class WebAppRegistrarMutable;
class WebAppSyncBridge;
class WebAppTranslationManager;
class WebAppUiManager;
class WebContentsManager;
class WebAppProfileDeletionManager;
#if BUILDFLAG(IS_CHROMEOS)
class WebAppRunOnOsLoginManager;
class IwaBundleCacheManager;
#endif
// WebAppProvider is the heart of Chrome web app code.
//
// Connects Web App features, such as the installation of default and
// policy-managed web apps, with Profiles (as WebAppProvider is a
// Profile-linked KeyedService) and their associated PrefService.
//
// Lifecycle notes:
// - WebAppProvider and its sub-managers are not ready for use until the
// on_registry_ready() event has fired. Its database must be loaded from
// disk before it can be interacted with.
// Example of waiting for on_registry_ready():
// WebAppProvider* provider = WebAppProvider::GetForWebApps(profile);
// provider->on_registry_ready().Post(
// FROM_HERE,
// base::BindOnce([](WebAppProvider& provider) {
// ...
// }, std::ref(*provider)));
// - All subsystems are constructed independently of each other in the
// WebAppProvider constructor.
// - Subsystem construction should have no side effects and start no tasks.
// - Tests can replace any of the subsystems before Start() is called.
// - Similarly, in destruction, subsystems should not refer to each other.
class WebAppProvider : public KeyedService {
public:
// Deprecated: Use GetForWebApps instead.
static WebAppProvider* GetDeprecated(Profile* profile);
// This returns a WebAppProvider for the given `profile`, or `nullptr` if
// installed web apps are not supported on the given `profile`. Use
// `web_app::AreWebAppsEnabled` to determine if web apps are supported on a
// profile.
// Note: On ChromeOS, to support the system web app implementation, this also
// considers the `profile`'s 'original' profile, if `AreWebAppsEnabled`
// returns `false` for `profile`.
// TODO(https://crbug.com/384063076): Stop returning the WebAppProvider for
// profiles where `AreWebAppsEnabled` returns `false` to support CrOS system
// web apps.
static WebAppProvider* GetForWebApps(Profile* profile);
// Returns the WebAppProvider for the current process.
//
// Avoid using this function where possible and prefer GetForWebApps which
// provides a guarantee they are being called from the correct process.
// TODO(https://crbug.com/384063076): Stop returning the WebAppProvider for
// profiles where `AreWebAppsEnabled` returns `false` to support CrOS system
// web apps.
static WebAppProvider* GetForLocalAppsUnchecked(Profile* profile);
// Return the WebAppProvider for tests. Blocks if the web app registry is not
// yet ready.
// This returns `nullptr` if installed web apps are not supported on the
// given `profile`. Use `web_app::AreWebAppsEnabled` to determine if web apps
// are supported on a profile.
// Note: On ChromeOS, to support the system web app implementation, this also
// considers the `profile`'s 'original' profile, if `AreWebAppsEnabled`
// returns `false` for `profile`.
// TODO(https://crbug.com/384063076): Stop returning the WebAppProvider for
// profiles where `AreWebAppsEnabled` returns `false` to support CrOS system
// web apps.
static WebAppProvider* GetForTest(Profile* profile);
// See `GetForWebApps` above for when this returns `nullptr`.
static WebAppProvider* GetForWebContents(content::WebContents* web_contents);
using OsIntegrationManagerFactory =
std::unique_ptr<OsIntegrationManager> (*)(Profile*);
explicit WebAppProvider(Profile* profile);
WebAppProvider(const WebAppProvider&) = delete;
WebAppProvider& operator=(const WebAppProvider&) = delete;
~WebAppProvider() override;
// Start the Web App system. This will run subsystem startup tasks.
void Start();
// Read/write to web app system should use `scheduler()` to guarantee safe
// access. This is safe to access even if the `WebAppProvider` is not ready.
WebAppCommandScheduler& scheduler();
// This is safe to access even if the `WebAppProvider` is not ready.
WebAppCommandManager& command_manager();
// Web App sub components. These should only be accessed after
// `on_registry_ready()` is signaled.
// Unsafe access to the app registry model. For safe access use locks (see
// chrome/browser/web_applications/locks/ for more info).
WebAppRegistrar& registrar_unsafe();
const WebAppRegistrar& registrar_unsafe() const;
// Must be exclusively accessed by WebAppSyncBridge.
WebAppRegistrarMutable& registrar_mutable(base::PassKey<WebAppSyncBridge>);
// Unsafe access to the WebAppSyncBridge. Reading or data from here should be
// considered an 'uncommitted read', and writing data is unsafe and could
// interfere with other operations. For safe access use locks to ensure no
// operations (like install/update/uninstall/etc) are currently running. See
// chrome/browser/web_applications/locks/ for more info.
WebAppSyncBridge& sync_bridge_unsafe();
// UIs can use WebAppInstallManager for user-initiated Web Apps install.
WebAppInstallManager& install_manager();
// Implements persistence for Web Apps install.
WebAppInstallFinalizer& install_finalizer();
// Keeps app metadata up to date with site manifests.
ManifestUpdateManager& manifest_update_manager();
// Clients can use ExternallyManagedAppManager to install, uninstall, and
// update Web Apps.
ExternallyManagedAppManager& externally_managed_app_manager();
// Clients can use WebAppPolicyManager to request updates of policy installed
// Web Apps.
WebAppPolicyManager& policy_manager();
// `IsolatedWebAppInstallationManager` is the entry point for Isolated Web App
// installation.
IsolatedWebAppInstallationManager& isolated_web_app_installation_manager();
// Keeps Isolated Web Apps up to date by regularly checking for updates,
// downloading them, and applying them.
IsolatedWebAppUpdateManager& iwa_update_manager();
#if BUILDFLAG(IS_CHROMEOS)
// Runs web apps on OS login.
WebAppRunOnOsLoginManager& run_on_os_login_manager();
// Isolated Web App bundle cache manager.
IwaBundleCacheManager& iwa_cache_manager();
#endif
IsolatedWebAppPolicyManager& iwa_policy_manager();
WebAppUiManager& ui_manager();
WebAppAudioFocusIdMap& audio_focus_id_map();
// Interface for file access, allowing mocking for tests. `scoped_refptr` for
// thread safety as this is used on other task runners.
scoped_refptr<FileUtilsWrapper> file_utils();
// Implements fetching of app icons.
WebAppIconManager& icon_manager();
WebAppTranslationManager& translation_manager();
// Manage all OS hooks that need to be deployed during Web Apps install
OsIntegrationManager& os_integration_manager();
const OsIntegrationManager& os_integration_manager() const;
WebAppOriginAssociationManager& origin_association_manager();
WebContentsManager& web_contents_manager();
PreinstalledWebAppManager& preinstalled_web_app_manager();
ExtensionsManager& extensions_manager();
GeneratedIconFixManager& generated_icon_fix_manager();
AbstractWebAppDatabaseFactory& database_factory();
VisitedManifestManager& visited_manifest_manager();
NavigationCapturingLog& navigation_capturing_log();
// KeyedService:
void Shutdown() override;
// Signals when app registry becomes ready.
const base::OneShotEvent& on_registry_ready() const {
return on_registry_ready_;
}
// Signals when external app managers have finished calling
// `SynchronizeInstalledApps`, which means that all installs or uninstalls for
// external managers have been scheduled. Specifically these calls are
// triggered from the PreinstalledWebAppManager and the WebAppPolicyManager.
// Note: This does not include the call from the ChromeOS SystemWebAppManager,
// which is a separate keyed service.
const base::OneShotEvent& on_external_managers_synchronized() const {
return on_external_managers_synchronized_;
}
// Returns whether the app registry is ready.
bool is_registry_ready() const { return is_registry_ready_; }
base::WeakPtr<WebAppProvider> AsWeakPtr();
// Returns a nullptr in the default implementation
virtual FakeWebAppProvider* AsFakeWebAppProviderForTesting();
#if BUILDFLAG(IS_MAC)
void DoDelayedPostStartupWork();
#endif
protected:
virtual void StartImpl();
void CreateSubsystems(Profile* profile);
// Wire together subsystems but do not start them (yet).
void ConnectSubsystems();
// Start sync bridge. All other subsystems depend on it.
void StartSyncBridge();
void OnSyncBridgeReady();
void CheckIsConnected() const;
std::unique_ptr<AbstractWebAppDatabaseFactory> database_factory_;
std::unique_ptr<WebAppRegistrarMutable> registrar_;
std::unique_ptr<WebAppSyncBridge> sync_bridge_;
std::unique_ptr<PreinstalledWebAppManager> preinstalled_web_app_manager_;
std::unique_ptr<WebAppIconManager> icon_manager_;
std::unique_ptr<WebAppTranslationManager> translation_manager_;
std::unique_ptr<WebAppInstallFinalizer> install_finalizer_;
std::unique_ptr<ManifestUpdateManager> manifest_update_manager_;
std::unique_ptr<ExternallyManagedAppManager> externally_managed_app_manager_;
std::unique_ptr<WebAppAudioFocusIdMap> audio_focus_id_map_;
std::unique_ptr<WebAppInstallManager> install_manager_;
std::unique_ptr<WebAppPolicyManager> web_app_policy_manager_;
std::unique_ptr<IsolatedWebAppInstallationManager>
isolated_web_app_installation_manager_;
std::unique_ptr<IsolatedWebAppUpdateManager> iwa_update_manager_;
std::unique_ptr<IsolatedWebAppPolicyManager> isolated_web_app_policy_manager_;
#if BUILDFLAG(IS_CHROMEOS)
std::unique_ptr<WebAppRunOnOsLoginManager> web_app_run_on_os_login_manager_;
std::unique_ptr<IwaBundleCacheManager> iwa_cache_manager_;
#endif // BUILDFLAG(IS_CHROMEOS)
std::unique_ptr<WebAppUiManager> ui_manager_;
std::unique_ptr<OsIntegrationManager> os_integration_manager_;
std::unique_ptr<WebAppCommandManager> command_manager_;
std::unique_ptr<WebAppCommandScheduler> command_scheduler_;
std::unique_ptr<WebAppOriginAssociationManager> origin_association_manager_;
std::unique_ptr<WebContentsManager> web_contents_manager_;
std::unique_ptr<ExtensionsManager> extensions_manager_;
std::unique_ptr<GeneratedIconFixManager> generated_icon_fix_manager_;
scoped_refptr<FileUtilsWrapper> file_utils_;
std::unique_ptr<VisitedManifestManager> visited_manifest_manager_;
std::unique_ptr<NavigationCapturingLog> navigation_capturing_log_;
std::unique_ptr<WebAppProfileDeletionManager> profile_deletion_manager_;
base::OneShotEvent on_registry_ready_;
base::OneShotEvent on_external_managers_synchronized_;
const raw_ptr<Profile> profile_;
// Ensures that ConnectSubsystems() is not called after Start().
bool started_ = false;
bool connected_ = false;
bool is_registry_ready_ = false;
base::WeakPtrFactory<WebAppProvider> weak_ptr_factory_{this};
};
} // namespace web_app
#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_
|