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 CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_PRIVATE_WEBSTORE_PRIVATE_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_PRIVATE_WEBSTORE_PRIVATE_API_H_
10 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_delegate.h"
11 #include "chrome/browser/extensions/active_install_data.h"
12 #include "chrome/browser/extensions/bundle_installer.h"
13 #include "chrome/browser/extensions/chrome_extension_function_details.h"
14 #include "chrome/browser/extensions/extension_install_prompt.h"
15 #include "chrome/browser/extensions/webstore_install_helper.h"
16 #include "chrome/browser/extensions/webstore_installer.h"
17 #include "chrome/common/extensions/api/webstore_private.h"
18 #include "chrome/common/extensions/webstore_install_result.h"
19 #include "extensions/browser/extension_function.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
22 class GPUFeatureChecker
;
28 namespace extensions
{
30 class WebstorePrivateApi
{
32 // Allows you to override the WebstoreInstaller delegate for testing.
33 static void SetWebstoreInstallerDelegateForTesting(
34 WebstoreInstaller::Delegate
* delegate
);
36 // Gets the pending approval for the |extension_id| in |profile|. Pending
37 // approvals are held between the calls to beginInstallWithManifest and
38 // completeInstall. This should only be used for testing.
39 static scoped_ptr
<WebstoreInstaller::Approval
> PopApprovalForTesting(
40 Profile
* profile
, const std::string
& extension_id
);
43 // Base class for webstorePrivate functions that show a permission prompt.
44 template<typename Params
>
45 class WebstorePrivateFunctionWithPermissionPrompt
46 : public UIThreadExtensionFunction
,
47 public ExtensionInstallPrompt::Delegate
,
48 public WebstoreInstallHelper::Delegate
{
50 WebstorePrivateFunctionWithPermissionPrompt();
53 ~WebstorePrivateFunctionWithPermissionPrompt() override
;
55 // May be implemented by subclasses to add their own code to the
56 // ExtensionFunction::Run implementation. Return a non-null ResponseValue to
57 // trigger an immediate response.
58 virtual ExtensionFunction::ResponseValue
RunExtraForResponse();
60 // Must be implemented by subclasses to call one of the Confirm* methods on
62 virtual void ShowPrompt(ExtensionInstallPrompt
* install_prompt
) = 0;
64 // May be implemented by subclasses to add their own code to the
65 // ExtensionInstallPrompt::Delegate::InstallUIProceed and InstallUIAbort
67 virtual void InstallUIProceedHook() {}
68 virtual void InstallUIAbortHook(bool user_initiated
) {}
70 // Must be implemented by subclasses to forward to their own Results::Create
72 virtual scoped_ptr
<base::ListValue
> CreateResults(
73 api::webstore_private::Result result
) const = 0;
75 ExtensionFunction::ResponseValue
BuildResponse(
76 api::webstore_private::Result result
,
77 const std::string
& error
);
79 const typename
Params::Details
& details() const { return params_
->details
; }
80 const SkBitmap
& icon() const { return icon_
; }
81 const scoped_refptr
<Extension
>& dummy_extension() const {
82 return dummy_extension_
;
85 scoped_ptr
<base::DictionaryValue
> PassParsedManifest();
89 ExtensionFunction::ResponseAction
Run() override
;
91 // WebstoreInstallHelper::Delegate:
92 void OnWebstoreParseSuccess(const std::string
& id
,
94 base::DictionaryValue
* parsed_manifest
) override
;
95 void OnWebstoreParseFailure(const std::string
& id
,
96 InstallHelperResultCode result
,
97 const std::string
& error_message
) override
;
99 // ExtensionInstallPrompt::Delegate:
100 void InstallUIProceed() override
;
101 void InstallUIAbort(bool user_initiated
) override
;
103 // This stores the input parameters to the function.
104 scoped_ptr
<Params
> params_
;
106 // The results of parsing manifest_ and icon_data_.
107 scoped_ptr
<base::DictionaryValue
> parsed_manifest_
;
110 // A dummy Extension object we create for the purposes of using
111 // ExtensionInstallPrompt to prompt for confirmation of the install.
112 scoped_refptr
<Extension
> dummy_extension_
;
114 // The class that displays the install prompt.
115 scoped_ptr
<ExtensionInstallPrompt
> install_prompt_
;
118 class WebstorePrivateBeginInstallWithManifest3Function
119 : public WebstorePrivateFunctionWithPermissionPrompt
120 <api::webstore_private::BeginInstallWithManifest3::Params
> {
122 DECLARE_EXTENSION_FUNCTION("webstorePrivate.beginInstallWithManifest3",
123 WEBSTOREPRIVATE_BEGININSTALLWITHMANIFEST3
)
125 WebstorePrivateBeginInstallWithManifest3Function();
128 ~WebstorePrivateBeginInstallWithManifest3Function() override
;
130 ExtensionFunction::ResponseValue
RunExtraForResponse() override
;
131 void ShowPrompt(ExtensionInstallPrompt
* install_prompt
) override
;
132 void InstallUIProceedHook() override
;
133 void InstallUIAbortHook(bool user_initiated
) override
;
134 scoped_ptr
<base::ListValue
> CreateResults(
135 api::webstore_private::Result result
) const override
;
137 ChromeExtensionFunctionDetails chrome_details_
;
139 scoped_ptr
<ScopedActiveInstall
> scoped_active_install_
;
142 class WebstorePrivateCompleteInstallFunction
143 : public UIThreadExtensionFunction
,
144 public WebstoreInstaller::Delegate
{
146 DECLARE_EXTENSION_FUNCTION("webstorePrivate.completeInstall",
147 WEBSTOREPRIVATE_COMPLETEINSTALL
)
149 WebstorePrivateCompleteInstallFunction();
152 ~WebstorePrivateCompleteInstallFunction() override
;
154 // ExtensionFunction:
155 ExtensionFunction::ResponseAction
Run() override
;
157 // WebstoreInstaller::Delegate:
158 void OnExtensionInstallSuccess(const std::string
& id
) override
;
159 void OnExtensionInstallFailure(
160 const std::string
& id
,
161 const std::string
& error
,
162 WebstoreInstaller::FailureReason reason
) override
;
164 void OnInstallSuccess(const std::string
& id
);
166 ChromeExtensionFunctionDetails chrome_details_
;
168 scoped_ptr
<WebstoreInstaller::Approval
> approval_
;
169 scoped_ptr
<ScopedActiveInstall
> scoped_active_install_
;
172 class WebstorePrivateShowPermissionPromptForDelegatedInstallFunction
173 : public WebstorePrivateFunctionWithPermissionPrompt
174 <api::webstore_private::ShowPermissionPromptForDelegatedInstall::
177 DECLARE_EXTENSION_FUNCTION(
178 "webstorePrivate.showPermissionPromptForDelegatedInstall",
179 WEBSTOREPRIVATE_SHOWPERMISSIONPROMPTFORDELEGATEDINSTALL
)
181 WebstorePrivateShowPermissionPromptForDelegatedInstallFunction();
184 ~WebstorePrivateShowPermissionPromptForDelegatedInstallFunction() override
;
186 void ShowPrompt(ExtensionInstallPrompt
* install_prompt
) override
;
187 scoped_ptr
<base::ListValue
> CreateResults(
188 api::webstore_private::Result result
) const override
;
191 // Base class for webstorePrivate functions that deal with bundles.
192 template<typename Params
>
193 class WebstorePrivateFunctionWithBundle
194 : public UIThreadExtensionFunction
,
195 public chrome::BitmapFetcherDelegate
{
197 WebstorePrivateFunctionWithBundle();
200 ~WebstorePrivateFunctionWithBundle() override
;
202 const typename
Params::Details
& details() const { return params_
->details
; }
203 extensions::BundleInstaller
* bundle() { return bundle_
.get(); }
204 ChromeExtensionFunctionDetails
* chrome_details() { return &chrome_details_
; }
206 void set_auth_user(const std::string
& user
) { auth_user_
= user
; }
207 void set_delegated_user(const std::string
& user
) {
208 delegated_user_
= user
;
211 // Called after |params_| has been created.
212 virtual void ProcessParams() = 0;
213 // Called for each bundle item before showing them in the dialog.
214 virtual bool ShouldSkipItem(const std::string
& id
) const = 0;
215 // Called after the user has confirmed the dialog.
216 virtual void OnInstallApprovalHook() = 0;
219 // ExtensionFunction:
220 ExtensionFunction::ResponseAction
Run() override
;
222 // chrome::BitmapFetcherDelegate:
223 void OnFetchComplete(const GURL
& url
, const SkBitmap
* bitmap
) override
;
225 void OnInstallApproval(BundleInstaller::ApprovalState state
);
227 ChromeExtensionFunctionDetails chrome_details_
;
229 // This stores the input parameters to the function.
230 scoped_ptr
<Params
> params_
;
232 std::string auth_user_
;
233 std::string delegated_user_
;
235 scoped_ptr
<extensions::BundleInstaller
> bundle_
;
237 scoped_ptr
<chrome::BitmapFetcher
> icon_fetcher_
;
240 class WebstorePrivateInstallBundleFunction
241 : public WebstorePrivateFunctionWithBundle
242 <api::webstore_private::InstallBundle::Params
> {
244 DECLARE_EXTENSION_FUNCTION("webstorePrivate.installBundle",
245 WEBSTOREPRIVATE_INSTALLBUNDLE
)
247 WebstorePrivateInstallBundleFunction();
250 ~WebstorePrivateInstallBundleFunction() override
;
252 // WebstorePrivateFunctionWithBundle:
253 void ProcessParams() override
;
254 bool ShouldSkipItem(const std::string
& id
) const override
;
255 void OnInstallApprovalHook() override
;
257 void OnInstallComplete();
260 class WebstorePrivateShowPermissionPromptForDelegatedBundleInstallFunction
261 : public WebstorePrivateFunctionWithBundle
262 <api::webstore_private::
263 ShowPermissionPromptForDelegatedBundleInstall::Params
> {
265 DECLARE_EXTENSION_FUNCTION(
266 "webstorePrivate.showPermissionPromptForDelegatedBundleInstall",
267 WEBSTOREPRIVATE_SHOWPERMISSIONPROMPTFORDELEGATEDBUNDLEINSTALL
)
269 WebstorePrivateShowPermissionPromptForDelegatedBundleInstallFunction();
272 ~WebstorePrivateShowPermissionPromptForDelegatedBundleInstallFunction()
275 // WebstorePrivateFunctionWithBundle:
276 void ProcessParams() override
;
277 bool ShouldSkipItem(const std::string
& id
) const override
;
278 void OnInstallApprovalHook() override
;
281 class WebstorePrivateEnableAppLauncherFunction
282 : public UIThreadExtensionFunction
{
284 DECLARE_EXTENSION_FUNCTION("webstorePrivate.enableAppLauncher",
285 WEBSTOREPRIVATE_ENABLEAPPLAUNCHER
)
287 WebstorePrivateEnableAppLauncherFunction();
290 ~WebstorePrivateEnableAppLauncherFunction() override
;
292 // ExtensionFunction:
293 ExtensionFunction::ResponseAction
Run() override
;
295 ChromeExtensionFunctionDetails chrome_details_
;
298 class WebstorePrivateGetBrowserLoginFunction
299 : public UIThreadExtensionFunction
{
301 DECLARE_EXTENSION_FUNCTION("webstorePrivate.getBrowserLogin",
302 WEBSTOREPRIVATE_GETBROWSERLOGIN
)
304 WebstorePrivateGetBrowserLoginFunction();
307 ~WebstorePrivateGetBrowserLoginFunction() override
;
309 // ExtensionFunction:
310 ExtensionFunction::ResponseAction
Run() override
;
312 ChromeExtensionFunctionDetails chrome_details_
;
315 class WebstorePrivateGetStoreLoginFunction
316 : public UIThreadExtensionFunction
{
318 DECLARE_EXTENSION_FUNCTION("webstorePrivate.getStoreLogin",
319 WEBSTOREPRIVATE_GETSTORELOGIN
)
321 WebstorePrivateGetStoreLoginFunction();
324 ~WebstorePrivateGetStoreLoginFunction() override
;
326 // ExtensionFunction:
327 ExtensionFunction::ResponseAction
Run() override
;
329 ChromeExtensionFunctionDetails chrome_details_
;
332 class WebstorePrivateSetStoreLoginFunction
333 : public UIThreadExtensionFunction
{
335 DECLARE_EXTENSION_FUNCTION("webstorePrivate.setStoreLogin",
336 WEBSTOREPRIVATE_SETSTORELOGIN
)
338 WebstorePrivateSetStoreLoginFunction();
341 ~WebstorePrivateSetStoreLoginFunction() override
;
343 // ExtensionFunction:
344 ExtensionFunction::ResponseAction
Run() override
;
346 ChromeExtensionFunctionDetails chrome_details_
;
349 class WebstorePrivateGetWebGLStatusFunction
350 : public UIThreadExtensionFunction
{
352 DECLARE_EXTENSION_FUNCTION("webstorePrivate.getWebGLStatus",
353 WEBSTOREPRIVATE_GETWEBGLSTATUS
)
355 WebstorePrivateGetWebGLStatusFunction();
358 ~WebstorePrivateGetWebGLStatusFunction() override
;
360 // ExtensionFunction:
361 ExtensionFunction::ResponseAction
Run() override
;
363 void OnFeatureCheck(bool feature_allowed
);
365 scoped_refptr
<GPUFeatureChecker
> feature_checker_
;
368 class WebstorePrivateGetIsLauncherEnabledFunction
369 : public UIThreadExtensionFunction
{
371 DECLARE_EXTENSION_FUNCTION("webstorePrivate.getIsLauncherEnabled",
372 WEBSTOREPRIVATE_GETISLAUNCHERENABLED
)
374 WebstorePrivateGetIsLauncherEnabledFunction();
377 ~WebstorePrivateGetIsLauncherEnabledFunction() override
;
379 // ExtensionFunction:
380 ExtensionFunction::ResponseAction
Run() override
;
382 void OnIsLauncherCheckCompleted(bool is_enabled
);
385 class WebstorePrivateIsInIncognitoModeFunction
386 : public UIThreadExtensionFunction
{
388 DECLARE_EXTENSION_FUNCTION("webstorePrivate.isInIncognitoMode",
389 WEBSTOREPRIVATE_ISININCOGNITOMODEFUNCTION
)
391 WebstorePrivateIsInIncognitoModeFunction();
394 ~WebstorePrivateIsInIncognitoModeFunction() override
;
396 // ExtensionFunction:
397 ExtensionFunction::ResponseAction
Run() override
;
399 ChromeExtensionFunctionDetails chrome_details_
;
402 class WebstorePrivateLaunchEphemeralAppFunction
403 : public UIThreadExtensionFunction
{
405 DECLARE_EXTENSION_FUNCTION("webstorePrivate.launchEphemeralApp",
406 WEBSTOREPRIVATE_LAUNCHEPHEMERALAPP
)
408 WebstorePrivateLaunchEphemeralAppFunction();
411 ~WebstorePrivateLaunchEphemeralAppFunction() override
;
413 // ExtensionFunction:
414 ExtensionFunction::ResponseAction
Run() override
;
416 void OnLaunchComplete(webstore_install::Result result
,
417 const std::string
& error
);
419 ExtensionFunction::ResponseValue
BuildResponse(
420 api::webstore_private::Result result
,
421 const std::string
& error
);
423 ChromeExtensionFunctionDetails chrome_details_
;
426 class WebstorePrivateGetEphemeralAppsEnabledFunction
427 : public UIThreadExtensionFunction
{
429 DECLARE_EXTENSION_FUNCTION("webstorePrivate.getEphemeralAppsEnabled",
430 WEBSTOREPRIVATE_GETEPHEMERALAPPSENABLED
)
432 WebstorePrivateGetEphemeralAppsEnabledFunction();
435 ~WebstorePrivateGetEphemeralAppsEnabledFunction() override
;
437 // ExtensionFunction:
438 ExtensionFunction::ResponseAction
Run() override
;
441 } // namespace extensions
443 #endif // CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_PRIVATE_WEBSTORE_PRIVATE_API_H_