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
{
26 ~ManagementFunction() override
{}
29 class AsyncManagementFunction
: public AsyncExtensionFunction
{
31 ~AsyncManagementFunction() override
{}
34 class ManagementGetAllFunction
: public ManagementFunction
{
36 DECLARE_EXTENSION_FUNCTION("management.getAll", MANAGEMENT_GETALL
)
39 ~ManagementGetAllFunction() override
{}
42 bool RunSync() override
;
45 class ManagementGetFunction
: public ManagementFunction
{
47 DECLARE_EXTENSION_FUNCTION("management.get", MANAGEMENT_GET
)
50 ~ManagementGetFunction() override
{}
53 bool RunSync() override
;
56 class ManagementGetSelfFunction
: public ManagementFunction
{
58 DECLARE_EXTENSION_FUNCTION("management.getSelf", MANAGEMENT_GETSELF
)
61 ~ManagementGetSelfFunction() override
{}
64 bool RunSync() override
;
67 class ManagementGetPermissionWarningsByIdFunction
: public ManagementFunction
{
69 DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsById",
70 MANAGEMENT_GETPERMISSIONWARNINGSBYID
)
73 ~ManagementGetPermissionWarningsByIdFunction() override
{}
76 bool RunSync() override
;
79 class ManagementGetPermissionWarningsByManifestFunction
80 : public AsyncExtensionFunction
{
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
);
90 ~ManagementGetPermissionWarningsByManifestFunction() override
{}
93 bool RunAsync() override
;
96 class ManagementLaunchAppFunction
: public ManagementFunction
{
98 DECLARE_EXTENSION_FUNCTION("management.launchApp", MANAGEMENT_LAUNCHAPP
)
101 ~ManagementLaunchAppFunction() override
{}
103 // ExtensionFunction:
104 bool RunSync() override
;
107 class ManagementSetEnabledFunction
: public AsyncManagementFunction
{
109 DECLARE_EXTENSION_FUNCTION("management.setEnabled", MANAGEMENT_SETENABLED
)
111 ManagementSetEnabledFunction();
113 void InstallUIProceed();
114 void InstallUIAbort(bool user_initiated
);
117 ~ManagementSetEnabledFunction() override
;
119 // ExtensionFunction:
120 bool RunAsync() override
;
123 std::string extension_id_
;
125 scoped_ptr
<InstallPromptDelegate
> install_prompt_
;
128 class ManagementUninstallFunctionBase
: public AsyncManagementFunction
{
130 ManagementUninstallFunctionBase();
132 static void SetAutoConfirmForTest(bool should_proceed
);
134 void ExtensionUninstallAccepted();
135 void ExtensionUninstallCanceled();
138 ~ManagementUninstallFunctionBase() override
;
140 bool Uninstall(const std::string
& extension_id
, bool show_confirm_dialog
);
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
{
155 DECLARE_EXTENSION_FUNCTION("management.uninstall", MANAGEMENT_UNINSTALL
)
157 ManagementUninstallFunction();
160 ~ManagementUninstallFunction() override
;
162 bool RunAsync() override
;
165 class ManagementUninstallSelfFunction
: public ManagementUninstallFunctionBase
{
167 DECLARE_EXTENSION_FUNCTION("management.uninstallSelf",
168 MANAGEMENT_UNINSTALLSELF
);
170 ManagementUninstallSelfFunction();
173 ~ManagementUninstallSelfFunction() override
;
175 bool RunAsync() override
;
178 class ManagementCreateAppShortcutFunction
: public AsyncManagementFunction
{
180 DECLARE_EXTENSION_FUNCTION("management.createAppShortcut",
181 MANAGEMENT_CREATEAPPSHORTCUT
);
183 ManagementCreateAppShortcutFunction();
185 void OnCloseShortcutPrompt(bool created
);
187 static void SetAutoConfirmForTest(bool should_proceed
);
190 ~ManagementCreateAppShortcutFunction() override
;
192 bool RunAsync() override
;
195 class ManagementSetLaunchTypeFunction
: public ManagementFunction
{
197 DECLARE_EXTENSION_FUNCTION("management.setLaunchType",
198 MANAGEMENT_SETLAUNCHTYPE
);
201 ~ManagementSetLaunchTypeFunction() override
{}
203 bool RunSync() override
;
206 class ManagementGenerateAppForLinkFunction
: public AsyncManagementFunction
{
208 DECLARE_EXTENSION_FUNCTION("management.generateAppForLink",
209 MANAGEMENT_GENERATEAPPFORLINK
);
211 ManagementGenerateAppForLinkFunction();
213 void FinishCreateBookmarkApp(const Extension
* extension
,
214 const WebApplicationInfo
& web_app_info
);
217 ~ManagementGenerateAppForLinkFunction() override
;
219 bool RunAsync() override
;
222 scoped_ptr
<AppForLinkDelegate
> app_for_link_delegate_
;
225 class ManagementEventRouter
: public ExtensionRegistryObserver
{
227 explicit ManagementEventRouter(content::BrowserContext
* context
);
228 ~ManagementEventRouter() override
;
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
{
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(); }
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_