OLD | NEW |
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" | |
11 #include "content/browser/frame_host/render_frame_host_impl.h" | 10 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 11 #include "content/browser/geolocation/geolocation_provider_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" | |
14 #include "content/browser/renderer_host/render_view_host_impl.h" | |
15 #include "content/browser/web_contents/web_contents_impl.h" | 13 #include "content/browser/web_contents/web_contents_impl.h" |
16 #include "content/public/browser/browser_context.h" | |
17 #include "content/public/browser/content_browser_client.h" | 14 #include "content/public/browser/content_browser_client.h" |
18 #include "content/public/common/geoposition.h" | |
19 #include "content/common/geolocation_messages.h" | 15 #include "content/common/geolocation_messages.h" |
20 | 16 |
21 namespace content { | 17 namespace content { |
22 namespace { | |
23 | |
24 // Geoposition error codes for reporting in UMA. | |
25 enum GeopositionErrorCode { | |
26 // NOTE: Do not renumber these as that would confuse interpretation of | |
27 // previously logged data. When making changes, also update the enum list | |
28 // in tools/metrics/histograms/histograms.xml to keep it in sync. | |
29 | |
30 // There was no error. | |
31 GEOPOSITION_ERROR_CODE_NONE = 0, | |
32 | |
33 // User denied use of geolocation. | |
34 GEOPOSITION_ERROR_CODE_PERMISSION_DENIED = 1, | |
35 | |
36 // Geoposition could not be determined. | |
37 GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE = 2, | |
38 | |
39 // Timeout. | |
40 GEOPOSITION_ERROR_CODE_TIMEOUT = 3, | |
41 | |
42 // NOTE: Add entries only immediately above this line. | |
43 GEOPOSITION_ERROR_CODE_COUNT = 4 | |
44 }; | |
45 | |
46 void RecordGeopositionErrorCode(Geoposition::ErrorCode error_code) { | |
47 GeopositionErrorCode code = GEOPOSITION_ERROR_CODE_NONE; | |
48 switch (error_code) { | |
49 case Geoposition::ERROR_CODE_NONE: | |
50 code = GEOPOSITION_ERROR_CODE_NONE; | |
51 break; | |
52 case Geoposition::ERROR_CODE_PERMISSION_DENIED: | |
53 code = GEOPOSITION_ERROR_CODE_PERMISSION_DENIED; | |
54 break; | |
55 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE: | |
56 code = GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE; | |
57 break; | |
58 case Geoposition::ERROR_CODE_TIMEOUT: | |
59 code = GEOPOSITION_ERROR_CODE_TIMEOUT; | |
60 break; | |
61 } | |
62 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode", | |
63 code, | |
64 GEOPOSITION_ERROR_CODE_COUNT); | |
65 } | |
66 | |
67 } // namespace | |
68 | 18 |
69 GeolocationDispatcherHost::PendingPermission::PendingPermission( | 19 GeolocationDispatcherHost::PendingPermission::PendingPermission( |
70 int render_frame_id, | 20 int render_frame_id, |
71 int render_process_id, | 21 int render_process_id, |
72 int bridge_id, | 22 int bridge_id, |
73 const GURL& origin) | 23 const GURL& origin) |
74 : render_frame_id(render_frame_id), | 24 : render_frame_id(render_frame_id), |
75 render_process_id(render_process_id), | 25 render_process_id(render_process_id), |
76 bridge_id(bridge_id), | 26 bridge_id(bridge_id), |
77 origin(origin) { | 27 origin(origin) { |
78 } | 28 } |
79 | 29 |
80 GeolocationDispatcherHost::PendingPermission::~PendingPermission() { | 30 GeolocationDispatcherHost::PendingPermission::~PendingPermission() { |
81 } | 31 } |
82 | 32 |
83 GeolocationDispatcherHost::GeolocationDispatcherHost( | 33 GeolocationDispatcherHost::GeolocationDispatcherHost( |
84 WebContents* web_contents) | 34 WebContents* web_contents) |
85 : WebContentsObserver(web_contents), | 35 : WebContentsObserver(web_contents), |
86 paused_(false), | |
87 weak_factory_(this) { | 36 weak_factory_(this) { |
88 // This is initialized by WebContentsImpl. Do not add any non-trivial | 37 // This is initialized by WebContentsImpl. Do not add any non-trivial |
89 // initialization here, defer to OnStartUpdating which is triggered whenever | 38 // initialization here, defer to OnStartUpdating which is triggered whenever |
90 // a javascript geolocation object is actually initialized. | 39 // a javascript geolocation object is actually initialized. |
91 } | 40 } |
92 | 41 |
93 GeolocationDispatcherHost::~GeolocationDispatcherHost() { | 42 GeolocationDispatcherHost::~GeolocationDispatcherHost() { |
94 } | 43 } |
95 | 44 |
96 void GeolocationDispatcherHost::SetOverride( | |
97 scoped_ptr<Geoposition> geoposition) { | |
98 geoposition_override_.swap(geoposition); | |
99 RefreshGeolocationOptions(); | |
100 OnLocationUpdate(*geoposition_override_); | |
101 } | |
102 | |
103 void GeolocationDispatcherHost::ClearOverride() { | |
104 geoposition_override_.reset(); | |
105 RefreshGeolocationOptions(); | |
106 } | |
107 | |
108 void GeolocationDispatcherHost::RenderFrameDeleted( | 45 void GeolocationDispatcherHost::RenderFrameDeleted( |
109 RenderFrameHost* render_frame_host) { | 46 RenderFrameHost* render_frame_host) { |
110 OnStopUpdating(render_frame_host); | |
111 | |
112 CancelPermissionRequestsForFrame(render_frame_host); | 47 CancelPermissionRequestsForFrame(render_frame_host); |
113 } | 48 } |
114 | 49 |
115 void GeolocationDispatcherHost::RenderViewHostChanged( | |
116 RenderViewHost* old_host, | |
117 RenderViewHost* new_host) { | |
118 updating_frames_.clear(); | |
119 paused_ = false; | |
120 geolocation_subscription_.reset(); | |
121 } | |
122 | |
123 void GeolocationDispatcherHost::DidNavigateAnyFrame( | 50 void GeolocationDispatcherHost::DidNavigateAnyFrame( |
124 RenderFrameHost* render_frame_host, | 51 RenderFrameHost* render_frame_host, |
125 const LoadCommittedDetails& details, | 52 const LoadCommittedDetails& details, |
126 const FrameNavigateParams& params) { | 53 const FrameNavigateParams& params) { |
127 if (details.is_in_page) | 54 if (details.is_in_page) |
128 return; | 55 return; |
129 | 56 |
130 CancelPermissionRequestsForFrame(render_frame_host); | 57 CancelPermissionRequestsForFrame(render_frame_host); |
131 } | 58 } |
132 | 59 |
133 bool GeolocationDispatcherHost::OnMessageReceived( | 60 bool GeolocationDispatcherHost::OnMessageReceived( |
134 const IPC::Message& msg, RenderFrameHost* render_frame_host) { | 61 const IPC::Message& msg, RenderFrameHost* render_frame_host) { |
135 bool handled = true; | 62 bool handled = true; |
136 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg, | 63 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg, |
137 render_frame_host) | 64 render_frame_host) |
138 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, | 65 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, |
139 OnRequestPermission) | 66 OnRequestPermission) |
140 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) | |
141 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) | |
142 IPC_MESSAGE_UNHANDLED(handled = false) | 67 IPC_MESSAGE_UNHANDLED(handled = false) |
143 IPC_END_MESSAGE_MAP() | 68 IPC_END_MESSAGE_MAP() |
144 return handled; | 69 return handled; |
145 } | 70 } |
146 | 71 |
147 void GeolocationDispatcherHost::OnLocationUpdate( | |
148 const Geoposition& geoposition) { | |
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
150 | |
151 RecordGeopositionErrorCode(geoposition.error_code); | |
152 if (paused_) | |
153 return; | |
154 | |
155 for (std::map<RenderFrameHost*, bool>::iterator i = updating_frames_.begin(); | |
156 i != updating_frames_.end(); ++i) { | |
157 UpdateGeoposition(i->first, geoposition); | |
158 } | |
159 } | |
160 | |
161 void GeolocationDispatcherHost::UpdateGeoposition( | |
162 RenderFrameHost* frame, | |
163 const Geoposition& geoposition) { | |
164 RenderFrameHost* top_frame = frame; | |
165 while (top_frame->GetParent()) { | |
166 top_frame = top_frame->GetParent(); | |
167 } | |
168 GetContentClient()->browser()->DidUseGeolocationPermission( | |
169 web_contents(), | |
170 frame->GetLastCommittedURL().GetOrigin(), | |
171 top_frame->GetLastCommittedURL().GetOrigin()); | |
172 | |
173 frame->Send(new GeolocationMsg_PositionUpdated( | |
174 frame->GetRoutingID(), geoposition)); | |
175 } | |
176 | |
177 void GeolocationDispatcherHost::OnRequestPermission( | 72 void GeolocationDispatcherHost::OnRequestPermission( |
178 RenderFrameHost* render_frame_host, | 73 RenderFrameHost* render_frame_host, |
179 int bridge_id, | 74 int bridge_id, |
180 const GURL& requesting_origin, | 75 const GURL& requesting_origin, |
181 bool user_gesture) { | 76 bool user_gesture) { |
182 int render_process_id = render_frame_host->GetProcess()->GetID(); | 77 int render_process_id = render_frame_host->GetProcess()->GetID(); |
183 int render_frame_id = render_frame_host->GetRoutingID(); | 78 int render_frame_id = render_frame_host->GetRoutingID(); |
184 | 79 |
185 PendingPermission pending_permission( | 80 PendingPermission pending_permission( |
186 render_frame_id, render_process_id, bridge_id, requesting_origin); | 81 render_frame_id, render_process_id, bridge_id, requesting_origin); |
187 pending_permissions_.push_back(pending_permission); | 82 pending_permissions_.push_back(pending_permission); |
188 | 83 |
189 GetContentClient()->browser()->RequestGeolocationPermission( | 84 GetContentClient()->browser()->RequestGeolocationPermission( |
190 web_contents(), | 85 web_contents(), |
191 bridge_id, | 86 bridge_id, |
192 requesting_origin, | 87 requesting_origin, |
193 user_gesture, | 88 user_gesture, |
194 base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse, | 89 base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse, |
195 weak_factory_.GetWeakPtr(), | 90 weak_factory_.GetWeakPtr(), |
196 render_process_id, render_frame_id, bridge_id)); | 91 render_process_id, render_frame_id, bridge_id)); |
197 } | 92 } |
198 | 93 |
199 void GeolocationDispatcherHost::OnStartUpdating( | |
200 RenderFrameHost* render_frame_host, | |
201 const GURL& requesting_origin, | |
202 bool enable_high_accuracy) { | |
203 // StartUpdating() can be invoked as a result of high-accuracy mode | |
204 // being enabled / disabled. No need to record the dispatcher again. | |
205 UMA_HISTOGRAM_BOOLEAN( | |
206 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", | |
207 enable_high_accuracy); | |
208 | |
209 updating_frames_[render_frame_host] = enable_high_accuracy; | |
210 RefreshGeolocationOptions(); | |
211 if (geoposition_override_.get()) | |
212 UpdateGeoposition(render_frame_host, *geoposition_override_); | |
213 } | |
214 | |
215 void GeolocationDispatcherHost::OnStopUpdating( | |
216 RenderFrameHost* render_frame_host) { | |
217 updating_frames_.erase(render_frame_host); | |
218 RefreshGeolocationOptions(); | |
219 } | |
220 | |
221 void GeolocationDispatcherHost::PauseOrResume(bool should_pause) { | |
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
223 paused_ = should_pause; | |
224 RefreshGeolocationOptions(); | |
225 if (geoposition_override_.get()) | |
226 OnLocationUpdate(*geoposition_override_); | |
227 } | |
228 | |
229 void GeolocationDispatcherHost::RefreshGeolocationOptions() { | |
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
231 | |
232 if (updating_frames_.empty() || paused_ || geoposition_override_.get()) { | |
233 geolocation_subscription_.reset(); | |
234 return; | |
235 } | |
236 | |
237 bool high_accuracy = false; | |
238 for (std::map<RenderFrameHost*, bool>::iterator i = | |
239 updating_frames_.begin(); i != updating_frames_.end(); ++i) { | |
240 if (i->second) { | |
241 high_accuracy = true; | |
242 break; | |
243 } | |
244 } | |
245 geolocation_subscription_ = GeolocationProvider::GetInstance()-> | |
246 AddLocationUpdateCallback( | |
247 base::Bind(&GeolocationDispatcherHost::OnLocationUpdate, | |
248 base::Unretained(this)), | |
249 high_accuracy); | |
250 } | |
251 | |
252 void GeolocationDispatcherHost::SendGeolocationPermissionResponse( | 94 void GeolocationDispatcherHost::SendGeolocationPermissionResponse( |
253 int render_process_id, | 95 int render_process_id, |
254 int render_frame_id, | 96 int render_frame_id, |
255 int bridge_id, | 97 int bridge_id, |
256 bool allowed) { | 98 bool allowed) { |
257 for (size_t i = 0; i < pending_permissions_.size(); ++i) { | 99 for (size_t i = 0; i < pending_permissions_.size(); ++i) { |
258 if (pending_permissions_[i].render_process_id == render_process_id && | 100 if (pending_permissions_[i].render_process_id == render_process_id && |
259 pending_permissions_[i].render_frame_id == render_frame_id && | 101 pending_permissions_[i].render_frame_id == render_frame_id && |
260 pending_permissions_[i].bridge_id == bridge_id) { | 102 pending_permissions_[i].bridge_id == bridge_id) { |
261 RenderFrameHost* render_frame_host = | 103 RenderFrameHost* render_frame_host = |
(...skipping 27 matching lines...) Expand all Loading... |
289 GetContentClient()->browser()->CancelGeolocationPermissionRequest( | 131 GetContentClient()->browser()->CancelGeolocationPermissionRequest( |
290 web_contents(), | 132 web_contents(), |
291 pending_permissions_[i].bridge_id, | 133 pending_permissions_[i].bridge_id, |
292 pending_permissions_[i].origin); | 134 pending_permissions_[i].origin); |
293 pending_permissions_.erase(pending_permissions_.begin() + i); | 135 pending_permissions_.erase(pending_permissions_.begin() + i); |
294 } | 136 } |
295 } | 137 } |
296 } | 138 } |
297 | 139 |
298 } // namespace content | 140 } // namespace content |
OLD | NEW |