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>
9 #include <vector>
10 
11 #include "base/memory/weak_ptr.h"
12 #include "base/one_shot_event.h"
13 #include "chrome/browser/web_applications/components/app_registrar.h"
14 #include "chrome/browser/web_applications/components/pending_app_manager.h"
15 #include "chrome/browser/web_applications/components/web_app_id.h"
16 #include "chrome/browser/web_applications/components/web_app_provider_base.h"
17 
18 class Profile;
19 
20 namespace content {
21 class WebContents;
22 }
23 
24 namespace user_prefs {
25 class PrefRegistrySyncable;
26 }
27 
28 namespace web_app {
29 
30 // Forward declarations of generalized interfaces.
31 class AppRegistryController;
32 class AppIconManager;
33 class ExternalWebAppManager;
34 class InstallFinalizer;
35 class ManifestUpdateManager;
36 class SystemWebAppManager;
37 class WebAppAudioFocusIdMap;
38 class WebAppInstallManager;
39 class WebAppPolicyManager;
40 class WebAppUiManager;
41 class OsIntegrationManager;
42 
43 // Forward declarations for new extension-independent subsystems.
44 class WebAppDatabaseFactory;
45 class WebAppMigrationManager;
46 class WebAppMigrationUserDisplayModeCleanUp;
47 
48 // Connects Web App features, such as the installation of default and
49 // policy-managed web apps, with Profiles (as WebAppProvider is a
50 // Profile-linked KeyedService) and their associated PrefService.
51 //
52 // Lifecycle notes:
53 // All subsystems are constructed independently of each other in the
54 // WebAppProvider constructor.
55 // Subsystem construction should have no side effects and start no tasks.
56 // Tests can replace any of the subsystems before Start() is called.
57 // Similarly, in destruction, subsystems should not refer to each other.
58 class WebAppProvider : public WebAppProviderBase {
59  public:
60   static WebAppProvider* Get(Profile* profile);
61   static WebAppProvider* GetForWebContents(content::WebContents* web_contents);
62 
63   explicit WebAppProvider(Profile* profile);
64   WebAppProvider(const WebAppProvider&) = delete;
65   WebAppProvider& operator=(const WebAppProvider&) = delete;
66   ~WebAppProvider() override;
67 
68   // Start the Web App system. This will run subsystem startup tasks.
69   void Start();
70 
71   // WebAppProviderBase:
72   AppRegistrar& registrar() override;
73   AppRegistryController& registry_controller() override;
74   InstallManager& install_manager() override;
75   InstallFinalizer& install_finalizer() override;
76   ManifestUpdateManager& manifest_update_manager() override;
77   PendingAppManager& pending_app_manager() override;
78   WebAppPolicyManager& policy_manager() override;
79   WebAppUiManager& ui_manager() override;
80   WebAppAudioFocusIdMap& audio_focus_id_map() override;
81   AppIconManager& icon_manager() override;
82   SystemWebAppManager& system_web_app_manager() override;
83   OsIntegrationManager& os_integration_manager() override;
84 
85   // KeyedService:
86   void Shutdown() override;
87 
88   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
89 
90   // Signals when app registry becomes ready.
on_registry_ready()91   const base::OneShotEvent& on_registry_ready() const {
92     return on_registry_ready_;
93   }
94 
external_web_app_manager_for_testing()95   ExternalWebAppManager& external_web_app_manager_for_testing() {
96     return *external_web_app_manager_;
97   }
98 
99  protected:
100   virtual void StartImpl();
101   void OnDatabaseMigrationCompleted(bool success);
102 
103   // Create subsystems that work with either BMO and Extension backends.
104   void CreateCommonSubsystems(Profile* profile);
105   // Create extension-independent subsystems.
106   void CreateWebAppsSubsystems(Profile* profile);
107 
108   // Create legacy extension-based subsystems.
109   void CreateBookmarkAppsSubsystems(Profile* profile);
110   std::unique_ptr<InstallFinalizer> CreateBookmarkAppInstallFinalizer(
111       Profile* profile);
112 
113   // Wire together subsystems but do not start them (yet).
114   void ConnectSubsystems();
115 
116   // Start registry controller. All other subsystems depend on it.
117   void StartRegistryController();
118   void OnRegistryControllerReady();
119 
120   void CheckIsConnected() const;
121 
122   // New extension-independent subsystems:
123   std::unique_ptr<WebAppDatabaseFactory> database_factory_;
124   // migration_manager_ can be nullptr if no migration needed.
125   std::unique_ptr<WebAppMigrationManager> migration_manager_;
126   // user_display_mode_migration_issue_ can be nullptr if no clean up needed.
127   std::unique_ptr<WebAppMigrationUserDisplayModeCleanUp>
128       migration_user_display_mode_clean_up_;
129 
130   // Generalized subsystems:
131   std::unique_ptr<AppRegistrar> registrar_;
132   std::unique_ptr<AppRegistryController> registry_controller_;
133   std::unique_ptr<ExternalWebAppManager> external_web_app_manager_;
134   std::unique_ptr<AppIconManager> icon_manager_;
135   std::unique_ptr<InstallFinalizer> install_finalizer_;
136   std::unique_ptr<ManifestUpdateManager> manifest_update_manager_;
137   std::unique_ptr<PendingAppManager> pending_app_manager_;
138   std::unique_ptr<SystemWebAppManager> system_web_app_manager_;
139   std::unique_ptr<WebAppAudioFocusIdMap> audio_focus_id_map_;
140   std::unique_ptr<WebAppInstallManager> install_manager_;
141   std::unique_ptr<WebAppPolicyManager> web_app_policy_manager_;
142   std::unique_ptr<WebAppUiManager> ui_manager_;
143   std::unique_ptr<OsIntegrationManager> os_integration_manager_;
144 
145   base::OneShotEvent on_registry_ready_;
146 
147   Profile* const profile_;
148 
149   // Ensures that ConnectSubsystems() is not called after Start().
150   bool started_ = false;
151   bool connected_ = false;
152 
153   base::WeakPtrFactory<WebAppProvider> weak_ptr_factory_{this};
154 
155 };
156 
157 }  // namespace web_app
158 
159 #endif  // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_
160