| Index: ios/chrome/browser/web/blocked_popup_tab_helper.mm
|
| diff --git a/ios/chrome/browser/web/blocked_popup_handler.mm b/ios/chrome/browser/web/blocked_popup_tab_helper.mm
|
| similarity index 67%
|
| rename from ios/chrome/browser/web/blocked_popup_handler.mm
|
| rename to ios/chrome/browser/web/blocked_popup_tab_helper.mm
|
| index 40c6b71212cced9327d62f0164fdc50de0c0997d..4f5aacbb845d81a49782b16c252656c134c385c4 100644
|
| --- a/ios/chrome/browser/web/blocked_popup_handler.mm
|
| +++ b/ios/chrome/browser/web/blocked_popup_tab_helper.mm
|
| @@ -2,7 +2,7 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#import "ios/chrome/browser/web/blocked_popup_handler.h"
|
| +#import "ios/chrome/browser/web/blocked_popup_tab_helper.h"
|
|
|
| #include <memory>
|
| #include <utility>
|
| @@ -10,6 +10,7 @@
|
| #include "base/format_macros.h"
|
| #include "base/mac/bundle_locations.h"
|
| #include "base/mac/foundation_util.h"
|
| +#include "base/memory/ptr_util.h"
|
| #include "base/strings/stringprintf.h"
|
| #include "base/strings/utf_string_conversions.h"
|
| #include "components/content_settings/core/browser/host_content_settings_map.h"
|
| @@ -17,12 +18,15 @@
|
| #include "components/infobars/core/infobar.h"
|
| #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
|
| #include "ios/chrome/browser/content_settings/host_content_settings_map_factory.h"
|
| +#include "ios/chrome/browser/infobars/infobar_manager_impl.h"
|
| #include "ios/chrome/grit/ios_strings.h"
|
| #include "ios/web/public/referrer.h"
|
| #include "net/base/mac/url_conversions.h"
|
| #include "ui/base/l10n/l10n_util.h"
|
| #include "ui/gfx/image/image.h"
|
|
|
| +DEFINE_WEB_STATE_USER_DATA_KEY(BlockedPopupTabHelper);
|
| +
|
| namespace {
|
| // The infobar to display when a popup is blocked.
|
| class BlockPopupInfoBarDelegate : public ConfirmInfoBarDelegate {
|
| @@ -63,10 +67,10 @@ class BlockPopupInfoBarDelegate : public ConfirmInfoBarDelegate {
|
| std::vector<web::BlockedPopupInfo>::iterator it;
|
| scoped_refptr<HostContentSettingsMap> host_content_map_settings(
|
| ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state_));
|
| - for (it = popups_.begin(); it != popups_.end(); ++it) {
|
| - it->ShowPopup();
|
| + for (auto& it : popups_) {
|
| + it.ShowPopup();
|
| host_content_map_settings->SetContentSettingCustomScope(
|
| - ContentSettingsPattern::FromURL(it->referrer().url),
|
| + ContentSettingsPattern::FromURL(it.referrer().url),
|
| ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS,
|
| std::string(), CONTENT_SETTING_ALLOW);
|
| }
|
| @@ -85,55 +89,60 @@ class BlockPopupInfoBarDelegate : public ConfirmInfoBarDelegate {
|
| };
|
| } // namespace
|
|
|
| -BlockedPopupHandler::BlockedPopupHandler(ios::ChromeBrowserState* browser_state)
|
| - : browser_state_(browser_state),
|
| - delegate_(nullptr),
|
| - infobar_(nullptr),
|
| - scoped_observer_(this) {}
|
| -
|
| -BlockedPopupHandler::~BlockedPopupHandler() {}
|
| +BlockedPopupTabHelper::BlockedPopupTabHelper(web::WebState* web_state)
|
| + : web_state_(web_state), infobar_(nullptr), scoped_observer_(this) {}
|
|
|
| -void BlockedPopupHandler::SetDelegate(
|
| - id<BlockedPopupHandlerDelegate> delegate) {
|
| - scoped_observer_.RemoveAll();
|
| - delegate_ = delegate;
|
| - if (delegate_)
|
| - scoped_observer_.Add([delegate_ infoBarManager]);
|
| -}
|
| +BlockedPopupTabHelper::~BlockedPopupTabHelper() = default;
|
|
|
| -void BlockedPopupHandler::HandlePopup(
|
| +void BlockedPopupTabHelper::HandlePopup(
|
| const web::BlockedPopupInfo& blocked_popup_info) {
|
| - if (!delegate_)
|
| - return;
|
| -
|
| popups_.push_back(blocked_popup_info);
|
| ShowInfoBar();
|
| }
|
|
|
| -void BlockedPopupHandler::OnInfoBarRemoved(infobars::InfoBar* infobar,
|
| - bool animate) {
|
| +void BlockedPopupTabHelper::OnInfoBarRemoved(infobars::InfoBar* infobar,
|
| + bool animate) {
|
| if (infobar == infobar_) {
|
| infobar_ = nullptr;
|
| popups_.clear();
|
| + scoped_observer_.RemoveAll();
|
| }
|
| }
|
|
|
| -void BlockedPopupHandler::OnManagerShuttingDown(
|
| +void BlockedPopupTabHelper::OnManagerShuttingDown(
|
| infobars::InfoBarManager* infobar_manager) {
|
| scoped_observer_.Remove(infobar_manager);
|
| }
|
|
|
| -void BlockedPopupHandler::ShowInfoBar() {
|
| - if (!popups_.size() || !delegate_)
|
| +void BlockedPopupTabHelper::ShowInfoBar() {
|
| + infobars::InfoBarManager* infobar_manager =
|
| + InfoBarManagerImpl::FromWebState(web_state_);
|
| + if (!popups_.size() || !infobar_manager)
|
| return;
|
| - infobars::InfoBarManager* infobar_manager = [delegate_ infoBarManager];
|
| +
|
| + RegisterAsInfoBarManagerObserverIfNeeded(infobar_manager);
|
| +
|
| + ios::ChromeBrowserState* browser_state =
|
| + ios::ChromeBrowserState::FromBrowserState(web_state_->GetBrowserState());
|
| std::unique_ptr<infobars::InfoBar> infobar =
|
| infobar_manager->CreateConfirmInfoBar(
|
| - std::unique_ptr<ConfirmInfoBarDelegate>(
|
| - new BlockPopupInfoBarDelegate(browser_state_, popups_)));
|
| + base::MakeUnique<BlockPopupInfoBarDelegate>(browser_state, popups_));
|
| if (infobar_) {
|
| infobar_ = infobar_manager->ReplaceInfoBar(infobar_, std::move(infobar));
|
| } else {
|
| infobar_ = infobar_manager->AddInfoBar(std::move(infobar));
|
| }
|
| }
|
| +
|
| +void BlockedPopupTabHelper::RegisterAsInfoBarManagerObserverIfNeeded(
|
| + infobars::InfoBarManager* infobar_manager) {
|
| + DCHECK(infobar_manager);
|
| +
|
| + if (scoped_observer_.IsObserving(infobar_manager)) {
|
| + return;
|
| + }
|
| +
|
| + // Verify that this object is never observing more than one InfoBarManager.
|
| + DCHECK(!scoped_observer_.IsObservingSources());
|
| + scoped_observer_.Add(infobar_manager);
|
| +}
|
|
|