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