| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/callback_helpers.h" |
| 9 #include "base/location.h" | 10 #include "base/location.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 14 #include "chrome/browser/extensions/bundle_installer.h" | 15 #include "chrome/browser/extensions/bundle_installer.h" |
| 15 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" | 16 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" |
| 16 #include "chrome/browser/extensions/extension_util.h" | 17 #include "chrome/browser/extensions/extension_util.h" |
| 17 #include "chrome/browser/extensions/permissions_updater.h" | 18 #include "chrome/browser/extensions/permissions_updater.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 SkBitmap GetDefaultIconBitmapForMaxScaleFactor(bool is_app) { | 140 SkBitmap GetDefaultIconBitmapForMaxScaleFactor(bool is_app) { |
| 140 const gfx::ImageSkia& image = is_app ? | 141 const gfx::ImageSkia& image = is_app ? |
| 141 extensions::util::GetDefaultAppIcon() : | 142 extensions::util::GetDefaultAppIcon() : |
| 142 extensions::util::GetDefaultExtensionIcon(); | 143 extensions::util::GetDefaultExtensionIcon(); |
| 143 return image.GetRepresentation( | 144 return image.GetRepresentation( |
| 144 gfx::ImageSkia::GetMaxSupportedScale()).sk_bitmap(); | 145 gfx::ImageSkia::GetMaxSupportedScale()).sk_bitmap(); |
| 145 } | 146 } |
| 146 | 147 |
| 147 // If auto confirm is enabled then posts a task to proceed with or cancel the | 148 // If auto confirm is enabled then posts a task to proceed with or cancel the |
| 148 // install and returns true. Otherwise returns false. | 149 // install and returns true. Otherwise returns false. |
| 149 bool AutoConfirmPrompt(ExtensionInstallPrompt::Delegate* delegate) { | 150 bool AutoConfirmPrompt(ExtensionInstallPrompt::DoneCallback* callback) { |
| 150 switch (extensions::ScopedTestDialogAutoConfirm::GetAutoConfirmValue()) { | 151 switch (extensions::ScopedTestDialogAutoConfirm::GetAutoConfirmValue()) { |
| 151 case extensions::ScopedTestDialogAutoConfirm::NONE: | 152 case extensions::ScopedTestDialogAutoConfirm::NONE: |
| 152 return false; | 153 return false; |
| 153 // We use PostTask instead of calling the delegate directly here, because in | 154 // We use PostTask instead of calling the callback directly here, because in |
| 154 // the real implementations it's highly likely the message loop will be | 155 // the real implementations it's highly likely the message loop will be |
| 155 // pumping a few times before the user clicks accept or cancel. | 156 // pumping a few times before the user clicks accept or cancel. |
| 156 case extensions::ScopedTestDialogAutoConfirm::ACCEPT: | 157 case extensions::ScopedTestDialogAutoConfirm::ACCEPT: |
| 157 base::MessageLoop::current()->PostTask( | 158 base::MessageLoop::current()->PostTask( |
| 158 FROM_HERE, | 159 FROM_HERE, base::Bind(base::ResetAndReturn(callback), |
| 159 base::Bind(&ExtensionInstallPrompt::Delegate::InstallUIProceed, | 160 ExtensionInstallPrompt::Result::ACCEPTED)); |
| 160 base::Unretained(delegate))); | |
| 161 return true; | 161 return true; |
| 162 case extensions::ScopedTestDialogAutoConfirm::CANCEL: | 162 case extensions::ScopedTestDialogAutoConfirm::CANCEL: |
| 163 base::ThreadTaskRunnerHandle::Get()->PostTask( | 163 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 164 FROM_HERE, | 164 FROM_HERE, base::Bind(base::ResetAndReturn(callback), |
| 165 base::Bind(&ExtensionInstallPrompt::Delegate::InstallUIAbort, | 165 ExtensionInstallPrompt::Result::USER_CANCELED)); |
| 166 base::Unretained(delegate), | |
| 167 true)); | |
| 168 return true; | 166 return true; |
| 169 } | 167 } |
| 170 | 168 |
| 171 NOTREACHED(); | 169 NOTREACHED(); |
| 172 return false; | 170 return false; |
| 173 } | 171 } |
| 174 | 172 |
| 175 Profile* ProfileForWebContents(content::WebContents* web_contents) { | 173 Profile* ProfileForWebContents(content::WebContents* web_contents) { |
| 176 if (!web_contents) | 174 if (!web_contents) |
| 177 return NULL; | 175 return NULL; |
| (...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 error); | 615 error); |
| 618 } | 616 } |
| 619 | 617 |
| 620 ExtensionInstallPrompt::ExtensionInstallPrompt(content::WebContents* contents) | 618 ExtensionInstallPrompt::ExtensionInstallPrompt(content::WebContents* contents) |
| 621 : profile_(ProfileForWebContents(contents)), | 619 : profile_(ProfileForWebContents(contents)), |
| 622 ui_loop_(base::MessageLoop::current()), | 620 ui_loop_(base::MessageLoop::current()), |
| 623 extension_(NULL), | 621 extension_(NULL), |
| 624 install_ui_(extensions::CreateExtensionInstallUI( | 622 install_ui_(extensions::CreateExtensionInstallUI( |
| 625 ProfileForWebContents(contents))), | 623 ProfileForWebContents(contents))), |
| 626 show_params_(new ExtensionInstallPromptShowParams(contents)), | 624 show_params_(new ExtensionInstallPromptShowParams(contents)), |
| 627 delegate_(NULL), | |
| 628 did_call_show_dialog_(false) { | 625 did_call_show_dialog_(false) { |
| 629 } | 626 } |
| 630 | 627 |
| 631 ExtensionInstallPrompt::ExtensionInstallPrompt(Profile* profile, | 628 ExtensionInstallPrompt::ExtensionInstallPrompt(Profile* profile, |
| 632 gfx::NativeWindow native_window) | 629 gfx::NativeWindow native_window) |
| 633 : profile_(profile), | 630 : profile_(profile), |
| 634 ui_loop_(base::MessageLoop::current()), | 631 ui_loop_(base::MessageLoop::current()), |
| 635 extension_(NULL), | 632 extension_(NULL), |
| 636 install_ui_(extensions::CreateExtensionInstallUI(profile)), | 633 install_ui_(extensions::CreateExtensionInstallUI(profile)), |
| 637 show_params_( | 634 show_params_( |
| 638 new ExtensionInstallPromptShowParams(profile, native_window)), | 635 new ExtensionInstallPromptShowParams(profile, native_window)), |
| 639 delegate_(NULL), | |
| 640 did_call_show_dialog_(false) { | 636 did_call_show_dialog_(false) { |
| 641 } | 637 } |
| 642 | 638 |
| 643 ExtensionInstallPrompt::~ExtensionInstallPrompt() { | 639 ExtensionInstallPrompt::~ExtensionInstallPrompt() { |
| 644 } | 640 } |
| 645 | 641 |
| 646 void ExtensionInstallPrompt::ShowDialog( | 642 void ExtensionInstallPrompt::ShowDialog( |
| 647 Delegate* delegate, | 643 const DoneCallback& done_callback, |
| 648 const Extension* extension, | 644 const Extension* extension, |
| 649 const SkBitmap* icon, | 645 const SkBitmap* icon, |
| 650 const ShowDialogCallback& show_dialog_callback) { | 646 const ShowDialogCallback& show_dialog_callback) { |
| 651 ShowDialog(delegate, extension, icon, | 647 ShowDialog(done_callback, extension, icon, |
| 652 make_scoped_ptr(new Prompt(INSTALL_PROMPT)), show_dialog_callback); | 648 make_scoped_ptr(new Prompt(INSTALL_PROMPT)), show_dialog_callback); |
| 653 } | 649 } |
| 654 | 650 |
| 655 void ExtensionInstallPrompt::ShowDialog( | 651 void ExtensionInstallPrompt::ShowDialog( |
| 656 Delegate* delegate, | 652 const DoneCallback& done_callback, |
| 657 const Extension* extension, | 653 const Extension* extension, |
| 658 const SkBitmap* icon, | 654 const SkBitmap* icon, |
| 659 scoped_ptr<Prompt> prompt, | 655 scoped_ptr<Prompt> prompt, |
| 660 const ShowDialogCallback& show_dialog_callback) { | 656 const ShowDialogCallback& show_dialog_callback) { |
| 661 ShowDialog(delegate, extension, icon, std::move(prompt), nullptr, | 657 ShowDialog(done_callback, extension, icon, std::move(prompt), nullptr, |
| 662 show_dialog_callback); | 658 show_dialog_callback); |
| 663 } | 659 } |
| 664 | 660 |
| 665 void ExtensionInstallPrompt::ShowDialog( | 661 void ExtensionInstallPrompt::ShowDialog( |
| 666 Delegate* delegate, | 662 const DoneCallback& done_callback, |
| 667 const Extension* extension, | 663 const Extension* extension, |
| 668 const SkBitmap* icon, | 664 const SkBitmap* icon, |
| 669 scoped_ptr<Prompt> prompt, | 665 scoped_ptr<Prompt> prompt, |
| 670 scoped_ptr<const PermissionSet> custom_permissions, | 666 scoped_ptr<const PermissionSet> custom_permissions, |
| 671 const ShowDialogCallback& show_dialog_callback) { | 667 const ShowDialogCallback& show_dialog_callback) { |
| 672 DCHECK(ui_loop_ == base::MessageLoop::current()); | 668 DCHECK(ui_loop_ == base::MessageLoop::current()); |
| 673 DCHECK(prompt); | 669 DCHECK(prompt); |
| 674 extension_ = extension; | 670 extension_ = extension; |
| 675 delegate_ = delegate; | 671 done_callback_ = done_callback; |
| 676 if (icon && !icon->empty()) | 672 if (icon && !icon->empty()) |
| 677 SetIcon(icon); | 673 SetIcon(icon); |
| 678 prompt_ = std::move(prompt); | 674 prompt_ = std::move(prompt); |
| 679 custom_permissions_ = std::move(custom_permissions); | 675 custom_permissions_ = std::move(custom_permissions); |
| 680 show_dialog_callback_ = show_dialog_callback; | 676 show_dialog_callback_ = show_dialog_callback; |
| 681 | 677 |
| 682 // We special-case themes to not show any confirm UI. Instead they are | 678 // We special-case themes to not show any confirm UI. Instead they are |
| 683 // immediately installed, and then we show an infobar (see OnInstallSuccess) | 679 // immediately installed, and then we show an infobar (see OnInstallSuccess) |
| 684 // to allow the user to revert if they don't like it. | 680 // to allow the user to revert if they don't like it. |
| 685 // | 681 // |
| 686 // We don't do this in the case where off-store extension installs are | 682 // We don't do this in the case where off-store extension installs are |
| 687 // disabled because in that case, we don't show the dangerous download UI, so | 683 // disabled because in that case, we don't show the dangerous download UI, so |
| 688 // we need the UI confirmation. | 684 // we need the UI confirmation. |
| 689 if (extension->is_theme()) { | 685 if (extension->is_theme()) { |
| 690 if (extension->from_webstore() || | 686 if (extension->from_webstore() || |
| 691 extensions::FeatureSwitch::easy_off_store_install()->IsEnabled()) { | 687 extensions::FeatureSwitch::easy_off_store_install()->IsEnabled()) { |
| 692 delegate->InstallUIProceed(); | 688 base::ResetAndReturn(&done_callback_).Run(Result::ACCEPTED); |
| 693 return; | 689 return; |
| 694 } | 690 } |
| 695 } | 691 } |
| 696 | 692 |
| 697 LoadImageIfNeeded(); | 693 LoadImageIfNeeded(); |
| 698 } | 694 } |
| 699 | 695 |
| 700 void ExtensionInstallPrompt::OnInstallSuccess(const Extension* extension, | 696 void ExtensionInstallPrompt::OnInstallSuccess(const Extension* extension, |
| 701 SkBitmap* icon) { | 697 SkBitmap* icon) { |
| 702 extension_ = extension; | 698 extension_ = extension; |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 break; | 822 break; |
| 827 } | 823 } |
| 828 case LAUNCH_PROMPT_DEPRECATED: | 824 case LAUNCH_PROMPT_DEPRECATED: |
| 829 default: | 825 default: |
| 830 NOTREACHED() << "Unknown message"; | 826 NOTREACHED() << "Unknown message"; |
| 831 return; | 827 return; |
| 832 } | 828 } |
| 833 prompt_->set_icon(gfx::Image::CreateFrom1xBitmap(icon_)); | 829 prompt_->set_icon(gfx::Image::CreateFrom1xBitmap(icon_)); |
| 834 | 830 |
| 835 if (show_params_->WasParentDestroyed()) { | 831 if (show_params_->WasParentDestroyed()) { |
| 836 delegate_->InstallUIAbort(false); | 832 base::ResetAndReturn(&done_callback_).Run(Result::ABORTED); |
| 837 return; | 833 return; |
| 838 } | 834 } |
| 839 | 835 |
| 840 g_last_prompt_type_for_tests = prompt_->type(); | 836 g_last_prompt_type_for_tests = prompt_->type(); |
| 841 did_call_show_dialog_ = true; | 837 did_call_show_dialog_ = true; |
| 842 | 838 |
| 843 if (AutoConfirmPrompt(delegate_)) | 839 if (AutoConfirmPrompt(&done_callback_)) |
| 844 return; | 840 return; |
| 845 | 841 |
| 846 if (show_dialog_callback_.is_null()) | 842 if (show_dialog_callback_.is_null()) |
| 847 GetDefaultShowDialogCallback().Run(show_params_.get(), delegate_, | 843 show_dialog_callback_ = GetDefaultShowDialogCallback(); |
| 848 std::move(prompt_)); | 844 base::ResetAndReturn(&show_dialog_callback_) |
| 849 else | 845 .Run(show_params_.get(), base::ResetAndReturn(&done_callback_), |
| 850 show_dialog_callback_.Run(show_params_.get(), delegate_, | 846 std::move(prompt_)); |
| 851 std::move(prompt_)); | |
| 852 } | 847 } |
| OLD | NEW |