Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / runtime / runtime_api.h
blobe330521985518133218c86bdf3f05577f0db1f2a
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 core_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(core_api::runtime::PlatformInfo* info);
66 bool RestartDevice(std::string* error_message);
68 private:
69 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>;
71 // ExtensionRegistryObserver implementation.
72 void OnExtensionLoaded(content::BrowserContext* browser_context,
73 const Extension* extension) override;
74 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
75 const Extension* extension,
76 bool is_update,
77 bool from_ephemeral,
78 const std::string& old_name) override;
79 void OnExtensionUninstalled(content::BrowserContext* browser_context,
80 const Extension* extension,
81 UninstallReason reason) override;
83 // BrowserContextKeyedAPI implementation:
84 static const char* service_name() { return "RuntimeAPI"; }
85 static const bool kServiceRedirectedInIncognito = true;
86 static const bool kServiceIsNULLWhileTesting = true;
87 void Shutdown() override;
89 // extensions::UpdateObserver overrides:
90 void OnAppUpdateAvailable(const Extension* extension) override;
91 void OnChromeUpdateAvailable() override;
93 // ProcessManagerObserver implementation:
94 void OnBackgroundHostStartup(const Extension* extension) override;
96 scoped_ptr<RuntimeAPIDelegate> delegate_;
98 content::BrowserContext* browser_context_;
100 // True if we should dispatch the chrome.runtime.onInstalled event with
101 // reason "chrome_update" upon loading each extension.
102 bool dispatch_chrome_updated_event_;
104 content::NotificationRegistrar registrar_;
106 // Listen to extension notifications.
107 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
108 extension_registry_observer_;
109 ScopedObserver<ProcessManager, ProcessManagerObserver>
110 process_manager_observer_;
112 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI);
115 template <>
116 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies();
118 class RuntimeEventRouter {
119 public:
120 // Dispatches the onStartup event to all currently-loaded extensions.
121 static void DispatchOnStartupEvent(content::BrowserContext* context,
122 const std::string& extension_id);
124 // Dispatches the onInstalled event to the given extension.
125 static void DispatchOnInstalledEvent(content::BrowserContext* context,
126 const std::string& extension_id,
127 const base::Version& old_version,
128 bool chrome_updated);
130 // Dispatches the onUpdateAvailable event to the given extension.
131 static void DispatchOnUpdateAvailableEvent(
132 content::BrowserContext* context,
133 const std::string& extension_id,
134 const base::DictionaryValue* manifest);
136 // Dispatches the onBrowserUpdateAvailable event to all extensions.
137 static void DispatchOnBrowserUpdateAvailableEvent(
138 content::BrowserContext* context);
140 // Dispatches the onRestartRequired event to the given app.
141 static void DispatchOnRestartRequiredEvent(
142 content::BrowserContext* context,
143 const std::string& app_id,
144 core_api::runtime::OnRestartRequired::Reason reason);
146 // Does any work needed at extension uninstall (e.g. load uninstall url).
147 static void OnExtensionUninstalled(content::BrowserContext* context,
148 const std::string& extension_id,
149 UninstallReason reason);
152 class RuntimeGetBackgroundPageFunction : public UIThreadExtensionFunction {
153 public:
154 DECLARE_EXTENSION_FUNCTION("runtime.getBackgroundPage",
155 RUNTIME_GETBACKGROUNDPAGE)
157 protected:
158 ~RuntimeGetBackgroundPageFunction() override {}
159 ResponseAction Run() override;
161 private:
162 void OnPageLoaded(ExtensionHost*);
165 class RuntimeSetUninstallURLFunction : public UIThreadExtensionFunction {
166 public:
167 DECLARE_EXTENSION_FUNCTION("runtime.setUninstallURL", RUNTIME_SETUNINSTALLURL)
169 protected:
170 ~RuntimeSetUninstallURLFunction() override {}
171 ResponseAction Run() override;
174 class RuntimeReloadFunction : public UIThreadExtensionFunction {
175 public:
176 DECLARE_EXTENSION_FUNCTION("runtime.reload", RUNTIME_RELOAD)
178 protected:
179 ~RuntimeReloadFunction() override {}
180 ResponseAction Run() override;
183 class RuntimeRequestUpdateCheckFunction : public UIThreadExtensionFunction {
184 public:
185 DECLARE_EXTENSION_FUNCTION("runtime.requestUpdateCheck",
186 RUNTIME_REQUESTUPDATECHECK)
188 protected:
189 ~RuntimeRequestUpdateCheckFunction() override {}
190 ResponseAction Run() override;
192 private:
193 void CheckComplete(const RuntimeAPIDelegate::UpdateCheckResult& result);
196 class RuntimeRestartFunction : public UIThreadExtensionFunction {
197 public:
198 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART)
200 protected:
201 ~RuntimeRestartFunction() override {}
202 ResponseAction Run() override;
205 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction {
206 public:
207 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo",
208 RUNTIME_GETPLATFORMINFO);
210 protected:
211 ~RuntimeGetPlatformInfoFunction() override {}
212 ResponseAction Run() override;
215 class RuntimeGetPackageDirectoryEntryFunction
216 : public UIThreadExtensionFunction {
217 public:
218 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry",
219 RUNTIME_GETPACKAGEDIRECTORYENTRY)
221 protected:
222 ~RuntimeGetPackageDirectoryEntryFunction() override {}
223 ResponseAction Run() override;
226 } // namespace extensions
228 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_