Chromium Code Reviews| 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_tab_helper.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" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 26 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 27 | 27 |
| 28 DEFINE_WEB_STATE_USER_DATA_KEY(BlockedPopupTabHelper); | 28 DEFINE_WEB_STATE_USER_DATA_KEY(BlockedPopupTabHelper); |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 // The infobar to display when a popup is blocked. | 31 // The infobar to display when a popup is blocked. |
| 32 class BlockPopupInfoBarDelegate : public ConfirmInfoBarDelegate { | 32 class BlockPopupInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 33 public: | 33 public: |
| 34 BlockPopupInfoBarDelegate(ios::ChromeBrowserState* browser_state, | 34 BlockPopupInfoBarDelegate(ios::ChromeBrowserState* browser_state, |
| 35 web::WebState* web_state, | |
| 35 const std::vector<web::BlockedPopupInfo>& popups) | 36 const std::vector<web::BlockedPopupInfo>& popups) |
| 36 : browser_state_(browser_state), popups_(popups) {} | 37 : browser_state_(browser_state), web_state_(web_state), popups_(popups) {} |
| 37 | 38 |
| 38 ~BlockPopupInfoBarDelegate() override {} | 39 ~BlockPopupInfoBarDelegate() override {} |
| 39 | 40 |
| 40 InfoBarIdentifier GetIdentifier() const override { | 41 InfoBarIdentifier GetIdentifier() const override { |
| 41 return POPUP_BLOCKED_INFOBAR_DELEGATE; | 42 return POPUP_BLOCKED_INFOBAR_DELEGATE; |
| 42 } | 43 } |
| 43 | 44 |
| 44 gfx::Image GetIcon() const override { | 45 gfx::Image GetIcon() const override { |
| 45 if (icon_.IsEmpty()) { | 46 if (icon_.IsEmpty()) { |
| 46 icon_ = gfx::Image([UIImage imageNamed:@"infobar_popup_blocker"]); | 47 icon_ = gfx::Image([UIImage imageNamed:@"infobar_popup_blocker"]); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 60 | 61 |
| 61 base::string16 GetButtonLabel(InfoBarButton button) const override { | 62 base::string16 GetButtonLabel(InfoBarButton button) const override { |
| 62 DCHECK(button == BUTTON_OK); | 63 DCHECK(button == BUTTON_OK); |
| 63 return l10n_util::GetStringUTF16(IDS_IOS_POPUPS_ALWAYS_SHOW_MOBILE); | 64 return l10n_util::GetStringUTF16(IDS_IOS_POPUPS_ALWAYS_SHOW_MOBILE); |
| 64 } | 65 } |
| 65 | 66 |
| 66 bool Accept() override { | 67 bool Accept() override { |
| 67 std::vector<web::BlockedPopupInfo>::iterator it; | 68 std::vector<web::BlockedPopupInfo>::iterator it; |
| 68 scoped_refptr<HostContentSettingsMap> host_content_map_settings( | 69 scoped_refptr<HostContentSettingsMap> host_content_map_settings( |
| 69 ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state_)); | 70 ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state_)); |
| 70 for (auto& it : popups_) { | 71 for (auto& popup : popups_) { |
| 71 it.ShowPopup(); | 72 web::WebState::OpenURLParams params( |
| 73 popup.url(), popup.referrer(), WindowOpenDisposition::NEW_POPUP, | |
| 74 ui::PAGE_TRANSITION_LINK, true /* is_renderer_initiated */); | |
| 75 web_state_->OpenURL(params); | |
|
rohitrao (ping after 24h)
2017/02/28 22:51:57
Is there any way for this |web_state_| pointer to
Eugene But (OOO till 7-30)
2017/02/28 23:50:22
Correct, the answer is no. I tested this in debugg
| |
| 72 host_content_map_settings->SetContentSettingCustomScope( | 76 host_content_map_settings->SetContentSettingCustomScope( |
| 73 ContentSettingsPattern::FromURL(it.referrer().url), | 77 ContentSettingsPattern::FromURL(popup.referrer().url), |
| 74 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS, | 78 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS, |
| 75 std::string(), CONTENT_SETTING_ALLOW); | 79 std::string(), CONTENT_SETTING_ALLOW); |
| 76 } | 80 } |
| 77 return true; | 81 return true; |
| 78 } | 82 } |
| 79 | 83 |
| 80 int GetButtons() const override { return BUTTON_OK; } | 84 int GetButtons() const override { return BUTTON_OK; } |
| 81 | 85 |
| 82 private: | 86 private: |
| 83 // The browser state to access user preferences. | |
| 84 ios::ChromeBrowserState* browser_state_; | 87 ios::ChromeBrowserState* browser_state_; |
| 88 web::WebState* web_state_; | |
| 85 // The popups to open. | 89 // The popups to open. |
| 86 std::vector<web::BlockedPopupInfo> popups_; | 90 std::vector<web::BlockedPopupInfo> popups_; |
| 87 // The icon to display. | 91 // The icon to display. |
| 88 mutable gfx::Image icon_; | 92 mutable gfx::Image icon_; |
| 89 }; | 93 }; |
| 90 } // namespace | 94 } // namespace |
| 91 | 95 |
| 92 BlockedPopupTabHelper::BlockedPopupTabHelper(web::WebState* web_state) | 96 BlockedPopupTabHelper::BlockedPopupTabHelper(web::WebState* web_state) |
| 93 : web_state_(web_state), infobar_(nullptr), scoped_observer_(this) {} | 97 : web_state_(web_state), infobar_(nullptr), scoped_observer_(this) {} |
| 94 | 98 |
| 95 BlockedPopupTabHelper::~BlockedPopupTabHelper() = default; | 99 BlockedPopupTabHelper::~BlockedPopupTabHelper() = default; |
| 96 | 100 |
| 97 bool BlockedPopupTabHelper::ShouldBlockPopup(const GURL& source_url) { | 101 bool BlockedPopupTabHelper::ShouldBlockPopup(const GURL& source_url) { |
| 98 HostContentSettingsMap* settings_map = | 102 HostContentSettingsMap* settings_map = |
| 99 ios::HostContentSettingsMapFactory::GetForBrowserState(GetBrowserState()); | 103 ios::HostContentSettingsMapFactory::GetForBrowserState(GetBrowserState()); |
| 100 ContentSetting setting = settings_map->GetContentSetting( | 104 ContentSetting setting = settings_map->GetContentSetting( |
| 101 source_url, source_url, CONTENT_SETTINGS_TYPE_POPUPS, std::string()); | 105 source_url, source_url, CONTENT_SETTINGS_TYPE_POPUPS, std::string()); |
| 102 return setting != CONTENT_SETTING_ALLOW; | 106 return setting != CONTENT_SETTING_ALLOW; |
| 103 } | 107 } |
| 104 | 108 |
| 105 void BlockedPopupTabHelper::HandlePopup( | 109 void BlockedPopupTabHelper::HandlePopup( |
| 106 const web::BlockedPopupInfo& blocked_popup_info) { | 110 const web::BlockedPopupInfo& blocked_popup_info) { |
| 111 DCHECK(ShouldBlockPopup(blocked_popup_info.referrer().url)); | |
| 107 popups_.push_back(blocked_popup_info); | 112 popups_.push_back(blocked_popup_info); |
| 108 ShowInfoBar(); | 113 ShowInfoBar(); |
| 109 } | 114 } |
| 110 | 115 |
| 111 void BlockedPopupTabHelper::OnInfoBarRemoved(infobars::InfoBar* infobar, | 116 void BlockedPopupTabHelper::OnInfoBarRemoved(infobars::InfoBar* infobar, |
| 112 bool animate) { | 117 bool animate) { |
| 113 if (infobar == infobar_) { | 118 if (infobar == infobar_) { |
| 114 infobar_ = nullptr; | 119 infobar_ = nullptr; |
| 115 popups_.clear(); | 120 popups_.clear(); |
| 116 scoped_observer_.RemoveAll(); | 121 scoped_observer_.RemoveAll(); |
| 117 } | 122 } |
| 118 } | 123 } |
| 119 | 124 |
| 120 void BlockedPopupTabHelper::OnManagerShuttingDown( | 125 void BlockedPopupTabHelper::OnManagerShuttingDown( |
| 121 infobars::InfoBarManager* infobar_manager) { | 126 infobars::InfoBarManager* infobar_manager) { |
| 122 scoped_observer_.Remove(infobar_manager); | 127 scoped_observer_.Remove(infobar_manager); |
| 123 } | 128 } |
| 124 | 129 |
| 125 void BlockedPopupTabHelper::ShowInfoBar() { | 130 void BlockedPopupTabHelper::ShowInfoBar() { |
| 126 infobars::InfoBarManager* infobar_manager = | 131 infobars::InfoBarManager* infobar_manager = |
| 127 InfoBarManagerImpl::FromWebState(web_state_); | 132 InfoBarManagerImpl::FromWebState(web_state_); |
| 128 if (!popups_.size() || !infobar_manager) | 133 if (!popups_.size() || !infobar_manager) |
| 129 return; | 134 return; |
| 130 | 135 |
| 131 RegisterAsInfoBarManagerObserverIfNeeded(infobar_manager); | 136 RegisterAsInfoBarManagerObserverIfNeeded(infobar_manager); |
| 132 | 137 |
| 133 std::unique_ptr<BlockPopupInfoBarDelegate> delegate( | 138 std::unique_ptr<BlockPopupInfoBarDelegate> delegate( |
| 134 base::MakeUnique<BlockPopupInfoBarDelegate>(GetBrowserState(), popups_)); | 139 base::MakeUnique<BlockPopupInfoBarDelegate>(GetBrowserState(), web_state_, |
| 140 popups_)); | |
| 135 std::unique_ptr<infobars::InfoBar> infobar = | 141 std::unique_ptr<infobars::InfoBar> infobar = |
| 136 infobar_manager->CreateConfirmInfoBar(std::move(delegate)); | 142 infobar_manager->CreateConfirmInfoBar(std::move(delegate)); |
| 137 if (infobar_) { | 143 if (infobar_) { |
| 138 infobar_ = infobar_manager->ReplaceInfoBar(infobar_, std::move(infobar)); | 144 infobar_ = infobar_manager->ReplaceInfoBar(infobar_, std::move(infobar)); |
| 139 } else { | 145 } else { |
| 140 infobar_ = infobar_manager->AddInfoBar(std::move(infobar)); | 146 infobar_ = infobar_manager->AddInfoBar(std::move(infobar)); |
| 141 } | 147 } |
| 142 } | 148 } |
| 143 | 149 |
| 144 ios::ChromeBrowserState* BlockedPopupTabHelper::GetBrowserState() const { | 150 ios::ChromeBrowserState* BlockedPopupTabHelper::GetBrowserState() const { |
| 145 return ios::ChromeBrowserState::FromBrowserState( | 151 return ios::ChromeBrowserState::FromBrowserState( |
| 146 web_state_->GetBrowserState()); | 152 web_state_->GetBrowserState()); |
| 147 } | 153 } |
| 148 | 154 |
| 149 void BlockedPopupTabHelper::RegisterAsInfoBarManagerObserverIfNeeded( | 155 void BlockedPopupTabHelper::RegisterAsInfoBarManagerObserverIfNeeded( |
| 150 infobars::InfoBarManager* infobar_manager) { | 156 infobars::InfoBarManager* infobar_manager) { |
| 151 DCHECK(infobar_manager); | 157 DCHECK(infobar_manager); |
| 152 | 158 |
| 153 if (scoped_observer_.IsObserving(infobar_manager)) { | 159 if (scoped_observer_.IsObserving(infobar_manager)) { |
| 154 return; | 160 return; |
| 155 } | 161 } |
| 156 | 162 |
| 157 // Verify that this object is never observing more than one InfoBarManager. | 163 // Verify that this object is never observing more than one InfoBarManager. |
| 158 DCHECK(!scoped_observer_.IsObservingSources()); | 164 DCHECK(!scoped_observer_.IsObservingSources()); |
| 159 scoped_observer_.Add(infobar_manager); | 165 scoped_observer_.Add(infobar_manager); |
| 160 } | 166 } |
| OLD | NEW |