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/permissions/coalesced_permission_message.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 CrxInstallError
;
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 // This enum is associated with Extensions.InstallPrompt_Type UMA histogram.
56 // Do not modify existing values and add new values only to the end.
58 UNSET_PROMPT_TYPE
= -1,
60 INLINE_INSTALL_PROMPT
,
61 BUNDLE_INSTALL_PROMPT
,
64 EXTERNAL_INSTALL_PROMPT
,
65 POST_INSTALL_PERMISSIONS_PROMPT
,
67 REMOTE_INSTALL_PROMPT
,
69 DELEGATED_PERMISSIONS_PROMPT
,
70 DELEGATED_BUNDLE_PERMISSIONS_PROMPT
,
74 // The last prompt type to display; only used for testing.
75 static PromptType g_last_prompt_type_for_tests
;
77 // Enumeration for permissions and retained files details.
79 PERMISSIONS_DETAILS
= 0,
80 WITHHELD_PERMISSIONS_DETAILS
,
81 RETAINED_FILES_DETAILS
,
82 RETAINED_DEVICES_DETAILS
,
85 // This enum is used to differentiate regular and withheld permissions for
86 // segregation in the install prompt.
87 enum PermissionsType
{
88 REGULAR_PERMISSIONS
= 0,
93 static std::string
PromptTypeToString(PromptType type
);
95 // Extra information needed to display an installation or uninstallation
96 // prompt. Gets populated with raw data and exposes getters for formatted
97 // strings so that the GTK/views/Cocoa install dialogs don't have to repeat
99 // Ref-counted because we pass around the prompt independent of the full
100 // ExtensionInstallPrompt.
101 class Prompt
: public base::RefCountedThreadSafe
<Prompt
> {
103 explicit Prompt(PromptType type
);
106 const extensions::CoalescedPermissionMessages
& permissions
,
107 PermissionsType permissions_type
);
108 void SetIsShowingDetails(DetailsType type
,
110 bool is_showing_details
);
111 void SetWebstoreData(const std::string
& localized_user_count
,
112 bool show_user_count
,
113 double average_rating
,
116 PromptType
type() const { return type_
; }
117 void set_type(PromptType type
) { type_
= type
; }
119 // Getters for UI element labels.
120 base::string16
GetDialogTitle() const;
121 int GetDialogButtons() const;
122 // Returns the empty string when there should be no "accept" button.
123 base::string16
GetAcceptButtonLabel() const;
124 base::string16
GetAbortButtonLabel() const;
125 base::string16
GetPermissionsHeading(
126 PermissionsType permissions_type
) const;
127 base::string16
GetRetainedFilesHeading() const;
128 base::string16
GetRetainedDevicesHeading() const;
130 bool ShouldShowPermissions() const;
132 // Getters for webstore metadata. Only populated when the type is
133 // INLINE_INSTALL_PROMPT, EXTERNAL_INSTALL_PROMPT, or REPAIR_PROMPT.
135 // The star display logic replicates the one used by the webstore (from
136 // components.ratingutils.setFractionalYellowStars). Callers pass in an
137 // "appender", which will be repeatedly called back with the star images
138 // that they append to the star display area.
139 typedef void(*StarAppender
)(const gfx::ImageSkia
*, void*);
140 void AppendRatingStars(StarAppender appender
, void* data
) const;
141 base::string16
GetRatingCount() const;
142 base::string16
GetUserCount() const;
143 size_t GetPermissionCount(PermissionsType permissions_type
) const;
144 size_t GetPermissionsDetailsCount(PermissionsType permissions_type
) const;
145 base::string16
GetPermission(size_t index
,
146 PermissionsType permissions_type
) const;
147 base::string16
GetPermissionsDetails(
149 PermissionsType permissions_type
) const;
150 bool GetIsShowingDetails(DetailsType type
, size_t index
) const;
151 size_t GetRetainedFileCount() const;
152 base::string16
GetRetainedFile(size_t index
) const;
153 size_t GetRetainedDeviceCount() const;
154 base::string16
GetRetainedDeviceMessageString(size_t index
) const;
156 // Populated for BUNDLE_INSTALL_PROMPT and
157 // DELEGATED_BUNDLE_PERMISSIONS_PROMPT.
158 const extensions::BundleInstaller
* bundle() const { return bundle_
; }
159 void set_bundle(const extensions::BundleInstaller
* bundle
) {
163 // Populated for all other types.
164 const extensions::Extension
* extension() const { return extension_
; }
165 void set_extension(const extensions::Extension
* extension
) {
166 extension_
= extension
;
169 // May be populated for POST_INSTALL_PERMISSIONS_PROMPT.
170 void set_retained_files(const std::vector
<base::FilePath
>& retained_files
) {
171 retained_files_
= retained_files
;
173 void set_retained_device_messages(
174 const std::vector
<base::string16
>& retained_device_messages
) {
175 retained_device_messages_
= retained_device_messages
;
178 const std::string
& delegated_username() const {
179 return delegated_username_
;
181 void set_delegated_username(const std::string
& delegated_username
) {
182 delegated_username_
= delegated_username
;
185 const gfx::Image
& icon() const { return icon_
; }
186 void set_icon(const gfx::Image
& icon
) { icon_
= icon
; }
188 bool has_webstore_data() const { return has_webstore_data_
; }
191 friend class base::RefCountedThreadSafe
<Prompt
>;
193 struct InstallPromptPermissions
{
194 InstallPromptPermissions();
195 ~InstallPromptPermissions();
197 std::vector
<base::string16
> permissions
;
198 std::vector
<base::string16
> details
;
199 std::vector
<bool> is_showing_details
;
204 bool ShouldDisplayRevokeButton() const;
206 // Returns the InstallPromptPermissions corresponding to
207 // |permissions_type|.
208 InstallPromptPermissions
& GetPermissionsForType(
209 PermissionsType permissions_type
);
210 const InstallPromptPermissions
& GetPermissionsForType(
211 PermissionsType permissions_type
) const;
213 bool ShouldDisplayRevokeFilesButton() const;
217 // Permissions that are being requested (may not be all of an extension's
218 // permissions if only additional ones are being requested)
219 InstallPromptPermissions prompt_permissions_
;
220 // Permissions that will be withheld upon install.
221 InstallPromptPermissions withheld_prompt_permissions_
;
223 bool is_showing_details_for_retained_files_
;
224 bool is_showing_details_for_retained_devices_
;
226 // The extension or bundle being installed.
227 const extensions::Extension
* extension_
;
228 const extensions::BundleInstaller
* bundle_
;
230 std::string delegated_username_
;
232 // The icon to be displayed.
235 // These fields are populated only when the prompt type is
236 // INLINE_INSTALL_PROMPT
237 // Already formatted to be locale-specific.
238 std::string localized_user_count_
;
239 // Range is kMinExtensionRating to kMaxExtensionRating
240 double average_rating_
;
243 // Whether we should display the user count (we anticipate this will be
244 // false if localized_user_count_ represents the number zero).
245 bool show_user_count_
;
247 // Whether or not this prompt has been populated with data from the
249 bool has_webstore_data_
;
251 std::vector
<base::FilePath
> retained_files_
;
252 std::vector
<base::string16
> retained_device_messages_
;
254 DISALLOW_COPY_AND_ASSIGN(Prompt
);
257 static const int kMinExtensionRating
= 0;
258 static const int kMaxExtensionRating
= 5;
262 // We call this method to signal that the installation should continue.
263 virtual void InstallUIProceed() = 0;
265 // We call this method to signal that the installation should stop, with
266 // |user_initiated| true if the installation was stopped by the user.
267 virtual void InstallUIAbort(bool user_initiated
) = 0;
270 virtual ~Delegate() {}
273 typedef base::Callback
<void(ExtensionInstallPromptShowParams
*,
274 ExtensionInstallPrompt::Delegate
*,
275 scoped_refptr
<ExtensionInstallPrompt::Prompt
>)>
278 // Callback to show the default extension install dialog.
279 // The implementations of this function are platform-specific.
280 static ShowDialogCallback
GetDefaultShowDialogCallback();
282 // Creates a dummy extension from the |manifest|, replacing the name and
283 // description with the localizations if provided.
284 static scoped_refptr
<extensions::Extension
> GetLocalizedExtensionForDisplay(
285 const base::DictionaryValue
* manifest
,
286 int flags
, // Extension::InitFromValueFlags
287 const std::string
& id
,
288 const std::string
& localized_name
,
289 const std::string
& localized_description
,
292 // Creates a prompt with a parent web content.
293 explicit ExtensionInstallPrompt(content::WebContents
* contents
);
295 // Creates a prompt with a profile and a native window. The most recently
296 // active browser window (or a new browser window if there are no browser
297 // windows) is used if a new tab needs to be opened.
298 ExtensionInstallPrompt(Profile
* profile
, gfx::NativeWindow native_window
);
300 virtual ~ExtensionInstallPrompt();
302 extensions::ExtensionInstallUI
* install_ui() const {
303 return install_ui_
.get();
306 // This is called by the bundle installer to verify whether the bundle
307 // should be installed.
309 // We *MUST* eventually call either Proceed() or Abort() on |bundle|.
310 virtual void ConfirmBundleInstall(
311 extensions::BundleInstaller
* bundle
,
312 const SkBitmap
* icon
,
313 const extensions::PermissionSet
* permissions
);
315 // This is called by the bundle installer to verify the permissions for a
316 // delegated bundle install.
318 // We *MUST* eventually call either Proceed() or Abort() on |bundle|.
319 virtual void ConfirmPermissionsForDelegatedBundleInstall(
320 extensions::BundleInstaller
* bundle
,
321 const std::string
& delegated_username
,
322 const SkBitmap
* icon
,
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 or DELEGATED_BUNDLE_PERMISSIONS_PROMPT.
444 const extensions::Extension
* extension_
;
446 // The bundle we are showing the UI for, if type BUNDLE_INSTALL_PROMPT or
447 // DELEGATED_BUNDLE_PERMISSIONS_PROMPT.
448 const extensions::BundleInstaller
* bundle_
;
450 // The name of the user we are asking about, if type
451 // DELEGATED_PERMISSIONS_PROMPT or DELEGATED_BUNDLE_PERMISSIONS_PROMPT.
452 std::string delegated_username_
;
454 // A custom set of permissions to show in the install prompt instead of the
455 // extension's active permissions.
456 scoped_refptr
<const extensions::PermissionSet
> custom_permissions_
;
458 // The object responsible for doing the UI specific actions.
459 scoped_ptr
<extensions::ExtensionInstallUI
> install_ui_
;
461 // Parameters to show the confirmation UI.
462 scoped_ptr
<ExtensionInstallPromptShowParams
> show_params_
;
464 // The delegate we will call Proceed/Abort on after confirmation UI.
467 // A pre-filled prompt.
468 scoped_refptr
<Prompt
> prompt_
;
470 // Used to show the confirm dialog.
471 ShowDialogCallback show_dialog_callback_
;
474 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_PROMPT_H_