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. 4 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_ 7 8 #include <string> 9 10 #include "chrome/browser/extensions/chrome_extension_function.h" 11 #include "chrome/common/extensions/api/runtime.h" 12 #include "components/browser_context_keyed_service/browser_context_keyed_service.h" 13 #include "content/public/browser/notification_observer.h" 14 #include "content/public/browser/notification_registrar.h" 15 #include "extensions/browser/update_observer.h" 16 17 class Profile; 18 19 namespace base { 20 class Version; 21 } 22 23 namespace content { 24 class BrowserContext; 25 } 26 27 namespace extensions { 28 class Extension; 29 class ExtensionHost; 30 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 BrowserContextKeyedService, 35 public content::NotificationObserver, 36 public extensions::UpdateObserver { 37 public: 38 explicit RuntimeAPI(content::BrowserContext* context); 39 virtual ~RuntimeAPI(); 40 41 // content::NotificationObserver overrides: 42 virtual void Observe(int type, 43 const content::NotificationSource& source, 44 const content::NotificationDetails& details) OVERRIDE; 45 46 private: 47 void OnExtensionsReady(); 48 void OnExtensionLoaded(const Extension* extension); 49 void OnExtensionInstalled(const Extension* extension); 50 void OnExtensionUninstalled(const Extension* extension); 51 52 // extensions::UpdateObserver overrides: 53 virtual void OnAppUpdateAvailable(const Extension* extension) OVERRIDE; 54 virtual void OnChromeUpdateAvailable() OVERRIDE; 55 56 content::BrowserContext* browser_context_; 57 58 // True if we should dispatch the chrome.runtime.onInstalled event with 59 // reason "chrome_update" upon loading each extension. 60 bool dispatch_chrome_updated_event_; 61 62 // Whether the API registered with the ExtensionService to receive 63 // update notifications. 64 bool registered_for_updates_; 65 66 content::NotificationRegistrar registrar_; 67 68 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); 69 }; 70 71 class RuntimeEventRouter { 72 public: 73 // Dispatches the onStartup event to all currently-loaded extensions. 74 static void DispatchOnStartupEvent(content::BrowserContext* context, 75 const std::string& extension_id); 76 77 // Dispatches the onInstalled event to the given extension. 78 static void DispatchOnInstalledEvent(content::BrowserContext* context, 79 const std::string& extension_id, 80 const base::Version& old_version, 81 bool chrome_updated); 82 83 // Dispatches the onUpdateAvailable event to the given extension. 84 static void DispatchOnUpdateAvailableEvent( 85 Profile* profile, 86 const std::string& extension_id, 87 const base::DictionaryValue* manifest); 88 89 // Dispatches the onBrowserUpdateAvailable event to all extensions. 90 static void DispatchOnBrowserUpdateAvailableEvent(Profile* profile); 91 92 // Dispatches the onRestartRequired event to the given app. 93 static void DispatchOnRestartRequiredEvent( 94 Profile* profile, 95 const std::string& app_id, 96 api::runtime::OnRestartRequired::Reason reason); 97 98 // Does any work needed at extension uninstall (e.g. load uninstall url). 99 static void OnExtensionUninstalled(Profile* profile, 100 const std::string& extension_id); 101 }; 102 103 class RuntimeGetBackgroundPageFunction : public ChromeAsyncExtensionFunction { 104 public: 105 DECLARE_EXTENSION_FUNCTION("runtime.getBackgroundPage", 106 RUNTIME_GETBACKGROUNDPAGE) 107 108 protected: 109 virtual ~RuntimeGetBackgroundPageFunction() {} 110 virtual bool RunImpl() OVERRIDE; 111 112 private: 113 void OnPageLoaded(ExtensionHost*); 114 }; 115 116 class RuntimeSetUninstallUrlFunction : public ChromeSyncExtensionFunction { 117 public: 118 DECLARE_EXTENSION_FUNCTION("runtime.setUninstallUrl", 119 RUNTIME_SETUNINSTALLURL) 120 121 protected: 122 virtual ~RuntimeSetUninstallUrlFunction() {} 123 virtual bool RunImpl() OVERRIDE; 124 }; 125 126 class RuntimeReloadFunction : public ChromeSyncExtensionFunction { 127 public: 128 DECLARE_EXTENSION_FUNCTION("runtime.reload", RUNTIME_RELOAD) 129 130 protected: 131 virtual ~RuntimeReloadFunction() {} 132 virtual bool RunImpl() OVERRIDE; 133 }; 134 135 class RuntimeRequestUpdateCheckFunction : public ChromeAsyncExtensionFunction, 136 public content::NotificationObserver { 137 public: 138 DECLARE_EXTENSION_FUNCTION("runtime.requestUpdateCheck", 139 RUNTIME_REQUESTUPDATECHECK) 140 141 RuntimeRequestUpdateCheckFunction(); 142 protected: 143 virtual ~RuntimeRequestUpdateCheckFunction() {} 144 virtual bool RunImpl() OVERRIDE; 145 146 // Implements content::NotificationObserver interface. 147 virtual void Observe(int type, 148 const content::NotificationSource& source, 149 const content::NotificationDetails& details) OVERRIDE; 150 private: 151 void CheckComplete(); 152 void ReplyUpdateFound(const std::string& version); 153 154 content::NotificationRegistrar registrar_; 155 bool did_reply_; 156 }; 157 158 class RuntimeRestartFunction : public ChromeSyncExtensionFunction { 159 public: 160 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) 161 162 protected: 163 virtual ~RuntimeRestartFunction() {} 164 virtual bool RunImpl() OVERRIDE; 165 }; 166 167 class RuntimeGetPlatformInfoFunction : public ChromeSyncExtensionFunction { 168 public: 169 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", 170 RUNTIME_GETPLATFORMINFO); 171 protected: 172 virtual ~RuntimeGetPlatformInfoFunction() {} 173 virtual bool RunImpl() OVERRIDE; 174 }; 175 176 class RuntimeGetPackageDirectoryEntryFunction 177 : public ChromeSyncExtensionFunction { 178 public: 179 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", 180 RUNTIME_GETPACKAGEDIRECTORYENTRY) 181 182 protected: 183 virtual ~RuntimeGetPackageDirectoryEntryFunction() {} 184 virtual bool RunImpl() OVERRIDE; 185 }; 186 187 } // namespace extensions 188 189 #endif // CHROME_BROWSER_EXTENSIONS_API_RUNTIME_RUNTIME_API_H_ 190