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