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_
10 #include "content/public/browser/notification_observer.h"
11 #include "content/public/browser/notification_registrar.h"
12 #include "extensions/browser/api/runtime/runtime_api_delegate.h"
13 #include "extensions/browser/browser_context_keyed_api_factory.h"
14 #include "extensions/browser/extension_function.h"
15 #include "extensions/browser/process_manager_observer.h"
16 #include "extensions/browser/update_observer.h"
17 #include "extensions/common/api/runtime.h"
27 namespace extensions
{
38 // Runtime API dispatches onStartup, onInstalled, and similar events to
39 // extensions. There is one instance shared between a browser context and
40 // its related incognito instance.
41 class RuntimeAPI
: public BrowserContextKeyedAPI
,
42 public content::NotificationObserver
,
43 public UpdateObserver
,
44 public ProcessManagerObserver
{
46 static BrowserContextKeyedAPIFactory
<RuntimeAPI
>* GetFactoryInstance();
48 explicit RuntimeAPI(content::BrowserContext
* context
);
49 virtual ~RuntimeAPI();
51 // content::NotificationObserver overrides:
52 virtual void Observe(int type
,
53 const content::NotificationSource
& source
,
54 const content::NotificationDetails
& details
) OVERRIDE
;
56 void ReloadExtension(const std::string
& extension_id
);
57 bool CheckForUpdates(const std::string
& extension_id
,
58 const RuntimeAPIDelegate::UpdateCheckCallback
& callback
);
59 void OpenURL(const GURL
& uninstall_url
);
60 bool GetPlatformInfo(core_api::runtime::PlatformInfo
* info
);
61 bool RestartDevice(std::string
* error_message
);
64 friend class BrowserContextKeyedAPIFactory
<RuntimeAPI
>;
66 void OnExtensionsReady();
67 void OnExtensionLoaded(const Extension
* extension
);
68 void OnExtensionInstalled(const Extension
* extension
);
69 void OnExtensionUninstalled(const Extension
* extension
);
71 // BrowserContextKeyedAPI implementation:
72 static const char* service_name() { return "RuntimeAPI"; }
73 static const bool kServiceRedirectedInIncognito
= true;
74 static const bool kServiceIsNULLWhileTesting
= true;
75 virtual void Shutdown() OVERRIDE
;
77 // extensions::UpdateObserver overrides:
78 virtual void OnAppUpdateAvailable(const Extension
* extension
) OVERRIDE
;
79 virtual void OnChromeUpdateAvailable() OVERRIDE
;
81 // ProcessManagerObserver implementation:
82 virtual void OnBackgroundHostStartup(const Extension
* extension
) OVERRIDE
;
84 scoped_ptr
<RuntimeAPIDelegate
> delegate_
;
86 content::BrowserContext
* browser_context_
;
88 // True if we should dispatch the chrome.runtime.onInstalled event with
89 // reason "chrome_update" upon loading each extension.
90 bool dispatch_chrome_updated_event_
;
92 content::NotificationRegistrar registrar_
;
94 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI
);
97 class RuntimeEventRouter
{
99 // Dispatches the onStartup event to all currently-loaded extensions.
100 static void DispatchOnStartupEvent(content::BrowserContext
* context
,
101 const std::string
& extension_id
);
103 // Dispatches the onInstalled event to the given extension.
104 static void DispatchOnInstalledEvent(content::BrowserContext
* context
,
105 const std::string
& extension_id
,
106 const base::Version
& old_version
,
107 bool chrome_updated
);
109 // Dispatches the onUpdateAvailable event to the given extension.
110 static void DispatchOnUpdateAvailableEvent(
111 content::BrowserContext
* context
,
112 const std::string
& extension_id
,
113 const base::DictionaryValue
* manifest
);
115 // Dispatches the onBrowserUpdateAvailable event to all extensions.
116 static void DispatchOnBrowserUpdateAvailableEvent(
117 content::BrowserContext
* context
);
119 // Dispatches the onRestartRequired event to the given app.
120 static void DispatchOnRestartRequiredEvent(
121 content::BrowserContext
* context
,
122 const std::string
& app_id
,
123 core_api::runtime::OnRestartRequired::Reason reason
);
125 // Does any work needed at extension uninstall (e.g. load uninstall url).
126 static void OnExtensionUninstalled(content::BrowserContext
* context
,
127 const std::string
& extension_id
);
130 class RuntimeGetBackgroundPageFunction
: public UIThreadExtensionFunction
{
132 DECLARE_EXTENSION_FUNCTION("runtime.getBackgroundPage",
133 RUNTIME_GETBACKGROUNDPAGE
)
136 virtual ~RuntimeGetBackgroundPageFunction() {}
137 virtual ResponseAction
Run() OVERRIDE
;
140 void OnPageLoaded(ExtensionHost
*);
143 class RuntimeSetUninstallURLFunction
: public UIThreadExtensionFunction
{
145 DECLARE_EXTENSION_FUNCTION("runtime.setUninstallURL", RUNTIME_SETUNINSTALLURL
)
148 virtual ~RuntimeSetUninstallURLFunction() {}
149 virtual ResponseAction
Run() OVERRIDE
;
152 class RuntimeReloadFunction
: public UIThreadExtensionFunction
{
154 DECLARE_EXTENSION_FUNCTION("runtime.reload", RUNTIME_RELOAD
)
157 virtual ~RuntimeReloadFunction() {}
158 virtual ResponseAction
Run() OVERRIDE
;
161 class RuntimeRequestUpdateCheckFunction
: public UIThreadExtensionFunction
{
163 DECLARE_EXTENSION_FUNCTION("runtime.requestUpdateCheck",
164 RUNTIME_REQUESTUPDATECHECK
)
167 virtual ~RuntimeRequestUpdateCheckFunction() {}
168 virtual ResponseAction
Run() OVERRIDE
;
171 void CheckComplete(const RuntimeAPIDelegate::UpdateCheckResult
& result
);
174 class RuntimeRestartFunction
: public UIThreadExtensionFunction
{
176 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART
)
179 virtual ~RuntimeRestartFunction() {}
180 virtual ResponseAction
Run() OVERRIDE
;
183 class RuntimeGetPlatformInfoFunction
: public UIThreadExtensionFunction
{
185 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo",
186 RUNTIME_GETPLATFORMINFO
);
189 virtual ~RuntimeGetPlatformInfoFunction() {}
190 virtual ResponseAction
Run() OVERRIDE
;
193 class RuntimeGetPackageDirectoryEntryFunction
194 : public UIThreadExtensionFunction
{
196 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry",
197 RUNTIME_GETPACKAGEDIRECTORYENTRY
)
200 virtual ~RuntimeGetPackageDirectoryEntryFunction() {}
201 virtual ResponseAction
Run() OVERRIDE
;
204 } // namespace extensions
206 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_