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
3 // found in the LICENSE file.
5 #include "content/browser/geolocation/geolocation_dispatcher_host.h"
10 #include "base/metrics/histogram.h"
11 #include "content/browser/frame_host/render_frame_host_impl.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"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/content_browser_client.h"
18 #include "content/public/common/geoposition.h"
19 #include "content/common/geolocation_messages.h"
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.
30 // There was no error.
31 GEOPOSITION_ERROR_CODE_NONE
= 0,
33 // User denied use of geolocation.
34 GEOPOSITION_ERROR_CODE_PERMISSION_DENIED
= 1,
36 // Geoposition could not be determined.
37 GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE
= 2,
40 GEOPOSITION_ERROR_CODE_TIMEOUT
= 3,
42 // NOTE: Add entries only immediately above this line.
43 GEOPOSITION_ERROR_CODE_COUNT
= 4
46 void RecordGeopositionErrorCode(Geoposition::ErrorCode error_code
) {
47 GeopositionErrorCode code
= GEOPOSITION_ERROR_CODE_NONE
;
49 case Geoposition::ERROR_CODE_NONE
:
50 code
= GEOPOSITION_ERROR_CODE_NONE
;
52 case Geoposition::ERROR_CODE_PERMISSION_DENIED
:
53 code
= GEOPOSITION_ERROR_CODE_PERMISSION_DENIED
;
55 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE
:
56 code
= GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE
;
58 case Geoposition::ERROR_CODE_TIMEOUT
:
59 code
= GEOPOSITION_ERROR_CODE_TIMEOUT
;
62 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode",
64 GEOPOSITION_ERROR_CODE_COUNT
);
69 GeolocationDispatcherHost::PendingPermission::PendingPermission(
71 int render_process_id
,
74 : render_frame_id(render_frame_id
),
75 render_process_id(render_process_id
),
80 GeolocationDispatcherHost::PendingPermission::~PendingPermission() {
83 GeolocationDispatcherHost::GeolocationDispatcherHost(
84 WebContents
* web_contents
)
85 : WebContentsObserver(web_contents
),
88 // This is initialized by WebContentsImpl. Do not add any non-trivial
89 // initialization here, defer to OnStartUpdating which is triggered whenever
90 // a javascript geolocation object is actually initialized.
93 GeolocationDispatcherHost::~GeolocationDispatcherHost() {
96 void GeolocationDispatcherHost::SetOverride(
97 scoped_ptr
<Geoposition
> geoposition
) {
98 geoposition_override_
.swap(geoposition
);
99 RefreshGeolocationOptions();
100 OnLocationUpdate(*geoposition_override_
);
103 void GeolocationDispatcherHost::ClearOverride() {
104 geoposition_override_
.reset();
105 RefreshGeolocationOptions();
108 void GeolocationDispatcherHost::RenderFrameDeleted(
109 RenderFrameHost
* render_frame_host
) {
110 OnStopUpdating(render_frame_host
);
112 CancelPermissionRequestsForFrame(render_frame_host
);
115 void GeolocationDispatcherHost::RenderViewHostChanged(
116 RenderViewHost
* old_host
,
117 RenderViewHost
* new_host
) {
118 updating_frames_
.clear();
120 geolocation_subscription_
.reset();
123 void GeolocationDispatcherHost::DidNavigateAnyFrame(
124 RenderFrameHost
* render_frame_host
,
125 const LoadCommittedDetails
& details
,
126 const FrameNavigateParams
& params
) {
127 if (details
.is_in_page
)
130 CancelPermissionRequestsForFrame(render_frame_host
);
133 bool GeolocationDispatcherHost::OnMessageReceived(
134 const IPC::Message
& msg
, RenderFrameHost
* render_frame_host
) {
136 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost
, msg
,
138 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission
,
140 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating
, OnStartUpdating
)
141 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating
, OnStopUpdating
)
142 IPC_MESSAGE_UNHANDLED(handled
= false)
143 IPC_END_MESSAGE_MAP()
147 void GeolocationDispatcherHost::OnLocationUpdate(
148 const Geoposition
& geoposition
) {
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
151 RecordGeopositionErrorCode(geoposition
.error_code
);
155 for (std::map
<RenderFrameHost
*, bool>::iterator i
= updating_frames_
.begin();
156 i
!= updating_frames_
.end(); ++i
) {
157 UpdateGeoposition(i
->first
, geoposition
);
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();
168 GetContentClient()->browser()->RegisterPermissionUsage(
169 content::PERMISSION_GEOLOCATION
,
171 frame
->GetLastCommittedURL().GetOrigin(),
172 top_frame
->GetLastCommittedURL().GetOrigin());
174 frame
->Send(new GeolocationMsg_PositionUpdated(
175 frame
->GetRoutingID(), geoposition
));
178 void GeolocationDispatcherHost::OnRequestPermission(
179 RenderFrameHost
* render_frame_host
,
181 const GURL
& requesting_origin
,
183 int render_process_id
= render_frame_host
->GetProcess()->GetID();
184 int render_frame_id
= render_frame_host
->GetRoutingID();
186 PendingPermission
pending_permission(
187 render_frame_id
, render_process_id
, bridge_id
, requesting_origin
);
188 pending_permissions_
.push_back(pending_permission
);
190 GetContentClient()->browser()->RequestPermission(
191 content::PERMISSION_GEOLOCATION
,
196 base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse
,
197 weak_factory_
.GetWeakPtr(),
203 void GeolocationDispatcherHost::OnStartUpdating(
204 RenderFrameHost
* render_frame_host
,
205 const GURL
& requesting_origin
,
206 bool enable_high_accuracy
) {
207 // StartUpdating() can be invoked as a result of high-accuracy mode
208 // being enabled / disabled. No need to record the dispatcher again.
209 UMA_HISTOGRAM_BOOLEAN(
210 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy",
211 enable_high_accuracy
);
213 updating_frames_
[render_frame_host
] = enable_high_accuracy
;
214 RefreshGeolocationOptions();
215 if (geoposition_override_
.get())
216 UpdateGeoposition(render_frame_host
, *geoposition_override_
);
219 void GeolocationDispatcherHost::OnStopUpdating(
220 RenderFrameHost
* render_frame_host
) {
221 updating_frames_
.erase(render_frame_host
);
222 RefreshGeolocationOptions();
225 void GeolocationDispatcherHost::PauseOrResume(bool should_pause
) {
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
227 paused_
= should_pause
;
228 RefreshGeolocationOptions();
229 if (geoposition_override_
.get())
230 OnLocationUpdate(*geoposition_override_
);
233 void GeolocationDispatcherHost::RefreshGeolocationOptions() {
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
236 if (updating_frames_
.empty() || paused_
|| geoposition_override_
.get()) {
237 geolocation_subscription_
.reset();
241 bool high_accuracy
= false;
242 for (std::map
<RenderFrameHost
*, bool>::iterator i
=
243 updating_frames_
.begin(); i
!= updating_frames_
.end(); ++i
) {
245 high_accuracy
= true;
249 geolocation_subscription_
= GeolocationProvider::GetInstance()->
250 AddLocationUpdateCallback(
251 base::Bind(&GeolocationDispatcherHost::OnLocationUpdate
,
252 base::Unretained(this)),
256 void GeolocationDispatcherHost::SendGeolocationPermissionResponse(
257 int render_process_id
,
261 for (size_t i
= 0; i
< pending_permissions_
.size(); ++i
) {
262 if (pending_permissions_
[i
].render_process_id
== render_process_id
&&
263 pending_permissions_
[i
].render_frame_id
== render_frame_id
&&
264 pending_permissions_
[i
].bridge_id
== bridge_id
) {
265 RenderFrameHost
* render_frame_host
=
266 RenderFrameHost::FromID(render_process_id
, render_frame_id
);
267 if (render_frame_host
) {
268 render_frame_host
->Send(new GeolocationMsg_PermissionSet(
269 render_frame_id
, bridge_id
, allowed
));
273 GeolocationProviderImpl::GetInstance()->
274 UserDidOptIntoLocationServices();
277 pending_permissions_
.erase(pending_permissions_
.begin() + i
);
285 void GeolocationDispatcherHost::CancelPermissionRequestsForFrame(
286 RenderFrameHost
* render_frame_host
) {
287 int render_process_id
= render_frame_host
->GetProcess()->GetID();
288 int render_frame_id
= render_frame_host
->GetRoutingID();
290 for (size_t i
= 0; i
< pending_permissions_
.size(); ++i
) {
291 if (pending_permissions_
[i
].render_process_id
== render_process_id
&&
292 pending_permissions_
[i
].render_frame_id
== render_frame_id
) {
293 GetContentClient()->browser()->CancelPermissionRequest(
294 content::PERMISSION_GEOLOCATION
,
296 pending_permissions_
[i
].bridge_id
,
297 pending_permissions_
[i
].origin
);
298 pending_permissions_
.erase(pending_permissions_
.begin() + i
);
303 } // namespace content