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_MANAGEMENT_MANAGEMENT_API_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_H_ | |
7 | |
8 #include "base/compiler_specific.h" | |
9 #include "base/scoped_observer.h" | |
10 #include "base/task/cancelable_task_tracker.h" | |
11 #include "chrome/browser/extensions/bookmark_app_helper.h" | |
12 #include "chrome/browser/extensions/chrome_extension_function.h" | |
13 #include "chrome/browser/extensions/extension_install_prompt.h" | |
14 #include "chrome/browser/extensions/extension_uninstall_dialog.h" | |
15 #include "chrome/common/web_application_info.h" | |
16 #include "components/favicon_base/favicon_types.h" | |
17 #include "components/keyed_service/core/keyed_service.h" | |
18 #include "extensions/browser/browser_context_keyed_api_factory.h" | |
19 #include "extensions/browser/event_router.h" | |
20 #include "extensions/browser/extension_registry_observer.h" | |
21 | |
22 class ExtensionService; | |
23 class ExtensionUninstallDialog; | |
24 | |
25 namespace extensions { | |
26 class ExtensionRegistry; | |
27 | |
28 class ManagementFunction : public ChromeSyncExtensionFunction { | |
29 protected: | |
30 ~ManagementFunction() override {} | |
31 | |
32 ExtensionService* service(); | |
33 }; | |
34 | |
35 class AsyncManagementFunction : public ChromeAsyncExtensionFunction { | |
36 protected: | |
37 ~AsyncManagementFunction() override {} | |
38 | |
39 ExtensionService* service(); | |
40 }; | |
41 | |
42 class ManagementGetAllFunction : public ManagementFunction { | |
43 public: | |
44 DECLARE_EXTENSION_FUNCTION("management.getAll", MANAGEMENT_GETALL) | |
45 | |
46 protected: | |
47 ~ManagementGetAllFunction() override {} | |
48 | |
49 // ExtensionFunction: | |
50 bool RunSync() override; | |
51 }; | |
52 | |
53 class ManagementGetFunction : public ManagementFunction { | |
54 public: | |
55 DECLARE_EXTENSION_FUNCTION("management.get", MANAGEMENT_GET) | |
56 | |
57 protected: | |
58 ~ManagementGetFunction() override {} | |
59 | |
60 // ExtensionFunction: | |
61 bool RunSync() override; | |
62 }; | |
63 | |
64 class ManagementGetSelfFunction : public ManagementFunction { | |
65 public: | |
66 DECLARE_EXTENSION_FUNCTION("management.getSelf", MANAGEMENT_GETSELF) | |
67 | |
68 protected: | |
69 ~ManagementGetSelfFunction() override {} | |
70 | |
71 // ExtensionFunction: | |
72 bool RunSync() override; | |
73 }; | |
74 | |
75 class ManagementGetPermissionWarningsByIdFunction : public ManagementFunction { | |
76 public: | |
77 DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsById", | |
78 MANAGEMENT_GETPERMISSIONWARNINGSBYID) | |
79 | |
80 protected: | |
81 ~ManagementGetPermissionWarningsByIdFunction() override {} | |
82 | |
83 // ExtensionFunction: | |
84 bool RunSync() override; | |
85 }; | |
86 | |
87 class ManagementGetPermissionWarningsByManifestFunction | |
88 : public ChromeAsyncExtensionFunction { | |
89 public: | |
90 DECLARE_EXTENSION_FUNCTION( | |
91 "management.getPermissionWarningsByManifest", | |
92 MANAGEMENT_GETPERMISSIONWARNINGSBYMANIFEST); | |
93 | |
94 // Called when utility process finishes. | |
95 void OnParseSuccess(scoped_ptr<base::DictionaryValue> parsed_manifest); | |
96 void OnParseFailure(const std::string& error); | |
97 | |
98 protected: | |
99 ~ManagementGetPermissionWarningsByManifestFunction() override {} | |
100 | |
101 // ExtensionFunction: | |
102 bool RunAsync() override; | |
103 }; | |
104 | |
105 class ManagementLaunchAppFunction : public ManagementFunction { | |
106 public: | |
107 DECLARE_EXTENSION_FUNCTION("management.launchApp", MANAGEMENT_LAUNCHAPP) | |
108 | |
109 protected: | |
110 ~ManagementLaunchAppFunction() override {} | |
111 | |
112 // ExtensionFunction: | |
113 bool RunSync() override; | |
114 }; | |
115 | |
116 class ManagementSetEnabledFunction : public AsyncManagementFunction, | |
117 public ExtensionInstallPrompt::Delegate { | |
118 public: | |
119 DECLARE_EXTENSION_FUNCTION("management.setEnabled", MANAGEMENT_SETENABLED) | |
120 | |
121 ManagementSetEnabledFunction(); | |
122 | |
123 protected: | |
124 ~ManagementSetEnabledFunction() override; | |
125 | |
126 // ExtensionFunction: | |
127 bool RunAsync() override; | |
128 | |
129 // ExtensionInstallPrompt::Delegate. | |
130 void InstallUIProceed() override; | |
131 void InstallUIAbort(bool user_initiated) override; | |
132 | |
133 private: | |
134 std::string extension_id_; | |
135 | |
136 // Used for prompting to re-enable items with permissions escalation updates. | |
137 scoped_ptr<ExtensionInstallPrompt> install_prompt_; | |
138 }; | |
139 | |
140 class ManagementUninstallFunctionBase : public AsyncManagementFunction, | |
141 public ExtensionUninstallDialog::Delegate { | |
142 public: | |
143 ManagementUninstallFunctionBase(); | |
144 | |
145 static void SetAutoConfirmForTest(bool should_proceed); | |
146 | |
147 // ExtensionUninstallDialog::Delegate implementation. | |
148 void ExtensionUninstallAccepted() override; | |
149 void ExtensionUninstallCanceled() override; | |
150 | |
151 protected: | |
152 ~ManagementUninstallFunctionBase() override; | |
153 | |
154 bool Uninstall(const std::string& extension_id, bool show_confirm_dialog); | |
155 private: | |
156 | |
157 // If should_uninstall is true, this method does the actual uninstall. | |
158 // If |show_uninstall_dialog|, then this function will be called by one of the | |
159 // Accepted/Canceled callbacks. Otherwise, it's called directly from RunAsync. | |
160 void Finish(bool should_uninstall); | |
161 | |
162 std::string extension_id_; | |
163 scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_; | |
164 }; | |
165 | |
166 class ManagementUninstallFunction : public ManagementUninstallFunctionBase { | |
167 public: | |
168 DECLARE_EXTENSION_FUNCTION("management.uninstall", MANAGEMENT_UNINSTALL) | |
169 | |
170 ManagementUninstallFunction(); | |
171 | |
172 private: | |
173 ~ManagementUninstallFunction() override; | |
174 | |
175 bool RunAsync() override; | |
176 }; | |
177 | |
178 class ManagementUninstallSelfFunction : public ManagementUninstallFunctionBase { | |
179 public: | |
180 DECLARE_EXTENSION_FUNCTION("management.uninstallSelf", | |
181 MANAGEMENT_UNINSTALLSELF); | |
182 | |
183 ManagementUninstallSelfFunction(); | |
184 | |
185 private: | |
186 ~ManagementUninstallSelfFunction() override; | |
187 | |
188 bool RunAsync() override; | |
189 }; | |
190 | |
191 class ManagementCreateAppShortcutFunction : public AsyncManagementFunction { | |
192 public: | |
193 DECLARE_EXTENSION_FUNCTION("management.createAppShortcut", | |
194 MANAGEMENT_CREATEAPPSHORTCUT); | |
195 | |
196 ManagementCreateAppShortcutFunction(); | |
197 | |
198 void OnCloseShortcutPrompt(bool created); | |
199 | |
200 static void SetAutoConfirmForTest(bool should_proceed); | |
201 | |
202 protected: | |
203 ~ManagementCreateAppShortcutFunction() override; | |
204 | |
205 bool RunAsync() override; | |
206 }; | |
207 | |
208 class ManagementSetLaunchTypeFunction : public ManagementFunction { | |
209 public: | |
210 DECLARE_EXTENSION_FUNCTION("management.setLaunchType", | |
211 MANAGEMENT_SETLAUNCHTYPE); | |
212 | |
213 protected: | |
214 ~ManagementSetLaunchTypeFunction() override {} | |
215 | |
216 bool RunSync() override; | |
217 }; | |
218 | |
219 class ManagementGenerateAppForLinkFunction : public AsyncManagementFunction { | |
220 public: | |
221 DECLARE_EXTENSION_FUNCTION("management.generateAppForLink", | |
222 MANAGEMENT_GENERATEAPPFORLINK); | |
223 | |
224 ManagementGenerateAppForLinkFunction(); | |
225 | |
226 protected: | |
227 ~ManagementGenerateAppForLinkFunction() override; | |
228 | |
229 bool RunAsync() override; | |
230 | |
231 private: | |
232 void OnFaviconForApp(const favicon_base::FaviconImageResult& image_result); | |
233 void FinishCreateBookmarkApp(const Extension* extension, | |
234 const WebApplicationInfo& web_app_info); | |
235 | |
236 std::string title_; | |
237 GURL launch_url_; | |
238 | |
239 scoped_ptr<BookmarkAppHelper> bookmark_app_helper_; | |
240 | |
241 // Used for favicon loading tasks. | |
242 base::CancelableTaskTracker cancelable_task_tracker_; | |
243 }; | |
244 | |
245 class ManagementEventRouter : public ExtensionRegistryObserver { | |
246 public: | |
247 explicit ManagementEventRouter(content::BrowserContext* context); | |
248 ~ManagementEventRouter() override; | |
249 | |
250 private: | |
251 // ExtensionRegistryObserver implementation. | |
252 void OnExtensionLoaded(content::BrowserContext* browser_context, | |
253 const Extension* extension) override; | |
254 void OnExtensionUnloaded(content::BrowserContext* browser_context, | |
255 const Extension* extension, | |
256 UnloadedExtensionInfo::Reason reason) override; | |
257 void OnExtensionInstalled(content::BrowserContext* browser_context, | |
258 const Extension* extension, | |
259 bool is_update) override; | |
260 void OnExtensionUninstalled(content::BrowserContext* browser_context, | |
261 const Extension* extension, | |
262 extensions::UninstallReason reason) override; | |
263 | |
264 // Dispatches management api events to listening extensions. | |
265 void BroadcastEvent(const Extension* extension, const char* event_name); | |
266 | |
267 content::BrowserContext* browser_context_; | |
268 | |
269 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | |
270 extension_registry_observer_; | |
271 | |
272 DISALLOW_COPY_AND_ASSIGN(ManagementEventRouter); | |
273 }; | |
274 | |
275 class ManagementAPI : public BrowserContextKeyedAPI, | |
276 public EventRouter::Observer { | |
277 public: | |
278 explicit ManagementAPI(content::BrowserContext* context); | |
279 ~ManagementAPI() override; | |
280 | |
281 // KeyedService implementation. | |
282 void Shutdown() override; | |
283 | |
284 // BrowserContextKeyedAPI implementation. | |
285 static BrowserContextKeyedAPIFactory<ManagementAPI>* GetFactoryInstance(); | |
286 | |
287 // EventRouter::Observer implementation. | |
288 void OnListenerAdded(const EventListenerInfo& details) override; | |
289 | |
290 private: | |
291 friend class BrowserContextKeyedAPIFactory<ManagementAPI>; | |
292 | |
293 content::BrowserContext* browser_context_; | |
294 | |
295 // BrowserContextKeyedAPI implementation. | |
296 static const char* service_name() { | |
297 return "ManagementAPI"; | |
298 } | |
299 static const bool kServiceIsNULLWhileTesting = true; | |
300 | |
301 // Created lazily upon OnListenerAdded. | |
302 scoped_ptr<ManagementEventRouter> management_event_router_; | |
303 | |
304 DISALLOW_COPY_AND_ASSIGN(ManagementAPI); | |
305 }; | |
306 | |
307 } // namespace extensions | |
308 | |
309 #endif // CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_H_ | |
OLD | NEW |