popup_blocked_infobar_delegate.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2013 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. #include "components/blocked_content/android/popup_blocked_infobar_delegate.h"
  5. #include <stddef.h>
  6. #include <utility>
  7. #include "components/blocked_content/android/popup_blocked_helper.h"
  8. #include "components/content_settings/core/browser/host_content_settings_map.h"
  9. #include "components/content_settings/core/common/content_settings.h"
  10. #include "components/content_settings/core/common/content_settings_types.h"
  11. #include "components/infobars/android/confirm_infobar.h"
  12. #include "components/infobars/content/content_infobar_manager.h"
  13. #include "components/infobars/core/infobar.h"
  14. #include "components/prefs/pref_service.h"
  15. #include "components/resources/android/theme_resources.h"
  16. #include "components/strings/grit/components_strings.h"
  17. #include "ui/base/l10n/l10n_util.h"
  18. namespace blocked_content {
  19. // static
  20. bool PopupBlockedInfoBarDelegate::Create(
  21. infobars::ContentInfoBarManager* infobar_manager,
  22. int num_popups,
  23. HostContentSettingsMap* settings_map,
  24. base::OnceClosure on_accept_callback) {
  25. const GURL& url = infobar_manager->web_contents()->GetURL();
  26. auto infobar = std::make_unique<infobars::ConfirmInfoBar>(
  27. base::WrapUnique<PopupBlockedInfoBarDelegate>(
  28. new PopupBlockedInfoBarDelegate(num_popups, url, settings_map,
  29. std::move(on_accept_callback))));
  30. // See if there is an existing popup infobar already.
  31. // TODO(dfalcantara) When triggering more than one popup the infobar
  32. // will be shown once, then hide then be shown again.
  33. // This will be fixed once we have an in place replace infobar mechanism.
  34. for (size_t i = 0; i < infobar_manager->infobar_count(); ++i) {
  35. infobars::InfoBar* existing_infobar = infobar_manager->infobar_at(i);
  36. if (existing_infobar->delegate()->AsPopupBlockedInfoBarDelegate()) {
  37. infobar_manager->ReplaceInfoBar(existing_infobar, std::move(infobar));
  38. return false;
  39. }
  40. }
  41. infobar_manager->AddInfoBar(std::move(infobar));
  42. return true;
  43. }
  44. PopupBlockedInfoBarDelegate::~PopupBlockedInfoBarDelegate() = default;
  45. infobars::InfoBarDelegate::InfoBarIdentifier
  46. PopupBlockedInfoBarDelegate::GetIdentifier() const {
  47. return POPUP_BLOCKED_INFOBAR_DELEGATE_MOBILE;
  48. }
  49. int PopupBlockedInfoBarDelegate::GetIconId() const {
  50. return IDR_ANDROID_INFOBAR_BLOCKED_POPUPS;
  51. }
  52. PopupBlockedInfoBarDelegate*
  53. PopupBlockedInfoBarDelegate::AsPopupBlockedInfoBarDelegate() {
  54. return this;
  55. }
  56. PopupBlockedInfoBarDelegate::PopupBlockedInfoBarDelegate(
  57. int num_popups,
  58. const GURL& url,
  59. HostContentSettingsMap* map,
  60. base::OnceClosure on_accept_callback)
  61. : ConfirmInfoBarDelegate(),
  62. num_popups_(num_popups),
  63. url_(url),
  64. map_(map),
  65. on_accept_callback_(std::move(on_accept_callback)) {
  66. can_show_popups_ = !PopupSettingManagedByPolicy(map, url);
  67. }
  68. std::u16string PopupBlockedInfoBarDelegate::GetMessageText() const {
  69. return l10n_util::GetPluralStringFUTF16(IDS_POPUPS_BLOCKED_INFOBAR_TEXT,
  70. num_popups_);
  71. }
  72. int PopupBlockedInfoBarDelegate::GetButtons() const {
  73. if (!can_show_popups_)
  74. return 0;
  75. int buttons = BUTTON_OK;
  76. return buttons;
  77. }
  78. std::u16string PopupBlockedInfoBarDelegate::GetButtonLabel(
  79. InfoBarButton button) const {
  80. switch (button) {
  81. case BUTTON_OK:
  82. return l10n_util::GetStringUTF16(IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW);
  83. case BUTTON_CANCEL:
  84. return l10n_util::GetStringUTF16(IDS_PERMISSION_DENY);
  85. default:
  86. NOTREACHED();
  87. break;
  88. }
  89. return std::u16string();
  90. }
  91. bool PopupBlockedInfoBarDelegate::Accept() {
  92. DCHECK(can_show_popups_);
  93. // Create exceptions.
  94. map_->SetNarrowestContentSetting(url_, url_, ContentSettingsType::POPUPS,
  95. CONTENT_SETTING_ALLOW);
  96. // Launch popups.
  97. content::WebContents* web_contents =
  98. infobars::ContentInfoBarManager::WebContentsFromInfoBar(infobar());
  99. ShowBlockedPopups(web_contents);
  100. if (on_accept_callback_)
  101. std::move(on_accept_callback_).Run();
  102. return true;
  103. }
  104. } // namespace blocked_content