Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / extensions / browser / api / management / management_api.h
blobab7fb847199c764ae2e545e0c7502ec685639307
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;
24 class ManagementFunction : public SyncExtensionFunction {
25 protected:
26 ~ManagementFunction() override {}
29 class AsyncManagementFunction : public AsyncExtensionFunction {
30 protected:
31 ~AsyncManagementFunction() override {}
34 class ManagementGetAllFunction : public ManagementFunction {
35 public:
36 DECLARE_EXTENSION_FUNCTION("management.getAll", MANAGEMENT_GETALL)
38 protected:
39 ~ManagementGetAllFunction() override {}
41 // ExtensionFunction:
42 bool RunSync() override;
45 class ManagementGetFunction : public ManagementFunction {
46 public:
47 DECLARE_EXTENSION_FUNCTION("management.get", MANAGEMENT_GET)
49 protected:
50 ~ManagementGetFunction() override {}
52 // ExtensionFunction:
53 bool RunSync() override;
56 class ManagementGetSelfFunction : public ManagementFunction {
57 public:
58 DECLARE_EXTENSION_FUNCTION("management.getSelf", MANAGEMENT_GETSELF)
60 protected:
61 ~ManagementGetSelfFunction() override {}
63 // ExtensionFunction:
64 bool RunSync() override;
67 class ManagementGetPermissionWarningsByIdFunction : public ManagementFunction {
68 public:
69 DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsById",
70 MANAGEMENT_GETPERMISSIONWARNINGSBYID)
72 protected:
73 ~ManagementGetPermissionWarningsByIdFunction() override {}
75 // ExtensionFunction:
76 bool RunSync() override;
79 class ManagementGetPermissionWarningsByManifestFunction
80 : public AsyncExtensionFunction {
81 public:
82 DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsByManifest",
83 MANAGEMENT_GETPERMISSIONWARNINGSBYMANIFEST);
85 // Called when utility process finishes.
86 void OnParseSuccess(scoped_ptr<base::Value> value);
87 void OnParseFailure(const std::string& error);
89 protected:
90 ~ManagementGetPermissionWarningsByManifestFunction() override {}
92 // ExtensionFunction:
93 bool RunAsync() override;
96 class ManagementLaunchAppFunction : public ManagementFunction {
97 public:
98 DECLARE_EXTENSION_FUNCTION("management.launchApp", MANAGEMENT_LAUNCHAPP)
100 protected:
101 ~ManagementLaunchAppFunction() override {}
103 // ExtensionFunction:
104 bool RunSync() override;
107 class ManagementSetEnabledFunction : public AsyncManagementFunction {
108 public:
109 DECLARE_EXTENSION_FUNCTION("management.setEnabled", MANAGEMENT_SETENABLED)
111 ManagementSetEnabledFunction();
113 void InstallUIProceed();
114 void InstallUIAbort(bool user_initiated);
116 protected:
117 ~ManagementSetEnabledFunction() override;
119 // ExtensionFunction:
120 bool RunAsync() override;
122 private:
123 std::string extension_id_;
125 scoped_ptr<InstallPromptDelegate> install_prompt_;
128 class ManagementUninstallFunctionBase : public AsyncManagementFunction {
129 public:
130 ManagementUninstallFunctionBase();
132 static void SetAutoConfirmForTest(bool should_proceed);
134 void ExtensionUninstallAccepted();
135 void ExtensionUninstallCanceled();
137 protected:
138 ~ManagementUninstallFunctionBase() override;
140 bool Uninstall(const std::string& extension_id, bool show_confirm_dialog);
142 private:
143 // If should_uninstall is true, this method does the actual uninstall.
144 // If |show_uninstall_dialog|, then this function will be called by one of the
145 // Accepted/Canceled callbacks. Otherwise, it's called directly from RunAsync.
146 void Finish(bool should_uninstall);
148 std::string extension_id_;
150 scoped_ptr<UninstallDialogDelegate> uninstall_dialog_;
153 class ManagementUninstallFunction : public ManagementUninstallFunctionBase {
154 public:
155 DECLARE_EXTENSION_FUNCTION("management.uninstall", MANAGEMENT_UNINSTALL)
157 ManagementUninstallFunction();
159 private:
160 ~ManagementUninstallFunction() override;
162 bool RunAsync() override;
165 class ManagementUninstallSelfFunction : public ManagementUninstallFunctionBase {
166 public:
167 DECLARE_EXTENSION_FUNCTION("management.uninstallSelf",
168 MANAGEMENT_UNINSTALLSELF);
170 ManagementUninstallSelfFunction();
172 private:
173 ~ManagementUninstallSelfFunction() override;
175 bool RunAsync() override;
178 class ManagementCreateAppShortcutFunction : public AsyncManagementFunction {
179 public:
180 DECLARE_EXTENSION_FUNCTION("management.createAppShortcut",
181 MANAGEMENT_CREATEAPPSHORTCUT);
183 ManagementCreateAppShortcutFunction();
185 void OnCloseShortcutPrompt(bool created);
187 static void SetAutoConfirmForTest(bool should_proceed);
189 protected:
190 ~ManagementCreateAppShortcutFunction() override;
192 bool RunAsync() override;
195 class ManagementSetLaunchTypeFunction : public ManagementFunction {
196 public:
197 DECLARE_EXTENSION_FUNCTION("management.setLaunchType",
198 MANAGEMENT_SETLAUNCHTYPE);
200 protected:
201 ~ManagementSetLaunchTypeFunction() override {}
203 bool RunSync() override;
206 class ManagementGenerateAppForLinkFunction : public AsyncManagementFunction {
207 public:
208 DECLARE_EXTENSION_FUNCTION("management.generateAppForLink",
209 MANAGEMENT_GENERATEAPPFORLINK);
211 ManagementGenerateAppForLinkFunction();
213 void FinishCreateBookmarkApp(const Extension* extension,
214 const WebApplicationInfo& web_app_info);
216 protected:
217 ~ManagementGenerateAppForLinkFunction() override;
219 bool RunAsync() override;
221 private:
222 scoped_ptr<AppForLinkDelegate> app_for_link_delegate_;
225 class ManagementEventRouter : public ExtensionRegistryObserver {
226 public:
227 explicit ManagementEventRouter(content::BrowserContext* context);
228 ~ManagementEventRouter() override;
230 private:
231 // ExtensionRegistryObserver implementation.
232 void OnExtensionLoaded(content::BrowserContext* browser_context,
233 const Extension* extension) override;
234 void OnExtensionUnloaded(content::BrowserContext* browser_context,
235 const Extension* extension,
236 UnloadedExtensionInfo::Reason reason) override;
237 void OnExtensionInstalled(content::BrowserContext* browser_context,
238 const Extension* extension,
239 bool is_update) override;
240 void OnExtensionUninstalled(content::BrowserContext* browser_context,
241 const Extension* extension,
242 extensions::UninstallReason reason) override;
244 // Dispatches management api events to listening extensions.
245 void BroadcastEvent(const Extension* extension, const char* event_name);
247 content::BrowserContext* browser_context_;
249 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
250 extension_registry_observer_;
252 DISALLOW_COPY_AND_ASSIGN(ManagementEventRouter);
255 class ManagementAPI : public BrowserContextKeyedAPI,
256 public EventRouter::Observer {
257 public:
258 explicit ManagementAPI(content::BrowserContext* context);
259 ~ManagementAPI() override;
261 // KeyedService implementation.
262 void Shutdown() override;
264 // BrowserContextKeyedAPI implementation.
265 static BrowserContextKeyedAPIFactory<ManagementAPI>* GetFactoryInstance();
267 // EventRouter::Observer implementation.
268 void OnListenerAdded(const EventListenerInfo& details) override;
270 // Returns the ManagementAPI delegate.
271 const ManagementAPIDelegate* GetDelegate() const { return delegate_.get(); }
273 private:
274 friend class BrowserContextKeyedAPIFactory<ManagementAPI>;
276 content::BrowserContext* browser_context_;
278 // BrowserContextKeyedAPI implementation.
279 static const char* service_name() { return "ManagementAPI"; }
280 static const bool kServiceIsNULLWhileTesting = true;
281 static const bool kServiceRedirectedInIncognito = true;
283 // Created lazily upon OnListenerAdded.
284 scoped_ptr<ManagementEventRouter> management_event_router_;
286 scoped_ptr<ManagementAPIDelegate> delegate_;
288 DISALLOW_COPY_AND_ASSIGN(ManagementAPI);
291 } // namespace extensions
293 #endif // EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_