OLD | NEW |
(Empty) | |
| 1 // Copyright 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 #import "ios/chrome/browser/web/blocked_popup_handler.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/format_macros.h" |
| 11 #include "base/mac/bundle_locations.h" |
| 12 #include "base/mac/foundation_util.h" |
| 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 16 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 17 #include "components/infobars/core/infobar.h" |
| 18 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 19 #include "ios/chrome/browser/content_settings/host_content_settings_map_factory.
h" |
| 20 #include "ios/chrome/grit/ios_strings.h" |
| 21 #include "ios/web/public/referrer.h" |
| 22 #include "net/base/mac/url_conversions.h" |
| 23 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/gfx/image/image.h" |
| 25 |
| 26 namespace { |
| 27 // The infobar to display when a popup is blocked. |
| 28 class BlockPopupInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 29 public: |
| 30 BlockPopupInfoBarDelegate(ios::ChromeBrowserState* browser_state, |
| 31 const std::vector<web::BlockedPopupInfo>& popups) |
| 32 : browser_state_(browser_state), popups_(popups) {} |
| 33 |
| 34 ~BlockPopupInfoBarDelegate() override {} |
| 35 |
| 36 InfoBarIdentifier GetIdentifier() const override { |
| 37 return POPUP_BLOCKED_INFOBAR_DELEGATE; |
| 38 } |
| 39 |
| 40 gfx::Image GetIcon() const override { |
| 41 if (icon_.IsEmpty()) { |
| 42 icon_ = gfx::Image([UIImage imageNamed:@"infobar_popup_blocker"]); |
| 43 } |
| 44 return icon_; |
| 45 } |
| 46 |
| 47 infobars::InfoBarDelegate::Type GetInfoBarType() const override { |
| 48 return WARNING_TYPE; |
| 49 } |
| 50 |
| 51 base::string16 GetMessageText() const override { |
| 52 return l10n_util::GetStringFUTF16( |
| 53 IDS_IOS_POPUPS_BLOCKED_MOBILE, |
| 54 base::UTF8ToUTF16(base::StringPrintf("%" PRIuS, popups_.size()))); |
| 55 } |
| 56 |
| 57 base::string16 GetButtonLabel(InfoBarButton button) const override { |
| 58 DCHECK(button == BUTTON_OK); |
| 59 return l10n_util::GetStringUTF16(IDS_IOS_POPUPS_ALWAYS_SHOW_MOBILE); |
| 60 } |
| 61 |
| 62 bool Accept() override { |
| 63 std::vector<web::BlockedPopupInfo>::iterator it; |
| 64 scoped_refptr<HostContentSettingsMap> host_content_map_settings( |
| 65 ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state_)); |
| 66 for (it = popups_.begin(); it != popups_.end(); ++it) { |
| 67 it->ShowPopup(); |
| 68 host_content_map_settings->SetContentSettingCustomScope( |
| 69 ContentSettingsPattern::FromURL(it->referrer().url), |
| 70 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS, |
| 71 std::string(), CONTENT_SETTING_ALLOW); |
| 72 } |
| 73 return true; |
| 74 } |
| 75 |
| 76 int GetButtons() const override { return BUTTON_OK; } |
| 77 |
| 78 private: |
| 79 // The browser state to access user preferences. |
| 80 ios::ChromeBrowserState* browser_state_; |
| 81 // The popups to open. |
| 82 std::vector<web::BlockedPopupInfo> popups_; |
| 83 // The icon to display. |
| 84 mutable gfx::Image icon_; |
| 85 }; |
| 86 } // namespace |
| 87 |
| 88 BlockedPopupHandler::BlockedPopupHandler(ios::ChromeBrowserState* browser_state) |
| 89 : browser_state_(browser_state), |
| 90 delegate_(nullptr), |
| 91 infobar_(nullptr), |
| 92 scoped_observer_(this) {} |
| 93 |
| 94 BlockedPopupHandler::~BlockedPopupHandler() {} |
| 95 |
| 96 void BlockedPopupHandler::SetDelegate( |
| 97 id<BlockedPopupHandlerDelegate> delegate) { |
| 98 scoped_observer_.RemoveAll(); |
| 99 delegate_ = delegate; |
| 100 if (delegate_) |
| 101 scoped_observer_.Add([delegate_ infoBarManager]); |
| 102 } |
| 103 |
| 104 void BlockedPopupHandler::HandlePopup( |
| 105 const web::BlockedPopupInfo& blocked_popup_info) { |
| 106 if (!delegate_) |
| 107 return; |
| 108 |
| 109 popups_.push_back(blocked_popup_info); |
| 110 ShowInfoBar(); |
| 111 } |
| 112 |
| 113 void BlockedPopupHandler::OnInfoBarRemoved(infobars::InfoBar* infobar, |
| 114 bool animate) { |
| 115 if (infobar == infobar_) { |
| 116 infobar_ = nullptr; |
| 117 popups_.clear(); |
| 118 } |
| 119 } |
| 120 |
| 121 void BlockedPopupHandler::OnManagerShuttingDown( |
| 122 infobars::InfoBarManager* infobar_manager) { |
| 123 scoped_observer_.Remove(infobar_manager); |
| 124 } |
| 125 |
| 126 void BlockedPopupHandler::ShowInfoBar() { |
| 127 if (!popups_.size() || !delegate_) |
| 128 return; |
| 129 infobars::InfoBarManager* infobar_manager = [delegate_ infoBarManager]; |
| 130 std::unique_ptr<infobars::InfoBar> infobar = |
| 131 infobar_manager->CreateConfirmInfoBar( |
| 132 std::unique_ptr<ConfirmInfoBarDelegate>( |
| 133 new BlockPopupInfoBarDelegate(browser_state_, popups_))); |
| 134 if (infobar_) { |
| 135 infobar_ = infobar_manager->ReplaceInfoBar(infobar_, std::move(infobar)); |
| 136 } else { |
| 137 infobar_ = infobar_manager->AddInfoBar(std::move(infobar)); |
| 138 } |
| 139 } |
OLD | NEW |