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

Side by Side Diff: content/browser/geolocation/geolocation_dispatcher_host.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
« no previous file with comments | « content/browser/geolocation/geolocation_dispatcher_host.h ('k') | content/content_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "content/browser/geolocation/geolocation_dispatcher_host.h" 5 #include "content/browser/geolocation/geolocation_dispatcher_host.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "content/browser/frame_host/render_frame_host_impl.h" 11 #include "content/browser/frame_host/render_frame_host_impl.h"
12 #include "content/browser/renderer_host/render_message_filter.h" 12 #include "content/browser/renderer_host/render_message_filter.h"
13 #include "content/browser/renderer_host/render_process_host_impl.h" 13 #include "content/browser/renderer_host/render_process_host_impl.h"
14 #include "content/browser/renderer_host/render_view_host_impl.h" 14 #include "content/browser/renderer_host/render_view_host_impl.h"
15 #include "content/browser/web_contents/web_contents_impl.h" 15 #include "content/browser/web_contents/web_contents_impl.h"
16 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/geolocation_permission_context.h" 17 #include "content/public/browser/content_browser_client.h"
18 #include "content/public/common/geoposition.h" 18 #include "content/public/common/geoposition.h"
19 #include "content/common/geolocation_messages.h" 19 #include "content/common/geolocation_messages.h"
20 20
21 namespace content { 21 namespace content {
22 namespace { 22 namespace {
23 23
24 // Geoposition error codes for reporting in UMA. 24 // Geoposition error codes for reporting in UMA.
25 enum GeopositionErrorCode { 25 enum GeopositionErrorCode {
26 // NOTE: Do not renumber these as that would confuse interpretation of 26 // NOTE: Do not renumber these as that would confuse interpretation of
27 // previously logged data. When making changes, also update the enum list 27 // previously logged data. When making changes, also update the enum list
(...skipping 29 matching lines...) Expand all
57 break; 57 break;
58 case Geoposition::ERROR_CODE_TIMEOUT: 58 case Geoposition::ERROR_CODE_TIMEOUT:
59 code = GEOPOSITION_ERROR_CODE_TIMEOUT; 59 code = GEOPOSITION_ERROR_CODE_TIMEOUT;
60 break; 60 break;
61 } 61 }
62 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode", 62 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode",
63 code, 63 code,
64 GEOPOSITION_ERROR_CODE_COUNT); 64 GEOPOSITION_ERROR_CODE_COUNT);
65 } 65 }
66 66
67 void SendGeolocationPermissionResponse(int render_process_id,
68 int render_frame_id,
69 int bridge_id,
70 bool allowed) {
71 RenderFrameHost* render_frame_host =
72 RenderFrameHost::FromID(render_process_id, render_frame_id);
73 if (!render_frame_host)
74 return;
75 render_frame_host->Send(
76 new GeolocationMsg_PermissionSet(render_frame_id, bridge_id, allowed));
77
78 if (allowed)
79 GeolocationProviderImpl::GetInstance()->UserDidOptIntoLocationServices();
80 }
81
82 } // namespace 67 } // namespace
83 68
69 GeolocationDispatcherHost::PendingPermission::PendingPermission(
70 int render_frame_id,
71 int render_process_id,
72 int bridge_id)
73 : render_frame_id(render_frame_id),
74 render_process_id(render_process_id),
75 bridge_id(bridge_id) {
76 }
77
78 GeolocationDispatcherHost::PendingPermission::~PendingPermission() {
79 }
80
84 GeolocationDispatcherHost::GeolocationDispatcherHost( 81 GeolocationDispatcherHost::GeolocationDispatcherHost(
85 WebContents* web_contents) 82 WebContents* web_contents)
86 : WebContentsObserver(web_contents), 83 : WebContentsObserver(web_contents),
87 paused_(false) { 84 paused_(false),
85 weak_factory_(this) {
88 // This is initialized by WebContentsImpl. Do not add any non-trivial 86 // This is initialized by WebContentsImpl. Do not add any non-trivial
89 // initialization here, defer to OnStartUpdating which is triggered whenever 87 // initialization here, defer to OnStartUpdating which is triggered whenever
90 // a javascript geolocation object is actually initialized. 88 // a javascript geolocation object is actually initialized.
91 } 89 }
92 90
93 GeolocationDispatcherHost::~GeolocationDispatcherHost() { 91 GeolocationDispatcherHost::~GeolocationDispatcherHost() {
94 } 92 }
95 93
96 void GeolocationDispatcherHost::RenderFrameDeleted( 94 void GeolocationDispatcherHost::RenderFrameDeleted(
97 RenderFrameHost* render_frame_host) { 95 RenderFrameHost* render_frame_host) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 i->first->Send(new GeolocationMsg_PositionUpdated( 133 i->first->Send(new GeolocationMsg_PositionUpdated(
136 i->first->GetRoutingID(), geoposition)); 134 i->first->GetRoutingID(), geoposition));
137 } 135 }
138 } 136 }
139 137
140 void GeolocationDispatcherHost::OnRequestPermission( 138 void GeolocationDispatcherHost::OnRequestPermission(
141 RenderFrameHost* render_frame_host, 139 RenderFrameHost* render_frame_host,
142 int bridge_id, 140 int bridge_id,
143 const GURL& requesting_frame, 141 const GURL& requesting_frame,
144 bool user_gesture) { 142 bool user_gesture) {
145 GeolocationPermissionContext* context =
146 web_contents()->GetBrowserContext()->GetGeolocationPermissionContext();
147 int render_process_id = render_frame_host->GetProcess()->GetID(); 143 int render_process_id = render_frame_host->GetProcess()->GetID();
148 int render_frame_id = render_frame_host->GetRoutingID(); 144 int render_frame_id = render_frame_host->GetRoutingID();
149 if (context) { 145
150 context->RequestGeolocationPermission( 146 PendingPermission pending_permission(
151 web_contents(), 147 render_frame_id, render_process_id, bridge_id);
152 bridge_id, 148 pending_permissions_.push_back(pending_permission);
153 requesting_frame, 149
154 user_gesture, 150 GetContentClient()->browser()->RequestGeolocationPermission(
155 base::Bind(&SendGeolocationPermissionResponse, 151 web_contents(),
156 render_process_id, 152 bridge_id,
157 render_frame_id, 153 requesting_frame,
158 bridge_id)); 154 user_gesture,
159 } else { 155 base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse,
160 SendGeolocationPermissionResponse( 156 weak_factory_.GetWeakPtr(),
161 render_process_id, render_frame_id, bridge_id, true); 157 render_process_id, render_frame_id, bridge_id),
162 } 158 &pending_permissions_.back().cancel);
163 } 159 }
164 160
165 void GeolocationDispatcherHost::OnCancelPermissionRequest( 161 void GeolocationDispatcherHost::OnCancelPermissionRequest(
166 RenderFrameHost* render_frame_host, 162 RenderFrameHost* render_frame_host,
167 int bridge_id, 163 int bridge_id,
168 const GURL& requesting_frame) { 164 const GURL& requesting_frame) {
169 GeolocationPermissionContext* context = 165 int render_process_id = render_frame_host->GetProcess()->GetID();
170 web_contents()->GetBrowserContext()->GetGeolocationPermissionContext(); 166 int render_frame_id = render_frame_host->GetRoutingID();
171 if (context) { 167 for (size_t i = 0; i < pending_permissions_.size(); ++i) {
172 context->CancelGeolocationPermissionRequest( 168 if (pending_permissions_[i].render_process_id == render_process_id &&
173 web_contents(), bridge_id, requesting_frame); 169 pending_permissions_[i].render_frame_id == render_frame_id &&
170 pending_permissions_[i].bridge_id == bridge_id) {
171 if (!pending_permissions_[i].cancel.is_null())
172 pending_permissions_[i].cancel.Run();
173 pending_permissions_.erase(pending_permissions_.begin() + i);
174 return;
175 }
174 } 176 }
175 } 177 }
176 178
177 void GeolocationDispatcherHost::OnStartUpdating( 179 void GeolocationDispatcherHost::OnStartUpdating(
178 RenderFrameHost* render_frame_host, 180 RenderFrameHost* render_frame_host,
179 const GURL& requesting_frame, 181 const GURL& requesting_frame,
180 bool enable_high_accuracy) { 182 bool enable_high_accuracy) {
181 // StartUpdating() can be invoked as a result of high-accuracy mode 183 // StartUpdating() can be invoked as a result of high-accuracy mode
182 // being enabled / disabled. No need to record the dispatcher again. 184 // being enabled / disabled. No need to record the dispatcher again.
183 UMA_HISTOGRAM_BOOLEAN( 185 UMA_HISTOGRAM_BOOLEAN(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 break; 218 break;
217 } 219 }
218 } 220 }
219 geolocation_subscription_ = GeolocationProvider::GetInstance()-> 221 geolocation_subscription_ = GeolocationProvider::GetInstance()->
220 AddLocationUpdateCallback( 222 AddLocationUpdateCallback(
221 base::Bind(&GeolocationDispatcherHost::OnLocationUpdate, 223 base::Bind(&GeolocationDispatcherHost::OnLocationUpdate,
222 base::Unretained(this)), 224 base::Unretained(this)),
223 high_accuracy); 225 high_accuracy);
224 } 226 }
225 227
228 void GeolocationDispatcherHost::SendGeolocationPermissionResponse(
229 int render_process_id,
230 int render_frame_id,
231 int bridge_id,
232 bool allowed) {
233 for (size_t i = 0; i < pending_permissions_.size(); ++i) {
234 if (pending_permissions_[i].render_process_id == render_process_id &&
235 pending_permissions_[i].render_frame_id == render_frame_id &&
236 pending_permissions_[i].bridge_id == bridge_id) {
237 RenderFrameHost* render_frame_host =
238 RenderFrameHost::FromID(render_process_id, render_frame_id);
239 if (render_frame_host) {
240 render_frame_host->Send(new GeolocationMsg_PermissionSet(
241 render_frame_id, bridge_id, allowed));
242 }
243
244 if (allowed) {
245 GeolocationProviderImpl::GetInstance()->
246 UserDidOptIntoLocationServices();
247 }
248
249 pending_permissions_.erase(pending_permissions_.begin() + i);
250 return;
251 }
252 }
253
254 NOTREACHED();
255 }
256
226 } // namespace content 257 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/geolocation/geolocation_dispatcher_host.h ('k') | content/content_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698