blob: 2b91bad2570cf3bf15ca06400fb5a06f10997848 [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
Alexey Baskakov7f4bffa2019-01-08 02:12:3811#include "base/callback.h"
Nigel Tao192c3302018-07-19 15:17:2812#include "base/macros.h"
Dominick Ng7b4ad0b82018-08-03 15:29:1013#include "base/memory/weak_ptr.h"
14#include "chrome/browser/web_applications/components/pending_app_manager.h"
Becca Hughes6f494292018-12-19 10:57:2115#include "chrome/browser/web_applications/components/web_app_helpers.h"
Nigel Tao192c3302018-07-19 15:17:2816#include "components/keyed_service/core/keyed_service.h"
Giovanni Ortuño Urquidifd7ed2862018-08-29 13:26:4917#include "content/public/browser/notification_observer.h"
18#include "content/public/browser/notification_registrar.h"
Nigel Tao192c3302018-07-19 15:17:2819
20class Profile;
21
Alexey Baskakovfd3894e2018-10-16 06:09:5822namespace content {
23class WebContents;
24}
25
Alexey Baskakov396edb92018-08-02 05:11:4226namespace user_prefs {
27class PrefRegistrySyncable;
28}
29
Nigel Tao192c3302018-07-19 15:17:2830namespace web_app {
31
Alexey Baskakovfd3894e2018-10-16 06:09:5832// Forward declarations of generalized interfaces.
Giovanni Ortuño Urquidi4f86bf272018-07-31 02:09:0333class PendingAppManager;
Alexey Baskakovfd3894e2018-10-16 06:09:5834class InstallManager;
Becca Hughes6f494292018-12-19 10:57:2135class WebAppAudioFocusIdMap;
Alexey Baskakovfbe86f42018-12-10 00:56:0336class WebAppTabHelperBase;
Alexey Baskakovfd3894e2018-10-16 06:09:5837
38// Forward declarations for new extension-independent subsystems.
Alexey Baskakovac8c4b02018-11-07 06:10:0239class WebAppDatabase;
40class WebAppDatabaseFactory;
Alexey Baskakovf14ab1872018-11-28 07:37:1941class WebAppIconManager;
Alexey Baskakovfd3894e2018-10-16 06:09:5842class WebAppRegistrar;
43
44// Forward declarations for legacy extension-based subsystems.
Nigel Tao192c3302018-07-19 15:17:2845class WebAppPolicyManager;
Christopher Lam0999fe352018-09-24 05:01:2646class SystemWebAppManager;
Nigel Tao192c3302018-07-19 15:17:2847
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.
Giovanni Ortuño Urquidifd7ed2862018-08-29 13:26:4951class WebAppProvider : public KeyedService,
52 public content::NotificationObserver {
Nigel Tao192c3302018-07-19 15:17:2853 public:
54 static WebAppProvider* Get(Profile* profile);
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:4155 static WebAppProvider* GetForWebContents(content::WebContents* web_contents);
Nigel Tao192c3302018-07-19 15:17:2856
Giovanni Ortuño Urquidi8ea2f1e2018-08-06 01:13:0557 explicit WebAppProvider(Profile* profile);
Alexey Baskakovd0392752019-01-10 08:38:2858 ~WebAppProvider() override;
59
60 // 1st pass: Just create subsystems.
61 void CreateSubsystems();
62 // 2nd pass: Initialize subsystems.
63 void Init();
Nigel Tao192c3302018-07-19 15:17:2864
Giovanni Ortuño Urquidi4f86bf272018-07-31 02:09:0365 // Clients can use PendingAppManager to install, uninstall, and update
66 // Web Apps.
67 PendingAppManager& pending_app_manager() { return *pending_app_manager_; }
68
Alexey Baskakov396edb92018-08-02 05:11:4269 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
Alexey Baskakovfbe86f42018-12-10 00:56:0370 static WebAppTabHelperBase* CreateTabHelper(
71 content::WebContents* web_contents);
Alexey Baskakov396edb92018-08-02 05:11:4272
Alexey Baskakovfd3894e2018-10-16 06:09:5873 // Returns true if a bookmark can be installed for a given |web_contents|.
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:4174 static bool CanInstallWebApp(content::WebContents* web_contents);
Alexey Baskakovfd3894e2018-10-16 06:09:5875
76 // Starts a bookmark installation process for a given |web_contents|.
77 static void InstallWebApp(content::WebContents* web_contents,
78 bool force_shortcut_app);
79
Giovanni Ortuño Urquidifd7ed2862018-08-29 13:26:4980 void Reset();
81
82 // content::NotificationObserver
83 void Observe(int type,
84 const content::NotificationSource& source,
85 const content::NotificationDetails& details) override;
Giovanni Ortuño Urquidi2fb415b02018-08-22 07:18:5186
Alexey Baskakov7f4bffa2019-01-08 02:12:3887 // Fires when app registry becomes ready.
88 // Consider to use base::ObserverList or extensions::OneShotEvent if many
89 // subscribers needed.
90 void SetRegistryReadyCallback(base::OnceClosure callback);
91
Alexey Baskakovda220372019-01-08 00:21:1692 // Count a number of all apps which are installed by user (non-default).
Alexey Baskakov7f4bffa2019-01-08 02:12:3893 // Requires app registry to be in a ready state.
Alexey Baskakovda220372019-01-08 00:21:1694 int CountUserInstalledApps() const;
95
Alexey Baskakovd0392752019-01-10 08:38:2896 protected:
Alexey Baskakovfd3894e2018-10-16 06:09:5897 // Create extension-independent subsystems.
98 void CreateWebAppsSubsystems(Profile* profile);
99 // ... or create legacy extension-based subsystems.
100 void CreateBookmarkAppsSubsystems(Profile* profile);
101
Alexey Baskakov7f4bffa2019-01-08 02:12:38102 void OnRegistryReady();
103
Dominick Ng4b176fd2018-08-20 16:15:56104 void OnScanForExternalWebApps(
Dominick Ng7b4ad0b82018-08-03 15:29:10105 std::vector<web_app::PendingAppManager::AppInfo>);
106
Alexey Baskakovfd3894e2018-10-16 06:09:58107 // New extension-independent subsystems:
Becca Hughes6f494292018-12-19 10:57:21108 std::unique_ptr<WebAppAudioFocusIdMap> audio_focus_id_map_;
Alexey Baskakovac8c4b02018-11-07 06:10:02109 std::unique_ptr<WebAppDatabaseFactory> database_factory_;
110 std::unique_ptr<WebAppDatabase> database_;
Alexey Baskakovfd3894e2018-10-16 06:09:58111 std::unique_ptr<WebAppRegistrar> registrar_;
Alexey Baskakovf14ab1872018-11-28 07:37:19112 std::unique_ptr<WebAppIconManager> icon_manager_;
Alexey Baskakovfd3894e2018-10-16 06:09:58113
114 // New generalized subsystems:
115 std::unique_ptr<InstallManager> install_manager_;
Giovanni Ortuño Urquidi4f86bf272018-07-31 02:09:03116 std::unique_ptr<PendingAppManager> pending_app_manager_;
Alexey Baskakovfd3894e2018-10-16 06:09:58117
118 // Legacy extension-based subsystems:
Nigel Tao192c3302018-07-19 15:17:28119 std::unique_ptr<WebAppPolicyManager> web_app_policy_manager_;
Christopher Lam0999fe352018-09-24 05:01:26120 std::unique_ptr<SystemWebAppManager> system_web_app_manager_;
Nigel Tao192c3302018-07-19 15:17:28121
Alexey Baskakov3a5a2cf2018-10-10 02:44:43122 content::NotificationRegistrar notification_registrar_;
Giovanni Ortuño Urquidifd7ed2862018-08-29 13:26:49123
Alexey Baskakov7f4bffa2019-01-08 02:12:38124 base::OnceClosure registry_ready_callback_;
125 bool registry_is_ready_ = false;
126
Alexey Baskakovda220372019-01-08 00:21:16127 Profile* profile_;
128
Dominick Ng7b4ad0b82018-08-03 15:29:10129 base::WeakPtrFactory<WebAppProvider> weak_ptr_factory_{this};
130
Nigel Tao192c3302018-07-19 15:17:28131 DISALLOW_COPY_AND_ASSIGN(WebAppProvider);
132};
133
134} // namespace web_app
135
136#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_