| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_ | 6 #define EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 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" | 10 #include "content/public/browser/notification_observer.h" |
| 13 #include "content/public/browser/notification_registrar.h" | 11 #include "content/public/browser/notification_registrar.h" |
| 12 #include "extensions/browser/api/runtime/runtime_api_delegate.h" |
| 14 #include "extensions/browser/browser_context_keyed_api_factory.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" | 15 #include "extensions/browser/process_manager_observer.h" |
| 16 #include "extensions/browser/update_observer.h" | 16 #include "extensions/browser/update_observer.h" |
| 17 | 17 #include "extensions/common/api/runtime.h" |
| 18 class Profile; | |
| 19 | 18 |
| 20 namespace base { | 19 namespace base { |
| 21 class Version; | 20 class Version; |
| 22 } | 21 } |
| 23 | 22 |
| 24 namespace content { | 23 namespace content { |
| 25 class BrowserContext; | 24 class BrowserContext; |
| 26 } | 25 } |
| 27 | 26 |
| 28 namespace extensions { | 27 namespace extensions { |
| 28 |
| 29 namespace core_api { |
| 30 namespace runtime { |
| 31 struct PlatformInfo; |
| 32 } |
| 33 } |
| 34 |
| 29 class Extension; | 35 class Extension; |
| 30 class ExtensionHost; | 36 class ExtensionHost; |
| 31 | 37 |
| 32 // Runtime API dispatches onStartup, onInstalled, and similar events to | 38 // Runtime API dispatches onStartup, onInstalled, and similar events to |
| 33 // extensions. There is one instance shared between a browser context and | 39 // extensions. There is one instance shared between a browser context and |
| 34 // its related incognito instance. | 40 // its related incognito instance. |
| 35 class RuntimeAPI : public BrowserContextKeyedAPI, | 41 class RuntimeAPI : public BrowserContextKeyedAPI, |
| 36 public content::NotificationObserver, | 42 public content::NotificationObserver, |
| 37 public UpdateObserver, | 43 public UpdateObserver, |
| 38 public ProcessManagerObserver { | 44 public ProcessManagerObserver { |
| 39 public: | 45 public: |
| 40 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); | 46 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); |
| 41 | 47 |
| 42 explicit RuntimeAPI(content::BrowserContext* context); | 48 explicit RuntimeAPI(content::BrowserContext* context); |
| 43 virtual ~RuntimeAPI(); | 49 virtual ~RuntimeAPI(); |
| 44 | 50 |
| 45 // content::NotificationObserver overrides: | 51 // content::NotificationObserver overrides: |
| 46 virtual void Observe(int type, | 52 virtual void Observe(int type, |
| 47 const content::NotificationSource& source, | 53 const content::NotificationSource& source, |
| 48 const content::NotificationDetails& details) OVERRIDE; | 54 const content::NotificationDetails& details) OVERRIDE; |
| 49 | 55 |
| 50 void MaybeReloadExtension(const std::string& extension_id); | 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); |
| 51 | 62 |
| 52 private: | 63 private: |
| 53 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; | 64 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; |
| 54 | 65 |
| 55 void OnExtensionsReady(); | 66 void OnExtensionsReady(); |
| 56 void OnExtensionLoaded(const Extension* extension); | 67 void OnExtensionLoaded(const Extension* extension); |
| 57 void OnExtensionInstalled(const Extension* extension); | 68 void OnExtensionInstalled(const Extension* extension); |
| 58 void OnExtensionUninstalled(const Extension* extension); | 69 void OnExtensionUninstalled(const Extension* extension); |
| 59 | 70 |
| 60 // BrowserContextKeyedAPI implementation: | 71 // BrowserContextKeyedAPI implementation: |
| 61 static const char* service_name() { return "RuntimeAPI"; } | 72 static const char* service_name() { return "RuntimeAPI"; } |
| 62 static const bool kServiceRedirectedInIncognito = true; | 73 static const bool kServiceRedirectedInIncognito = true; |
| 63 static const bool kServiceIsNULLWhileTesting = true; | 74 static const bool kServiceIsNULLWhileTesting = true; |
| 64 virtual void Shutdown() OVERRIDE; | 75 virtual void Shutdown() OVERRIDE; |
| 65 | 76 |
| 66 // extensions::UpdateObserver overrides: | 77 // extensions::UpdateObserver overrides: |
| 67 virtual void OnAppUpdateAvailable(const Extension* extension) OVERRIDE; | 78 virtual void OnAppUpdateAvailable(const Extension* extension) OVERRIDE; |
| 68 virtual void OnChromeUpdateAvailable() OVERRIDE; | 79 virtual void OnChromeUpdateAvailable() OVERRIDE; |
| 69 | 80 |
| 70 // ProcessManagerObserver implementation: | 81 // ProcessManagerObserver implementation: |
| 71 virtual void OnBackgroundHostStartup(const Extension* extension) OVERRIDE; | 82 virtual void OnBackgroundHostStartup(const Extension* extension) OVERRIDE; |
| 72 | 83 |
| 84 scoped_ptr<RuntimeAPIDelegate> delegate_; |
| 85 |
| 73 content::BrowserContext* browser_context_; | 86 content::BrowserContext* browser_context_; |
| 74 | 87 |
| 75 // True if we should dispatch the chrome.runtime.onInstalled event with | 88 // True if we should dispatch the chrome.runtime.onInstalled event with |
| 76 // reason "chrome_update" upon loading each extension. | 89 // reason "chrome_update" upon loading each extension. |
| 77 bool dispatch_chrome_updated_event_; | 90 bool dispatch_chrome_updated_event_; |
| 78 | 91 |
| 79 // Whether the API registered with the ExtensionService to receive | |
| 80 // update notifications. | |
| 81 bool registered_for_updates_; | |
| 82 | |
| 83 content::NotificationRegistrar registrar_; | 92 content::NotificationRegistrar registrar_; |
| 84 | 93 |
| 85 // Map to prevent extensions from getting stuck in reload loops. Maps | |
| 86 // extension id to the last time it was reloaded and the number of times | |
| 87 // it was reloaded with not enough time in between reloads. | |
| 88 std::map<std::string, std::pair<base::TimeTicks, int> > last_reload_time_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); | 94 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); |
| 91 }; | 95 }; |
| 92 | 96 |
| 93 class RuntimeEventRouter { | 97 class RuntimeEventRouter { |
| 94 public: | 98 public: |
| 95 // Dispatches the onStartup event to all currently-loaded extensions. | 99 // Dispatches the onStartup event to all currently-loaded extensions. |
| 96 static void DispatchOnStartupEvent(content::BrowserContext* context, | 100 static void DispatchOnStartupEvent(content::BrowserContext* context, |
| 97 const std::string& extension_id); | 101 const std::string& extension_id); |
| 98 | 102 |
| 99 // Dispatches the onInstalled event to the given extension. | 103 // Dispatches the onInstalled event to the given extension. |
| 100 static void DispatchOnInstalledEvent(content::BrowserContext* context, | 104 static void DispatchOnInstalledEvent(content::BrowserContext* context, |
| 101 const std::string& extension_id, | 105 const std::string& extension_id, |
| 102 const base::Version& old_version, | 106 const base::Version& old_version, |
| 103 bool chrome_updated); | 107 bool chrome_updated); |
| 104 | 108 |
| 105 // Dispatches the onUpdateAvailable event to the given extension. | 109 // Dispatches the onUpdateAvailable event to the given extension. |
| 106 static void DispatchOnUpdateAvailableEvent( | 110 static void DispatchOnUpdateAvailableEvent( |
| 107 Profile* profile, | 111 content::BrowserContext* context, |
| 108 const std::string& extension_id, | 112 const std::string& extension_id, |
| 109 const base::DictionaryValue* manifest); | 113 const base::DictionaryValue* manifest); |
| 110 | 114 |
| 111 // Dispatches the onBrowserUpdateAvailable event to all extensions. | 115 // Dispatches the onBrowserUpdateAvailable event to all extensions. |
| 112 static void DispatchOnBrowserUpdateAvailableEvent(Profile* profile); | 116 static void DispatchOnBrowserUpdateAvailableEvent( |
| 117 content::BrowserContext* context); |
| 113 | 118 |
| 114 // Dispatches the onRestartRequired event to the given app. | 119 // Dispatches the onRestartRequired event to the given app. |
| 115 static void DispatchOnRestartRequiredEvent( | 120 static void DispatchOnRestartRequiredEvent( |
| 116 Profile* profile, | 121 content::BrowserContext* context, |
| 117 const std::string& app_id, | 122 const std::string& app_id, |
| 118 api::runtime::OnRestartRequired::Reason reason); | 123 core_api::runtime::OnRestartRequired::Reason reason); |
| 119 | 124 |
| 120 // Does any work needed at extension uninstall (e.g. load uninstall url). | 125 // Does any work needed at extension uninstall (e.g. load uninstall url). |
| 121 static void OnExtensionUninstalled(Profile* profile, | 126 static void OnExtensionUninstalled(content::BrowserContext* context, |
| 122 const std::string& extension_id); | 127 const std::string& extension_id); |
| 123 }; | 128 }; |
| 124 | 129 |
| 125 class RuntimeGetBackgroundPageFunction : public ChromeAsyncExtensionFunction { | 130 class RuntimeGetBackgroundPageFunction : public UIThreadExtensionFunction { |
| 126 public: | 131 public: |
| 127 DECLARE_EXTENSION_FUNCTION("runtime.getBackgroundPage", | 132 DECLARE_EXTENSION_FUNCTION("runtime.getBackgroundPage", |
| 128 RUNTIME_GETBACKGROUNDPAGE) | 133 RUNTIME_GETBACKGROUNDPAGE) |
| 129 | 134 |
| 130 protected: | 135 protected: |
| 131 virtual ~RuntimeGetBackgroundPageFunction() {} | 136 virtual ~RuntimeGetBackgroundPageFunction() {} |
| 132 virtual bool RunAsync() OVERRIDE; | 137 virtual ResponseAction Run() OVERRIDE; |
| 133 | 138 |
| 134 private: | 139 private: |
| 135 void OnPageLoaded(ExtensionHost*); | 140 void OnPageLoaded(ExtensionHost*); |
| 136 }; | 141 }; |
| 137 | 142 |
| 138 class RuntimeSetUninstallURLFunction : public ChromeSyncExtensionFunction { | 143 class RuntimeSetUninstallURLFunction : public UIThreadExtensionFunction { |
| 139 public: | 144 public: |
| 140 DECLARE_EXTENSION_FUNCTION("runtime.setUninstallURL", | 145 DECLARE_EXTENSION_FUNCTION("runtime.setUninstallURL", RUNTIME_SETUNINSTALLURL) |
| 141 RUNTIME_SETUNINSTALLURL) | |
| 142 | 146 |
| 143 protected: | 147 protected: |
| 144 virtual ~RuntimeSetUninstallURLFunction() {} | 148 virtual ~RuntimeSetUninstallURLFunction() {} |
| 145 virtual bool RunSync() OVERRIDE; | 149 virtual ResponseAction Run() OVERRIDE; |
| 146 }; | 150 }; |
| 147 | 151 |
| 148 class RuntimeReloadFunction : public ChromeSyncExtensionFunction { | 152 class RuntimeReloadFunction : public UIThreadExtensionFunction { |
| 149 public: | 153 public: |
| 150 DECLARE_EXTENSION_FUNCTION("runtime.reload", RUNTIME_RELOAD) | 154 DECLARE_EXTENSION_FUNCTION("runtime.reload", RUNTIME_RELOAD) |
| 151 | 155 |
| 152 protected: | 156 protected: |
| 153 virtual ~RuntimeReloadFunction() {} | 157 virtual ~RuntimeReloadFunction() {} |
| 154 virtual bool RunSync() OVERRIDE; | 158 virtual ResponseAction Run() OVERRIDE; |
| 155 }; | 159 }; |
| 156 | 160 |
| 157 class RuntimeRequestUpdateCheckFunction : public ChromeAsyncExtensionFunction, | 161 class RuntimeRequestUpdateCheckFunction : public UIThreadExtensionFunction { |
| 158 public content::NotificationObserver { | |
| 159 public: | 162 public: |
| 160 DECLARE_EXTENSION_FUNCTION("runtime.requestUpdateCheck", | 163 DECLARE_EXTENSION_FUNCTION("runtime.requestUpdateCheck", |
| 161 RUNTIME_REQUESTUPDATECHECK) | 164 RUNTIME_REQUESTUPDATECHECK) |
| 162 | 165 |
| 163 RuntimeRequestUpdateCheckFunction(); | |
| 164 protected: | 166 protected: |
| 165 virtual ~RuntimeRequestUpdateCheckFunction() {} | 167 virtual ~RuntimeRequestUpdateCheckFunction() {} |
| 166 virtual bool RunAsync() OVERRIDE; | 168 virtual ResponseAction Run() OVERRIDE; |
| 167 | 169 |
| 168 // Implements content::NotificationObserver interface. | |
| 169 virtual void Observe(int type, | |
| 170 const content::NotificationSource& source, | |
| 171 const content::NotificationDetails& details) OVERRIDE; | |
| 172 private: | 170 private: |
| 173 void CheckComplete(); | 171 void CheckComplete(const RuntimeAPIDelegate::UpdateCheckResult& result); |
| 174 void ReplyUpdateFound(const std::string& version); | |
| 175 | |
| 176 content::NotificationRegistrar registrar_; | |
| 177 bool did_reply_; | |
| 178 }; | 172 }; |
| 179 | 173 |
| 180 class RuntimeRestartFunction : public ChromeSyncExtensionFunction { | 174 class RuntimeRestartFunction : public UIThreadExtensionFunction { |
| 181 public: | 175 public: |
| 182 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) | 176 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) |
| 183 | 177 |
| 184 protected: | 178 protected: |
| 185 virtual ~RuntimeRestartFunction() {} | 179 virtual ~RuntimeRestartFunction() {} |
| 186 virtual bool RunSync() OVERRIDE; | 180 virtual ResponseAction Run() OVERRIDE; |
| 187 }; | 181 }; |
| 188 | 182 |
| 189 class RuntimeGetPlatformInfoFunction : public ChromeSyncExtensionFunction { | 183 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { |
| 190 public: | 184 public: |
| 191 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", | 185 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", |
| 192 RUNTIME_GETPLATFORMINFO); | 186 RUNTIME_GETPLATFORMINFO); |
| 187 |
| 193 protected: | 188 protected: |
| 194 virtual ~RuntimeGetPlatformInfoFunction() {} | 189 virtual ~RuntimeGetPlatformInfoFunction() {} |
| 195 virtual bool RunSync() OVERRIDE; | 190 virtual ResponseAction Run() OVERRIDE; |
| 196 }; | 191 }; |
| 197 | 192 |
| 198 class RuntimeGetPackageDirectoryEntryFunction | 193 class RuntimeGetPackageDirectoryEntryFunction |
| 199 : public ChromeSyncExtensionFunction { | 194 : public UIThreadExtensionFunction { |
| 200 public: | 195 public: |
| 201 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", | 196 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", |
| 202 RUNTIME_GETPACKAGEDIRECTORYENTRY) | 197 RUNTIME_GETPACKAGEDIRECTORYENTRY) |
| 203 | 198 |
| 204 protected: | 199 protected: |
| 205 virtual ~RuntimeGetPackageDirectoryEntryFunction() {} | 200 virtual ~RuntimeGetPackageDirectoryEntryFunction() {} |
| 206 virtual bool RunSync() OVERRIDE; | 201 virtual ResponseAction Run() OVERRIDE; |
| 207 }; | 202 }; |
| 208 | 203 |
| 209 } // namespace extensions | 204 } // namespace extensions |
| 210 | 205 |
| 211 #endif // CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_ | 206 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| OLD | NEW |