[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/extensions/extension_install_prompt.h" |
| 6 | |
| 7 | #include <map> |
| 8 | |
| 9 | #include "base/command_line.h" |
| 10 | #include "base/file_util.h" |
| 11 | #include "base/message_loop.h" |
| 12 | #include "base/string_number_conversions.h" |
| 13 | #include "base/string_util.h" |
| 14 | #include "base/stringprintf.h" |
| 15 | #include "base/utf_string_conversions.h" |
| 16 | #include "chrome/browser/extensions/bundle_installer.h" |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 17 | #include "chrome/browser/extensions/extension_install_ui.h" |
[email protected] | fc507794 | 2012-08-15 21:37:59 | [diff] [blame] | 18 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 19 | #include "chrome/browser/profiles/profile.h" |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 20 | #include "chrome/browser/signin/token_service.h" |
| 21 | #include "chrome/browser/signin/token_service_factory.h" |
[email protected] | 32fc4ff7 | 2012-06-15 21:50:01 | [diff] [blame] | 22 | #include "chrome/browser/ui/browser.h" |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 23 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | b62084b | 2012-06-12 01:53:30 | [diff] [blame] | 24 | #include "chrome/browser/ui/tab_contents/tab_contents.h" |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 25 | #include "chrome/common/chrome_switches.h" |
| 26 | #include "chrome/common/extensions/extension.h" |
[email protected] | faf8719 | 2012-08-17 00:07:59 | [diff] [blame] | 27 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 28 | #include "chrome/common/extensions/extension_icon_set.h" |
| 29 | #include "chrome/common/extensions/extension_manifest_constants.h" |
| 30 | #include "chrome/common/extensions/extension_resource.h" |
[email protected] | 544471a | 2012-10-13 05:27:09 | [diff] [blame^] | 31 | #include "chrome/common/extensions/feature_switch.h" |
[email protected] | bebe1d0 | 2012-08-02 20:17:09 | [diff] [blame] | 32 | #include "chrome/common/extensions/permissions/permission_set.h" |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 33 | #include "chrome/common/extensions/url_pattern.h" |
[email protected] | fc507794 | 2012-08-15 21:37:59 | [diff] [blame] | 34 | #include "chrome/common/pref_names.h" |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 35 | #include "content/public/browser/page_navigator.h" |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 36 | #include "grit/chromium_strings.h" |
| 37 | #include "grit/generated_resources.h" |
[email protected] | 2a28133 | 2012-07-11 22:20:23 | [diff] [blame] | 38 | #include "grit/theme_resources.h" |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 39 | #include "ui/base/l10n/l10n_util.h" |
| 40 | #include "ui/base/resource/resource_bundle.h" |
| 41 | #include "ui/gfx/image/image.h" |
| 42 | |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 43 | using extensions::BundleInstaller; |
| 44 | using extensions::Extension; |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 45 | using extensions::PermissionSet; |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 46 | |
| 47 | static const int kTitleIds[ExtensionInstallPrompt::NUM_PROMPT_TYPES] = { |
| 48 | 0, // The regular install prompt depends on what's being installed. |
| 49 | IDS_EXTENSION_INLINE_INSTALL_PROMPT_TITLE, |
| 50 | IDS_EXTENSION_INSTALL_PROMPT_TITLE, |
| 51 | IDS_EXTENSION_RE_ENABLE_PROMPT_TITLE, |
| 52 | IDS_EXTENSION_PERMISSIONS_PROMPT_TITLE |
| 53 | }; |
| 54 | static const int kHeadingIds[ExtensionInstallPrompt::NUM_PROMPT_TYPES] = { |
| 55 | IDS_EXTENSION_INSTALL_PROMPT_HEADING, |
| 56 | 0, // Inline installs use the extension name. |
| 57 | 0, // Heading for bundle installs depends on the bundle contents. |
| 58 | IDS_EXTENSION_RE_ENABLE_PROMPT_HEADING, |
| 59 | IDS_EXTENSION_PERMISSIONS_PROMPT_HEADING |
| 60 | }; |
| 61 | static const int kAcceptButtonIds[ExtensionInstallPrompt::NUM_PROMPT_TYPES] = { |
| 62 | IDS_EXTENSION_PROMPT_INSTALL_BUTTON, |
| 63 | IDS_EXTENSION_PROMPT_INSTALL_BUTTON, |
| 64 | IDS_EXTENSION_PROMPT_INSTALL_BUTTON, |
| 65 | IDS_EXTENSION_PROMPT_RE_ENABLE_BUTTON, |
| 66 | IDS_EXTENSION_PROMPT_PERMISSIONS_BUTTON |
| 67 | }; |
| 68 | static const int kAbortButtonIds[ExtensionInstallPrompt::NUM_PROMPT_TYPES] = { |
| 69 | 0, // These all use the platform's default cancel label. |
| 70 | 0, |
| 71 | 0, |
| 72 | 0, |
| 73 | IDS_EXTENSION_PROMPT_PERMISSIONS_ABORT_BUTTON |
| 74 | }; |
| 75 | static const int kPermissionsHeaderIds[ |
| 76 | ExtensionInstallPrompt::NUM_PROMPT_TYPES] = { |
| 77 | IDS_EXTENSION_PROMPT_WILL_HAVE_ACCESS_TO, |
| 78 | IDS_EXTENSION_PROMPT_WILL_HAVE_ACCESS_TO, |
| 79 | IDS_EXTENSION_PROMPT_THESE_WILL_HAVE_ACCESS_TO, |
| 80 | IDS_EXTENSION_PROMPT_WILL_NOW_HAVE_ACCESS_TO, |
| 81 | IDS_EXTENSION_PROMPT_WANTS_ACCESS_TO, |
| 82 | }; |
[email protected] | fc507794 | 2012-08-15 21:37:59 | [diff] [blame] | 83 | static const int kOAuthHeaderIds[ |
| 84 | ExtensionInstallPrompt::NUM_PROMPT_TYPES] = { |
| 85 | IDS_EXTENSION_PROMPT_OAUTH_HEADER, |
| 86 | 0, // Inline installs don't show OAuth permissions. |
| 87 | 0, // Bundle installs don't show OAuth permissions. |
| 88 | IDS_EXTENSION_PROMPT_OAUTH_REENABLE_HEADER, |
| 89 | IDS_EXTENSION_PROMPT_OAUTH_PERMISSIONS_HEADER, |
| 90 | }; |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 91 | |
| 92 | namespace { |
| 93 | |
| 94 | // Size of extension icon in top left of dialog. |
| 95 | const int kIconSize = 69; |
| 96 | |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 97 | // Returns pixel size under maximal scale factor for the icon whose device |
| 98 | // independent size is |size_in_dip| |
| 99 | int GetSizeForMaxScaleFactor(int size_in_dip) { |
| 100 | std::vector<ui::ScaleFactor> supported_scale_factors = |
| 101 | ui::GetSupportedScaleFactors(); |
| 102 | // Scale factors are in ascending order, so the last one is the one we need. |
| 103 | ui::ScaleFactor max_scale_factor = supported_scale_factors.back(); |
| 104 | float max_scale_factor_scale = ui::GetScaleFactorScale(max_scale_factor); |
| 105 | |
| 106 | return static_cast<int>(size_in_dip * max_scale_factor_scale); |
| 107 | } |
| 108 | |
| 109 | // Returns bitmap for the default icon with size equal to the default icon's |
| 110 | // pixel size under maximal supported scale factor. |
| 111 | SkBitmap GetDefaultIconBitmapForMaxScaleFactor(bool is_app) { |
| 112 | std::vector<ui::ScaleFactor> supported_scale_factors = |
| 113 | ui::GetSupportedScaleFactors(); |
| 114 | // Scale factors are in ascending order, so the last one is the one we need. |
| 115 | ui::ScaleFactor max_scale_factor = |
| 116 | supported_scale_factors[supported_scale_factors.size() - 1]; |
| 117 | |
| 118 | return Extension::GetDefaultIcon(is_app). |
| 119 | GetRepresentation(max_scale_factor).sk_bitmap(); |
| 120 | } |
| 121 | |
[email protected] | af6efb2 | 2012-10-12 02:23:05 | [diff] [blame] | 122 | // If auto confirm is enabled then posts a task to proceed with or cancel the |
| 123 | // install and returns true. Otherwise returns false. |
| 124 | bool AutoConfirmPrompt(ExtensionInstallPrompt::Delegate* delegate) { |
| 125 | const CommandLine* cmdline = CommandLine::ForCurrentProcess(); |
| 126 | if (!cmdline->HasSwitch(switches::kAppsGalleryInstallAutoConfirmForTests)) |
| 127 | return false; |
| 128 | std::string value = cmdline->GetSwitchValueASCII( |
| 129 | switches::kAppsGalleryInstallAutoConfirmForTests); |
| 130 | |
| 131 | // We use PostTask instead of calling the delegate directly here, because in |
| 132 | // the real implementations it's highly likely the message loop will be |
| 133 | // pumping a few times before the user clicks accept or cancel. |
| 134 | if (value == "accept") { |
| 135 | MessageLoop::current()->PostTask( |
| 136 | FROM_HERE, |
| 137 | base::Bind(&ExtensionInstallPrompt::Delegate::InstallUIProceed, |
| 138 | base::Unretained(delegate))); |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | if (value == "cancel") { |
| 143 | MessageLoop::current()->PostTask( |
| 144 | FROM_HERE, |
| 145 | base::Bind(&ExtensionInstallPrompt::Delegate::InstallUIAbort, |
| 146 | base::Unretained(delegate), |
| 147 | true)); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | NOTREACHED(); |
| 152 | return false; |
| 153 | } |
| 154 | |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 155 | } // namespace |
| 156 | |
[email protected] | fc507794 | 2012-08-15 21:37:59 | [diff] [blame] | 157 | ExtensionInstallPrompt::Prompt::Prompt(Profile* profile, PromptType type) |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 158 | : type_(type), |
| 159 | extension_(NULL), |
| 160 | bundle_(NULL), |
| 161 | average_rating_(0.0), |
[email protected] | fc507794 | 2012-08-15 21:37:59 | [diff] [blame] | 162 | rating_count_(0), |
| 163 | profile_(profile) { |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | ExtensionInstallPrompt::Prompt::~Prompt() { |
| 167 | } |
| 168 | |
| 169 | void ExtensionInstallPrompt::Prompt::SetPermissions( |
| 170 | const std::vector<string16>& permissions) { |
| 171 | permissions_ = permissions; |
| 172 | } |
| 173 | |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 174 | void ExtensionInstallPrompt::Prompt::SetOAuthIssueAdvice( |
| 175 | const IssueAdviceInfo& issue_advice) { |
| 176 | oauth_issue_advice_ = issue_advice; |
| 177 | } |
| 178 | |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 179 | void ExtensionInstallPrompt::Prompt::SetInlineInstallWebstoreData( |
| 180 | const std::string& localized_user_count, |
| 181 | double average_rating, |
| 182 | int rating_count) { |
| 183 | CHECK_EQ(INLINE_INSTALL_PROMPT, type_); |
| 184 | localized_user_count_ = localized_user_count; |
| 185 | average_rating_ = average_rating; |
| 186 | rating_count_ = rating_count; |
| 187 | } |
| 188 | |
| 189 | string16 ExtensionInstallPrompt::Prompt::GetDialogTitle() const { |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 190 | int resource_id = kTitleIds[type_]; |
| 191 | |
| 192 | if (type_ == INSTALL_PROMPT) { |
| 193 | if (extension_->is_app()) |
| 194 | resource_id = IDS_EXTENSION_INSTALL_APP_PROMPT_TITLE; |
| 195 | else if (extension_->is_theme()) |
| 196 | resource_id = IDS_EXTENSION_INSTALL_THEME_PROMPT_TITLE; |
| 197 | else |
| 198 | resource_id = IDS_EXTENSION_INSTALL_EXTENSION_PROMPT_TITLE; |
| 199 | } |
| 200 | |
| 201 | return l10n_util::GetStringUTF16(resource_id); |
| 202 | } |
| 203 | |
| 204 | string16 ExtensionInstallPrompt::Prompt::GetHeading() const { |
| 205 | if (type_ == INLINE_INSTALL_PROMPT) { |
| 206 | return UTF8ToUTF16(extension_->name()); |
| 207 | } else if (type_ == BUNDLE_INSTALL_PROMPT) { |
| 208 | return bundle_->GetHeadingTextFor(BundleInstaller::Item::STATE_PENDING); |
| 209 | } else { |
| 210 | return l10n_util::GetStringFUTF16( |
| 211 | kHeadingIds[type_], UTF8ToUTF16(extension_->name())); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | string16 ExtensionInstallPrompt::Prompt::GetAcceptButtonLabel() const { |
| 216 | return l10n_util::GetStringUTF16(kAcceptButtonIds[type_]); |
| 217 | } |
| 218 | |
| 219 | bool ExtensionInstallPrompt::Prompt::HasAbortButtonLabel() const { |
| 220 | return kAbortButtonIds[type_] > 0; |
| 221 | } |
| 222 | |
| 223 | string16 ExtensionInstallPrompt::Prompt::GetAbortButtonLabel() const { |
| 224 | CHECK(HasAbortButtonLabel()); |
| 225 | return l10n_util::GetStringUTF16(kAbortButtonIds[type_]); |
| 226 | } |
| 227 | |
| 228 | string16 ExtensionInstallPrompt::Prompt::GetPermissionsHeading() const { |
| 229 | return l10n_util::GetStringUTF16(kPermissionsHeaderIds[type_]); |
| 230 | } |
| 231 | |
[email protected] | 813475e | 2012-07-02 23:54:18 | [diff] [blame] | 232 | string16 ExtensionInstallPrompt::Prompt::GetOAuthHeading() const { |
[email protected] | fc507794 | 2012-08-15 21:37:59 | [diff] [blame] | 233 | string16 username(ASCIIToUTF16("[email protected]")); |
| 234 | // |profile_| can be NULL in unit tests. |
| 235 | if (profile_) { |
| 236 | username = UTF8ToUTF16(profile_->GetPrefs()->GetString( |
| 237 | prefs::kGoogleServicesUsername)); |
| 238 | } |
| 239 | int resource_id = kOAuthHeaderIds[type_]; |
| 240 | return l10n_util::GetStringFUTF16(resource_id, username); |
[email protected] | 813475e | 2012-07-02 23:54:18 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 243 | void ExtensionInstallPrompt::Prompt::AppendRatingStars( |
| 244 | StarAppender appender, void* data) const { |
| 245 | CHECK(appender); |
| 246 | CHECK_EQ(INLINE_INSTALL_PROMPT, type_); |
| 247 | int rating_integer = floor(average_rating_); |
| 248 | double rating_fractional = average_rating_ - rating_integer; |
| 249 | |
| 250 | if (rating_fractional > 0.66) { |
| 251 | rating_integer++; |
| 252 | } |
| 253 | |
| 254 | if (rating_fractional < 0.33 || rating_fractional > 0.66) { |
| 255 | rating_fractional = 0; |
| 256 | } |
| 257 | |
| 258 | ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 259 | int i; |
| 260 | for (i = 0; i < rating_integer; i++) { |
| 261 | appender(rb.GetImageSkiaNamed(IDR_EXTENSIONS_RATING_STAR_ON), data); |
| 262 | } |
| 263 | if (rating_fractional) { |
| 264 | appender(rb.GetImageSkiaNamed(IDR_EXTENSIONS_RATING_STAR_HALF_LEFT), data); |
| 265 | i++; |
| 266 | } |
| 267 | for (; i < kMaxExtensionRating; i++) { |
| 268 | appender(rb.GetImageSkiaNamed(IDR_EXTENSIONS_RATING_STAR_OFF), data); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | string16 ExtensionInstallPrompt::Prompt::GetRatingCount() const { |
| 273 | CHECK_EQ(INLINE_INSTALL_PROMPT, type_); |
| 274 | return l10n_util::GetStringFUTF16( |
| 275 | IDS_EXTENSION_RATING_COUNT, |
| 276 | UTF8ToUTF16(base::IntToString(rating_count_))); |
| 277 | } |
| 278 | |
| 279 | string16 ExtensionInstallPrompt::Prompt::GetUserCount() const { |
| 280 | CHECK_EQ(INLINE_INSTALL_PROMPT, type_); |
| 281 | return l10n_util::GetStringFUTF16( |
| 282 | IDS_EXTENSION_USER_COUNT, |
| 283 | UTF8ToUTF16(localized_user_count_)); |
| 284 | } |
| 285 | |
| 286 | size_t ExtensionInstallPrompt::Prompt::GetPermissionCount() const { |
| 287 | return permissions_.size(); |
| 288 | } |
| 289 | |
| 290 | string16 ExtensionInstallPrompt::Prompt::GetPermission(size_t index) const { |
| 291 | CHECK_LT(index, permissions_.size()); |
[email protected] | ef2654e4 | 2012-08-11 03:57:56 | [diff] [blame] | 292 | return permissions_[index]; |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 293 | } |
| 294 | |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 295 | size_t ExtensionInstallPrompt::Prompt::GetOAuthIssueCount() const { |
| 296 | return oauth_issue_advice_.size(); |
| 297 | } |
| 298 | |
| 299 | const IssueAdviceInfoEntry& ExtensionInstallPrompt::Prompt::GetOAuthIssue( |
| 300 | size_t index) const { |
| 301 | CHECK_LT(index, oauth_issue_advice_.size()); |
| 302 | return oauth_issue_advice_[index]; |
| 303 | } |
| 304 | |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 305 | // static |
| 306 | scoped_refptr<Extension> |
| 307 | ExtensionInstallPrompt::GetLocalizedExtensionForDisplay( |
| 308 | const DictionaryValue* manifest, |
[email protected] | c422a86 | 2012-07-31 15:46:13 | [diff] [blame] | 309 | int flags, |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 310 | const std::string& id, |
| 311 | const std::string& localized_name, |
| 312 | const std::string& localized_description, |
| 313 | std::string* error) { |
| 314 | scoped_ptr<DictionaryValue> localized_manifest; |
| 315 | if (!localized_name.empty() || !localized_description.empty()) { |
| 316 | localized_manifest.reset(manifest->DeepCopy()); |
| 317 | if (!localized_name.empty()) { |
| 318 | localized_manifest->SetString(extension_manifest_keys::kName, |
| 319 | localized_name); |
| 320 | } |
| 321 | if (!localized_description.empty()) { |
| 322 | localized_manifest->SetString(extension_manifest_keys::kDescription, |
| 323 | localized_description); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return Extension::Create( |
| 328 | FilePath(), |
| 329 | Extension::INTERNAL, |
| 330 | localized_manifest.get() ? *localized_manifest.get() : *manifest, |
[email protected] | c422a86 | 2012-07-31 15:46:13 | [diff] [blame] | 331 | flags, |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 332 | id, |
| 333 | error); |
| 334 | } |
| 335 | |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 336 | ExtensionInstallPrompt::ExtensionInstallPrompt( |
| 337 | gfx::NativeWindow parent, |
| 338 | content::PageNavigator* navigator, |
| 339 | Profile* profile) |
[email protected] | 16798da83 | 2012-08-30 20:46:04 | [diff] [blame] | 340 | : record_oauth2_grant_(false), |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 341 | parent_(parent), |
| 342 | navigator_(navigator), |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 343 | ui_loop_(MessageLoop::current()), |
| 344 | extension_(NULL), |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 345 | install_ui_(ExtensionInstallUI::Create(profile)), |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 346 | delegate_(NULL), |
[email protected] | fc507794 | 2012-08-15 21:37:59 | [diff] [blame] | 347 | prompt_(profile, UNSET_PROMPT_TYPE), |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 348 | prompt_type_(UNSET_PROMPT_TYPE), |
| 349 | ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { |
| 350 | } |
| 351 | |
| 352 | ExtensionInstallPrompt::~ExtensionInstallPrompt() { |
| 353 | } |
| 354 | |
| 355 | void ExtensionInstallPrompt::ConfirmBundleInstall( |
| 356 | extensions::BundleInstaller* bundle, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 357 | const PermissionSet* permissions) { |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 358 | DCHECK(ui_loop_ == MessageLoop::current()); |
| 359 | bundle_ = bundle; |
| 360 | permissions_ = permissions; |
| 361 | delegate_ = bundle; |
| 362 | prompt_type_ = BUNDLE_INSTALL_PROMPT; |
| 363 | |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 364 | FetchOAuthIssueAdviceIfNeeded(); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 365 | } |
| 366 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 367 | void ExtensionInstallPrompt::ConfirmStandaloneInstall( |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 368 | Delegate* delegate, |
| 369 | const Extension* extension, |
| 370 | SkBitmap* icon, |
| 371 | const ExtensionInstallPrompt::Prompt& prompt) { |
| 372 | DCHECK(ui_loop_ == MessageLoop::current()); |
| 373 | extension_ = extension; |
| 374 | permissions_ = extension->GetActivePermissions(); |
| 375 | delegate_ = delegate; |
| 376 | prompt_ = prompt; |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 377 | prompt_type_ = prompt.type(); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 378 | |
| 379 | SetIcon(icon); |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 380 | FetchOAuthIssueAdviceIfNeeded(); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 381 | } |
| 382 | |
[email protected] | af6efb2 | 2012-10-12 02:23:05 | [diff] [blame] | 383 | void ExtensionInstallPrompt::ConfirmWebstoreInstall( |
| 384 | Delegate* delegate, |
| 385 | const Extension* extension, |
| 386 | const SkBitmap* icon, |
| 387 | const ShowDialogCallback& show_dialog_callback) { |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 388 | // SetIcon requires |extension_| to be set. ConfirmInstall will setup the |
| 389 | // remaining fields. |
| 390 | extension_ = extension; |
| 391 | SetIcon(icon); |
[email protected] | af6efb2 | 2012-10-12 02:23:05 | [diff] [blame] | 392 | ConfirmInstall(delegate, extension, show_dialog_callback); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | af6efb2 | 2012-10-12 02:23:05 | [diff] [blame] | 395 | void ExtensionInstallPrompt::ConfirmInstall( |
| 396 | Delegate* delegate, |
| 397 | const Extension* extension, |
| 398 | const ShowDialogCallback& show_dialog_callback) { |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 399 | DCHECK(ui_loop_ == MessageLoop::current()); |
| 400 | extension_ = extension; |
| 401 | permissions_ = extension->GetActivePermissions(); |
| 402 | delegate_ = delegate; |
| 403 | prompt_type_ = INSTALL_PROMPT; |
[email protected] | af6efb2 | 2012-10-12 02:23:05 | [diff] [blame] | 404 | show_dialog_callback_ = show_dialog_callback; |
| 405 | DCHECK(!show_dialog_callback_.is_null()); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 406 | |
| 407 | // We special-case themes to not show any confirm UI. Instead they are |
| 408 | // immediately installed, and then we show an infobar (see OnInstallSuccess) |
| 409 | // to allow the user to revert if they don't like it. |
| 410 | // |
| 411 | // We don't do this in the case where off-store extension installs are |
| 412 | // disabled because in that case, we don't show the dangerous download UI, so |
| 413 | // we need the UI confirmation. |
| 414 | if (extension->is_theme()) { |
| 415 | if (extension->from_webstore() || |
[email protected] | 544471a | 2012-10-13 05:27:09 | [diff] [blame^] | 416 | extensions::FeatureSwitch::easy_off_store_install()->IsEnabled()) { |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 417 | delegate->InstallUIProceed(); |
| 418 | return; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | LoadImageIfNeeded(); |
| 423 | } |
| 424 | |
| 425 | void ExtensionInstallPrompt::ConfirmReEnable(Delegate* delegate, |
| 426 | const Extension* extension) { |
| 427 | DCHECK(ui_loop_ == MessageLoop::current()); |
| 428 | extension_ = extension; |
| 429 | permissions_ = extension->GetActivePermissions(); |
| 430 | delegate_ = delegate; |
| 431 | prompt_type_ = RE_ENABLE_PROMPT; |
| 432 | |
| 433 | LoadImageIfNeeded(); |
| 434 | } |
| 435 | |
| 436 | void ExtensionInstallPrompt::ConfirmPermissions( |
| 437 | Delegate* delegate, |
| 438 | const Extension* extension, |
[email protected] | c2e66e1 | 2012-06-27 06:27:06 | [diff] [blame] | 439 | const PermissionSet* permissions) { |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 440 | DCHECK(ui_loop_ == MessageLoop::current()); |
| 441 | extension_ = extension; |
| 442 | permissions_ = permissions; |
| 443 | delegate_ = delegate; |
| 444 | prompt_type_ = PERMISSIONS_PROMPT; |
| 445 | |
| 446 | LoadImageIfNeeded(); |
| 447 | } |
| 448 | |
[email protected] | f746b3f | 2012-07-03 17:53:37 | [diff] [blame] | 449 | void ExtensionInstallPrompt::ConfirmIssueAdvice( |
| 450 | Delegate* delegate, |
| 451 | const Extension* extension, |
| 452 | const IssueAdviceInfo& issue_advice) { |
| 453 | DCHECK(ui_loop_ == MessageLoop::current()); |
| 454 | extension_ = extension; |
| 455 | delegate_ = delegate; |
| 456 | prompt_type_ = PERMISSIONS_PROMPT; |
| 457 | |
| 458 | record_oauth2_grant_ = true; |
| 459 | prompt_.SetOAuthIssueAdvice(issue_advice); |
| 460 | |
| 461 | LoadImageIfNeeded(); |
| 462 | } |
| 463 | |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 464 | void ExtensionInstallPrompt::OnInstallSuccess(const Extension* extension, |
| 465 | SkBitmap* icon) { |
| 466 | extension_ = extension; |
| 467 | SetIcon(icon); |
| 468 | |
| 469 | install_ui_->OnInstallSuccess(extension, &icon_); |
| 470 | } |
| 471 | |
[email protected] | bf3d9df | 2012-07-24 23:20:27 | [diff] [blame] | 472 | void ExtensionInstallPrompt::OnInstallFailure( |
| 473 | const extensions::CrxInstallerError& error) { |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 474 | install_ui_->OnInstallFailure(error); |
| 475 | } |
| 476 | |
| 477 | void ExtensionInstallPrompt::SetIcon(const SkBitmap* image) { |
| 478 | if (image) |
| 479 | icon_ = *image; |
| 480 | else |
| 481 | icon_ = SkBitmap(); |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 482 | if (icon_.empty()) { |
| 483 | // Let's set default icon bitmap whose size is equal to the default icon's |
| 484 | // pixel size under maximal supported scale factor. If the bitmap is larger |
| 485 | // than the one we need, it will be scaled down by the ui code. |
| 486 | icon_ = GetDefaultIconBitmapForMaxScaleFactor(extension_->is_app()); |
| 487 | } |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void ExtensionInstallPrompt::OnImageLoaded(const gfx::Image& image, |
| 491 | const std::string& extension_id, |
| 492 | int index) { |
| 493 | SetIcon(image.IsEmpty() ? NULL : image.ToSkBitmap()); |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 494 | FetchOAuthIssueAdviceIfNeeded(); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void ExtensionInstallPrompt::LoadImageIfNeeded() { |
| 498 | // Bundle install prompts do not have an icon. |
| 499 | if (!icon_.empty()) { |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 500 | FetchOAuthIssueAdviceIfNeeded(); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | |
| 504 | // Load the image asynchronously. For the response, check OnImageLoaded. |
| 505 | ExtensionResource image = |
[email protected] | faf8719 | 2012-08-17 00:07:59 | [diff] [blame] | 506 | extension_->GetIconResource(extension_misc::EXTENSION_ICON_LARGE, |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 507 | ExtensionIconSet::MATCH_BIGGER); |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 508 | // Load the icon whose pixel size is large enough to be displayed under |
| 509 | // maximal supported scale factor. UI code will scale the icon down if needed. |
| 510 | // TODO(tbarzic): We should use IconImage here and load the required bitmap |
| 511 | // lazily. |
| 512 | int pixel_size = GetSizeForMaxScaleFactor(kIconSize); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 513 | tracker_.LoadImage(extension_, image, |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 514 | gfx::Size(pixel_size, pixel_size), |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 515 | ImageLoadingTracker::DONT_CACHE); |
| 516 | } |
| 517 | |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 518 | void ExtensionInstallPrompt::FetchOAuthIssueAdviceIfNeeded() { |
[email protected] | 668ed2c | 2012-09-12 10:52:41 | [diff] [blame] | 519 | // |extension_| may be NULL, e.g. in the bundle install case. |
| 520 | if (!extension_ || |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 521 | prompt_type_ == BUNDLE_INSTALL_PROMPT || |
[email protected] | 668ed2c | 2012-09-12 10:52:41 | [diff] [blame] | 522 | prompt_type_ == INLINE_INSTALL_PROMPT || |
| 523 | prompt_.GetOAuthIssueCount() != 0U) { |
| 524 | ShowConfirmation(); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | const Extension::OAuth2Info& oauth2_info = extension_->oauth2_info(); |
| 529 | if (oauth2_info.client_id.empty() || |
| 530 | oauth2_info.scopes.empty()) { |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 531 | ShowConfirmation(); |
| 532 | return; |
| 533 | } |
| 534 | |
[email protected] | 7cac526 | 2012-09-26 17:32:25 | [diff] [blame] | 535 | Profile* profile = install_ui_->profile(); |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 536 | TokenService* token_service = TokenServiceFactory::GetForProfile(profile); |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 537 | |
| 538 | token_flow_.reset(new OAuth2MintTokenFlow( |
| 539 | profile->GetRequestContext(), |
| 540 | this, |
| 541 | OAuth2MintTokenFlow::Parameters( |
| 542 | token_service->GetOAuth2LoginRefreshToken(), |
| 543 | extension_->id(), |
| 544 | oauth2_info.client_id, |
[email protected] | d4a37f1c | 2012-07-09 21:36:13 | [diff] [blame] | 545 | oauth2_info.scopes, |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 546 | OAuth2MintTokenFlow::MODE_ISSUE_ADVICE))); |
| 547 | token_flow_->Start(); |
| 548 | } |
| 549 | |
| 550 | void ExtensionInstallPrompt::OnIssueAdviceSuccess( |
| 551 | const IssueAdviceInfo& advice_info) { |
| 552 | prompt_.SetOAuthIssueAdvice(advice_info); |
| 553 | record_oauth2_grant_ = true; |
| 554 | ShowConfirmation(); |
| 555 | } |
| 556 | |
| 557 | void ExtensionInstallPrompt::OnMintTokenFailure( |
| 558 | const GoogleServiceAuthError& error) { |
| 559 | ShowConfirmation(); |
| 560 | } |
| 561 | |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 562 | void ExtensionInstallPrompt::ShowConfirmation() { |
| 563 | prompt_.set_type(prompt_type_); |
[email protected] | f746b3f | 2012-07-03 17:53:37 | [diff] [blame] | 564 | |
[email protected] | bebe1d0 | 2012-08-02 20:17:09 | [diff] [blame] | 565 | if (permissions_) { |
[email protected] | a6e5c9b | 2012-08-09 04:06:25 | [diff] [blame] | 566 | Extension::Type extension_type = prompt_type_ == BUNDLE_INSTALL_PROMPT ? |
| 567 | Extension::TYPE_UNKNOWN : extension_->GetType(); |
| 568 | prompt_.SetPermissions(permissions_->GetWarningMessages(extension_type)); |
[email protected] | bebe1d0 | 2012-08-02 20:17:09 | [diff] [blame] | 569 | } |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 570 | |
| 571 | switch (prompt_type_) { |
| 572 | case PERMISSIONS_PROMPT: |
| 573 | case RE_ENABLE_PROMPT: |
| 574 | case INLINE_INSTALL_PROMPT: |
| 575 | case INSTALL_PROMPT: { |
| 576 | prompt_.set_extension(extension_); |
| 577 | prompt_.set_icon(gfx::Image(icon_)); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 578 | break; |
| 579 | } |
| 580 | case BUNDLE_INSTALL_PROMPT: { |
| 581 | prompt_.set_bundle(bundle_); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 582 | break; |
| 583 | } |
| 584 | default: |
| 585 | NOTREACHED() << "Unknown message"; |
[email protected] | af6efb2 | 2012-10-12 02:23:05 | [diff] [blame] | 586 | return; |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 587 | } |
[email protected] | af6efb2 | 2012-10-12 02:23:05 | [diff] [blame] | 588 | |
| 589 | if (AutoConfirmPrompt(delegate_)) |
| 590 | return; |
| 591 | |
| 592 | DCHECK(!show_dialog_callback_.is_null()); |
| 593 | show_dialog_callback_.Run(parent_, navigator_, delegate_, prompt_); |
[email protected] | c82da8c4 | 2012-06-08 19:49:11 | [diff] [blame] | 594 | } |
[email protected] | b70a2d9 | 2012-06-28 19:51:21 | [diff] [blame] | 595 | |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 596 | namespace chrome { |
| 597 | |
| 598 | ExtensionInstallPrompt* CreateExtensionInstallPromptWithBrowser( |
| 599 | Browser* browser) { |
| 600 | // |browser| can be NULL in unit tests. |
| 601 | if (!browser) |
| 602 | return new ExtensionInstallPrompt(NULL, NULL, NULL); |
| 603 | gfx::NativeWindow parent = |
| 604 | browser->window() ? browser->window()->GetNativeWindow() : NULL; |
| 605 | return new ExtensionInstallPrompt(parent, browser, browser->profile()); |
| 606 | } |
| 607 | |
| 608 | } // namespace chrome |