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

Side by Side Diff: chrome/browser/geolocation/geolocation_infobar_queue_controller.cc

Issue 11183018: Refactor ChromeGeolocationPermissionContext (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updates from comments Created 8 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 #include "chrome/browser/geolocation/geolocation_infobar_queue_controller.h" 5 #include "chrome/browser/geolocation/geolocation_infobar_queue_controller.h"
6 6
7 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h"
8 #include "chrome/browser/content_settings/host_content_settings_map.h" 7 #include "chrome/browser/content_settings/host_content_settings_map.h"
9 #include "chrome/browser/google/google_util.h" 8 #include "chrome/browser/geolocation/geolocation_confirm_infobar_delegate.h"
10 #include "chrome/browser/infobars/infobar.h" 9 #include "chrome/browser/infobars/infobar.h"
11 #include "chrome/browser/infobars/infobar_tab_helper.h" 10 #include "chrome/browser/infobars/infobar_tab_helper.h"
12 #include "chrome/browser/prefs/pref_service.h" 11 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/tab_contents/tab_util.h" 13 #include "chrome/browser/tab_contents/tab_util.h"
15 #include "chrome/common/chrome_notification_types.h" 14 #include "chrome/common/chrome_notification_types.h"
16 #include "chrome/common/content_settings.h" 15 #include "chrome/common/content_settings.h"
17 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/navigation_details.h"
20 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/notification_details.h" 18 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_source.h" 19 #include "content/public/browser/notification_source.h"
23 #include "content/public/browser/notification_types.h" 20 #include "content/public/browser/notification_types.h"
24 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
25 #include "grit/generated_resources.h"
26 #include "grit/locale_settings.h"
27 #include "grit/theme_resources.h"
28 #include "net/base/net_util.h"
29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/base/l10n/l10n_util.h"
31 22
32 using content::BrowserThread; 23 using content::BrowserThread;
33 using content::NavigationEntry;
34 using content::OpenURLParams;
35 using content::Referrer;
36 using content::WebContents; 24 using content::WebContents;
37 25
38 // GeolocationConfirmInfoBarDelegate ------------------------------------------
39
40 namespace {
41
42 class GeolocationConfirmInfoBarDelegate : public ConfirmInfoBarDelegate {
43 public:
44 GeolocationConfirmInfoBarDelegate(
45 InfoBarTabHelper* infobar_helper,
46 GeolocationInfoBarQueueController* controller,
47 int render_process_id,
48 int render_view_id,
49 int bridge_id,
50 const GURL& requesting_frame_url,
51 const std::string& display_languages);
52
53 int render_process_id() const { return render_process_id_; }
54 int render_view_id() const { return render_view_id_; }
55
56 private:
57
58 // ConfirmInfoBarDelegate:
59 virtual gfx::Image* GetIcon() const OVERRIDE;
60 virtual Type GetInfoBarType() const OVERRIDE;
61 virtual string16 GetMessageText() const OVERRIDE;
62 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE;
63 virtual bool Accept() OVERRIDE;
64 virtual bool Cancel() OVERRIDE;
65 virtual string16 GetLinkText() const OVERRIDE;
66 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE;
67
68 GeolocationInfoBarQueueController* controller_;
69 int render_process_id_;
70 int render_view_id_;
71 int bridge_id_;
72
73 GURL requesting_frame_url_;
74 std::string display_languages_;
75
76 DISALLOW_IMPLICIT_CONSTRUCTORS(GeolocationConfirmInfoBarDelegate);
77 };
78
79 GeolocationConfirmInfoBarDelegate::GeolocationConfirmInfoBarDelegate(
80 InfoBarTabHelper* infobar_helper,
81 GeolocationInfoBarQueueController* controller,
82 int render_process_id,
83 int render_view_id,
84 int bridge_id,
85 const GURL& requesting_frame_url,
86 const std::string& display_languages)
87 : ConfirmInfoBarDelegate(infobar_helper),
88 controller_(controller),
89 render_process_id_(render_process_id),
90 render_view_id_(render_view_id),
91 bridge_id_(bridge_id),
92 requesting_frame_url_(requesting_frame_url),
93 display_languages_(display_languages) {
94 const NavigationEntry* committed_entry =
95 infobar_helper->GetWebContents()->GetController().GetLastCommittedEntry();
96 set_contents_unique_id(committed_entry ? committed_entry->GetUniqueID() : 0);
97 }
98
99 gfx::Image* GeolocationConfirmInfoBarDelegate::GetIcon() const {
100 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
101 IDR_GEOLOCATION_INFOBAR_ICON);
102 }
103
104 InfoBarDelegate::Type
105 GeolocationConfirmInfoBarDelegate::GetInfoBarType() const {
106 return PAGE_ACTION_TYPE;
107 }
108
109 string16 GeolocationConfirmInfoBarDelegate::GetMessageText() const {
110 return l10n_util::GetStringFUTF16(IDS_GEOLOCATION_INFOBAR_QUESTION,
111 net::FormatUrl(requesting_frame_url_.GetOrigin(), display_languages_));
112 }
113
114 string16 GeolocationConfirmInfoBarDelegate::GetButtonLabel(
115 InfoBarButton button) const {
116 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
117 IDS_GEOLOCATION_ALLOW_BUTTON : IDS_GEOLOCATION_DENY_BUTTON);
118 }
119
120 bool GeolocationConfirmInfoBarDelegate::Accept() {
121 controller_->OnPermissionSet(render_process_id_, render_view_id_, bridge_id_,
122 requesting_frame_url_, owner()->GetWebContents()->GetURL(), true);
123 return true;
124 }
125
126 bool GeolocationConfirmInfoBarDelegate::Cancel() {
127 controller_->OnPermissionSet(render_process_id_, render_view_id_, bridge_id_,
128 requesting_frame_url_, owner()->GetWebContents()->GetURL(),
129 false);
130 return true;
131 }
132
133 string16 GeolocationConfirmInfoBarDelegate::GetLinkText() const {
134 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
135 }
136
137 bool GeolocationConfirmInfoBarDelegate::LinkClicked(
138 WindowOpenDisposition disposition) {
139 const char kGeolocationLearnMoreUrl[] =
140 #if defined(OS_CHROMEOS)
141 "https://www.google.com/support/chromeos/bin/answer.py?answer=142065";
142 #else
143 "https://www.google.com/support/chrome/bin/answer.py?answer=142065";
144 #endif
145
146 OpenURLParams params(
147 google_util::AppendGoogleLocaleParam(GURL(kGeolocationLearnMoreUrl)),
148 Referrer(),
149 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
150 content::PAGE_TRANSITION_LINK, false);
151 owner()->GetWebContents()->OpenURL(params);
152 return false; // Do not dismiss the info bar.
153 }
154
155 } // namespace
156
157
158 // GeolocationInfoBarQueueController::PendingInfoBarRequest ------------------- 26 // GeolocationInfoBarQueueController::PendingInfoBarRequest -------------------
159 27
160 struct GeolocationInfoBarQueueController::PendingInfoBarRequest { 28 struct GeolocationInfoBarQueueController::PendingInfoBarRequest {
161 public: 29 public:
162 PendingInfoBarRequest(int render_process_id, 30 PendingInfoBarRequest(int render_process_id,
163 int render_view_id, 31 int render_view_id,
164 int bridge_id, 32 int bridge_id,
165 const GURL& requesting_frame, 33 const GURL& requesting_frame,
166 const GURL& embedder, 34 const GURL& embedder,
167 base::Callback<void(bool)> callback); 35 PermissionDecidedCallback callback);
168 36
169 bool IsForTab(int p_render_process_id, int p_render_view_id) const; 37 bool IsForTab(int p_render_process_id, int p_render_view_id) const;
170 bool IsForPair(const GURL& p_requesting_frame, 38 bool IsForPair(const GURL& p_requesting_frame,
171 const GURL& p_embedder) const; 39 const GURL& p_embedder) const;
172 bool Equals(int p_render_process_id, 40 bool Equals(int p_render_process_id,
173 int p_render_view_id, 41 int p_render_view_id,
174 int p_bridge_id) const; 42 int p_bridge_id) const;
175 43
176 int render_process_id; 44 int render_process_id;
177 int render_view_id; 45 int render_view_id;
178 int bridge_id; 46 int bridge_id;
179 GURL requesting_frame; 47 GURL requesting_frame;
180 GURL embedder; 48 GURL embedder;
181 base::Callback<void(bool)> callback; 49 PermissionDecidedCallback callback;
182 GeolocationConfirmInfoBarDelegate* infobar_delegate; 50 GeolocationConfirmInfoBarDelegate* infobar_delegate;
183 }; 51 };
184 52
185 GeolocationInfoBarQueueController::PendingInfoBarRequest::PendingInfoBarRequest( 53 GeolocationInfoBarQueueController::PendingInfoBarRequest::PendingInfoBarRequest(
186 int render_process_id, 54 int render_process_id,
187 int render_view_id, 55 int render_view_id,
188 int bridge_id, 56 int bridge_id,
189 const GURL& requesting_frame, 57 const GURL& requesting_frame,
190 const GURL& embedder, 58 const GURL& embedder,
191 base::Callback<void(bool)> callback) 59 PermissionDecidedCallback callback)
192 : render_process_id(render_process_id), 60 : render_process_id(render_process_id),
193 render_view_id(render_view_id), 61 render_view_id(render_view_id),
194 bridge_id(bridge_id), 62 bridge_id(bridge_id),
195 requesting_frame(requesting_frame), 63 requesting_frame(requesting_frame),
196 embedder(embedder), 64 embedder(embedder),
197 callback(callback), 65 callback(callback),
198 infobar_delegate(NULL) { 66 infobar_delegate(NULL) {
199 } 67 }
200 68
201 bool GeolocationInfoBarQueueController::PendingInfoBarRequest::IsForTab( 69 bool GeolocationInfoBarQueueController::PendingInfoBarRequest::IsForTab(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 } 114 }
247 115
248 bool GeolocationInfoBarQueueController::RequestEquals::operator()( 116 bool GeolocationInfoBarQueueController::RequestEquals::operator()(
249 const PendingInfoBarRequest& request) const { 117 const PendingInfoBarRequest& request) const {
250 return request.Equals(render_process_id_, render_view_id_, bridge_id_); 118 return request.Equals(render_process_id_, render_view_id_, bridge_id_);
251 } 119 }
252 120
253 // GeolocationInfoBarQueueController ------------------------------------------ 121 // GeolocationInfoBarQueueController ------------------------------------------
254 122
255 GeolocationInfoBarQueueController::GeolocationInfoBarQueueController( 123 GeolocationInfoBarQueueController::GeolocationInfoBarQueueController(
256 NotifyPermissionSetCallback notify_permission_set_callback,
257 Profile* profile) 124 Profile* profile)
258 : notify_permission_set_callback_(notify_permission_set_callback), 125 : profile_(profile) {
259 profile_(profile) {
260 } 126 }
261 127
262 GeolocationInfoBarQueueController::~GeolocationInfoBarQueueController() { 128 GeolocationInfoBarQueueController::~GeolocationInfoBarQueueController() {
263 } 129 }
264 130
265 void GeolocationInfoBarQueueController::CreateInfoBarRequest( 131 void GeolocationInfoBarQueueController::CreateInfoBarRequest(
266 int render_process_id, 132 int render_process_id,
267 int render_view_id, 133 int render_view_id,
268 int bridge_id, 134 int bridge_id,
269 const GURL& requesting_frame, 135 const GURL& requesting_frame,
270 const GURL& embedder, 136 const GURL& embedder,
271 base::Callback<void(bool)> callback) { 137 PermissionDecidedCallback callback) {
272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
273 139
274 // We shouldn't get duplicate requests. 140 // We shouldn't get duplicate requests.
275 DCHECK(std::find_if(pending_infobar_requests_.begin(), 141 DCHECK(std::find_if(pending_infobar_requests_.begin(),
276 pending_infobar_requests_.end(), 142 pending_infobar_requests_.end(),
277 RequestEquals(render_process_id, render_view_id, bridge_id)) == 143 RequestEquals(render_process_id, render_view_id, bridge_id)) ==
278 pending_infobar_requests_.end()); 144 pending_infobar_requests_.end());
279 145
280 InfoBarTabHelper* helper = GetInfoBarHelper(render_process_id, 146 InfoBarTabHelper* helper = GetInfoBarHelper(render_process_id,
281 render_view_id); 147 render_view_id);
(...skipping 30 matching lines...) Expand all
312 render_view_id); 178 render_view_id);
313 helper->RemoveInfoBar(delegate); 179 helper->RemoveInfoBar(delegate);
314 } 180 }
315 181
316 void GeolocationInfoBarQueueController::OnPermissionSet( 182 void GeolocationInfoBarQueueController::OnPermissionSet(
317 int render_process_id, 183 int render_process_id,
318 int render_view_id, 184 int render_view_id,
319 int bridge_id, 185 int bridge_id,
320 const GURL& requesting_frame, 186 const GURL& requesting_frame,
321 const GURL& embedder, 187 const GURL& embedder,
188 bool update_content_setting,
322 bool allowed) { 189 bool allowed) {
323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
324 191 if (update_content_setting) {
325 ContentSetting content_setting = 192 ContentSetting content_setting =
326 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; 193 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
327 profile_->GetHostContentSettingsMap()->SetContentSetting( 194 profile_->GetHostContentSettingsMap()->SetContentSetting(
328 ContentSettingsPattern::FromURLNoWildcard(requesting_frame.GetOrigin()), 195 ContentSettingsPattern::FromURLNoWildcard(requesting_frame.GetOrigin()),
329 ContentSettingsPattern::FromURLNoWildcard(embedder.GetOrigin()), 196 ContentSettingsPattern::FromURLNoWildcard(embedder.GetOrigin()),
330 CONTENT_SETTINGS_TYPE_GEOLOCATION, 197 CONTENT_SETTINGS_TYPE_GEOLOCATION,
331 std::string(), 198 std::string(),
332 content_setting); 199 content_setting);
333 200 }
334 // Cancel this request first, then notify listeners. TODO(pkasting): Why 201 // Cancel this request first, then notify listeners. TODO(pkasting): Why
335 // is this order important? 202 // is this order important?
336 PendingInfoBarRequests requests_to_notify; 203 PendingInfoBarRequests requests_to_notify;
337 PendingInfoBarRequests infobars_to_remove; 204 PendingInfoBarRequests infobars_to_remove;
338 for (PendingInfoBarRequests::iterator i = pending_infobar_requests_.begin(); 205 for (PendingInfoBarRequests::iterator i = pending_infobar_requests_.begin();
339 i != pending_infobar_requests_.end(); ) { 206 i != pending_infobar_requests_.end(); ) {
340 if (i->IsForPair(requesting_frame, embedder)) { 207 if (i->IsForPair(requesting_frame, embedder)) {
341 requests_to_notify.push_back(*i); 208 requests_to_notify.push_back(*i);
342 if (i->Equals(render_process_id, render_view_id, bridge_id)) { 209 if (i->Equals(render_process_id, render_view_id, bridge_id)) {
343 // The delegate that called us is i, and it's currently in either 210 // The delegate that called us is i, and it's currently in either
(...skipping 19 matching lines...) Expand all
363 for (PendingInfoBarRequests::iterator i = infobars_to_remove.begin(); 230 for (PendingInfoBarRequests::iterator i = infobars_to_remove.begin();
364 i != infobars_to_remove.end(); ++i ) { 231 i != infobars_to_remove.end(); ++i ) {
365 InfoBarTabHelper* helper = GetInfoBarHelper(i->render_process_id, 232 InfoBarTabHelper* helper = GetInfoBarHelper(i->render_process_id,
366 i->render_view_id); 233 i->render_view_id);
367 helper->RemoveInfoBar(i->infobar_delegate); 234 helper->RemoveInfoBar(i->infobar_delegate);
368 } 235 }
369 236
370 // Send out the permission notifications. 237 // Send out the permission notifications.
371 for (PendingInfoBarRequests::iterator i = requests_to_notify.begin(); 238 for (PendingInfoBarRequests::iterator i = requests_to_notify.begin();
372 i != requests_to_notify.end(); ++i ) { 239 i != requests_to_notify.end(); ++i ) {
373 notify_permission_set_callback_.Run( 240 i->callback.Run(allowed);
374 i->render_process_id, i->render_view_id,
375 i->bridge_id, i->requesting_frame,
376 i->callback, allowed);
377 } 241 }
378 } 242 }
379 243
380 void GeolocationInfoBarQueueController::Observe( 244 void GeolocationInfoBarQueueController::Observe(
381 int type, 245 int type,
382 const content::NotificationSource& source, 246 const content::NotificationSource& source,
383 const content::NotificationDetails& details) { 247 const content::NotificationDetails& details) {
384 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); 248 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
385 // We will receive this notification for all infobar closures, so we need to 249 // We will receive this notification for all infobar closures, so we need to
386 // check whether this is the geolocation infobar we're tracking. Note that the 250 // check whether this is the geolocation infobar we're tracking. Note that the
(...skipping 14 matching lines...) Expand all
401 content::Source<InfoBarTabHelper>(source).ptr(); 265 content::Source<InfoBarTabHelper>(source).ptr();
402 int render_process_id = i->render_process_id; 266 int render_process_id = i->render_process_id;
403 int render_view_id = i->render_view_id; 267 int render_view_id = i->render_view_id;
404 pending_infobar_requests_.erase(i); 268 pending_infobar_requests_.erase(i);
405 ShowQueuedInfoBar(render_process_id, render_view_id, helper); 269 ShowQueuedInfoBar(render_process_id, render_view_id, helper);
406 return; 270 return;
407 } 271 }
408 } 272 }
409 } 273 }
410 274
275 GeolocationConfirmInfoBarDelegate*
276 GeolocationInfoBarQueueController::CreateInfoBarDelegate(
277 InfoBarTabHelper* infobar_helper,
278 GeolocationInfoBarQueueController* controller,
279 int render_process_id,
280 int render_view_id,
281 int bridge_id,
282 const GURL& requesting_frame_url,
283 const std::string& display_languages) {
284 return new GeolocationConfirmInfoBarDelegate(infobar_helper,
285 controller,
286 render_process_id,
287 render_view_id,
288 bridge_id,
289 requesting_frame_url,
290 display_languages);
291 }
292
411 void GeolocationInfoBarQueueController::ShowQueuedInfoBar( 293 void GeolocationInfoBarQueueController::ShowQueuedInfoBar(
412 int render_process_id, 294 int render_process_id,
413 int render_view_id, 295 int render_view_id,
414 InfoBarTabHelper* helper) { 296 InfoBarTabHelper* helper) {
415 DCHECK(helper); 297 DCHECK(helper);
416 DCHECK(!AlreadyShowingInfoBar(render_process_id, render_view_id)); 298 DCHECK(!AlreadyShowingInfoBar(render_process_id, render_view_id));
417 for (PendingInfoBarRequests::iterator i = pending_infobar_requests_.begin(); 299 for (PendingInfoBarRequests::iterator i = pending_infobar_requests_.begin();
418 i != pending_infobar_requests_.end(); ++i) { 300 i != pending_infobar_requests_.end(); ++i) {
419 if (i->IsForTab(render_process_id, render_view_id) && 301 if (i->IsForTab(render_process_id, render_view_id) &&
420 !i->infobar_delegate) { 302 !i->infobar_delegate) {
421 RegisterForInfoBarNotifications(helper); 303 RegisterForInfoBarNotifications(helper);
422 i->infobar_delegate = new GeolocationConfirmInfoBarDelegate( 304 i->infobar_delegate = CreateInfoBarDelegate(
423 helper, this, render_process_id, 305 helper, this, render_process_id,
424 render_view_id, i->bridge_id, i->requesting_frame, 306 render_view_id, i->bridge_id, i->requesting_frame,
425 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); 307 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
426 helper->AddInfoBar(i->infobar_delegate); 308 helper->AddInfoBar(i->infobar_delegate);
427 return; 309 return;
428 } 310 }
429 } 311 }
430 UnregisterForInfoBarNotifications(helper); 312 UnregisterForInfoBarNotifications(helper);
431 } 313 }
432 314
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 void GeolocationInfoBarQueueController::UnregisterForInfoBarNotifications( 357 void GeolocationInfoBarQueueController::UnregisterForInfoBarNotifications(
476 InfoBarTabHelper* helper) { 358 InfoBarTabHelper* helper) {
477 if (registrar_.IsRegistered( 359 if (registrar_.IsRegistered(
478 this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, 360 this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
479 content::Source<InfoBarTabHelper>(helper))) { 361 content::Source<InfoBarTabHelper>(helper))) {
480 registrar_.Remove(this, 362 registrar_.Remove(this,
481 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, 363 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
482 content::Source<InfoBarTabHelper>(helper)); 364 content::Source<InfoBarTabHelper>(helper));
483 } 365 }
484 } 366 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698