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
{
27 ~ManagementFunction() override
{}
30 class AsyncManagementFunction
: public AsyncExtensionFunction
{
32 ~AsyncManagementFunction() override
{}
35 class ManagementGetAllFunction
: public ManagementFunction
{
37 DECLARE_EXTENSION_FUNCTION("management.getAll", MANAGEMENT_GETALL
)
40 ~ManagementGetAllFunction() override
{}
43 bool RunSync() override
;
46 class ManagementGetFunction
: public ManagementFunction
{
48 DECLARE_EXTENSION_FUNCTION("management.get", MANAGEMENT_GET
)
51 ~ManagementGetFunction() override
{}
54 bool RunSync() override
;
57 class ManagementGetSelfFunction
: public ManagementFunction
{
59 DECLARE_EXTENSION_FUNCTION("management.getSelf", MANAGEMENT_GETSELF
)
62 ~ManagementGetSelfFunction() override
{}
65 bool RunSync() override
;
68 class ManagementGetPermissionWarningsByIdFunction
: public ManagementFunction
{
70 DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsById",
71 MANAGEMENT_GETPERMISSIONWARNINGSBYID
)
74 ~ManagementGetPermissionWarningsByIdFunction() override
{}
77 bool RunSync() override
;
80 class ManagementGetPermissionWarningsByManifestFunction
81 : public AsyncExtensionFunction
{
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
);
91 ~ManagementGetPermissionWarningsByManifestFunction() override
{}
94 bool RunAsync() override
;
97 class ManagementLaunchAppFunction
: public ManagementFunction
{
99 DECLARE_EXTENSION_FUNCTION("management.launchApp", MANAGEMENT_LAUNCHAPP
)
102 ~ManagementLaunchAppFunction() override
{}
104 // ExtensionFunction:
105 bool RunSync() override
;
108 class ManagementSetEnabledFunction
: public UIThreadExtensionFunction
{
110 DECLARE_EXTENSION_FUNCTION("management.setEnabled", MANAGEMENT_SETENABLED
)
112 ManagementSetEnabledFunction();
114 void InstallUIProceed();
115 void InstallUIAbort(bool user_initiated
);
118 ~ManagementSetEnabledFunction() override
;
120 // ExtensionFunction:
121 ResponseAction
Run() override
;
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 AsyncManagementFunction
{
135 ManagementUninstallFunctionBase();
137 static void SetAutoConfirmForTest(bool should_proceed
);
139 void ExtensionUninstallAccepted();
140 void ExtensionUninstallCanceled();
143 ~ManagementUninstallFunctionBase() override
;
145 bool Uninstall(const std::string
& extension_id
, bool show_confirm_dialog
);
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 extension_id_
;
155 scoped_ptr
<UninstallDialogDelegate
> uninstall_dialog_
;
158 class ManagementUninstallFunction
: public ManagementUninstallFunctionBase
{
160 DECLARE_EXTENSION_FUNCTION("management.uninstall", MANAGEMENT_UNINSTALL
)
162 ManagementUninstallFunction();
165 ~ManagementUninstallFunction() override
;
167 bool RunAsync() override
;
170 class ManagementUninstallSelfFunction
: public ManagementUninstallFunctionBase
{
172 DECLARE_EXTENSION_FUNCTION("management.uninstallSelf",
173 MANAGEMENT_UNINSTALLSELF
);
175 ManagementUninstallSelfFunction();
178 ~ManagementUninstallSelfFunction() override
;
180 bool RunAsync() override
;
183 class ManagementCreateAppShortcutFunction
: public AsyncManagementFunction
{
185 DECLARE_EXTENSION_FUNCTION("management.createAppShortcut",
186 MANAGEMENT_CREATEAPPSHORTCUT
);
188 ManagementCreateAppShortcutFunction();
190 void OnCloseShortcutPrompt(bool created
);
192 static void SetAutoConfirmForTest(bool should_proceed
);
195 ~ManagementCreateAppShortcutFunction() override
;
197 bool RunAsync() override
;
200 class ManagementSetLaunchTypeFunction
: public ManagementFunction
{
202 DECLARE_EXTENSION_FUNCTION("management.setLaunchType",
203 MANAGEMENT_SETLAUNCHTYPE
);
206 ~ManagementSetLaunchTypeFunction() override
{}
208 bool RunSync() override
;
211 class ManagementGenerateAppForLinkFunction
: public AsyncManagementFunction
{
213 DECLARE_EXTENSION_FUNCTION("management.generateAppForLink",
214 MANAGEMENT_GENERATEAPPFORLINK
);
216 ManagementGenerateAppForLinkFunction();
218 void FinishCreateBookmarkApp(const Extension
* extension
,
219 const WebApplicationInfo
& web_app_info
);
222 ~ManagementGenerateAppForLinkFunction() override
;
224 bool RunAsync() override
;
227 scoped_ptr
<AppForLinkDelegate
> app_for_link_delegate_
;
230 class ManagementEventRouter
: public ExtensionRegistryObserver
{
232 explicit ManagementEventRouter(content::BrowserContext
* context
);
233 ~ManagementEventRouter() override
;
236 // ExtensionRegistryObserver implementation.
237 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
238 const Extension
* extension
) override
;
239 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
240 const Extension
* extension
,
241 UnloadedExtensionInfo::Reason reason
) override
;
242 void OnExtensionInstalled(content::BrowserContext
* browser_context
,
243 const Extension
* extension
,
244 bool is_update
) override
;
245 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
246 const Extension
* extension
,
247 extensions::UninstallReason reason
) override
;
249 // Dispatches management api events to listening extensions.
250 void BroadcastEvent(const Extension
* extension
, const char* event_name
);
252 content::BrowserContext
* browser_context_
;
254 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
255 extension_registry_observer_
;
257 DISALLOW_COPY_AND_ASSIGN(ManagementEventRouter
);
260 class ManagementAPI
: public BrowserContextKeyedAPI
,
261 public EventRouter::Observer
{
263 explicit ManagementAPI(content::BrowserContext
* context
);
264 ~ManagementAPI() override
;
266 // KeyedService implementation.
267 void Shutdown() override
;
269 // BrowserContextKeyedAPI implementation.
270 static BrowserContextKeyedAPIFactory
<ManagementAPI
>* GetFactoryInstance();
272 // EventRouter::Observer implementation.
273 void OnListenerAdded(const EventListenerInfo
& details
) override
;
275 // Returns the ManagementAPI delegate.
276 const ManagementAPIDelegate
* GetDelegate() const { return delegate_
.get(); }
279 friend class BrowserContextKeyedAPIFactory
<ManagementAPI
>;
281 content::BrowserContext
* browser_context_
;
283 // BrowserContextKeyedAPI implementation.
284 static const char* service_name() { return "ManagementAPI"; }
285 static const bool kServiceIsNULLWhileTesting
= true;
286 static const bool kServiceRedirectedInIncognito
= true;
288 // Created lazily upon OnListenerAdded.
289 scoped_ptr
<ManagementEventRouter
> management_event_router_
;
291 scoped_ptr
<ManagementAPIDelegate
> delegate_
;
293 DISALLOW_COPY_AND_ASSIGN(ManagementAPI
);
296 } // namespace extensions
298 #endif // EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_