Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: ios/chrome/browser/web/blocked_popup_tab_helper.mm

Issue 2640463002: [ios] Convert BlockedPopupHandler into a tab helper class. (Closed)
Patch Set: Review. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h" 15 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "components/infobars/core/confirm_infobar_delegate.h" 16 #include "components/infobars/core/confirm_infobar_delegate.h"
17 #include "components/infobars/core/infobar.h" 17 #include "components/infobars/core/infobar.h"
18 #include "ios/chrome/browser/browser_state/chrome_browser_state.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" 19 #include "ios/chrome/browser/content_settings/host_content_settings_map_factory. h"
20 #include "ios/chrome/browser/infobars/infobar_manager_impl.h"
20 #include "ios/chrome/grit/ios_strings.h" 21 #include "ios/chrome/grit/ios_strings.h"
21 #include "ios/web/public/referrer.h" 22 #include "ios/web/public/referrer.h"
22 #include "net/base/mac/url_conversions.h" 23 #include "net/base/mac/url_conversions.h"
23 #include "ui/base/l10n/l10n_util.h" 24 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/gfx/image/image.h" 25 #include "ui/gfx/image/image.h"
25 26
27 DEFINE_WEB_STATE_USER_DATA_KEY(BlockedPopupTabHelper);
28
26 namespace { 29 namespace {
27 // The infobar to display when a popup is blocked. 30 // The infobar to display when a popup is blocked.
28 class BlockPopupInfoBarDelegate : public ConfirmInfoBarDelegate { 31 class BlockPopupInfoBarDelegate : public ConfirmInfoBarDelegate {
29 public: 32 public:
30 BlockPopupInfoBarDelegate(ios::ChromeBrowserState* browser_state, 33 BlockPopupInfoBarDelegate(ios::ChromeBrowserState* browser_state,
31 const std::vector<web::BlockedPopupInfo>& popups) 34 const std::vector<web::BlockedPopupInfo>& popups)
32 : browser_state_(browser_state), popups_(popups) {} 35 : browser_state_(browser_state), popups_(popups) {}
33 36
34 ~BlockPopupInfoBarDelegate() override {} 37 ~BlockPopupInfoBarDelegate() override {}
35 38
(...skipping 20 matching lines...) Expand all
56 59
57 base::string16 GetButtonLabel(InfoBarButton button) const override { 60 base::string16 GetButtonLabel(InfoBarButton button) const override {
58 DCHECK(button == BUTTON_OK); 61 DCHECK(button == BUTTON_OK);
59 return l10n_util::GetStringUTF16(IDS_IOS_POPUPS_ALWAYS_SHOW_MOBILE); 62 return l10n_util::GetStringUTF16(IDS_IOS_POPUPS_ALWAYS_SHOW_MOBILE);
60 } 63 }
61 64
62 bool Accept() override { 65 bool Accept() override {
63 std::vector<web::BlockedPopupInfo>::iterator it; 66 std::vector<web::BlockedPopupInfo>::iterator it;
64 scoped_refptr<HostContentSettingsMap> host_content_map_settings( 67 scoped_refptr<HostContentSettingsMap> host_content_map_settings(
65 ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state_)); 68 ios::HostContentSettingsMapFactory::GetForBrowserState(browser_state_));
66 for (it = popups_.begin(); it != popups_.end(); ++it) { 69 for (it = popups_.begin(); it != popups_.end(); ++it) {
marq (ping after 24h) 2017/01/18 14:22:09 for (auto& it : popups_) ?
rohitrao (ping after 24h) 2017/01/23 15:30:29 Done.
67 it->ShowPopup(); 70 it->ShowPopup();
68 host_content_map_settings->SetContentSettingCustomScope( 71 host_content_map_settings->SetContentSettingCustomScope(
69 ContentSettingsPattern::FromURL(it->referrer().url), 72 ContentSettingsPattern::FromURL(it->referrer().url),
70 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS, 73 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_POPUPS,
71 std::string(), CONTENT_SETTING_ALLOW); 74 std::string(), CONTENT_SETTING_ALLOW);
72 } 75 }
73 return true; 76 return true;
74 } 77 }
75 78
76 int GetButtons() const override { return BUTTON_OK; } 79 int GetButtons() const override { return BUTTON_OK; }
77 80
78 private: 81 private:
79 // The browser state to access user preferences. 82 // The browser state to access user preferences.
80 ios::ChromeBrowserState* browser_state_; 83 ios::ChromeBrowserState* browser_state_;
81 // The popups to open. 84 // The popups to open.
82 std::vector<web::BlockedPopupInfo> popups_; 85 std::vector<web::BlockedPopupInfo> popups_;
83 // The icon to display. 86 // The icon to display.
84 mutable gfx::Image icon_; 87 mutable gfx::Image icon_;
85 }; 88 };
86 } // namespace 89 } // namespace
87 90
88 BlockedPopupHandler::BlockedPopupHandler(ios::ChromeBrowserState* browser_state) 91 BlockedPopupTabHelper::BlockedPopupTabHelper(web::WebState* web_state)
89 : browser_state_(browser_state), 92 : web_state_(web_state), infobar_(nullptr), scoped_observer_(this) {}
90 delegate_(nullptr),
91 infobar_(nullptr),
92 scoped_observer_(this) {}
93 93
94 BlockedPopupHandler::~BlockedPopupHandler() {} 94 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 {}?
95 95
96 void BlockedPopupHandler::SetDelegate( 96 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) { 97 const web::BlockedPopupInfo& blocked_popup_info) {
106 if (!delegate_)
107 return;
108
109 popups_.push_back(blocked_popup_info); 98 popups_.push_back(blocked_popup_info);
110 ShowInfoBar(); 99 ShowInfoBar();
111 } 100 }
112 101
113 void BlockedPopupHandler::OnInfoBarRemoved(infobars::InfoBar* infobar, 102 void BlockedPopupTabHelper::OnInfoBarRemoved(infobars::InfoBar* infobar,
114 bool animate) { 103 bool animate) {
115 if (infobar == infobar_) { 104 if (infobar == infobar_) {
116 infobar_ = nullptr; 105 infobar_ = nullptr;
117 popups_.clear(); 106 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.
118 } 107 }
119 } 108 }
120 109
121 void BlockedPopupHandler::OnManagerShuttingDown( 110 void BlockedPopupTabHelper::OnManagerShuttingDown(
122 infobars::InfoBarManager* infobar_manager) { 111 infobars::InfoBarManager* infobar_manager) {
123 scoped_observer_.Remove(infobar_manager); 112 scoped_observer_.Remove(infobar_manager);
124 } 113 }
125 114
126 void BlockedPopupHandler::ShowInfoBar() { 115 void BlockedPopupTabHelper::ShowInfoBar() {
127 if (!popups_.size() || !delegate_) 116 infobars::InfoBarManager* infobar_manager =
117 InfoBarManagerImpl::FromWebState(web_state_);
118 if (!popups_.size() || !infobar_manager)
128 return; 119 return;
129 infobars::InfoBarManager* infobar_manager = [delegate_ infoBarManager]; 120
121 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.
122
123 ios::ChromeBrowserState* browser_state =
124 ios::ChromeBrowserState::FromBrowserState(web_state_->GetBrowserState());
130 std::unique_ptr<infobars::InfoBar> infobar = 125 std::unique_ptr<infobars::InfoBar> infobar =
131 infobar_manager->CreateConfirmInfoBar( 126 infobar_manager->CreateConfirmInfoBar(
132 std::unique_ptr<ConfirmInfoBarDelegate>( 127 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.
133 new BlockPopupInfoBarDelegate(browser_state_, popups_))); 128 new BlockPopupInfoBarDelegate(browser_state, popups_)));
134 if (infobar_) { 129 if (infobar_) {
135 infobar_ = infobar_manager->ReplaceInfoBar(infobar_, std::move(infobar)); 130 infobar_ = infobar_manager->ReplaceInfoBar(infobar_, std::move(infobar));
136 } else { 131 } else {
137 infobar_ = infobar_manager->AddInfoBar(std::move(infobar)); 132 infobar_ = infobar_manager->AddInfoBar(std::move(infobar));
138 } 133 }
139 } 134 }
135
136 void BlockedPopupTabHelper::RegisterAsInfoBarManagerObserverIfNeeded() {
137 infobars::InfoBarManager* manager =
138 InfoBarManagerImpl::FromWebState(web_state_);
139 DCHECK(manager);
140
141 if (scoped_observer_.IsObserving(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(manager);
148 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698