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. 4 5 #ifndef EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ 6 #define EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ 7 8 #include <string> 9 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_observer.h" 18 #include "extensions/browser/update_observer.h" 19 #include "extensions/common/api/runtime.h" 20 21 namespace base { 22 class Version; 23 } 24 25 namespace content { 26 class BrowserContext; 27 } 28 29 namespace extensions { 30 31 namespace core_api { 32 namespace runtime { 33 struct PlatformInfo; 34 } 35 } 36 37 class Extension; 38 class ExtensionHost; 39 class ExtensionRegistry; 40 41 // Runtime API dispatches onStartup, onInstalled, and similar events to 42 // extensions. There is one instance shared between a browser context and 43 // its related incognito instance. 44 class RuntimeAPI : public BrowserContextKeyedAPI, 45 public content::NotificationObserver, 46 public ExtensionRegistryObserver, 47 public UpdateObserver, 48 public ProcessManagerObserver { 49 public: 50 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); 51 52 explicit RuntimeAPI(content::BrowserContext* context); 53 virtual ~RuntimeAPI(); 54 55 // content::NotificationObserver overrides: 56 virtual void Observe(int type, 57 const content::NotificationSource& source, 58 const content::NotificationDetails& details) OVERRIDE; 59 60 void ReloadExtension(const std::string& extension_id); 61 bool CheckForUpdates(const std::string& extension_id, 62 const RuntimeAPIDelegate::UpdateCheckCallback& callback); 63 void OpenURL(const GURL& uninstall_url); 64 bool GetPlatformInfo(core_api::runtime::PlatformInfo* info); 65 bool RestartDevice(std::string* error_message); 66 67 private: 68 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; 69 70 // ExtensionRegistryObserver implementation. 71 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, 72 const Extension* extension) OVERRIDE; 73 virtual void OnExtensionWillBeInstalled( 74 content::BrowserContext* browser_context, 75 const Extension* extension, 76 bool is_update, 77 bool from_ephemeral, 78 const std::string& old_name) OVERRIDE; 79 virtual void OnExtensionUninstalled(content::BrowserContext* browser_context, 80 const Extension* extension, 81 UninstallReason reason) OVERRIDE; 82 83 // BrowserContextKeyedAPI implementation: service_name()84 static const char* service_name() { return "RuntimeAPI"; } 85 static const bool kServiceRedirectedInIncognito = true; 86 static const bool kServiceIsNULLWhileTesting = true; 87 virtual void Shutdown() OVERRIDE; 88 89 // extensions::UpdateObserver overrides: 90 virtual void OnAppUpdateAvailable(const Extension* extension) OVERRIDE; 91 virtual void OnChromeUpdateAvailable() OVERRIDE; 92 93 // ProcessManagerObserver implementation: 94 virtual void OnBackgroundHostStartup(const Extension* extension) OVERRIDE; 95 96 scoped_ptr<RuntimeAPIDelegate> delegate_; 97 98 content::BrowserContext* browser_context_; 99 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_; 103 104 content::NotificationRegistrar registrar_; 105 106 // Listen to extension notifications. 107 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> 108 extension_registry_observer_; 109 110 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); 111 }; 112 113 class RuntimeEventRouter { 114 public: 115 // Dispatches the onStartup event to all currently-loaded extensions. 116 static void DispatchOnStartupEvent(content::BrowserContext* context, 117 const std::string& extension_id); 118 119 // Dispatches the onInstalled event to the given extension. 120 static void DispatchOnInstalledEvent(content::BrowserContext* context, 121 const std::string& extension_id, 122 const base::Version& old_version, 123 bool chrome_updated); 124 125 // Dispatches the onUpdateAvailable event to the given extension. 126 static void DispatchOnUpdateAvailableEvent( 127 content::BrowserContext* context, 128 const std::string& extension_id, 129 const base::DictionaryValue* manifest); 130 131 // Dispatches the onBrowserUpdateAvailable event to all extensions. 132 static void DispatchOnBrowserUpdateAvailableEvent( 133 content::BrowserContext* context); 134 135 // Dispatches the onRestartRequired event to the given app. 136 static void DispatchOnRestartRequiredEvent( 137 content::BrowserContext* context, 138 const std::string& app_id, 139 core_api::runtime::OnRestartRequired::Reason reason); 140 141 // Does any work needed at extension uninstall (e.g. load uninstall url). 142 static void OnExtensionUninstalled(content::BrowserContext* context, 143 const std::string& extension_id, 144 UninstallReason reason); 145 }; 146 147 class RuntimeGetBackgroundPageFunction : public UIThreadExtensionFunction { 148 public: 149 DECLARE_EXTENSION_FUNCTION("runtime.getBackgroundPage", 150 RUNTIME_GETBACKGROUNDPAGE) 151 152 protected: ~RuntimeGetBackgroundPageFunction()153 virtual ~RuntimeGetBackgroundPageFunction() {} 154 virtual ResponseAction Run() OVERRIDE; 155 156 private: 157 void OnPageLoaded(ExtensionHost*); 158 }; 159 160 class RuntimeSetUninstallURLFunction : public UIThreadExtensionFunction { 161 public: 162 DECLARE_EXTENSION_FUNCTION("runtime.setUninstallURL", RUNTIME_SETUNINSTALLURL) 163 164 protected: ~RuntimeSetUninstallURLFunction()165 virtual ~RuntimeSetUninstallURLFunction() {} 166 virtual ResponseAction Run() OVERRIDE; 167 }; 168 169 class RuntimeReloadFunction : public UIThreadExtensionFunction { 170 public: 171 DECLARE_EXTENSION_FUNCTION("runtime.reload", RUNTIME_RELOAD) 172 173 protected: ~RuntimeReloadFunction()174 virtual ~RuntimeReloadFunction() {} 175 virtual ResponseAction Run() OVERRIDE; 176 }; 177 178 class RuntimeRequestUpdateCheckFunction : public UIThreadExtensionFunction { 179 public: 180 DECLARE_EXTENSION_FUNCTION("runtime.requestUpdateCheck", 181 RUNTIME_REQUESTUPDATECHECK) 182 183 protected: ~RuntimeRequestUpdateCheckFunction()184 virtual ~RuntimeRequestUpdateCheckFunction() {} 185 virtual ResponseAction Run() OVERRIDE; 186 187 private: 188 void CheckComplete(const RuntimeAPIDelegate::UpdateCheckResult& result); 189 }; 190 191 class RuntimeRestartFunction : public UIThreadExtensionFunction { 192 public: 193 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) 194 195 protected: ~RuntimeRestartFunction()196 virtual ~RuntimeRestartFunction() {} 197 virtual ResponseAction Run() OVERRIDE; 198 }; 199 200 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { 201 public: 202 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", 203 RUNTIME_GETPLATFORMINFO); 204 205 protected: ~RuntimeGetPlatformInfoFunction()206 virtual ~RuntimeGetPlatformInfoFunction() {} 207 virtual ResponseAction Run() OVERRIDE; 208 }; 209 210 class RuntimeGetPackageDirectoryEntryFunction 211 : public UIThreadExtensionFunction { 212 public: 213 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", 214 RUNTIME_GETPACKAGEDIRECTORYENTRY) 215 216 protected: ~RuntimeGetPackageDirectoryEntryFunction()217 virtual ~RuntimeGetPackageDirectoryEntryFunction() {} 218 virtual ResponseAction Run() OVERRIDE; 219 }; 220 221 } // namespace extensions 222 223 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ 224