Chromium Code Reviews| 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 71% |
| rename from ios/chrome/browser/web/blocked_popup_handler.mm |
| rename to ios/chrome/browser/web/blocked_popup_tab_helper.mm |
| index 40c6b71212cced9327d62f0164fdc50de0c0997d..1723a22feb82e43fad231db8ae77185588ccf249 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> |
| @@ -17,12 +17,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 { |
| @@ -85,55 +88,61 @@ 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() {} |
|
Eugene But (OOO till 7-30)
2017/01/17 22:10:21
nit:
BlockedPopupTabHelper::~BlockedPopupTabHelp
rohitrao (ping after 24h)
2017/01/23 15:30:29
Is it better to use default rather than {}?
|
| -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(); |
|
sdefresne
2017/01/18 10:20:56
nit: you can stop observing at that point, can't y
rohitrao (ping after 24h)
2017/01/23 15:30:29
Done.
|
| } |
| } |
| -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(); |
|
sdefresne
2017/01/18 10:20:57
nit: maybe pass the infobar manager as parameter (
rohitrao (ping after 24h)
2017/01/23 15:30:29
Done.
|
| + |
| + ios::ChromeBrowserState* browser_state = |
| + ios::ChromeBrowserState::FromBrowserState(web_state_->GetBrowserState()); |
| std::unique_ptr<infobars::InfoBar> infobar = |
| infobar_manager->CreateConfirmInfoBar( |
| std::unique_ptr<ConfirmInfoBarDelegate>( |
|
sdefresne
2017/01/18 10:20:57
nit: I think you can use base::MakeUnique here
rohitrao (ping after 24h)
2017/01/23 15:30:29
Done.
|
| - new BlockPopupInfoBarDelegate(browser_state_, popups_))); |
| + new 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* manager = |
| + InfoBarManagerImpl::FromWebState(web_state_); |
| + DCHECK(manager); |
| + |
| + if (scoped_observer_.IsObserving(manager)) { |
| + return; |
| + } |
| + |
| + // Verify that this object is never observing more than one InfoBarManager. |
| + DCHECK(!scoped_observer_.IsObservingSources()); |
| + scoped_observer_.Add(manager); |
| +} |