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