Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / runtime / runtime_api.h
blob6adfe2d2bb548a7df7cc5df64e619e0d7e2d98ed
1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_
6 #define EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_
8 #include <string>
10 #include "base/scoped_observer.h"
11 #include "content/public/browser/notification_observer.h"
12 #include "content/public/browser/notification_registrar.h"
13 #include "extensions/browser/api/runtime/runtime_api_delegate.h"
14 #include "extensions/browser/browser_context_keyed_api_factory.h"
15 #include "extensions/browser/extension_function.h"
16 #include "extensions/browser/extension_registry_observer.h"
17 #include "extensions/browser/process_manager.h"
18 #include "extensions/browser/process_manager_observer.h"
19 #include "extensions/browser/update_observer.h"
20 #include "extensions/common/api/runtime.h"
22 namespace base {
23 class Version;
26 namespace content {
27 class BrowserContext;
30 namespace extensions {
32 namespace api {
33 namespace runtime {
34 struct PlatformInfo;
38 class Extension;
39 class ExtensionHost;
40 class ExtensionRegistry;
42 // Runtime API dispatches onStartup, onInstalled, and similar events to
43 // extensions. There is one instance shared between a browser context and
44 // its related incognito instance.
45 class RuntimeAPI : public BrowserContextKeyedAPI,
46 public content::NotificationObserver,
47 public ExtensionRegistryObserver,
48 public UpdateObserver,
49 public ProcessManagerObserver {
50 public:
51 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance();
53 explicit RuntimeAPI(content::BrowserContext* context);
54 ~RuntimeAPI() override;
56 // content::NotificationObserver overrides:
57 void Observe(int type,
58 const content::NotificationSource& source,
59 const content::NotificationDetails& details) override;
61 void ReloadExtension(const std::string& extension_id);
62 bool CheckForUpdates(const std::string& extension_id,
63 const RuntimeAPIDelegate::UpdateCheckCallback& callback);
64 void OpenURL(const GURL& uninstall_url);
65 bool GetPlatformInfo(api::runtime::PlatformInfo* info);
66 bool RestartDevice(std::string* error_message);
67 bool OpenOptionsPage(const Extension* extension);
69 private:
70 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>;
72 // ExtensionRegistryObserver implementation.
73 void OnExtensionLoaded(content::BrowserContext* browser_context,
74 const Extension* extension) override;
75 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
76 const Extension* extension,
77 bool is_update,
78 bool from_ephemeral,
79 const std::string& old_name) override;
80 void OnExtensionUninstalled(content::BrowserContext* browser_context,
81 const Extension* extension,
82 UninstallReason reason) override;
84 // BrowserContextKeyedAPI implementation:
85 static const char* service_name() { return "RuntimeAPI"; }
86 static const bool kServiceRedirectedInIncognito = true;
87 static const bool kServiceIsNULLWhileTesting = true;
88 void Shutdown() override;
90 // extensions::UpdateObserver overrides:
91 void OnAppUpdateAvailable(const Extension* extension) override;
92 void OnChromeUpdateAvailable() override;
94 // ProcessManagerObserver implementation:
95 void OnBackgroundHostStartup(const Extension* extension) override;
97 scoped_ptr<RuntimeAPIDelegate> delegate_;
99 content::BrowserContext* browser_context_;
101 // True if we should dispatch the chrome.runtime.onInstalled event with
102 // reason "chrome_update" upon loading each extension.
103 bool dispatch_chrome_updated_event_;
105 content::NotificationRegistrar registrar_;
107 // Listen to extension notifications.
108 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
109 extension_registry_observer_;
110 ScopedObserver<ProcessManager, ProcessManagerObserver>
111 process_manager_observer_;
113 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI);
116 template <>
117 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies();
119 class RuntimeEventRouter {
120 public:
121 // Dispatches the onStartup event to all currently-loaded extensions.
122 static void DispatchOnStartupEvent(content::BrowserContext* context,
123 const std::string& extension_id);
125 // Dispatches the onInstalled event to the given extension.
126 static void DispatchOnInstalledEvent(content::BrowserContext* context,
127 const std::string& extension_id,
128 const base::Version& old_version,
129 bool chrome_updated);
131 // Dispatches the onUpdateAvailable event to the given extension.
132 static void DispatchOnUpdateAvailableEvent(
133 content::BrowserContext* context,
134 const std::string& extension_id,
135 const base::DictionaryValue* manifest);
137 // Dispatches the onBrowserUpdateAvailable event to all extensions.
138 static void DispatchOnBrowserUpdateAvailableEvent(
139 content::BrowserContext* context);
141 // Dispatches the onRestartRequired event to the given app.
142 static void DispatchOnRestartRequiredEvent(
143 content::BrowserContext* context,
144 const std::string& app_id,
145 api::runtime::OnRestartRequiredReason reason);
147 // Does any work needed at extension uninstall (e.g. load uninstall url).
148 static void OnExtensionUninstalled(content::BrowserContext* context,
149 const std::string& extension_id,
150 UninstallReason reason);
153 class RuntimeGetBackgroundPageFunction : public UIThreadExtensionFunction {
154 public:
155 DECLARE_EXTENSION_FUNCTION("runtime.getBackgroundPage",
156 RUNTIME_GETBACKGROUNDPAGE)
158 protected:
159 ~RuntimeGetBackgroundPageFunction() override {}
160 ResponseAction Run() override;
162 private:
163 void OnPageLoaded(ExtensionHost*);
166 class RuntimeOpenOptionsPageFunction : public UIThreadExtensionFunction {
167 public:
168 DECLARE_EXTENSION_FUNCTION("runtime.openOptionsPage", RUNTIME_OPENOPTIONSPAGE)
170 protected:
171 ~RuntimeOpenOptionsPageFunction() override {}
172 ResponseAction Run() override;
175 class RuntimeSetUninstallURLFunction : public UIThreadExtensionFunction {
176 public:
177 DECLARE_EXTENSION_FUNCTION("runtime.setUninstallURL", RUNTIME_SETUNINSTALLURL)
179 protected:
180 ~RuntimeSetUninstallURLFunction() override {}
181 ResponseAction Run() override;
184 class RuntimeReloadFunction : public UIThreadExtensionFunction {
185 public:
186 DECLARE_EXTENSION_FUNCTION("runtime.reload", RUNTIME_RELOAD)
188 protected:
189 ~RuntimeReloadFunction() override {}
190 ResponseAction Run() override;
193 class RuntimeRequestUpdateCheckFunction : public UIThreadExtensionFunction {
194 public:
195 DECLARE_EXTENSION_FUNCTION("runtime.requestUpdateCheck",
196 RUNTIME_REQUESTUPDATECHECK)
198 protected:
199 ~RuntimeRequestUpdateCheckFunction() override {}
200 ResponseAction Run() override;
202 private:
203 void CheckComplete(const RuntimeAPIDelegate::UpdateCheckResult& result);
206 class RuntimeRestartFunction : public UIThreadExtensionFunction {
207 public:
208 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART)
210 protected:
211 ~RuntimeRestartFunction() override {}
212 ResponseAction Run() override;
215 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction {
216 public:
217 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo",
218 RUNTIME_GETPLATFORMINFO);
220 protected:
221 ~RuntimeGetPlatformInfoFunction() override {}
222 ResponseAction Run() override;
225 class RuntimeGetPackageDirectoryEntryFunction
226 : public UIThreadExtensionFunction {
227 public:
228 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry",
229 RUNTIME_GETPACKAGEDIRECTORYENTRY)
231 protected:
232 ~RuntimeGetPackageDirectoryEntryFunction() override {}
233 ResponseAction Run() override;
236 } // namespace extensions
238 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_