| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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 #import "ios/chrome/browser/web/blocked_popup_handler.h" | 5 #import "ios/chrome/browser/web/blocked_popup_tab_helper.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| 11 #include "base/mac/bundle_locations.h" | 11 #include "base/mac/bundle_locations.h" |
| 12 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
| 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 16 #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/confirm_infobar_delegate.h" |
| 17 #include "components/infobars/core/infobar.h" | 18 #include "components/infobars/core/infobar.h" |
| 18 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 19 #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/browser/content_settings/host_content_settings_map_factory.
h" |
| 21 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
| 20 #include "ios/chrome/grit/ios_strings.h" | 22 #include "ios/chrome/grit/ios_strings.h" |
| 21 #include "ios/web/public/referrer.h" | 23 #include "ios/web/public/referrer.h" |
| 22 #include "net/base/mac/url_conversions.h" | 24 #include "net/base/mac/url_conversions.h" |
| 23 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 25 | 27 |
| 28 DEFINE_WEB_STATE_USER_DATA_KEY(BlockedPopupTabHelper); |
| 29 |
| 26 namespace { | 30 namespace { |
| 27 // The infobar to display when a popup is blocked. | 31 // The infobar to display when a popup is blocked. |
| 28 class BlockPopupInfoBarDelegate : public ConfirmInfoBarDelegate { | 32 class BlockPopupInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 29 public: | 33 public: |
| 30 BlockPopupInfoBarDelegate(ios::ChromeBrowserState* browser_state, | 34 BlockPopupInfoBarDelegate(ios::ChromeBrowserState* browser_state, |
| 31 const std::vector<web::BlockedPopupInfo>& popups) | 35 const std::vector<web::BlockedPopupInfo>& popups) |
| 32 : browser_state_(browser_state), popups_(popups) {} | 36 : browser_state_(browser_state), popups_(popups) {} |
| 33 | 37 |
| 34 ~BlockPopupInfoBarDelegate() override {} | 38 ~BlockPopupInfoBarDelegate() override {} |
| 35 | 39 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 56 | 60 |
| 57 base::string16 GetButtonLabel(InfoBarButton button) const override { | 61 base::string16 GetButtonLabel(InfoBarButton button) const override { |
| 58 DCHECK(button == BUTTON_OK); | 62 DCHECK(button == BUTTON_OK); |
| 59 return l10n_util::GetStringUTF16(IDS_IOS_POPUPS_ALWAYS_SHOW_MOBILE); | 63 return l10n_util::GetStringUTF16(IDS_IOS_POPUPS_ALWAYS_SHOW_MOBILE); |
| 60 } | 64 } |
| 61 | 65 |
| 62 bool Accept() override { | 66 bool Accept() override { |
| 63 std::vector<web::BlockedPopupInfo>::iterator it; | 67 std::vector<web::BlockedPopupInfo>::iterator it; |
| 64 scoped_refptr<HostContentSettingsMap> host_content_map_settings( | 68 scoped_refptr<HostContentSettingsMap> host_content_map_settings( |
| 65 ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state_)); | 69 ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state_)); |
| 66 for (it = popups_.begin(); it != popups_.end(); ++it) { | 70 for (auto& it : popups_) { |
| 67 it->ShowPopup(); | 71 it.ShowPopup(); |
| 68 host_content_map_settings->SetContentSettingCustomScope( | 72 host_content_map_settings->SetContentSettingCustomScope( |
| 69 ContentSettingsPattern::FromURL(it->referrer().url), | 73 ContentSettingsPattern::FromURL(it.referrer().url), |
| 70 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS, | 74 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS, |
| 71 std::string(), CONTENT_SETTING_ALLOW); | 75 std::string(), CONTENT_SETTING_ALLOW); |
| 72 } | 76 } |
| 73 return true; | 77 return true; |
| 74 } | 78 } |
| 75 | 79 |
| 76 int GetButtons() const override { return BUTTON_OK; } | 80 int GetButtons() const override { return BUTTON_OK; } |
| 77 | 81 |
| 78 private: | 82 private: |
| 79 // The browser state to access user preferences. | 83 // The browser state to access user preferences. |
| 80 ios::ChromeBrowserState* browser_state_; | 84 ios::ChromeBrowserState* browser_state_; |
| 81 // The popups to open. | 85 // The popups to open. |
| 82 std::vector<web::BlockedPopupInfo> popups_; | 86 std::vector<web::BlockedPopupInfo> popups_; |
| 83 // The icon to display. | 87 // The icon to display. |
| 84 mutable gfx::Image icon_; | 88 mutable gfx::Image icon_; |
| 85 }; | 89 }; |
| 86 } // namespace | 90 } // namespace |
| 87 | 91 |
| 88 BlockedPopupHandler::BlockedPopupHandler(ios::ChromeBrowserState* browser_state) | 92 BlockedPopupTabHelper::BlockedPopupTabHelper(web::WebState* web_state) |
| 89 : browser_state_(browser_state), | 93 : web_state_(web_state), infobar_(nullptr), scoped_observer_(this) {} |
| 90 delegate_(nullptr), | |
| 91 infobar_(nullptr), | |
| 92 scoped_observer_(this) {} | |
| 93 | 94 |
| 94 BlockedPopupHandler::~BlockedPopupHandler() {} | 95 BlockedPopupTabHelper::~BlockedPopupTabHelper() = default; |
| 95 | 96 |
| 96 void BlockedPopupHandler::SetDelegate( | 97 void BlockedPopupTabHelper::HandlePopup( |
| 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) { | 98 const web::BlockedPopupInfo& blocked_popup_info) { |
| 106 if (!delegate_) | |
| 107 return; | |
| 108 | |
| 109 popups_.push_back(blocked_popup_info); | 99 popups_.push_back(blocked_popup_info); |
| 110 ShowInfoBar(); | 100 ShowInfoBar(); |
| 111 } | 101 } |
| 112 | 102 |
| 113 void BlockedPopupHandler::OnInfoBarRemoved(infobars::InfoBar* infobar, | 103 void BlockedPopupTabHelper::OnInfoBarRemoved(infobars::InfoBar* infobar, |
| 114 bool animate) { | 104 bool animate) { |
| 115 if (infobar == infobar_) { | 105 if (infobar == infobar_) { |
| 116 infobar_ = nullptr; | 106 infobar_ = nullptr; |
| 117 popups_.clear(); | 107 popups_.clear(); |
| 108 scoped_observer_.RemoveAll(); |
| 118 } | 109 } |
| 119 } | 110 } |
| 120 | 111 |
| 121 void BlockedPopupHandler::OnManagerShuttingDown( | 112 void BlockedPopupTabHelper::OnManagerShuttingDown( |
| 122 infobars::InfoBarManager* infobar_manager) { | 113 infobars::InfoBarManager* infobar_manager) { |
| 123 scoped_observer_.Remove(infobar_manager); | 114 scoped_observer_.Remove(infobar_manager); |
| 124 } | 115 } |
| 125 | 116 |
| 126 void BlockedPopupHandler::ShowInfoBar() { | 117 void BlockedPopupTabHelper::ShowInfoBar() { |
| 127 if (!popups_.size() || !delegate_) | 118 infobars::InfoBarManager* infobar_manager = |
| 119 InfoBarManagerImpl::FromWebState(web_state_); |
| 120 if (!popups_.size() || !infobar_manager) |
| 128 return; | 121 return; |
| 129 infobars::InfoBarManager* infobar_manager = [delegate_ infoBarManager]; | 122 |
| 123 RegisterAsInfoBarManagerObserverIfNeeded(infobar_manager); |
| 124 |
| 125 ios::ChromeBrowserState* browser_state = |
| 126 ios::ChromeBrowserState::FromBrowserState(web_state_->GetBrowserState()); |
| 130 std::unique_ptr<infobars::InfoBar> infobar = | 127 std::unique_ptr<infobars::InfoBar> infobar = |
| 131 infobar_manager->CreateConfirmInfoBar( | 128 infobar_manager->CreateConfirmInfoBar( |
| 132 std::unique_ptr<ConfirmInfoBarDelegate>( | 129 base::MakeUnique<BlockPopupInfoBarDelegate>(browser_state, popups_)); |
| 133 new BlockPopupInfoBarDelegate(browser_state_, popups_))); | |
| 134 if (infobar_) { | 130 if (infobar_) { |
| 135 infobar_ = infobar_manager->ReplaceInfoBar(infobar_, std::move(infobar)); | 131 infobar_ = infobar_manager->ReplaceInfoBar(infobar_, std::move(infobar)); |
| 136 } else { | 132 } else { |
| 137 infobar_ = infobar_manager->AddInfoBar(std::move(infobar)); | 133 infobar_ = infobar_manager->AddInfoBar(std::move(infobar)); |
| 138 } | 134 } |
| 139 } | 135 } |
| 136 |
| 137 void BlockedPopupTabHelper::RegisterAsInfoBarManagerObserverIfNeeded( |
| 138 infobars::InfoBarManager* infobar_manager) { |
| 139 DCHECK(infobar_manager); |
| 140 |
| 141 if (scoped_observer_.IsObserving(infobar_manager)) { |
| 142 return; |
| 143 } |
| 144 |
| 145 // Verify that this object is never observing more than one InfoBarManager. |
| 146 DCHECK(!scoped_observer_.IsObservingSources()); |
| 147 scoped_observer_.Add(infobar_manager); |
| 148 } |
| OLD | NEW |