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" |
12 #include "content/browser/renderer_host/render_message_filter.h" | 11 #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" | 12 #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" | 13 #include "content/public/browser/content_browser_client.h" |
18 #include "content/public/common/geoposition.h" | |
19 #include "content/common/geolocation_messages.h" | 14 #include "content/common/geolocation_messages.h" |
20 | 15 |
21 namespace content { | 16 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 | 17 |
69 GeolocationDispatcherHost::PendingPermission::PendingPermission( | 18 GeolocationDispatcherHost::PendingPermission::PendingPermission( |
70 int render_frame_id, | 19 int render_frame_id, |
71 int render_process_id, | 20 int render_process_id, |
72 int bridge_id) | 21 int bridge_id) |
73 : render_frame_id(render_frame_id), | 22 : render_frame_id(render_frame_id), |
74 render_process_id(render_process_id), | 23 render_process_id(render_process_id), |
75 bridge_id(bridge_id) { | 24 bridge_id(bridge_id) { |
76 } | 25 } |
77 | 26 |
78 GeolocationDispatcherHost::PendingPermission::~PendingPermission() { | 27 GeolocationDispatcherHost::PendingPermission::~PendingPermission() { |
79 } | 28 } |
80 | 29 |
81 GeolocationDispatcherHost::GeolocationDispatcherHost( | 30 GeolocationDispatcherHost::GeolocationDispatcherHost( |
82 WebContents* web_contents) | 31 WebContents* web_contents) |
83 : WebContentsObserver(web_contents), | 32 : WebContentsObserver(web_contents), |
84 paused_(false), | |
85 weak_factory_(this) { | 33 weak_factory_(this) { |
86 // This is initialized by WebContentsImpl. Do not add any non-trivial | 34 // This is initialized by WebContentsImpl. Do not add any non-trivial |
87 // initialization here, defer to OnStartUpdating which is triggered whenever | 35 // initialization here, defer to OnStartUpdating which is triggered whenever |
88 // a javascript geolocation object is actually initialized. | 36 // a javascript geolocation object is actually initialized. |
89 } | 37 } |
90 | 38 |
91 GeolocationDispatcherHost::~GeolocationDispatcherHost() { | 39 GeolocationDispatcherHost::~GeolocationDispatcherHost() { |
92 } | 40 } |
93 | 41 |
94 void GeolocationDispatcherHost::RenderFrameDeleted( | |
95 RenderFrameHost* render_frame_host) { | |
96 OnStopUpdating(render_frame_host); | |
97 } | |
98 | |
99 void GeolocationDispatcherHost::RenderViewHostChanged( | |
100 RenderViewHost* old_host, | |
101 RenderViewHost* new_host) { | |
102 updating_frames_.clear(); | |
103 paused_ = false; | |
104 geolocation_subscription_.reset(); | |
105 } | |
106 | |
107 bool GeolocationDispatcherHost::OnMessageReceived( | 42 bool GeolocationDispatcherHost::OnMessageReceived( |
108 const IPC::Message& msg, RenderFrameHost* render_frame_host) { | 43 const IPC::Message& msg, RenderFrameHost* render_frame_host) { |
109 bool handled = true; | 44 bool handled = true; |
110 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg, | 45 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg, |
111 render_frame_host) | 46 render_frame_host) |
112 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, | 47 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, |
113 OnRequestPermission) | 48 OnRequestPermission) |
114 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, | 49 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, |
115 OnCancelPermissionRequest) | 50 OnCancelPermissionRequest) |
116 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) | |
117 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) | |
118 IPC_MESSAGE_UNHANDLED(handled = false) | 51 IPC_MESSAGE_UNHANDLED(handled = false) |
119 IPC_END_MESSAGE_MAP() | 52 IPC_END_MESSAGE_MAP() |
120 return handled; | 53 return handled; |
121 } | 54 } |
122 | 55 |
123 void GeolocationDispatcherHost::OnLocationUpdate( | |
124 const Geoposition& geoposition) { | |
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
126 | |
127 RecordGeopositionErrorCode(geoposition.error_code); | |
128 if (paused_) | |
129 return; | |
130 | |
131 for (std::map<RenderFrameHost*, bool>::iterator i = updating_frames_.begin(); | |
132 i != updating_frames_.end(); ++i) { | |
133 RenderFrameHost* top_frame = i->first; | |
134 while (top_frame->GetParent()) { | |
135 top_frame = top_frame->GetParent(); | |
136 } | |
137 GetContentClient()->browser()->DidUseGeolocationPermission( | |
138 web_contents(), | |
139 i->first->GetLastCommittedURL().GetOrigin(), | |
140 top_frame->GetLastCommittedURL().GetOrigin()); | |
141 | |
142 i->first->Send(new GeolocationMsg_PositionUpdated( | |
143 i->first->GetRoutingID(), geoposition)); | |
144 } | |
145 } | |
146 | |
147 void GeolocationDispatcherHost::OnRequestPermission( | 56 void GeolocationDispatcherHost::OnRequestPermission( |
148 RenderFrameHost* render_frame_host, | 57 RenderFrameHost* render_frame_host, |
149 int bridge_id, | 58 int bridge_id, |
150 const GURL& requesting_frame, | 59 const GURL& requesting_frame, |
151 bool user_gesture) { | 60 bool user_gesture) { |
152 int render_process_id = render_frame_host->GetProcess()->GetID(); | 61 int render_process_id = render_frame_host->GetProcess()->GetID(); |
153 int render_frame_id = render_frame_host->GetRoutingID(); | 62 int render_frame_id = render_frame_host->GetRoutingID(); |
154 | 63 |
155 PendingPermission pending_permission( | 64 PendingPermission pending_permission( |
156 render_frame_id, render_process_id, bridge_id); | 65 render_frame_id, render_process_id, bridge_id); |
(...skipping 21 matching lines...) Expand all Loading... |
178 pending_permissions_[i].bridge_id == bridge_id) { | 87 pending_permissions_[i].bridge_id == bridge_id) { |
179 GetContentClient()->browser()->CancelGeolocationPermissionRequest( | 88 GetContentClient()->browser()->CancelGeolocationPermissionRequest( |
180 web_contents(), bridge_id, requesting_frame); | 89 web_contents(), bridge_id, requesting_frame); |
181 | 90 |
182 pending_permissions_.erase(pending_permissions_.begin() + i); | 91 pending_permissions_.erase(pending_permissions_.begin() + i); |
183 return; | 92 return; |
184 } | 93 } |
185 } | 94 } |
186 } | 95 } |
187 | 96 |
188 void GeolocationDispatcherHost::OnStartUpdating( | |
189 RenderFrameHost* render_frame_host, | |
190 const GURL& requesting_frame, | |
191 bool enable_high_accuracy) { | |
192 // StartUpdating() can be invoked as a result of high-accuracy mode | |
193 // being enabled / disabled. No need to record the dispatcher again. | |
194 UMA_HISTOGRAM_BOOLEAN( | |
195 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", | |
196 enable_high_accuracy); | |
197 | |
198 updating_frames_[render_frame_host] = enable_high_accuracy; | |
199 RefreshGeolocationOptions(); | |
200 } | |
201 | |
202 void GeolocationDispatcherHost::OnStopUpdating( | |
203 RenderFrameHost* render_frame_host) { | |
204 updating_frames_.erase(render_frame_host); | |
205 RefreshGeolocationOptions(); | |
206 } | |
207 | |
208 void GeolocationDispatcherHost::PauseOrResume(bool should_pause) { | |
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
210 paused_ = should_pause; | |
211 RefreshGeolocationOptions(); | |
212 } | |
213 | |
214 void GeolocationDispatcherHost::RefreshGeolocationOptions() { | |
215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
216 | |
217 if (updating_frames_.empty() || paused_) { | |
218 geolocation_subscription_.reset(); | |
219 return; | |
220 } | |
221 | |
222 bool high_accuracy = false; | |
223 for (std::map<RenderFrameHost*, bool>::iterator i = | |
224 updating_frames_.begin(); i != updating_frames_.end(); ++i) { | |
225 if (i->second) { | |
226 high_accuracy = true; | |
227 break; | |
228 } | |
229 } | |
230 geolocation_subscription_ = GeolocationProvider::GetInstance()-> | |
231 AddLocationUpdateCallback( | |
232 base::Bind(&GeolocationDispatcherHost::OnLocationUpdate, | |
233 base::Unretained(this)), | |
234 high_accuracy); | |
235 } | |
236 | |
237 void GeolocationDispatcherHost::SendGeolocationPermissionResponse( | 97 void GeolocationDispatcherHost::SendGeolocationPermissionResponse( |
238 int render_process_id, | 98 int render_process_id, |
239 int render_frame_id, | 99 int render_frame_id, |
240 int bridge_id, | 100 int bridge_id, |
241 bool allowed) { | 101 bool allowed) { |
242 for (size_t i = 0; i < pending_permissions_.size(); ++i) { | 102 for (size_t i = 0; i < pending_permissions_.size(); ++i) { |
243 if (pending_permissions_[i].render_process_id == render_process_id && | 103 if (pending_permissions_[i].render_process_id == render_process_id && |
244 pending_permissions_[i].render_frame_id == render_frame_id && | 104 pending_permissions_[i].render_frame_id == render_frame_id && |
245 pending_permissions_[i].bridge_id == bridge_id) { | 105 pending_permissions_[i].bridge_id == bridge_id) { |
246 RenderFrameHost* render_frame_host = | 106 RenderFrameHost* render_frame_host = |
(...skipping 10 matching lines...) Expand all Loading... |
257 | 117 |
258 pending_permissions_.erase(pending_permissions_.begin() + i); | 118 pending_permissions_.erase(pending_permissions_.begin() + i); |
259 return; | 119 return; |
260 } | 120 } |
261 } | 121 } |
262 | 122 |
263 NOTREACHED(); | 123 NOTREACHED(); |
264 } | 124 } |
265 | 125 |
266 } // namespace content | 126 } // namespace content |
OLD | NEW |