| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_install_prompt.h" | 5 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/extensions/bundle_installer.h" | 16 #include "chrome/browser/extensions/bundle_installer.h" |
| 17 #include "chrome/browser/extensions/extension_install_dialog.h" | 17 #include "chrome/browser/extensions/extension_install_dialog.h" |
| 18 #include "chrome/browser/extensions/extension_install_ui.h" | 18 #include "chrome/browser/extensions/extension_install_ui.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/signin/token_service.h" | |
| 21 #include "chrome/browser/signin/token_service_factory.h" | |
| 22 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/browser/ui/browser_navigator.h" | 21 #include "chrome/browser/ui/browser_navigator.h" |
| 24 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 22 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 25 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/extensions/extension.h" | 24 #include "chrome/common/extensions/extension.h" |
| 27 #include "chrome/common/extensions/extension_icon_set.h" | 25 #include "chrome/common/extensions/extension_icon_set.h" |
| 28 #include "chrome/common/extensions/extension_manifest_constants.h" | 26 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 29 #include "chrome/common/extensions/extension_resource.h" | 27 #include "chrome/common/extensions/extension_resource.h" |
| 30 #include "chrome/common/extensions/extension_switch_utils.h" | 28 #include "chrome/common/extensions/extension_switch_utils.h" |
| 31 #include "chrome/common/extensions/url_pattern.h" | 29 #include "chrome/common/extensions/url_pattern.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } | 95 } |
| 98 | 96 |
| 99 ExtensionInstallPrompt::Prompt::~Prompt() { | 97 ExtensionInstallPrompt::Prompt::~Prompt() { |
| 100 } | 98 } |
| 101 | 99 |
| 102 void ExtensionInstallPrompt::Prompt::SetPermissions( | 100 void ExtensionInstallPrompt::Prompt::SetPermissions( |
| 103 const std::vector<string16>& permissions) { | 101 const std::vector<string16>& permissions) { |
| 104 permissions_ = permissions; | 102 permissions_ = permissions; |
| 105 } | 103 } |
| 106 | 104 |
| 107 void ExtensionInstallPrompt::Prompt::SetOAuthIssueAdvice( | |
| 108 const IssueAdviceInfo& issue_advice) { | |
| 109 oauth_issue_advice_ = issue_advice; | |
| 110 } | |
| 111 | |
| 112 void ExtensionInstallPrompt::Prompt::SetInlineInstallWebstoreData( | 105 void ExtensionInstallPrompt::Prompt::SetInlineInstallWebstoreData( |
| 113 const std::string& localized_user_count, | 106 const std::string& localized_user_count, |
| 114 double average_rating, | 107 double average_rating, |
| 115 int rating_count) { | 108 int rating_count) { |
| 116 CHECK_EQ(INLINE_INSTALL_PROMPT, type_); | 109 CHECK_EQ(INLINE_INSTALL_PROMPT, type_); |
| 117 localized_user_count_ = localized_user_count; | 110 localized_user_count_ = localized_user_count; |
| 118 average_rating_ = average_rating; | 111 average_rating_ = average_rating; |
| 119 rating_count_ = rating_count; | 112 rating_count_ = rating_count; |
| 120 } | 113 } |
| 121 | 114 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 size_t ExtensionInstallPrompt::Prompt::GetPermissionCount() const { | 202 size_t ExtensionInstallPrompt::Prompt::GetPermissionCount() const { |
| 210 return permissions_.size(); | 203 return permissions_.size(); |
| 211 } | 204 } |
| 212 | 205 |
| 213 string16 ExtensionInstallPrompt::Prompt::GetPermission(size_t index) const { | 206 string16 ExtensionInstallPrompt::Prompt::GetPermission(size_t index) const { |
| 214 CHECK_LT(index, permissions_.size()); | 207 CHECK_LT(index, permissions_.size()); |
| 215 return l10n_util::GetStringFUTF16( | 208 return l10n_util::GetStringFUTF16( |
| 216 IDS_EXTENSION_PERMISSION_LINE, permissions_[index]); | 209 IDS_EXTENSION_PERMISSION_LINE, permissions_[index]); |
| 217 } | 210 } |
| 218 | 211 |
| 219 size_t ExtensionInstallPrompt::Prompt::GetOAuthIssueCount() const { | |
| 220 return oauth_issue_advice_.size(); | |
| 221 } | |
| 222 | |
| 223 const IssueAdviceInfoEntry& ExtensionInstallPrompt::Prompt::GetOAuthIssue( | |
| 224 size_t index) const { | |
| 225 CHECK_LT(index, oauth_issue_advice_.size()); | |
| 226 return oauth_issue_advice_[index]; | |
| 227 } | |
| 228 | |
| 229 // static | 212 // static |
| 230 scoped_refptr<Extension> | 213 scoped_refptr<Extension> |
| 231 ExtensionInstallPrompt::GetLocalizedExtensionForDisplay( | 214 ExtensionInstallPrompt::GetLocalizedExtensionForDisplay( |
| 232 const DictionaryValue* manifest, | 215 const DictionaryValue* manifest, |
| 233 const std::string& id, | 216 const std::string& id, |
| 234 const std::string& localized_name, | 217 const std::string& localized_name, |
| 235 const std::string& localized_description, | 218 const std::string& localized_description, |
| 236 std::string* error) { | 219 std::string* error) { |
| 237 scoped_ptr<DictionaryValue> localized_manifest; | 220 scoped_ptr<DictionaryValue> localized_manifest; |
| 238 if (!localized_name.empty() || !localized_description.empty()) { | 221 if (!localized_name.empty() || !localized_description.empty()) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 250 return Extension::Create( | 233 return Extension::Create( |
| 251 FilePath(), | 234 FilePath(), |
| 252 Extension::INTERNAL, | 235 Extension::INTERNAL, |
| 253 localized_manifest.get() ? *localized_manifest.get() : *manifest, | 236 localized_manifest.get() ? *localized_manifest.get() : *manifest, |
| 254 Extension::NO_FLAGS, | 237 Extension::NO_FLAGS, |
| 255 id, | 238 id, |
| 256 error); | 239 error); |
| 257 } | 240 } |
| 258 | 241 |
| 259 ExtensionInstallPrompt::ExtensionInstallPrompt(Browser* browser) | 242 ExtensionInstallPrompt::ExtensionInstallPrompt(Browser* browser) |
| 260 : record_oauth2_grant_(ShouldAutomaticallyApproveScopes()), | 243 : browser_(browser), |
| 261 browser_(browser), | |
| 262 ui_loop_(MessageLoop::current()), | 244 ui_loop_(MessageLoop::current()), |
| 263 extension_(NULL), | 245 extension_(NULL), |
| 264 install_ui_(ExtensionInstallUI::Create(browser)), | 246 install_ui_(ExtensionInstallUI::Create(browser)), |
| 265 delegate_(NULL), | 247 delegate_(NULL), |
| 266 prompt_(UNSET_PROMPT_TYPE), | 248 prompt_(UNSET_PROMPT_TYPE), |
| 267 prompt_type_(UNSET_PROMPT_TYPE), | 249 prompt_type_(UNSET_PROMPT_TYPE), |
| 268 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { | 250 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { |
| 269 } | 251 } |
| 270 | 252 |
| 271 ExtensionInstallPrompt::~ExtensionInstallPrompt() { | 253 ExtensionInstallPrompt::~ExtensionInstallPrompt() { |
| 272 } | 254 } |
| 273 | 255 |
| 274 void ExtensionInstallPrompt::ConfirmBundleInstall( | 256 void ExtensionInstallPrompt::ConfirmBundleInstall( |
| 275 extensions::BundleInstaller* bundle, | 257 extensions::BundleInstaller* bundle, |
| 276 const PermissionSet* permissions) { | 258 const PermissionSet* permissions) { |
| 277 DCHECK(ui_loop_ == MessageLoop::current()); | 259 DCHECK(ui_loop_ == MessageLoop::current()); |
| 278 bundle_ = bundle; | 260 bundle_ = bundle; |
| 279 permissions_ = permissions; | 261 permissions_ = permissions; |
| 280 delegate_ = bundle; | 262 delegate_ = bundle; |
| 281 prompt_type_ = BUNDLE_INSTALL_PROMPT; | 263 prompt_type_ = BUNDLE_INSTALL_PROMPT; |
| 282 | 264 |
| 283 FetchOAuthIssueAdviceIfNeeded(); | 265 ShowConfirmation(); |
| 284 } | 266 } |
| 285 | 267 |
| 286 void ExtensionInstallPrompt::ConfirmInlineInstall( | 268 void ExtensionInstallPrompt::ConfirmInlineInstall( |
| 287 Delegate* delegate, | 269 Delegate* delegate, |
| 288 const Extension* extension, | 270 const Extension* extension, |
| 289 SkBitmap* icon, | 271 SkBitmap* icon, |
| 290 const ExtensionInstallPrompt::Prompt& prompt) { | 272 const ExtensionInstallPrompt::Prompt& prompt) { |
| 291 DCHECK(ui_loop_ == MessageLoop::current()); | 273 DCHECK(ui_loop_ == MessageLoop::current()); |
| 292 extension_ = extension; | 274 extension_ = extension; |
| 293 permissions_ = extension->GetActivePermissions(); | 275 permissions_ = extension->GetActivePermissions(); |
| 294 delegate_ = delegate; | 276 delegate_ = delegate; |
| 295 prompt_ = prompt; | 277 prompt_ = prompt; |
| 296 prompt_type_ = INLINE_INSTALL_PROMPT; | 278 prompt_type_ = INLINE_INSTALL_PROMPT; |
| 297 | 279 |
| 298 SetIcon(icon); | 280 SetIcon(icon); |
| 299 FetchOAuthIssueAdviceIfNeeded(); | 281 ShowConfirmation(); |
| 300 } | 282 } |
| 301 | 283 |
| 302 void ExtensionInstallPrompt::ConfirmWebstoreInstall(Delegate* delegate, | 284 void ExtensionInstallPrompt::ConfirmWebstoreInstall(Delegate* delegate, |
| 303 const Extension* extension, | 285 const Extension* extension, |
| 304 const SkBitmap* icon) { | 286 const SkBitmap* icon) { |
| 305 // SetIcon requires |extension_| to be set. ConfirmInstall will setup the | 287 // SetIcon requires |extension_| to be set. ConfirmInstall will setup the |
| 306 // remaining fields. | 288 // remaining fields. |
| 307 extension_ = extension; | 289 extension_ = extension; |
| 308 SetIcon(icon); | 290 SetIcon(icon); |
| 309 ConfirmInstall(delegate, extension); | 291 ConfirmInstall(delegate, extension); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 else | 359 else |
| 378 icon_ = SkBitmap(); | 360 icon_ = SkBitmap(); |
| 379 if (icon_.empty()) | 361 if (icon_.empty()) |
| 380 icon_ = Extension::GetDefaultIcon(extension_->is_app()); | 362 icon_ = Extension::GetDefaultIcon(extension_->is_app()); |
| 381 } | 363 } |
| 382 | 364 |
| 383 void ExtensionInstallPrompt::OnImageLoaded(const gfx::Image& image, | 365 void ExtensionInstallPrompt::OnImageLoaded(const gfx::Image& image, |
| 384 const std::string& extension_id, | 366 const std::string& extension_id, |
| 385 int index) { | 367 int index) { |
| 386 SetIcon(image.IsEmpty() ? NULL : image.ToSkBitmap()); | 368 SetIcon(image.IsEmpty() ? NULL : image.ToSkBitmap()); |
| 387 FetchOAuthIssueAdviceIfNeeded(); | 369 ShowConfirmation(); |
| 388 } | 370 } |
| 389 | 371 |
| 390 void ExtensionInstallPrompt::LoadImageIfNeeded() { | 372 void ExtensionInstallPrompt::LoadImageIfNeeded() { |
| 391 // Bundle install prompts do not have an icon. | 373 // Bundle install prompts do not have an icon. |
| 392 if (!icon_.empty()) { | 374 if (!icon_.empty()) { |
| 393 FetchOAuthIssueAdviceIfNeeded(); | 375 ShowConfirmation(); |
| 394 return; | 376 return; |
| 395 } | 377 } |
| 396 | 378 |
| 397 // Load the image asynchronously. For the response, check OnImageLoaded. | 379 // Load the image asynchronously. For the response, check OnImageLoaded. |
| 398 ExtensionResource image = | 380 ExtensionResource image = |
| 399 extension_->GetIconResource(ExtensionIconSet::EXTENSION_ICON_LARGE, | 381 extension_->GetIconResource(ExtensionIconSet::EXTENSION_ICON_LARGE, |
| 400 ExtensionIconSet::MATCH_BIGGER); | 382 ExtensionIconSet::MATCH_BIGGER); |
| 401 tracker_.LoadImage(extension_, image, | 383 tracker_.LoadImage(extension_, image, |
| 402 gfx::Size(kIconSize, kIconSize), | 384 gfx::Size(kIconSize, kIconSize), |
| 403 ImageLoadingTracker::DONT_CACHE); | 385 ImageLoadingTracker::DONT_CACHE); |
| 404 } | 386 } |
| 405 | 387 |
| 406 void ExtensionInstallPrompt::FetchOAuthIssueAdviceIfNeeded() { | |
| 407 const Extension::OAuth2Info& oauth2_info = extension_->oauth2_info(); | |
| 408 if (ShouldAutomaticallyApproveScopes() || | |
| 409 oauth2_info.client_id.empty() || | |
| 410 permissions_->scopes().empty() || | |
| 411 prompt_type_ == BUNDLE_INSTALL_PROMPT || | |
| 412 prompt_type_ == INLINE_INSTALL_PROMPT) { | |
| 413 ShowConfirmation(); | |
| 414 return; | |
| 415 } | |
| 416 | |
| 417 Profile* profile = install_ui_->browser()->profile(); | |
| 418 TokenService* token_service = TokenServiceFactory::GetForProfile(profile); | |
| 419 std::vector<std::string> scopes; | |
| 420 scopes.assign(permissions_->scopes().begin(), permissions_->scopes().end()); | |
| 421 | |
| 422 token_flow_.reset(new OAuth2MintTokenFlow( | |
| 423 profile->GetRequestContext(), | |
| 424 this, | |
| 425 OAuth2MintTokenFlow::Parameters( | |
| 426 token_service->GetOAuth2LoginRefreshToken(), | |
| 427 extension_->id(), | |
| 428 oauth2_info.client_id, | |
| 429 scopes, | |
| 430 OAuth2MintTokenFlow::MODE_ISSUE_ADVICE))); | |
| 431 token_flow_->Start(); | |
| 432 } | |
| 433 | |
| 434 void ExtensionInstallPrompt::OnIssueAdviceSuccess( | |
| 435 const IssueAdviceInfo& advice_info) { | |
| 436 prompt_.SetOAuthIssueAdvice(advice_info); | |
| 437 record_oauth2_grant_ = true; | |
| 438 ShowConfirmation(); | |
| 439 } | |
| 440 | |
| 441 void ExtensionInstallPrompt::OnMintTokenFailure( | |
| 442 const GoogleServiceAuthError& error) { | |
| 443 ShowConfirmation(); | |
| 444 } | |
| 445 | |
| 446 void ExtensionInstallPrompt::ShowConfirmation() { | 388 void ExtensionInstallPrompt::ShowConfirmation() { |
| 447 prompt_.set_type(prompt_type_); | 389 prompt_.set_type(prompt_type_); |
| 448 prompt_.SetPermissions(permissions_->GetWarningMessages()); | 390 prompt_.SetPermissions(permissions_->GetWarningMessages()); |
| 449 | 391 |
| 450 switch (prompt_type_) { | 392 switch (prompt_type_) { |
| 451 case PERMISSIONS_PROMPT: | 393 case PERMISSIONS_PROMPT: |
| 452 case RE_ENABLE_PROMPT: | 394 case RE_ENABLE_PROMPT: |
| 453 case INLINE_INSTALL_PROMPT: | 395 case INLINE_INSTALL_PROMPT: |
| 454 case INSTALL_PROMPT: { | 396 case INSTALL_PROMPT: { |
| 455 prompt_.set_extension(extension_); | 397 prompt_.set_extension(extension_); |
| 456 prompt_.set_icon(gfx::Image(icon_)); | 398 prompt_.set_icon(gfx::Image(icon_)); |
| 457 ShowExtensionInstallDialog(browser_, delegate_, prompt_); | 399 ShowExtensionInstallDialog(browser_, delegate_, prompt_); |
| 458 break; | 400 break; |
| 459 } | 401 } |
| 460 case BUNDLE_INSTALL_PROMPT: { | 402 case BUNDLE_INSTALL_PROMPT: { |
| 461 prompt_.set_bundle(bundle_); | 403 prompt_.set_bundle(bundle_); |
| 462 ShowExtensionInstallDialog(browser_, delegate_, prompt_); | 404 ShowExtensionInstallDialog(browser_, delegate_, prompt_); |
| 463 break; | 405 break; |
| 464 } | 406 } |
| 465 default: | 407 default: |
| 466 NOTREACHED() << "Unknown message"; | 408 NOTREACHED() << "Unknown message"; |
| 467 break; | 409 break; |
| 468 } | 410 } |
| 469 } | 411 } |
| 470 | |
| 471 // static | |
| 472 bool ExtensionInstallPrompt::ShouldAutomaticallyApproveScopes() { | |
| 473 return !CommandLine::ForCurrentProcess()->HasSwitch( | |
| 474 switches::kDemandUserScopeApproval); | |
| 475 } | |
| OLD | NEW |