Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_install_prompt.h
blob98e0b2573814c0f95746793fe4d944678731ad0a
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_
8 #include <string>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string16.h"
16 #include "chrome/browser/extensions/crx_installer_error.h"
17 #include "chrome/browser/extensions/extension_install_prompt_experiment.h"
18 #include "extensions/common/url_pattern.h"
19 #include "google_apis/gaia/oauth2_mint_token_flow.h"
20 #include "google_apis/gaia/oauth2_token_service.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "ui/gfx/image/image.h"
23 #include "ui/gfx/image/image_skia.h"
24 #include "ui/gfx/native_widget_types.h"
26 class Browser;
27 class ExtensionInstallUI;
28 class InfoBarDelegate;
29 class Profile;
31 namespace base {
32 class DictionaryValue;
33 class MessageLoop;
34 } // namespace base
36 namespace content {
37 class PageNavigator;
38 class WebContents;
41 namespace extensions {
42 class BundleInstaller;
43 class Extension;
44 class ExtensionWebstorePrivateApiTest;
45 class MockGetAuthTokenFunction;
46 class PermissionSet;
47 } // namespace extensions
49 // Displays all the UI around extension installation.
50 class ExtensionInstallPrompt
51 : public OAuth2MintTokenFlow::Delegate,
52 public OAuth2TokenService::Consumer,
53 public base::SupportsWeakPtr<ExtensionInstallPrompt> {
54 public:
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.
57 enum PromptType {
58 UNSET_PROMPT_TYPE = -1,
59 INSTALL_PROMPT = 0,
60 INLINE_INSTALL_PROMPT,
61 BUNDLE_INSTALL_PROMPT,
62 RE_ENABLE_PROMPT,
63 PERMISSIONS_PROMPT,
64 EXTERNAL_INSTALL_PROMPT,
65 POST_INSTALL_PERMISSIONS_PROMPT,
66 LAUNCH_PROMPT,
67 NUM_PROMPT_TYPES
70 enum DetailsType {
71 PERMISSIONS_DETAILS = 0,
72 OAUTH_DETAILS,
73 RETAINED_FILES_DETAILS,
76 // Extra information needed to display an installation or uninstallation
77 // prompt. Gets populated with raw data and exposes getters for formatted
78 // strings so that the GTK/views/Cocoa install dialogs don't have to repeat
79 // that logic.
80 class Prompt {
81 public:
82 explicit Prompt(PromptType type);
83 ~Prompt();
85 // Sets the permission list for this prompt.
86 void SetPermissions(const std::vector<base::string16>& permissions);
87 // Sets the permission list details for this prompt.
88 void SetPermissionsDetails(const std::vector<base::string16>& details);
89 void SetIsShowingDetails(DetailsType type,
90 size_t index,
91 bool is_showing_details);
92 void SetInlineInstallWebstoreData(const std::string& localized_user_count,
93 bool show_user_count,
94 double average_rating,
95 int rating_count);
96 void SetOAuthIssueAdvice(const IssueAdviceInfo& issue_advice);
97 void SetUserNameFromProfile(Profile* profile);
99 PromptType type() const { return type_; }
100 void set_type(PromptType type) { type_ = type; }
102 // Getters for UI element labels.
103 base::string16 GetDialogTitle() const;
104 base::string16 GetHeading() const;
105 int GetDialogButtons() const;
106 bool HasAcceptButtonLabel() const;
107 base::string16 GetAcceptButtonLabel() const;
108 bool HasAbortButtonLabel() const;
109 base::string16 GetAbortButtonLabel() const;
110 base::string16 GetPermissionsHeading() const;
111 base::string16 GetOAuthHeading() const;
112 base::string16 GetRetainedFilesHeading() const;
114 bool ShouldShowPermissions() const;
115 bool ShouldShowExplanationText() const;
117 // Getters for webstore metadata. Only populated when the type is
118 // INLINE_INSTALL_PROMPT.
120 // The star display logic replicates the one used by the webstore (from
121 // components.ratingutils.setFractionalYellowStars). Callers pass in an
122 // "appender", which will be repeatedly called back with the star images
123 // that they append to the star display area.
124 typedef void(*StarAppender)(const gfx::ImageSkia*, void*);
125 void AppendRatingStars(StarAppender appender, void* data) const;
126 base::string16 GetRatingCount() const;
127 base::string16 GetUserCount() const;
128 size_t GetPermissionCount() const;
129 size_t GetPermissionsDetailsCount() const;
130 base::string16 GetPermission(size_t index) const;
131 base::string16 GetPermissionsDetails(size_t index) const;
132 bool GetIsShowingDetails(DetailsType type, size_t index) const;
133 size_t GetOAuthIssueCount() const;
134 const IssueAdviceInfoEntry& GetOAuthIssue(size_t index) const;
135 size_t GetRetainedFileCount() const;
136 base::string16 GetRetainedFile(size_t index) const;
138 // Populated for BUNDLE_INSTALL_PROMPT.
139 const extensions::BundleInstaller* bundle() const { return bundle_; }
140 void set_bundle(const extensions::BundleInstaller* bundle) {
141 bundle_ = bundle;
144 // Populated for all other types.
145 const extensions::Extension* extension() const { return extension_; }
146 void set_extension(const extensions::Extension* extension) {
147 extension_ = extension;
150 // May be populated for POST_INSTALL_PERMISSIONS_PROMPT.
151 void set_retained_files(const std::vector<base::FilePath>& retained_files) {
152 retained_files_ = retained_files;
155 const gfx::Image& icon() const { return icon_; }
156 void set_icon(const gfx::Image& icon) { icon_ = icon; }
158 const ExtensionInstallPromptExperiment* experiment() const {
159 return experiment_;
162 void set_experiment(ExtensionInstallPromptExperiment* experiment) {
163 experiment_ = experiment;
166 private:
167 bool ShouldDisplayRevokeFilesButton() const;
169 PromptType type_;
171 // Permissions that are being requested (may not be all of an extension's
172 // permissions if only additional ones are being requested)
173 std::vector<base::string16> permissions_;
174 std::vector<base::string16> details_;
175 std::vector<bool> is_showing_details_for_permissions_;
176 std::vector<bool> is_showing_details_for_oauth_;
177 bool is_showing_details_for_retained_files_;
179 // Descriptions and details for OAuth2 permissions to display to the user.
180 // These correspond to permission scopes.
181 IssueAdviceInfo oauth_issue_advice_;
183 // User name to be used in Oauth heading label.
184 base::string16 oauth_user_name_;
186 // The extension or bundle being installed.
187 const extensions::Extension* extension_;
188 const extensions::BundleInstaller* bundle_;
190 // The icon to be displayed.
191 gfx::Image icon_;
193 // These fields are populated only when the prompt type is
194 // INLINE_INSTALL_PROMPT
195 // Already formatted to be locale-specific.
196 std::string localized_user_count_;
197 // Range is kMinExtensionRating to kMaxExtensionRating
198 double average_rating_;
199 int rating_count_;
201 // Whether we should display the user count (we anticipate this will be
202 // false if localized_user_count_ represents the number zero).
203 bool show_user_count_;
205 std::vector<base::FilePath> retained_files_;
207 scoped_refptr<ExtensionInstallPromptExperiment> experiment_;
210 static const int kMinExtensionRating = 0;
211 static const int kMaxExtensionRating = 5;
213 class Delegate {
214 public:
215 // We call this method to signal that the installation should continue.
216 virtual void InstallUIProceed() = 0;
218 // We call this method to signal that the installation should stop, with
219 // |user_initiated| true if the installation was stopped by the user.
220 virtual void InstallUIAbort(bool user_initiated) = 0;
222 protected:
223 virtual ~Delegate() {}
226 // Parameters to show a prompt dialog. Two sets of the
227 // parameters are supported: either use a parent WebContents or use a
228 // parent NativeWindow + a PageNavigator.
229 struct ShowParams {
230 explicit ShowParams(content::WebContents* contents);
231 ShowParams(gfx::NativeWindow window, content::PageNavigator* navigator);
233 // Parent web contents of the install UI dialog. This can be NULL.
234 content::WebContents* parent_web_contents;
236 // NativeWindow parent and navigator. If initialized using a parent web
237 // contents, these are derived from it.
238 gfx::NativeWindow parent_window;
239 content::PageNavigator* navigator;
242 typedef base::Callback<void(const ExtensionInstallPrompt::ShowParams&,
243 ExtensionInstallPrompt::Delegate*,
244 const ExtensionInstallPrompt::Prompt&)>
245 ShowDialogCallback;
247 // Callback to show the default extension install dialog.
248 // The implementations of this function are platform-specific.
249 static ShowDialogCallback GetDefaultShowDialogCallback();
251 // Creates a dummy extension from the |manifest|, replacing the name and
252 // description with the localizations if provided.
253 static scoped_refptr<extensions::Extension> GetLocalizedExtensionForDisplay(
254 const base::DictionaryValue* manifest,
255 int flags, // Extension::InitFromValueFlags
256 const std::string& id,
257 const std::string& localized_name,
258 const std::string& localized_description,
259 std::string* error);
261 // Creates a prompt with a parent web content.
262 explicit ExtensionInstallPrompt(content::WebContents* contents);
264 // Creates a prompt with a profile, a native window and a page navigator.
265 ExtensionInstallPrompt(Profile* profile,
266 gfx::NativeWindow native_window,
267 content::PageNavigator* navigator);
269 virtual ~ExtensionInstallPrompt();
271 ExtensionInstallUI* install_ui() const { return install_ui_.get(); }
273 bool record_oauth2_grant() const { return record_oauth2_grant_; }
275 content::WebContents* parent_web_contents() const {
276 return show_params_.parent_web_contents;
279 // This is called by the bundle installer to verify whether the bundle
280 // should be installed.
282 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
283 virtual void ConfirmBundleInstall(
284 extensions::BundleInstaller* bundle,
285 const extensions::PermissionSet* permissions);
287 // This is called by the standalone installer to verify whether the install
288 // from the webstore should proceed.
290 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
291 virtual void ConfirmStandaloneInstall(Delegate* delegate,
292 const extensions::Extension* extension,
293 SkBitmap* icon,
294 const Prompt& prompt);
296 // This is called by the installer to verify whether the installation from
297 // the webstore should proceed. |show_dialog_callback| is optional and can be
298 // NULL.
300 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
301 virtual void ConfirmWebstoreInstall(
302 Delegate* delegate,
303 const extensions::Extension* extension,
304 const SkBitmap* icon,
305 const ShowDialogCallback& show_dialog_callback);
307 // This is called by the installer to verify whether the installation should
308 // proceed. This is declared virtual for testing. |show_dialog_callback| is
309 // optional and can be NULL.
311 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
312 virtual void ConfirmInstall(Delegate* delegate,
313 const extensions::Extension* extension,
314 const ShowDialogCallback& show_dialog_callback);
316 // This is called by the app handler launcher to verify whether the app
317 // should be re-enabled. This is declared virtual for testing.
319 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
320 virtual void ConfirmReEnable(Delegate* delegate,
321 const extensions::Extension* extension);
323 // This is called by the external install alert UI to verify whether the
324 // extension should be enabled (external extensions are installed disabled).
326 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
327 virtual void ConfirmExternalInstall(
328 Delegate* delegate,
329 const extensions::Extension* extension,
330 const ShowDialogCallback& show_dialog_callback);
332 // This is called by the extension permissions API to verify whether an
333 // extension may be granted additional permissions.
335 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
336 virtual void ConfirmPermissions(Delegate* delegate,
337 const extensions::Extension* extension,
338 const extensions::PermissionSet* permissions);
340 // This is called by the extension identity API to verify whether an
341 // extension can be granted an OAuth2 token.
343 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
344 virtual void ConfirmIssueAdvice(Delegate* delegate,
345 const extensions::Extension* extension,
346 const IssueAdviceInfo& issue_advice);
348 // This is called by the app handler launcher to review what permissions the
349 // extension or app currently has.
351 // We *MUST* eventually call either Proceed() or Abort() on |delegate|.
352 virtual void ReviewPermissions(
353 Delegate* delegate,
354 const extensions::Extension* extension,
355 const std::vector<base::FilePath>& retained_file_paths);
357 // Installation was successful. This is declared virtual for testing.
358 virtual void OnInstallSuccess(const extensions::Extension* extension,
359 SkBitmap* icon);
361 // Installation failed. This is declared virtual for testing.
362 virtual void OnInstallFailure(const extensions::CrxInstallerError& error);
364 protected:
365 friend class extensions::ExtensionWebstorePrivateApiTest;
366 friend class extensions::MockGetAuthTokenFunction;
367 friend class WebstoreStartupInstallUnpackFailureTest;
369 // Whether or not we should record the oauth2 grant upon successful install.
370 bool record_oauth2_grant_;
372 private:
373 friend class GalleryInstallApiTestObserver;
375 // Sets the icon that will be used in any UI. If |icon| is NULL, or contains
376 // an empty bitmap, then a default icon will be used instead.
377 void SetIcon(const SkBitmap* icon);
379 // ImageLoader callback.
380 void OnImageLoaded(const gfx::Image& image);
382 // Starts the process of showing a confirmation UI, which is split into two.
383 // 1) Set off a 'load icon' task.
384 // 2) Handle the load icon response and show the UI (OnImageLoaded).
385 void LoadImageIfNeeded();
387 // OAuth2TokenService::Consumer implementation:
388 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
389 const std::string& access_token,
390 const base::Time& expiration_time) OVERRIDE;
391 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
392 const GoogleServiceAuthError& error) OVERRIDE;
394 // OAuth2MintTokenFlow::Delegate implementation:
395 virtual void OnIssueAdviceSuccess(
396 const IssueAdviceInfo& issue_advice) OVERRIDE;
397 virtual void OnMintTokenFailure(
398 const GoogleServiceAuthError& error) OVERRIDE;
400 // Shows the actual UI (the icon should already be loaded).
401 void ShowConfirmation();
403 base::MessageLoop* ui_loop_;
405 // The extensions installation icon.
406 SkBitmap icon_;
408 // The extension we are showing the UI for, if type is not
409 // BUNDLE_INSTALL_PROMPT.
410 const extensions::Extension* extension_;
412 // The bundle we are showing the UI for, if type BUNDLE_INSTALL_PROMPT.
413 const extensions::BundleInstaller* bundle_;
415 // The permissions being prompted for.
416 scoped_refptr<const extensions::PermissionSet> permissions_;
418 // The object responsible for doing the UI specific actions.
419 scoped_ptr<ExtensionInstallUI> install_ui_;
421 // Parameters to show the confirmation UI.
422 ShowParams show_params_;
424 // The delegate we will call Proceed/Abort on after confirmation UI.
425 Delegate* delegate_;
427 // A pre-filled prompt.
428 Prompt prompt_;
430 scoped_ptr<OAuth2TokenService::Request> login_token_request_;
431 scoped_ptr<OAuth2MintTokenFlow> token_flow_;
433 // Used to show the confirm dialog.
434 ShowDialogCallback show_dialog_callback_;
437 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_INSTALL_PROMPT_H_