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_EXTENSION_INSTALL_PROMPT_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_PROMPT_H_
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/strings/string16.h"
18 #include "extensions/common/url_pattern.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/image/image_skia.h"
22 #include "ui/gfx/native_widget_types.h"
24 class ExtensionInstallPromptShowParams
;
28 class DictionaryValue
;
36 namespace extensions
{
37 class BundleInstaller
;
38 class CrxInstallError
;
40 class ExtensionInstallUI
;
41 class ExtensionWebstorePrivateApiTest
;
42 class MockGetAuthTokenFunction
;
44 } // namespace extensions
47 class InfoBarDelegate
;
50 // Displays all the UI around extension installation.
51 class ExtensionInstallPrompt
52 : public base::SupportsWeakPtr
<ExtensionInstallPrompt
> {
54 // A setting to cause extension/app installs from the webstore skip the normal
55 // confirmation dialog. This should only be used in tests.
56 enum AutoConfirmForTests
{
57 NONE
, // The prompt will show normally.
58 ACCEPT
, // The prompt will always accept.
59 CANCEL
, // The prompt will always cancel.
61 static AutoConfirmForTests g_auto_confirm_for_tests
;
63 // This enum is associated with Extensions.InstallPrompt_Type UMA histogram.
64 // Do not modify existing values and add new values only to the end.
66 UNSET_PROMPT_TYPE
= -1,
68 INLINE_INSTALL_PROMPT
,
69 BUNDLE_INSTALL_PROMPT
,
72 EXTERNAL_INSTALL_PROMPT
,
73 POST_INSTALL_PERMISSIONS_PROMPT
,
75 REMOTE_INSTALL_PROMPT
,
77 DELEGATED_PERMISSIONS_PROMPT
,
81 // The last prompt type to display; only used for testing.
82 static PromptType g_last_prompt_type_for_tests
;
84 // Enumeration for permissions and retained files details.
86 PERMISSIONS_DETAILS
= 0,
87 WITHHELD_PERMISSIONS_DETAILS
,
88 RETAINED_FILES_DETAILS
,
89 RETAINED_DEVICES_DETAILS
,
92 // This enum is used to differentiate regular and withheld permissions for
93 // segregation in the install prompt.
94 enum PermissionsType
{
95 REGULAR_PERMISSIONS
= 0,
100 static std::string
PromptTypeToString(PromptType type
);
102 // Extra information needed to display an installation or uninstallation
103 // prompt. Gets populated with raw data and exposes getters for formatted
104 // strings so that the GTK/views/Cocoa install dialogs don't have to repeat
106 // Ref-counted because we pass around the prompt independent of the full
107 // ExtensionInstallPrompt.
108 class Prompt
: public base::RefCountedThreadSafe
<Prompt
> {
110 explicit Prompt(PromptType type
);
112 // Sets the permission list for this prompt.
113 void SetPermissions(const std::vector
<base::string16
>& permissions
,
114 PermissionsType permissions_type
);
115 // Sets the permission list details for this prompt.
116 void SetPermissionsDetails(const std::vector
<base::string16
>& details
,
117 PermissionsType permissions_type
);
118 void SetIsShowingDetails(DetailsType type
,
120 bool is_showing_details
);
121 void SetWebstoreData(const std::string
& localized_user_count
,
122 bool show_user_count
,
123 double average_rating
,
126 PromptType
type() const { return type_
; }
127 void set_type(PromptType type
) { type_
= type
; }
129 // Getters for UI element labels.
130 base::string16
GetDialogTitle() const;
131 base::string16
GetHeading() const;
132 int GetDialogButtons() const;
133 bool HasAcceptButtonLabel() const;
134 base::string16
GetAcceptButtonLabel() const;
135 bool HasAbortButtonLabel() const;
136 base::string16
GetAbortButtonLabel() const;
137 base::string16
GetPermissionsHeading(
138 PermissionsType permissions_type
) const;
139 base::string16
GetRetainedFilesHeading() const;
140 base::string16
GetRetainedDevicesHeading() const;
142 bool ShouldShowPermissions() const;
144 // Getters for webstore metadata. Only populated when the type is
145 // INLINE_INSTALL_PROMPT.
147 // The star display logic replicates the one used by the webstore (from
148 // components.ratingutils.setFractionalYellowStars). Callers pass in an
149 // "appender", which will be repeatedly called back with the star images
150 // that they append to the star display area.
151 typedef void(*StarAppender
)(const gfx::ImageSkia
*, void*);
152 void AppendRatingStars(StarAppender appender
, void* data
) const;
153 base::string16
GetRatingCount() const;
154 base::string16
GetUserCount() const;
155 size_t GetPermissionCount(PermissionsType permissions_type
) const;
156 size_t GetPermissionsDetailsCount(PermissionsType permissions_type
) const;
157 base::string16
GetPermission(size_t index
,
158 PermissionsType permissions_type
) const;
159 base::string16
GetPermissionsDetails(
161 PermissionsType permissions_type
) const;
162 bool GetIsShowingDetails(DetailsType type
, size_t index
) const;
163 size_t GetRetainedFileCount() const;
164 base::string16
GetRetainedFile(size_t index
) const;
165 size_t GetRetainedDeviceCount() const;
166 base::string16
GetRetainedDeviceMessageString(size_t index
) const;
168 // Populated for BUNDLE_INSTALL_PROMPT.
169 const extensions::BundleInstaller
* bundle() const { return bundle_
; }
170 void set_bundle(const extensions::BundleInstaller
* bundle
) {
174 // Populated for all other types.
175 const extensions::Extension
* extension() const { return extension_
; }
176 void set_extension(const extensions::Extension
* extension
) {
177 extension_
= extension
;
180 // May be populated for POST_INSTALL_PERMISSIONS_PROMPT.
181 void set_retained_files(const std::vector
<base::FilePath
>& retained_files
) {
182 retained_files_
= retained_files
;
184 void set_retained_device_messages(
185 const std::vector
<base::string16
>& retained_device_messages
) {
186 retained_device_messages_
= retained_device_messages
;
189 const std::string
& delegated_username() const {
190 return delegated_username_
;
192 void set_delegated_username(const std::string
& delegated_username
) {
193 delegated_username_
= delegated_username
;
196 const gfx::Image
& icon() const { return icon_
; }
197 void set_icon(const gfx::Image
& icon
) { icon_
= icon
; }
199 bool has_webstore_data() const { return has_webstore_data_
; }
202 friend class base::RefCountedThreadSafe
<Prompt
>;
204 struct InstallPromptPermissions
{
205 InstallPromptPermissions();
206 ~InstallPromptPermissions();
208 std::vector
<base::string16
> permissions
;
209 std::vector
<base::string16
> details
;
210 std::vector
<bool> is_showing_details
;
215 bool ShouldDisplayRevokeButton() const;
217 // Returns the InstallPromptPermissions corresponding to
218 // |permissions_type|.
219 InstallPromptPermissions
& GetPermissionsForType(
220 PermissionsType permissions_type
);
221 const InstallPromptPermissions
& GetPermissionsForType(
222 PermissionsType permissions_type
) const;
224 bool ShouldDisplayRevokeFilesButton() const;
228 // Permissions that are being requested (may not be all of an extension's
229 // permissions if only additional ones are being requested)
230 InstallPromptPermissions prompt_permissions_
;
231 // Permissions that will be withheld upon install.
232 InstallPromptPermissions withheld_prompt_permissions_
;
234 bool is_showing_details_for_retained_files_
;
235 bool is_showing_details_for_retained_devices_
;
237 // The extension or bundle being installed.
238 const extensions::Extension
* extension_
;
239 const extensions::BundleInstaller
* bundle_
;
241 std::string delegated_username_
;
243 // The icon to be displayed.
246 // These fields are populated only when the prompt type is
247 // INLINE_INSTALL_PROMPT
248 // Already formatted to be locale-specific.
249 std::string localized_user_count_
;
250 // Range is kMinExtensionRating to kMaxExtensionRating
251 double average_rating_
;
254 // Whether we should display the user count (we anticipate this will be
255 // false if localized_user_count_ represents the number zero).
256 bool show_user_count_
;
258 // Whether or not this prompt has been populated with data from the
260 bool has_webstore_data_
;
262 std::vector
<base::FilePath
> retained_files_
;
263 std::vector
<base::string16
> retained_device_messages_
;
265 DISALLOW_COPY_AND_ASSIGN(Prompt
);
268 static const int kMinExtensionRating
= 0;
269 static const int kMaxExtensionRating
= 5;
273 // We call this method to signal that the installation should continue.
274 virtual void InstallUIProceed() = 0;
276 // We call this method to signal that the installation should stop, with
277 // |user_initiated| true if the installation was stopped by the user.
278 virtual void InstallUIAbort(bool user_initiated
) = 0;
281 virtual ~Delegate() {}
284 typedef base::Callback
<void(ExtensionInstallPromptShowParams
*,
285 ExtensionInstallPrompt::Delegate
*,
286 scoped_refptr
<ExtensionInstallPrompt::Prompt
>)>
289 // Callback to show the default extension install dialog.
290 // The implementations of this function are platform-specific.
291 static ShowDialogCallback
GetDefaultShowDialogCallback();
293 // Creates a dummy extension from the |manifest|, replacing the name and
294 // description with the localizations if provided.
295 static scoped_refptr
<extensions::Extension
> GetLocalizedExtensionForDisplay(
296 const base::DictionaryValue
* manifest
,
297 int flags
, // Extension::InitFromValueFlags
298 const std::string
& id
,
299 const std::string
& localized_name
,
300 const std::string
& localized_description
,
303 // Creates a prompt with a parent web content.
304 explicit ExtensionInstallPrompt(content::WebContents
* contents
);
306 // Creates a prompt with a profile and a native window. The most recently
307 // active browser window (or a new browser window if there are no browser
308 // windows) is used if a new tab needs to be opened.
309 ExtensionInstallPrompt(Profile
* profile
, gfx::NativeWindow native_window
);
311 virtual ~ExtensionInstallPrompt();
313 extensions::ExtensionInstallUI
* install_ui() const {
314 return install_ui_
.get();
317 // This is called by the bundle installer to verify whether the bundle
318 // should be installed.
320 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
321 virtual void ConfirmBundleInstall(
322 extensions::BundleInstaller
* bundle
,
323 const extensions::PermissionSet
* permissions
);
325 // This is called by the standalone installer to verify whether the install
326 // from the webstore should proceed.
328 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
329 virtual void ConfirmStandaloneInstall(Delegate
* delegate
,
330 const extensions::Extension
* extension
,
332 scoped_refptr
<Prompt
> prompt
);
334 // This is called by the installer to verify whether the installation from
335 // the webstore should proceed. |show_dialog_callback| is optional and can be
338 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
339 virtual void ConfirmWebstoreInstall(
341 const extensions::Extension
* extension
,
342 const SkBitmap
* icon
,
343 const ShowDialogCallback
& show_dialog_callback
);
345 // This is called by the installer to verify whether the installation should
346 // proceed. This is declared virtual for testing. |show_dialog_callback| is
347 // optional and can be NULL.
349 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
350 virtual void ConfirmInstall(Delegate
* delegate
,
351 const extensions::Extension
* extension
,
352 const ShowDialogCallback
& show_dialog_callback
);
354 // This is called by the webstore API to verify the permissions for a
355 // delegated install.
357 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
358 virtual void ConfirmPermissionsForDelegatedInstall(
360 const extensions::Extension
* extension
,
361 const std::string
& delegated_username
,
362 const SkBitmap
* icon
);
364 // This is called by the app handler launcher to verify whether the app
365 // should be re-enabled. This is declared virtual for testing.
367 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
368 virtual void ConfirmReEnable(Delegate
* delegate
,
369 const extensions::Extension
* extension
);
371 // This is called by the external install alert UI to verify whether the
372 // extension should be enabled (external extensions are installed disabled).
374 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
375 virtual void ConfirmExternalInstall(
377 const extensions::Extension
* extension
,
378 const ShowDialogCallback
& show_dialog_callback
,
379 scoped_refptr
<Prompt
> prompt
);
381 // This is called by the extension permissions API to verify whether an
382 // extension may be granted additional permissions.
384 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
385 virtual void ConfirmPermissions(Delegate
* delegate
,
386 const extensions::Extension
* extension
,
387 const extensions::PermissionSet
* permissions
);
389 // This is called by the app handler launcher to review what permissions the
390 // extension or app currently has.
392 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
393 virtual void ReviewPermissions(
395 const extensions::Extension
* extension
,
396 const std::vector
<base::FilePath
>& retained_file_paths
,
397 const std::vector
<base::string16
>& retained_device_messages
);
399 // Installation was successful. This is declared virtual for testing.
400 virtual void OnInstallSuccess(const extensions::Extension
* extension
,
403 // Installation failed. This is declared virtual for testing.
404 virtual void OnInstallFailure(const extensions::CrxInstallError
& error
);
406 void set_callback_for_test(const ShowDialogCallback
& show_dialog_callback
) {
407 show_dialog_callback_
= show_dialog_callback
;
411 friend class extensions::ExtensionWebstorePrivateApiTest
;
412 friend class WebstoreStartupInstallUnpackFailureTest
;
414 // Whether or not we should record the oauth2 grant upon successful install.
415 bool record_oauth2_grant_
;
418 friend class GalleryInstallApiTestObserver
;
420 // Sets the icon that will be used in any UI. If |icon| is NULL, or contains
421 // an empty bitmap, then a default icon will be used instead.
422 void SetIcon(const SkBitmap
* icon
);
424 // ImageLoader callback.
425 void OnImageLoaded(const gfx::Image
& image
);
427 // Starts the process of showing a confirmation UI, which is split into two.
428 // 1) Set off a 'load icon' task.
429 // 2) Handle the load icon response and show the UI (OnImageLoaded).
430 void LoadImageIfNeeded();
432 // Shows the actual UI (the icon should already be loaded).
433 void ShowConfirmation();
437 base::MessageLoop
* ui_loop_
;
439 // The extensions installation icon.
442 // The extension we are showing the UI for, if type is not
443 // BUNDLE_INSTALL_PROMPT.
444 const extensions::Extension
* extension_
;
446 // The bundle we are showing the UI for, if type BUNDLE_INSTALL_PROMPT.
447 const extensions::BundleInstaller
* bundle_
;
449 // The name of the user we are asking about, if type
450 // DELEGATED_PERMISSIONS_PROMPT.
451 std::string delegated_username_
;
453 // A custom set of permissions to show in the install prompt instead of the
454 // extension's active permissions.
455 scoped_refptr
<const extensions::PermissionSet
> custom_permissions_
;
457 // The object responsible for doing the UI specific actions.
458 scoped_ptr
<extensions::ExtensionInstallUI
> install_ui_
;
460 // Parameters to show the confirmation UI.
461 scoped_ptr
<ExtensionInstallPromptShowParams
> show_params_
;
463 // The delegate we will call Proceed/Abort on after confirmation UI.
466 // A pre-filled prompt.
467 scoped_refptr
<Prompt
> prompt_
;
469 // Used to show the confirm dialog.
470 ShowDialogCallback show_dialog_callback_
;
473 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_PROMPT_H_