blob: ad6a7dbdcb35eeb8bad48afa790c066a29c2731f [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);
Nigel Tao192c3302018-07-19 15:17:2858
Giovanni Ortuño Urquidi4f86bf272018-07-31 02:09:0359 // Clients can use PendingAppManager to install, uninstall, and update
60 // Web Apps.
61 PendingAppManager& pending_app_manager() { return *pending_app_manager_; }
62
Nigel Tao192c3302018-07-19 15:17:2863 ~WebAppProvider() override;
64
Alexey Baskakov396edb92018-08-02 05:11:4265 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
Alexey Baskakovfbe86f42018-12-10 00:56:0366 static WebAppTabHelperBase* CreateTabHelper(
67 content::WebContents* web_contents);
Alexey Baskakov396edb92018-08-02 05:11:4268
Alexey Baskakovfd3894e2018-10-16 06:09:5869 // Returns true if a bookmark can be installed for a given |web_contents|.
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:4170 static bool CanInstallWebApp(content::WebContents* web_contents);
Alexey Baskakovfd3894e2018-10-16 06:09:5871
72 // Starts a bookmark installation process for a given |web_contents|.
73 static void InstallWebApp(content::WebContents* web_contents,
74 bool force_shortcut_app);
75
Giovanni Ortuño Urquidifd7ed2862018-08-29 13:26:4976 void Reset();
77
78 // content::NotificationObserver
79 void Observe(int type,
80 const content::NotificationSource& source,
81 const content::NotificationDetails& details) override;
Giovanni Ortuño Urquidi2fb415b02018-08-22 07:18:5182
Alexey Baskakov7f4bffa2019-01-08 02:12:3883 // Fires when app registry becomes ready.
84 // Consider to use base::ObserverList or extensions::OneShotEvent if many
85 // subscribers needed.
86 void SetRegistryReadyCallback(base::OnceClosure callback);
87
Alexey Baskakovda220372019-01-08 00:21:1688 // Count a number of all apps which are installed by user (non-default).
Alexey Baskakov7f4bffa2019-01-08 02:12:3889 // Requires app registry to be in a ready state.
Alexey Baskakovda220372019-01-08 00:21:1690 int CountUserInstalledApps() const;
91
Nigel Tao192c3302018-07-19 15:17:2892 private:
Alexey Baskakovfd3894e2018-10-16 06:09:5893 // Create extension-independent subsystems.
94 void CreateWebAppsSubsystems(Profile* profile);
95 // ... or create legacy extension-based subsystems.
96 void CreateBookmarkAppsSubsystems(Profile* profile);
97
Alexey Baskakov7f4bffa2019-01-08 02:12:3898 void OnRegistryReady();
99
Dominick Ng4b176fd2018-08-20 16:15:56100 void OnScanForExternalWebApps(
Dominick Ng7b4ad0b82018-08-03 15:29:10101 std::vector<web_app::PendingAppManager::AppInfo>);
102
Alexey Baskakovfd3894e2018-10-16 06:09:58103 // New extension-independent subsystems:
Becca Hughes6f494292018-12-19 10:57:21104 std::unique_ptr<WebAppAudioFocusIdMap> audio_focus_id_map_;
Alexey Baskakovac8c4b02018-11-07 06:10:02105 std::unique_ptr<WebAppDatabaseFactory> database_factory_;
106 std::unique_ptr<WebAppDatabase> database_;
Alexey Baskakovfd3894e2018-10-16 06:09:58107 std::unique_ptr<WebAppRegistrar> registrar_;
Alexey Baskakovf14ab1872018-11-28 07:37:19108 std::unique_ptr<WebAppIconManager> icon_manager_;
Alexey Baskakovfd3894e2018-10-16 06:09:58109
110 // New generalized subsystems:
111 std::unique_ptr<InstallManager> install_manager_;
Giovanni Ortuño Urquidi4f86bf272018-07-31 02:09:03112 std::unique_ptr<PendingAppManager> pending_app_manager_;
Alexey Baskakovfd3894e2018-10-16 06:09:58113
114 // Legacy extension-based subsystems:
Nigel Tao192c3302018-07-19 15:17:28115 std::unique_ptr<WebAppPolicyManager> web_app_policy_manager_;
Christopher Lam0999fe352018-09-24 05:01:26116 std::unique_ptr<SystemWebAppManager> system_web_app_manager_;
Nigel Tao192c3302018-07-19 15:17:28117
Alexey Baskakov3a5a2cf2018-10-10 02:44:43118 content::NotificationRegistrar notification_registrar_;
Giovanni Ortuño Urquidifd7ed2862018-08-29 13:26:49119
Alexey Baskakov7f4bffa2019-01-08 02:12:38120 base::OnceClosure registry_ready_callback_;
121 bool registry_is_ready_ = false;
122
Alexey Baskakovda220372019-01-08 00:21:16123 Profile* profile_;
124
Dominick Ng7b4ad0b82018-08-03 15:29:10125 base::WeakPtrFactory<WebAppProvider> weak_ptr_factory_{this};
126
Nigel Tao192c3302018-07-19 15:17:28127 DISALLOW_COPY_AND_ASSIGN(WebAppProvider);
128};
129
130} // namespace web_app
131
132#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_PROVIDER_H_