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

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

Issue 2640463002: [ios] Convert BlockedPopupHandler into a tab helper class. (Closed)
Patch Set: 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_handler.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(BlockedPopupHandler);
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 // static
89 : browser_state_(browser_state), 92 void BlockedPopupHandler::CreateForWebState(web::WebState* web_state) {
90 delegate_(nullptr), 93 DCHECK(web_state);
91 infobar_(nullptr), 94 if (!FromWebState(web_state)) {
92 scoped_observer_(this) {} 95 web_state->SetUserData(UserDataKey(), new BlockedPopupHandler(web_state));
96 }
97 }
98
99 BlockedPopupHandler::BlockedPopupHandler(web::WebState* web_state)
100 : web_state_(web_state), infobar_(nullptr), scoped_observer_(this) {}
93 101
94 BlockedPopupHandler::~BlockedPopupHandler() {} 102 BlockedPopupHandler::~BlockedPopupHandler() {}
95 103
96 void BlockedPopupHandler::SetDelegate( 104 void BlockedPopupHandler::Initialize() {
97 id<BlockedPopupHandlerDelegate> delegate) { 105 infobars::InfoBarManager* manager =
98 scoped_observer_.RemoveAll(); 106 InfoBarManagerImpl::FromWebState(web_state_);
99 delegate_ = delegate; 107 DCHECK(manager);
100 if (delegate_) 108 scoped_observer_.Add(manager);
101 scoped_observer_.Add([delegate_ infoBarManager]);
102 } 109 }
103 110
104 void BlockedPopupHandler::HandlePopup( 111 void BlockedPopupHandler::HandlePopup(
105 const web::BlockedPopupInfo& blocked_popup_info) { 112 const web::BlockedPopupInfo& blocked_popup_info) {
106 if (!delegate_)
107 return;
108
109 popups_.push_back(blocked_popup_info); 113 popups_.push_back(blocked_popup_info);
110 ShowInfoBar(); 114 ShowInfoBar();
111 } 115 }
112 116
113 void BlockedPopupHandler::OnInfoBarRemoved(infobars::InfoBar* infobar, 117 void BlockedPopupHandler::OnInfoBarRemoved(infobars::InfoBar* infobar,
114 bool animate) { 118 bool animate) {
115 if (infobar == infobar_) { 119 if (infobar == infobar_) {
116 infobar_ = nullptr; 120 infobar_ = nullptr;
117 popups_.clear(); 121 popups_.clear();
118 } 122 }
119 } 123 }
120 124
121 void BlockedPopupHandler::OnManagerShuttingDown( 125 void BlockedPopupHandler::OnManagerShuttingDown(
122 infobars::InfoBarManager* infobar_manager) { 126 infobars::InfoBarManager* infobar_manager) {
123 scoped_observer_.Remove(infobar_manager); 127 scoped_observer_.Remove(infobar_manager);
124 } 128 }
125 129
126 void BlockedPopupHandler::ShowInfoBar() { 130 void BlockedPopupHandler::ShowInfoBar() {
127 if (!popups_.size() || !delegate_) 131 infobars::InfoBarManager* infobar_manager =
132 InfoBarManagerImpl::FromWebState(web_state_);
133 if (!popups_.size() || !infobar_manager)
128 return; 134 return;
129 infobars::InfoBarManager* infobar_manager = [delegate_ infoBarManager]; 135
136 ios::ChromeBrowserState* browser_state =
137 ios::ChromeBrowserState::FromBrowserState(web_state_->GetBrowserState());
130 std::unique_ptr<infobars::InfoBar> infobar = 138 std::unique_ptr<infobars::InfoBar> infobar =
131 infobar_manager->CreateConfirmInfoBar( 139 infobar_manager->CreateConfirmInfoBar(
132 std::unique_ptr<ConfirmInfoBarDelegate>( 140 std::unique_ptr<ConfirmInfoBarDelegate>(
133 new BlockPopupInfoBarDelegate(browser_state_, popups_))); 141 new BlockPopupInfoBarDelegate(browser_state, popups_)));
134 if (infobar_) { 142 if (infobar_) {
135 infobar_ = infobar_manager->ReplaceInfoBar(infobar_, std::move(infobar)); 143 infobar_ = infobar_manager->ReplaceInfoBar(infobar_, std::move(infobar));
136 } else { 144 } else {
137 infobar_ = infobar_manager->AddInfoBar(std::move(infobar)); 145 infobar_ = infobar_manager->AddInfoBar(std::move(infobar));
138 } 146 }
139 } 147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698