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

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

Issue 330143002: Simplify geolocation permission request in the Content API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 6 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/chrome_geolocation_permission_context.h" 5 #include "chrome/browser/geolocation/geolocation_permission_context.h"
6 6
7 #include <functional> 7 #include <functional>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/prefs/pref_service.h" 12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/content_settings/host_content_settings_map.h" 14 #include "chrome/browser/content_settings/host_content_settings_map.h"
15 #include "chrome/browser/content_settings/permission_request_id.h" 15 #include "chrome/browser/content_settings/permission_request_id.h"
16 #include "chrome/browser/content_settings/tab_specific_content_settings.h" 16 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/tab_contents/tab_util.h" 18 #include "chrome/browser/tab_contents/tab_util.h"
19 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" 19 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
20 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" 20 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
21 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
22 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/render_process_host.h" 23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/render_view_host.h" 24 #include "content/public/browser/render_view_host.h"
25 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
26 #include "grit/generated_resources.h" 26 #include "grit/generated_resources.h"
27 #include "grit/theme_resources.h" 27 #include "grit/theme_resources.h"
28 #include "net/base/net_util.h" 28 #include "net/base/net_util.h"
29 #include "ui/base/l10n/l10n_util.h" 29 #include "ui/base/l10n/l10n_util.h"
30 30
31 class GeolocationPermissionRequest : public PermissionBubbleRequest { 31 class GeolocationPermissionRequest : public PermissionBubbleRequest {
32 public: 32 public:
33 GeolocationPermissionRequest( 33 GeolocationPermissionRequest(GeolocationPermissionContext* context,
34 ChromeGeolocationPermissionContext* context, 34 const PermissionRequestID& id,
35 const PermissionRequestID& id, 35 const GURL& requesting_frame,
36 const GURL& requesting_frame, 36 bool user_gesture,
37 bool user_gesture, 37 base::Callback<void(bool)> callback,
38 base::Callback<void(bool)> callback, 38 const std::string& display_languages);
39 const std::string& display_languages);
40 virtual ~GeolocationPermissionRequest(); 39 virtual ~GeolocationPermissionRequest();
41 40
42 // PermissionBubbleDelegate: 41 // PermissionBubbleDelegate:
43 virtual int GetIconID() const OVERRIDE; 42 virtual int GetIconID() const OVERRIDE;
44 virtual base::string16 GetMessageText() const OVERRIDE; 43 virtual base::string16 GetMessageText() const OVERRIDE;
45 virtual base::string16 GetMessageTextFragment() const OVERRIDE; 44 virtual base::string16 GetMessageTextFragment() const OVERRIDE;
46 virtual bool HasUserGesture() const OVERRIDE; 45 virtual bool HasUserGesture() const OVERRIDE;
47 virtual GURL GetRequestingHostname() const OVERRIDE; 46 virtual GURL GetRequestingHostname() const OVERRIDE;
48 virtual void PermissionGranted() OVERRIDE; 47 virtual void PermissionGranted() OVERRIDE;
49 virtual void PermissionDenied() OVERRIDE; 48 virtual void PermissionDenied() OVERRIDE;
50 virtual void Cancelled() OVERRIDE; 49 virtual void Cancelled() OVERRIDE;
51 virtual void RequestFinished() OVERRIDE; 50 virtual void RequestFinished() OVERRIDE;
52 51
53 private: 52 private:
54 ChromeGeolocationPermissionContext* context_; 53 GeolocationPermissionContext* context_;
55 PermissionRequestID id_; 54 PermissionRequestID id_;
56 GURL requesting_frame_; 55 GURL requesting_frame_;
57 bool user_gesture_; 56 bool user_gesture_;
58 base::Callback<void(bool)> callback_; 57 base::Callback<void(bool)> callback_;
59 std::string display_languages_; 58 std::string display_languages_;
60 }; 59 };
61 60
62 GeolocationPermissionRequest::GeolocationPermissionRequest( 61 GeolocationPermissionRequest::GeolocationPermissionRequest(
63 ChromeGeolocationPermissionContext* context, 62 GeolocationPermissionContext* context,
64 const PermissionRequestID& id, 63 const PermissionRequestID& id,
65 const GURL& requesting_frame, 64 const GURL& requesting_frame,
66 bool user_gesture, 65 bool user_gesture,
67 base::Callback<void(bool)> callback, 66 base::Callback<void(bool)> callback,
68 const std::string& display_languages) 67 const std::string& display_languages)
69 : context_(context), 68 : context_(context),
70 id_(id), 69 id_(id),
71 requesting_frame_(requesting_frame), 70 requesting_frame_(requesting_frame),
72 user_gesture_(user_gesture), 71 user_gesture_(user_gesture),
73 callback_(callback), 72 callback_(callback),
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 105
107 void GeolocationPermissionRequest::Cancelled() { 106 void GeolocationPermissionRequest::Cancelled() {
108 } 107 }
109 108
110 void GeolocationPermissionRequest::RequestFinished() { 109 void GeolocationPermissionRequest::RequestFinished() {
111 // Deletes 'this'. 110 // Deletes 'this'.
112 context_->RequestFinished(this); 111 context_->RequestFinished(this);
113 } 112 }
114 113
115 114
116 ChromeGeolocationPermissionContext::ChromeGeolocationPermissionContext( 115 GeolocationPermissionContext::GeolocationPermissionContext(
117 Profile* profile) 116 Profile* profile)
118 : profile_(profile), 117 : profile_(profile),
119 shutting_down_(false), 118 shutting_down_(false),
120 extensions_context_(profile) { 119 extensions_context_(profile) {
121 } 120 }
122 121
123 ChromeGeolocationPermissionContext::~ChromeGeolocationPermissionContext() { 122 GeolocationPermissionContext::~GeolocationPermissionContext() {
124 // ChromeGeolocationPermissionContext may be destroyed on either the UI thread 123 // GeolocationPermissionContext may be destroyed on either the UI thread
125 // or the IO thread, but the PermissionQueueController must have been 124 // or the IO thread, but the PermissionQueueController must have been
126 // destroyed on the UI thread. 125 // destroyed on the UI thread.
127 DCHECK(!permission_queue_controller_.get()); 126 DCHECK(!permission_queue_controller_.get());
128 } 127 }
129 128
130 void ChromeGeolocationPermissionContext::RequestGeolocationPermission( 129 void GeolocationPermissionContext::RequestGeolocationPermission(
131 content::WebContents* web_contents, 130 content::WebContents* web_contents,
132 int bridge_id, 131 int bridge_id,
133 const GURL& requesting_frame, 132 const GURL& requesting_frame,
134 bool user_gesture, 133 bool user_gesture,
135 base::Callback<void(bool)> callback) { 134 base::Callback<void(bool)> result_callback,
135 base::Closure* cancel_callback) {
136 GURL requesting_frame_origin = requesting_frame.GetOrigin(); 136 GURL requesting_frame_origin = requesting_frame.GetOrigin();
137 if (shutting_down_) 137 if (shutting_down_)
138 return; 138 return;
139 139
140 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); 140 int render_process_id = web_contents->GetRenderProcessHost()->GetID();
141 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); 141 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID();
142 if (cancel_callback) {
143 *cancel_callback = base::Bind(
144 &GeolocationPermissionContext::CancelGeolocationPermissionRequest,
145 this, render_process_id, render_view_id, bridge_id);
146 }
147
142 const PermissionRequestID id( 148 const PermissionRequestID id(
143 render_process_id, render_view_id, bridge_id, GURL()); 149 render_process_id, render_view_id, bridge_id, GURL());
144 150
145 bool permission_set; 151 bool permission_set;
146 bool new_permission; 152 bool new_permission;
147 if (extensions_context_.RequestPermission( 153 if (extensions_context_.RequestPermission(
148 web_contents, id, bridge_id, requesting_frame, user_gesture, 154 web_contents, id, bridge_id, requesting_frame, user_gesture,
149 callback, &permission_set, &new_permission)) { 155 result_callback, &permission_set, &new_permission)) {
150 if (permission_set) { 156 if (permission_set) {
151 NotifyPermissionSet(id, requesting_frame_origin, callback, 157 NotifyPermissionSet(id, requesting_frame_origin, result_callback,
152 new_permission); 158 new_permission);
153 } 159 }
154 return; 160 return;
155 } 161 }
156 162
157 GURL embedder = web_contents->GetLastCommittedURL().GetOrigin(); 163 GURL embedder = web_contents->GetLastCommittedURL().GetOrigin();
158 if (!requesting_frame_origin.is_valid() || !embedder.is_valid()) { 164 if (!requesting_frame_origin.is_valid() || !embedder.is_valid()) {
159 LOG(WARNING) << "Attempt to use geolocation from an invalid URL: " 165 LOG(WARNING) << "Attempt to use geolocation from an invalid URL: "
160 << requesting_frame_origin << "," << embedder 166 << requesting_frame_origin << "," << embedder
161 << " (geolocation is not supported in popups)"; 167 << " (geolocation is not supported in popups)";
162 NotifyPermissionSet(id, requesting_frame_origin, callback, false); 168 NotifyPermissionSet(id, requesting_frame_origin, result_callback, false);
163 return; 169 return;
164 } 170 }
165 171
166 DecidePermission(web_contents, id, requesting_frame_origin, user_gesture, 172 DecidePermission(web_contents, id, requesting_frame_origin, user_gesture,
167 embedder, "", callback); 173 embedder, "", result_callback);
168 } 174 }
169 175
170 void ChromeGeolocationPermissionContext::CancelGeolocationPermissionRequest( 176 void GeolocationPermissionContext::CancelGeolocationPermissionRequest(
171 content::WebContents* web_contents, 177 int render_process_id,
172 int bridge_id, 178 int render_view_id,
173 const GURL& requesting_frame) { 179 int bridge_id) {
180 content::WebContents* web_contents = tab_util::GetWebContentsByID(
181 render_process_id, render_view_id);
174 if (extensions_context_.CancelPermissionRequest(web_contents, bridge_id)) 182 if (extensions_context_.CancelPermissionRequest(web_contents, bridge_id))
175 return; 183 return;
176 184
177 int render_process_id = web_contents->GetRenderProcessHost()->GetID();
178 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID();
179 CancelPendingInfobarRequest(PermissionRequestID( 185 CancelPendingInfobarRequest(PermissionRequestID(
180 render_process_id, render_view_id, bridge_id, GURL())); 186 render_process_id, render_view_id, bridge_id, GURL()));
181 } 187 }
182 188
183 void ChromeGeolocationPermissionContext::DecidePermission( 189 void GeolocationPermissionContext::DecidePermission(
184 content::WebContents* web_contents, 190 content::WebContents* web_contents,
185 const PermissionRequestID& id, 191 const PermissionRequestID& id,
186 const GURL& requesting_frame, 192 const GURL& requesting_frame,
187 bool user_gesture, 193 bool user_gesture,
188 const GURL& embedder, 194 const GURL& embedder,
189 const std::string& accept_button_label, 195 const std::string& accept_button_label,
190 base::Callback<void(bool)> callback) { 196 base::Callback<void(bool)> callback) {
191 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 197 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
192 198
193 ContentSetting content_setting = 199 ContentSetting content_setting =
(...skipping 18 matching lines...) Expand all
212 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages))); 218 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)));
213 GeolocationPermissionRequest* request = request_ptr.get(); 219 GeolocationPermissionRequest* request = request_ptr.get();
214 pending_requests_.add(id.ToString(), request_ptr.Pass()); 220 pending_requests_.add(id.ToString(), request_ptr.Pass());
215 mgr->AddRequest(request); 221 mgr->AddRequest(request);
216 } 222 }
217 } else { 223 } else {
218 // setting == ask. Prompt the user. 224 // setting == ask. Prompt the user.
219 QueueController()->CreateInfoBarRequest( 225 QueueController()->CreateInfoBarRequest(
220 id, requesting_frame, embedder, accept_button_label, 226 id, requesting_frame, embedder, accept_button_label,
221 base::Bind( 227 base::Bind(
222 &ChromeGeolocationPermissionContext::NotifyPermissionSet, 228 &GeolocationPermissionContext::NotifyPermissionSet,
223 base::Unretained(this), id, requesting_frame, callback)); 229 base::Unretained(this), id, requesting_frame, callback));
224 } 230 }
225 } 231 }
226 } 232 }
227 233
228 void ChromeGeolocationPermissionContext::CreateInfoBarRequest( 234 void GeolocationPermissionContext::CreateInfoBarRequest(
229 const PermissionRequestID& id, 235 const PermissionRequestID& id,
230 const GURL& requesting_frame, 236 const GURL& requesting_frame,
231 const GURL& embedder, 237 const GURL& embedder,
232 const std::string accept_button_label, 238 const std::string accept_button_label,
233 base::Callback<void(bool)> callback) { 239 base::Callback<void(bool)> callback) {
234 QueueController()->CreateInfoBarRequest( 240 QueueController()->CreateInfoBarRequest(
235 id, requesting_frame, embedder, accept_button_label, base::Bind( 241 id, requesting_frame, embedder, accept_button_label, base::Bind(
236 &ChromeGeolocationPermissionContext::NotifyPermissionSet, 242 &GeolocationPermissionContext::NotifyPermissionSet,
237 base::Unretained(this), id, requesting_frame, callback)); 243 base::Unretained(this), id, requesting_frame, callback));
238 } 244 }
239 245
240 void ChromeGeolocationPermissionContext::RequestFinished( 246 void GeolocationPermissionContext::RequestFinished(
241 GeolocationPermissionRequest* request) { 247 GeolocationPermissionRequest* request) {
242 base::ScopedPtrHashMap<std::string, 248 base::ScopedPtrHashMap<std::string,
243 GeolocationPermissionRequest>::iterator it; 249 GeolocationPermissionRequest>::iterator it;
244 for (it = pending_requests_.begin(); it != pending_requests_.end(); ++it) { 250 for (it = pending_requests_.begin(); it != pending_requests_.end(); ++it) {
245 if (it->second == request) { 251 if (it->second == request) {
246 pending_requests_.take_and_erase(it); 252 pending_requests_.take_and_erase(it);
247 return; 253 return;
248 } 254 }
249 } 255 }
250 } 256 }
251 257
252 258
253 void ChromeGeolocationPermissionContext::ShutdownOnUIThread() { 259 void GeolocationPermissionContext::ShutdownOnUIThread() {
254 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 260 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
255 permission_queue_controller_.reset(); 261 permission_queue_controller_.reset();
256 shutting_down_ = true; 262 shutting_down_ = true;
257 } 263 }
258 264
259 void ChromeGeolocationPermissionContext::PermissionDecided( 265 void GeolocationPermissionContext::PermissionDecided(
260 const PermissionRequestID& id, 266 const PermissionRequestID& id,
261 const GURL& requesting_frame, 267 const GURL& requesting_frame,
262 const GURL& embedder, 268 const GURL& embedder,
263 base::Callback<void(bool)> callback, 269 base::Callback<void(bool)> callback,
264 bool allowed) { 270 bool allowed) {
265 NotifyPermissionSet(id, requesting_frame, callback, allowed); 271 NotifyPermissionSet(id, requesting_frame, callback, allowed);
266 } 272 }
267 273
268 void ChromeGeolocationPermissionContext::NotifyPermissionSet( 274 void GeolocationPermissionContext::NotifyPermissionSet(
269 const PermissionRequestID& id, 275 const PermissionRequestID& id,
270 const GURL& requesting_frame, 276 const GURL& requesting_frame,
271 base::Callback<void(bool)> callback, 277 base::Callback<void(bool)> callback,
272 bool allowed) { 278 bool allowed) {
273 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 279 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
274 280
275 // WebContents may have gone away (or not exists for extension). 281 // WebContents may have gone away (or not exists for extension).
276 TabSpecificContentSettings* content_settings = 282 TabSpecificContentSettings* content_settings =
277 TabSpecificContentSettings::Get(id.render_process_id(), 283 TabSpecificContentSettings::Get(id.render_process_id(),
278 id.render_view_id()); 284 id.render_view_id());
279 if (content_settings) { 285 if (content_settings) {
280 content_settings->OnGeolocationPermissionSet(requesting_frame.GetOrigin(), 286 content_settings->OnGeolocationPermissionSet(requesting_frame.GetOrigin(),
281 allowed); 287 allowed);
282 } 288 }
283 289
284 callback.Run(allowed); 290 callback.Run(allowed);
285 } 291 }
286 292
287 PermissionQueueController* 293 PermissionQueueController*
288 ChromeGeolocationPermissionContext::QueueController() { 294 GeolocationPermissionContext::QueueController() {
289 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 295 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
290 DCHECK(!shutting_down_); 296 DCHECK(!shutting_down_);
291 if (!permission_queue_controller_) 297 if (!permission_queue_controller_)
292 permission_queue_controller_.reset(CreateQueueController()); 298 permission_queue_controller_.reset(CreateQueueController());
293 return permission_queue_controller_.get(); 299 return permission_queue_controller_.get();
294 } 300 }
295 301
296 PermissionQueueController* 302 PermissionQueueController*
297 ChromeGeolocationPermissionContext::CreateQueueController() { 303 GeolocationPermissionContext::CreateQueueController() {
298 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 304 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
299 return new PermissionQueueController(profile(), 305 return new PermissionQueueController(profile(),
300 CONTENT_SETTINGS_TYPE_GEOLOCATION); 306 CONTENT_SETTINGS_TYPE_GEOLOCATION);
301 } 307 }
302 308
303 void ChromeGeolocationPermissionContext::CancelPendingInfobarRequest( 309 void GeolocationPermissionContext::CancelPendingInfobarRequest(
304 const PermissionRequestID& id) { 310 const PermissionRequestID& id) {
305 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 311 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
306 if (shutting_down_) 312 if (shutting_down_)
307 return; 313 return;
308 314
309 if (PermissionBubbleManager::Enabled()) { 315 if (PermissionBubbleManager::Enabled()) {
310 GeolocationPermissionRequest* cancelling = 316 GeolocationPermissionRequest* cancelling =
311 pending_requests_.get(id.ToString()); 317 pending_requests_.get(id.ToString());
312 content::WebContents* web_contents = tab_util::GetWebContentsByID( 318 content::WebContents* web_contents = tab_util::GetWebContentsByID(
313 id.render_process_id(), id.render_view_id()); 319 id.render_process_id(), id.render_view_id());
314 if (cancelling != NULL && web_contents != NULL && 320 if (cancelling != NULL && web_contents != NULL &&
315 PermissionBubbleManager::FromWebContents(web_contents) != NULL) { 321 PermissionBubbleManager::FromWebContents(web_contents) != NULL) {
316 PermissionBubbleManager::FromWebContents(web_contents)-> 322 PermissionBubbleManager::FromWebContents(web_contents)->
317 CancelRequest(cancelling); 323 CancelRequest(cancelling);
318 } 324 }
319 return; 325 return;
320 } 326 }
321 327
322 QueueController()->CancelInfoBarRequest(id); 328 QueueController()->CancelInfoBarRequest(id);
323 } 329 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698