Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / management / management_api.h
blobcd1c5f68c41b08da08337caae1cde6ca9d8636d4
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.
5 #ifndef EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_
6 #define EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_
8 #include "base/compiler_specific.h"
9 #include "base/scoped_observer.h"
10 #include "components/keyed_service/core/keyed_service.h"
11 #include "extensions/browser/api/management/management_api_delegate.h"
12 #include "extensions/browser/browser_context_keyed_api_factory.h"
13 #include "extensions/browser/event_router.h"
14 #include "extensions/browser/extension_function.h"
15 #include "extensions/browser/extension_registry_observer.h"
17 class ExtensionRegistry;
18 class ExtensionUninstallDialog;
19 struct WebApplicationInfo;
21 namespace extensions {
22 class ExtensionRegistry;
23 class RequirementsChecker;
25 class ManagementFunction : public SyncExtensionFunction {
26 protected:
27 ~ManagementFunction() override {}
30 class AsyncManagementFunction : public AsyncExtensionFunction {
31 protected:
32 ~AsyncManagementFunction() override {}
35 class ManagementGetAllFunction : public ManagementFunction {
36 public:
37 DECLARE_EXTENSION_FUNCTION("management.getAll", MANAGEMENT_GETALL)
39 protected:
40 ~ManagementGetAllFunction() override {}
42 // ExtensionFunction:
43 bool RunSync() override;
46 class ManagementGetFunction : public ManagementFunction {
47 public:
48 DECLARE_EXTENSION_FUNCTION("management.get", MANAGEMENT_GET)
50 protected:
51 ~ManagementGetFunction() override {}
53 // ExtensionFunction:
54 bool RunSync() override;
57 class ManagementGetSelfFunction : public ManagementFunction {
58 public:
59 DECLARE_EXTENSION_FUNCTION("management.getSelf", MANAGEMENT_GETSELF)
61 protected:
62 ~ManagementGetSelfFunction() override {}
64 // ExtensionFunction:
65 bool RunSync() override;
68 class ManagementGetPermissionWarningsByIdFunction : public ManagementFunction {
69 public:
70 DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsById",
71 MANAGEMENT_GETPERMISSIONWARNINGSBYID)
73 protected:
74 ~ManagementGetPermissionWarningsByIdFunction() override {}
76 // ExtensionFunction:
77 bool RunSync() override;
80 class ManagementGetPermissionWarningsByManifestFunction
81 : public AsyncExtensionFunction {
82 public:
83 DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsByManifest",
84 MANAGEMENT_GETPERMISSIONWARNINGSBYMANIFEST);
86 // Called when utility process finishes.
87 void OnParseSuccess(scoped_ptr<base::Value> value);
88 void OnParseFailure(const std::string& error);
90 protected:
91 ~ManagementGetPermissionWarningsByManifestFunction() override {}
93 // ExtensionFunction:
94 bool RunAsync() override;
97 class ManagementLaunchAppFunction : public ManagementFunction {
98 public:
99 DECLARE_EXTENSION_FUNCTION("management.launchApp", MANAGEMENT_LAUNCHAPP)
101 protected:
102 ~ManagementLaunchAppFunction() override {}
104 // ExtensionFunction:
105 bool RunSync() override;
108 class ManagementSetEnabledFunction : public UIThreadExtensionFunction {
109 public:
110 DECLARE_EXTENSION_FUNCTION("management.setEnabled", MANAGEMENT_SETENABLED)
112 ManagementSetEnabledFunction();
114 void InstallUIProceed();
115 void InstallUIAbort(bool user_initiated);
117 protected:
118 ~ManagementSetEnabledFunction() override;
120 // ExtensionFunction:
121 ResponseAction Run() override;
123 private:
124 void OnRequirementsChecked(const std::vector<std::string>& requirements);
126 std::string extension_id_;
128 scoped_ptr<InstallPromptDelegate> install_prompt_;
130 scoped_ptr<RequirementsChecker> requirements_checker_;
133 class ManagementUninstallFunctionBase : public UIThreadExtensionFunction {
134 public:
135 ManagementUninstallFunctionBase();
137 static void SetAutoConfirmForTest(bool should_proceed);
139 void ExtensionUninstallAccepted();
140 void ExtensionUninstallCanceled();
142 protected:
143 ~ManagementUninstallFunctionBase() override;
144 ResponseAction Uninstall(const std::string& extension_id,
145 bool show_confirm_dialog);
147 private:
148 // If should_uninstall is true, this method does the actual uninstall.
149 // If |show_uninstall_dialog|, then this function will be called by one of the
150 // Accepted/Canceled callbacks. Otherwise, it's called directly from RunAsync.
151 void Finish(bool should_uninstall);
153 std::string target_extension_id_;
155 scoped_ptr<UninstallDialogDelegate> uninstall_dialog_;
158 class ManagementUninstallFunction : public ManagementUninstallFunctionBase {
159 public:
160 DECLARE_EXTENSION_FUNCTION("management.uninstall", MANAGEMENT_UNINSTALL)
161 ManagementUninstallFunction();
163 private:
164 ~ManagementUninstallFunction() override;
165 ResponseAction Run() override;
168 class ManagementUninstallSelfFunction : public ManagementUninstallFunctionBase {
169 public:
170 DECLARE_EXTENSION_FUNCTION("management.uninstallSelf",
171 MANAGEMENT_UNINSTALLSELF);
172 ManagementUninstallSelfFunction();
174 private:
175 ~ManagementUninstallSelfFunction() override;
176 ResponseAction Run() override;
179 class ManagementCreateAppShortcutFunction : public AsyncManagementFunction {
180 public:
181 DECLARE_EXTENSION_FUNCTION("management.createAppShortcut",
182 MANAGEMENT_CREATEAPPSHORTCUT);
184 ManagementCreateAppShortcutFunction();
186 void OnCloseShortcutPrompt(bool created);
188 static void SetAutoConfirmForTest(bool should_proceed);
190 protected:
191 ~ManagementCreateAppShortcutFunction() override;
193 bool RunAsync() override;
196 class ManagementSetLaunchTypeFunction : public ManagementFunction {
197 public:
198 DECLARE_EXTENSION_FUNCTION("management.setLaunchType",
199 MANAGEMENT_SETLAUNCHTYPE);
201 protected:
202 ~ManagementSetLaunchTypeFunction() override {}
204 bool RunSync() override;
207 class ManagementGenerateAppForLinkFunction : public AsyncManagementFunction {
208 public:
209 DECLARE_EXTENSION_FUNCTION("management.generateAppForLink",
210 MANAGEMENT_GENERATEAPPFORLINK);
212 ManagementGenerateAppForLinkFunction();
214 void FinishCreateBookmarkApp(const Extension* extension,
215 const WebApplicationInfo& web_app_info);
217 protected:
218 ~ManagementGenerateAppForLinkFunction() override;
220 bool RunAsync() override;
222 private:
223 scoped_ptr<AppForLinkDelegate> app_for_link_delegate_;
226 class ManagementEventRouter : public ExtensionRegistryObserver {
227 public:
228 explicit ManagementEventRouter(content::BrowserContext* context);
229 ~ManagementEventRouter() override;
231 private:
232 // ExtensionRegistryObserver implementation.
233 void OnExtensionLoaded(content::BrowserContext* browser_context,
234 const Extension* extension) override;
235 void OnExtensionUnloaded(content::BrowserContext* browser_context,
236 const Extension* extension,
237 UnloadedExtensionInfo::Reason reason) override;
238 void OnExtensionInstalled(content::BrowserContext* browser_context,
239 const Extension* extension,
240 bool is_update) override;
241 void OnExtensionUninstalled(content::BrowserContext* browser_context,
242 const Extension* extension,
243 extensions::UninstallReason reason) override;
245 // Dispatches management api events to listening extensions.
246 void BroadcastEvent(const Extension* extension, const char* event_name);
248 content::BrowserContext* browser_context_;
250 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
251 extension_registry_observer_;
253 DISALLOW_COPY_AND_ASSIGN(ManagementEventRouter);
256 class ManagementAPI : public BrowserContextKeyedAPI,
257 public EventRouter::Observer {
258 public:
259 explicit ManagementAPI(content::BrowserContext* context);
260 ~ManagementAPI() override;
262 // KeyedService implementation.
263 void Shutdown() override;
265 // BrowserContextKeyedAPI implementation.
266 static BrowserContextKeyedAPIFactory<ManagementAPI>* GetFactoryInstance();
268 // EventRouter::Observer implementation.
269 void OnListenerAdded(const EventListenerInfo& details) override;
271 // Returns the ManagementAPI delegate.
272 const ManagementAPIDelegate* GetDelegate() const { return delegate_.get(); }
274 private:
275 friend class BrowserContextKeyedAPIFactory<ManagementAPI>;
277 content::BrowserContext* browser_context_;
279 // BrowserContextKeyedAPI implementation.
280 static const char* service_name() { return "ManagementAPI"; }
281 static const bool kServiceIsNULLWhileTesting = true;
282 static const bool kServiceRedirectedInIncognito = true;
284 // Created lazily upon OnListenerAdded.
285 scoped_ptr<ManagementEventRouter> management_event_router_;
287 scoped_ptr<ManagementAPIDelegate> delegate_;
289 DISALLOW_COPY_AND_ASSIGN(ManagementAPI);
292 } // namespace extensions
294 #endif // EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_