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 "chrome/browser/extensions/extension_install_prompt_experiment.h"
19 #include "extensions/common/url_pattern.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
21 #include "ui/gfx/image/image.h"
22 #include "ui/gfx/image/image_skia.h"
23 #include "ui/gfx/native_widget_types.h"
25 class ExtensionInstallPromptShowParams
;
29 class DictionaryValue
;
37 namespace extensions
{
38 class BundleInstaller
;
39 class CrxInstallerError
;
41 class ExtensionInstallUI
;
42 class ExtensionWebstorePrivateApiTest
;
43 class MockGetAuthTokenFunction
;
45 } // namespace extensions
48 class InfoBarDelegate
;
51 // Displays all the UI around extension installation.
52 class ExtensionInstallPrompt
53 : public base::SupportsWeakPtr
<ExtensionInstallPrompt
> {
55 // A setting to cause extension/app installs from the webstore skip the normal
56 // confirmation dialog. This should only be used in tests.
57 enum AutoConfirmForTests
{
58 NONE
, // The prompt will show normally.
59 ACCEPT
, // The prompt will always accept.
60 CANCEL
, // The prompt will always cancel.
62 static AutoConfirmForTests g_auto_confirm_for_tests
;
64 // This enum is associated with Extensions.InstallPrompt_Type UMA histogram.
65 // Do not modify existing values and add new values only to the end.
67 UNSET_PROMPT_TYPE
= -1,
69 INLINE_INSTALL_PROMPT
,
70 BUNDLE_INSTALL_PROMPT
,
73 EXTERNAL_INSTALL_PROMPT
,
74 POST_INSTALL_PERMISSIONS_PROMPT
,
76 REMOTE_INSTALL_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
,
125 void SetUserNameFromProfile(Profile
* profile
);
127 PromptType
type() const { return type_
; }
128 void set_type(PromptType type
) { type_
= type
; }
130 // Getters for UI element labels.
131 base::string16
GetDialogTitle() const;
132 base::string16
GetHeading() const;
133 int GetDialogButtons() const;
134 bool HasAcceptButtonLabel() const;
135 base::string16
GetAcceptButtonLabel() const;
136 bool HasAbortButtonLabel() const;
137 base::string16
GetAbortButtonLabel() const;
138 base::string16
GetPermissionsHeading(
139 PermissionsType permissions_type
) const;
140 base::string16
GetRetainedFilesHeading() const;
141 base::string16
GetRetainedDevicesHeading() const;
143 bool ShouldShowPermissions() const;
144 bool ShouldShowExplanationText() const;
146 // Getters for webstore metadata. Only populated when the type is
147 // INLINE_INSTALL_PROMPT.
149 // The star display logic replicates the one used by the webstore (from
150 // components.ratingutils.setFractionalYellowStars). Callers pass in an
151 // "appender", which will be repeatedly called back with the star images
152 // that they append to the star display area.
153 typedef void(*StarAppender
)(const gfx::ImageSkia
*, void*);
154 void AppendRatingStars(StarAppender appender
, void* data
) const;
155 base::string16
GetRatingCount() const;
156 base::string16
GetUserCount() const;
157 size_t GetPermissionCount(PermissionsType permissions_type
) const;
158 size_t GetPermissionsDetailsCount(PermissionsType permissions_type
) const;
159 base::string16
GetPermission(size_t index
,
160 PermissionsType permissions_type
) const;
161 base::string16
GetPermissionsDetails(
163 PermissionsType permissions_type
) const;
164 bool GetIsShowingDetails(DetailsType type
, size_t index
) const;
165 size_t GetRetainedFileCount() const;
166 base::string16
GetRetainedFile(size_t index
) const;
167 size_t GetRetainedDeviceCount() const;
168 base::string16
GetRetainedDeviceMessageString(size_t index
) const;
170 // Populated for BUNDLE_INSTALL_PROMPT.
171 const extensions::BundleInstaller
* bundle() const { return bundle_
; }
172 void set_bundle(const extensions::BundleInstaller
* bundle
) {
176 // Populated for all other types.
177 const extensions::Extension
* extension() const { return extension_
; }
178 void set_extension(const extensions::Extension
* extension
) {
179 extension_
= extension
;
182 // May be populated for POST_INSTALL_PERMISSIONS_PROMPT.
183 void set_retained_files(const std::vector
<base::FilePath
>& retained_files
) {
184 retained_files_
= retained_files
;
186 void set_retained_device_messages(
187 const std::vector
<base::string16
>& retained_device_messages
) {
188 retained_device_messages_
= retained_device_messages
;
191 const gfx::Image
& icon() const { return icon_
; }
192 void set_icon(const gfx::Image
& icon
) { icon_
= icon
; }
194 bool has_webstore_data() const { return has_webstore_data_
; }
196 const ExtensionInstallPromptExperiment
* experiment() const {
197 return experiment_
.get();
199 void set_experiment(ExtensionInstallPromptExperiment
* experiment
) {
200 experiment_
= experiment
;
204 friend class base::RefCountedThreadSafe
<Prompt
>;
206 struct InstallPromptPermissions
{
207 InstallPromptPermissions();
208 ~InstallPromptPermissions();
210 std::vector
<base::string16
> permissions
;
211 std::vector
<base::string16
> details
;
212 std::vector
<bool> is_showing_details
;
217 bool ShouldDisplayRevokeButton() const;
219 // Returns the InstallPromptPermissions corresponding to
220 // |permissions_type|.
221 InstallPromptPermissions
& GetPermissionsForType(
222 PermissionsType permissions_type
);
223 const InstallPromptPermissions
& GetPermissionsForType(
224 PermissionsType permissions_type
) const;
226 bool ShouldDisplayRevokeFilesButton() const;
230 // Permissions that are being requested (may not be all of an extension's
231 // permissions if only additional ones are being requested)
232 InstallPromptPermissions prompt_permissions_
;
233 // Permissions that will be withheld upon install.
234 InstallPromptPermissions withheld_prompt_permissions_
;
236 bool is_showing_details_for_retained_files_
;
237 bool is_showing_details_for_retained_devices_
;
239 // The extension or bundle being installed.
240 const extensions::Extension
* extension_
;
241 const extensions::BundleInstaller
* bundle_
;
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 scoped_refptr
<ExtensionInstallPromptExperiment
> experiment_
;
267 DISALLOW_COPY_AND_ASSIGN(Prompt
);
270 static const int kMinExtensionRating
= 0;
271 static const int kMaxExtensionRating
= 5;
275 // We call this method to signal that the installation should continue.
276 virtual void InstallUIProceed() = 0;
278 // We call this method to signal that the installation should stop, with
279 // |user_initiated| true if the installation was stopped by the user.
280 virtual void InstallUIAbort(bool user_initiated
) = 0;
283 virtual ~Delegate() {}
286 typedef base::Callback
<void(ExtensionInstallPromptShowParams
*,
287 ExtensionInstallPrompt::Delegate
*,
288 scoped_refptr
<ExtensionInstallPrompt::Prompt
>)>
291 // Callback to show the default extension install dialog.
292 // The implementations of this function are platform-specific.
293 static ShowDialogCallback
GetDefaultShowDialogCallback();
295 // Creates a dummy extension from the |manifest|, replacing the name and
296 // description with the localizations if provided.
297 static scoped_refptr
<extensions::Extension
> GetLocalizedExtensionForDisplay(
298 const base::DictionaryValue
* manifest
,
299 int flags
, // Extension::InitFromValueFlags
300 const std::string
& id
,
301 const std::string
& localized_name
,
302 const std::string
& localized_description
,
305 // Creates a prompt with a parent web content.
306 explicit ExtensionInstallPrompt(content::WebContents
* contents
);
308 // Creates a prompt with a profile and a native window. The most recently
309 // active browser window (or a new browser window if there are no browser
310 // windows) is used if a new tab needs to be opened.
311 ExtensionInstallPrompt(Profile
* profile
, gfx::NativeWindow native_window
);
313 virtual ~ExtensionInstallPrompt();
315 extensions::ExtensionInstallUI
* install_ui() const {
316 return install_ui_
.get();
319 // This is called by the bundle installer to verify whether the bundle
320 // should be installed.
322 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
323 virtual void ConfirmBundleInstall(
324 extensions::BundleInstaller
* bundle
,
325 const extensions::PermissionSet
* permissions
);
327 // This is called by the standalone installer to verify whether the install
328 // from the webstore should proceed.
330 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
331 virtual void ConfirmStandaloneInstall(Delegate
* delegate
,
332 const extensions::Extension
* extension
,
334 scoped_refptr
<Prompt
> prompt
);
336 // This is called by the installer to verify whether the installation from
337 // the webstore should proceed. |show_dialog_callback| is optional and can be
340 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
341 virtual void ConfirmWebstoreInstall(
343 const extensions::Extension
* extension
,
344 const SkBitmap
* icon
,
345 const ShowDialogCallback
& show_dialog_callback
);
347 // This is called by the installer to verify whether the installation should
348 // proceed. This is declared virtual for testing. |show_dialog_callback| is
349 // optional and can be NULL.
351 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
352 virtual void ConfirmInstall(Delegate
* delegate
,
353 const extensions::Extension
* extension
,
354 const ShowDialogCallback
& show_dialog_callback
);
356 // This is called by the app handler launcher to verify whether the app
357 // should be re-enabled. This is declared virtual for testing.
359 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
360 virtual void ConfirmReEnable(Delegate
* delegate
,
361 const extensions::Extension
* extension
);
363 // This is called by the external install alert UI to verify whether the
364 // extension should be enabled (external extensions are installed disabled).
366 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
367 virtual void ConfirmExternalInstall(
369 const extensions::Extension
* extension
,
370 const ShowDialogCallback
& show_dialog_callback
,
371 scoped_refptr
<Prompt
> prompt
);
373 // This is called by the extension permissions API to verify whether an
374 // extension may be granted additional permissions.
376 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
377 virtual void ConfirmPermissions(Delegate
* delegate
,
378 const extensions::Extension
* extension
,
379 const extensions::PermissionSet
* permissions
);
381 // This is called by the app handler launcher to review what permissions the
382 // extension or app currently has.
384 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
385 virtual void ReviewPermissions(
387 const extensions::Extension
* extension
,
388 const std::vector
<base::FilePath
>& retained_file_paths
,
389 const std::vector
<base::string16
>& retained_device_messages
);
391 // Installation was successful. This is declared virtual for testing.
392 virtual void OnInstallSuccess(const extensions::Extension
* extension
,
395 // Installation failed. This is declared virtual for testing.
396 virtual void OnInstallFailure(const extensions::CrxInstallerError
& error
);
398 void set_callback_for_test(const ShowDialogCallback
& show_dialog_callback
) {
399 show_dialog_callback_
= show_dialog_callback
;
403 friend class extensions::ExtensionWebstorePrivateApiTest
;
404 friend class WebstoreStartupInstallUnpackFailureTest
;
406 // Whether or not we should record the oauth2 grant upon successful install.
407 bool record_oauth2_grant_
;
410 friend class GalleryInstallApiTestObserver
;
412 // Sets the icon that will be used in any UI. If |icon| is NULL, or contains
413 // an empty bitmap, then a default icon will be used instead.
414 void SetIcon(const SkBitmap
* icon
);
416 // ImageLoader callback.
417 void OnImageLoaded(const gfx::Image
& image
);
419 // Starts the process of showing a confirmation UI, which is split into two.
420 // 1) Set off a 'load icon' task.
421 // 2) Handle the load icon response and show the UI (OnImageLoaded).
422 void LoadImageIfNeeded();
424 // Shows the actual UI (the icon should already be loaded).
425 void ShowConfirmation();
429 base::MessageLoop
* ui_loop_
;
431 // The extensions installation icon.
434 // The extension we are showing the UI for, if type is not
435 // BUNDLE_INSTALL_PROMPT.
436 const extensions::Extension
* extension_
;
438 // The bundle we are showing the UI for, if type BUNDLE_INSTALL_PROMPT.
439 const extensions::BundleInstaller
* bundle_
;
441 // A custom set of permissions to show in the install prompt instead of the
442 // extension's active permissions.
443 scoped_refptr
<const extensions::PermissionSet
> custom_permissions_
;
445 // The object responsible for doing the UI specific actions.
446 scoped_ptr
<extensions::ExtensionInstallUI
> install_ui_
;
448 // Parameters to show the confirmation UI.
449 scoped_ptr
<ExtensionInstallPromptShowParams
> show_params_
;
451 // The delegate we will call Proceed/Abort on after confirmation UI.
454 // A pre-filled prompt.
455 scoped_refptr
<Prompt
> prompt_
;
457 // Used to show the confirm dialog.
458 ShowDialogCallback show_dialog_callback_
;
461 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_PROMPT_H_