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 "base/strings/string16.h"
11 #include "components/keyed_service/core/keyed_service.h"
12 #include "extensions/browser/api/management/management_api_delegate.h"
13 #include "extensions/browser/browser_context_keyed_api_factory.h"
14 #include "extensions/browser/event_router.h"
15 #include "extensions/browser/extension_function.h"
16 #include "extensions/browser/extension_registry_observer.h"
18 class ExtensionRegistry
;
19 class ExtensionUninstallDialog
;
20 struct WebApplicationInfo
;
22 namespace extensions
{
23 class ExtensionRegistry
;
24 class RequirementsChecker
;
26 class ManagementFunction
: public SyncExtensionFunction
{
28 ~ManagementFunction() override
{}
31 class AsyncManagementFunction
: public AsyncExtensionFunction
{
33 ~AsyncManagementFunction() override
{}
36 class ManagementGetAllFunction
: public ManagementFunction
{
38 DECLARE_EXTENSION_FUNCTION("management.getAll", MANAGEMENT_GETALL
)
41 ~ManagementGetAllFunction() override
{}
44 bool RunSync() override
;
47 class ManagementGetFunction
: public ManagementFunction
{
49 DECLARE_EXTENSION_FUNCTION("management.get", MANAGEMENT_GET
)
52 ~ManagementGetFunction() override
{}
55 bool RunSync() override
;
58 class ManagementGetSelfFunction
: public ManagementFunction
{
60 DECLARE_EXTENSION_FUNCTION("management.getSelf", MANAGEMENT_GETSELF
)
63 ~ManagementGetSelfFunction() override
{}
66 bool RunSync() override
;
69 class ManagementGetPermissionWarningsByIdFunction
: public ManagementFunction
{
71 DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsById",
72 MANAGEMENT_GETPERMISSIONWARNINGSBYID
)
75 ~ManagementGetPermissionWarningsByIdFunction() override
{}
78 bool RunSync() override
;
81 class ManagementGetPermissionWarningsByManifestFunction
82 : public AsyncExtensionFunction
{
84 DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsByManifest",
85 MANAGEMENT_GETPERMISSIONWARNINGSBYMANIFEST
);
87 // Called when utility process finishes.
88 void OnParseSuccess(scoped_ptr
<base::Value
> value
);
89 void OnParseFailure(const std::string
& error
);
92 ~ManagementGetPermissionWarningsByManifestFunction() override
{}
95 bool RunAsync() override
;
98 class ManagementLaunchAppFunction
: public ManagementFunction
{
100 DECLARE_EXTENSION_FUNCTION("management.launchApp", MANAGEMENT_LAUNCHAPP
)
103 ~ManagementLaunchAppFunction() override
{}
105 // ExtensionFunction:
106 bool RunSync() override
;
109 class ManagementSetEnabledFunction
: public UIThreadExtensionFunction
{
111 DECLARE_EXTENSION_FUNCTION("management.setEnabled", MANAGEMENT_SETENABLED
)
113 ManagementSetEnabledFunction();
115 void InstallUIProceed();
116 void InstallUIAbort(bool user_initiated
);
119 ~ManagementSetEnabledFunction() override
;
121 // ExtensionFunction:
122 ResponseAction
Run() override
;
125 void OnRequirementsChecked(const std::vector
<std::string
>& requirements
);
127 std::string extension_id_
;
129 scoped_ptr
<InstallPromptDelegate
> install_prompt_
;
131 scoped_ptr
<RequirementsChecker
> requirements_checker_
;
134 class ManagementUninstallFunctionBase
: public UIThreadExtensionFunction
{
136 ManagementUninstallFunctionBase();
138 static void SetAutoConfirmForTest(bool should_proceed
);
140 void OnExtensionUninstallDialogClosed(bool did_start_uninstall
,
141 const base::string16
& error
);
144 ~ManagementUninstallFunctionBase() override
;
145 ResponseAction
Uninstall(const std::string
& extension_id
,
146 bool show_confirm_dialog
);
149 // Uninstalls the extension without showing the dialog.
150 void UninstallExtension();
152 // Finishes and responds to the extension.
153 void Finish(bool did_start_uninstall
, const std::string
& error
);
155 std::string target_extension_id_
;
157 scoped_ptr
<UninstallDialogDelegate
> uninstall_dialog_
;
160 class ManagementUninstallFunction
: public ManagementUninstallFunctionBase
{
162 DECLARE_EXTENSION_FUNCTION("management.uninstall", MANAGEMENT_UNINSTALL
)
163 ManagementUninstallFunction();
166 ~ManagementUninstallFunction() override
;
167 ResponseAction
Run() override
;
170 class ManagementUninstallSelfFunction
: public ManagementUninstallFunctionBase
{
172 DECLARE_EXTENSION_FUNCTION("management.uninstallSelf",
173 MANAGEMENT_UNINSTALLSELF
);
174 ManagementUninstallSelfFunction();
177 ~ManagementUninstallSelfFunction() override
;
178 ResponseAction
Run() override
;
181 class ManagementCreateAppShortcutFunction
: public AsyncManagementFunction
{
183 DECLARE_EXTENSION_FUNCTION("management.createAppShortcut",
184 MANAGEMENT_CREATEAPPSHORTCUT
);
186 ManagementCreateAppShortcutFunction();
188 void OnCloseShortcutPrompt(bool created
);
190 static void SetAutoConfirmForTest(bool should_proceed
);
193 ~ManagementCreateAppShortcutFunction() override
;
195 bool RunAsync() override
;
198 class ManagementSetLaunchTypeFunction
: public ManagementFunction
{
200 DECLARE_EXTENSION_FUNCTION("management.setLaunchType",
201 MANAGEMENT_SETLAUNCHTYPE
);
204 ~ManagementSetLaunchTypeFunction() override
{}
206 bool RunSync() override
;
209 class ManagementGenerateAppForLinkFunction
: public AsyncManagementFunction
{
211 DECLARE_EXTENSION_FUNCTION("management.generateAppForLink",
212 MANAGEMENT_GENERATEAPPFORLINK
);
214 ManagementGenerateAppForLinkFunction();
216 void FinishCreateBookmarkApp(const Extension
* extension
,
217 const WebApplicationInfo
& web_app_info
);
220 ~ManagementGenerateAppForLinkFunction() override
;
222 bool RunAsync() override
;
225 scoped_ptr
<AppForLinkDelegate
> app_for_link_delegate_
;
228 class ManagementEventRouter
: public ExtensionRegistryObserver
{
230 explicit ManagementEventRouter(content::BrowserContext
* context
);
231 ~ManagementEventRouter() override
;
234 // ExtensionRegistryObserver implementation.
235 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
236 const Extension
* extension
) override
;
237 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
238 const Extension
* extension
,
239 UnloadedExtensionInfo::Reason reason
) override
;
240 void OnExtensionInstalled(content::BrowserContext
* browser_context
,
241 const Extension
* extension
,
242 bool is_update
) override
;
243 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
244 const Extension
* extension
,
245 extensions::UninstallReason reason
) override
;
247 // Dispatches management api events to listening extensions.
248 void BroadcastEvent(const Extension
* extension
, const char* event_name
);
250 content::BrowserContext
* browser_context_
;
252 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
253 extension_registry_observer_
;
255 DISALLOW_COPY_AND_ASSIGN(ManagementEventRouter
);
258 class ManagementAPI
: public BrowserContextKeyedAPI
,
259 public EventRouter::Observer
{
261 explicit ManagementAPI(content::BrowserContext
* context
);
262 ~ManagementAPI() override
;
264 // KeyedService implementation.
265 void Shutdown() override
;
267 // BrowserContextKeyedAPI implementation.
268 static BrowserContextKeyedAPIFactory
<ManagementAPI
>* GetFactoryInstance();
270 // EventRouter::Observer implementation.
271 void OnListenerAdded(const EventListenerInfo
& details
) override
;
273 // Returns the ManagementAPI delegate.
274 const ManagementAPIDelegate
* GetDelegate() const { return delegate_
.get(); }
277 friend class BrowserContextKeyedAPIFactory
<ManagementAPI
>;
279 content::BrowserContext
* browser_context_
;
281 // BrowserContextKeyedAPI implementation.
282 static const char* service_name() { return "ManagementAPI"; }
283 static const bool kServiceIsNULLWhileTesting
= true;
284 static const bool kServiceRedirectedInIncognito
= true;
286 // Created lazily upon OnListenerAdded.
287 scoped_ptr
<ManagementEventRouter
> management_event_router_
;
289 scoped_ptr
<ManagementAPIDelegate
> delegate_
;
291 DISALLOW_COPY_AND_ASSIGN(ManagementAPI
);
294 } // namespace extensions
296 #endif // EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_