1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_
10 #include "chrome/browser/extensions/chrome_extension_function.h"
11 #include "chrome/common/extensions/api/runtime.h"
12 #include "content/public/browser/notification_observer.h"
13 #include "content/public/browser/notification_registrar.h"
14 #include "extensions/browser/browser_context_keyed_api_factory.h"
15 #include "extensions/browser/update_observer.h"
27 namespace extensions {
31 // Runtime API dispatches onStartup, onInstalled, and similar events to
32 // extensions. There is one instance shared between a browser context and
33 // its related incognito instance.
34 class RuntimeAPI : public BrowserContextKeyedAPI,
35 public content::NotificationObserver,
36 public extensions::UpdateObserver {
38 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance();
40 explicit RuntimeAPI(content::BrowserContext* context);
41 virtual ~RuntimeAPI();
43 // content::NotificationObserver overrides:
44 virtual void Observe(int type,
45 const content::NotificationSource& source,
46 const content::NotificationDetails& details) OVERRIDE;
49 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>;
51 void OnExtensionsReady();
52 void OnExtensionLoaded(const Extension* extension);
53 void OnExtensionInstalled(const Extension* extension);
54 void OnExtensionUninstalled(const Extension* extension);
56 // BrowserContextKeyedAPI implementation:
57 static const char* service_name() { return "RuntimeAPI"; }
58 static const bool kServiceRedirectedInIncognito = true;
59 static const bool kServiceIsNULLWhileTesting = true;
61 // extensions::UpdateObserver overrides:
62 virtual void OnAppUpdateAvailable(const Extension* extension) OVERRIDE;
63 virtual void OnChromeUpdateAvailable() OVERRIDE;
65 content::BrowserContext* browser_context_;
67 // True if we should dispatch the chrome.runtime.onInstalled event with
68 // reason "chrome_update" upon loading each extension.
69 bool dispatch_chrome_updated_event_;
71 // Whether the API registered with the ExtensionService to receive
72 // update notifications.
73 bool registered_for_updates_;
75 content::NotificationRegistrar registrar_;
77 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI);
80 class RuntimeEventRouter {
82 // Dispatches the onStartup event to all currently-loaded extensions.
83 static void DispatchOnStartupEvent(content::BrowserContext* context,
84 const std::string& extension_id);
86 // Dispatches the onInstalled event to the given extension.
87 static void DispatchOnInstalledEvent(content::BrowserContext* context,
88 const std::string& extension_id,
89 const base::Version& old_version,
92 // Dispatches the onUpdateAvailable event to the given extension.
93 static void DispatchOnUpdateAvailableEvent(
95 const std::string& extension_id,
96 const base::DictionaryValue* manifest);
98 // Dispatches the onBrowserUpdateAvailable event to all extensions.
99 static void DispatchOnBrowserUpdateAvailableEvent(Profile* profile);
101 // Dispatches the onRestartRequired event to the given app.
102 static void DispatchOnRestartRequiredEvent(
104 const std::string& app_id,
105 api::runtime::OnRestartRequired::Reason reason);
107 // Does any work needed at extension uninstall (e.g. load uninstall url).
108 static void OnExtensionUninstalled(Profile* profile,
109 const std::string& extension_id);
112 class RuntimeGetBackgroundPageFunction : public ChromeAsyncExtensionFunction {
114 DECLARE_EXTENSION_FUNCTION("runtime.getBackgroundPage",
115 RUNTIME_GETBACKGROUNDPAGE)
118 virtual ~RuntimeGetBackgroundPageFunction() {}
119 virtual bool RunImpl() OVERRIDE;
122 void OnPageLoaded(ExtensionHost*);
125 class RuntimeSetUninstallURLFunction : public ChromeSyncExtensionFunction {
127 DECLARE_EXTENSION_FUNCTION("runtime.setUninstallURL",
128 RUNTIME_SETUNINSTALLURL)
131 virtual ~RuntimeSetUninstallURLFunction() {}
132 virtual bool RunImpl() OVERRIDE;
135 class RuntimeReloadFunction : public ChromeSyncExtensionFunction {
137 DECLARE_EXTENSION_FUNCTION("runtime.reload", RUNTIME_RELOAD)
140 virtual ~RuntimeReloadFunction() {}
141 virtual bool RunImpl() OVERRIDE;
144 class RuntimeRequestUpdateCheckFunction : public ChromeAsyncExtensionFunction,
145 public content::NotificationObserver {
147 DECLARE_EXTENSION_FUNCTION("runtime.requestUpdateCheck",
148 RUNTIME_REQUESTUPDATECHECK)
150 RuntimeRequestUpdateCheckFunction();
152 virtual ~RuntimeRequestUpdateCheckFunction() {}
153 virtual bool RunImpl() OVERRIDE;
155 // Implements content::NotificationObserver interface.
156 virtual void Observe(int type,
157 const content::NotificationSource& source,
158 const content::NotificationDetails& details) OVERRIDE;
160 void CheckComplete();
161 void ReplyUpdateFound(const std::string& version);
163 content::NotificationRegistrar registrar_;
167 class RuntimeRestartFunction : public ChromeSyncExtensionFunction {
169 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART)
172 virtual ~RuntimeRestartFunction() {}
173 virtual bool RunImpl() OVERRIDE;
176 class RuntimeGetPlatformInfoFunction : public ChromeSyncExtensionFunction {
178 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo",
179 RUNTIME_GETPLATFORMINFO);
181 virtual ~RuntimeGetPlatformInfoFunction() {}
182 virtual bool RunImpl() OVERRIDE;
185 class RuntimeGetPackageDirectoryEntryFunction
186 : public ChromeSyncExtensionFunction {
188 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry",
189 RUNTIME_GETPACKAGEDIRECTORYENTRY)
192 virtual ~RuntimeGetPackageDirectoryEntryFunction() {}
193 virtual bool RunImpl() OVERRIDE;
196 } // namespace extensions
198 #endif // CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_