cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / browser / geolocation / geolocation_dispatcher_host.cc
blobd677ba038edce8f533f83444485801676b1d7bfa
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"
7 #include <utility>
9 #include "base/bind.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"
21 namespace content {
22 namespace {
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,
39 // Timeout.
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;
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;
62 UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode",
63 code,
64 GEOPOSITION_ERROR_CODE_COUNT);
67 } // namespace
69 GeolocationDispatcherHost::PendingPermission::PendingPermission(
70 int render_frame_id,
71 int render_process_id,
72 int bridge_id,
73 const GURL& origin)
74 : render_frame_id(render_frame_id),
75 render_process_id(render_process_id),
76 bridge_id(bridge_id),
77 origin(origin) {
80 GeolocationDispatcherHost::PendingPermission::~PendingPermission() {
83 GeolocationDispatcherHost::GeolocationDispatcherHost(
84 WebContents* web_contents)
85 : WebContentsObserver(web_contents),
86 paused_(false),
87 weak_factory_(this) {
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();
119 paused_ = false;
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)
128 return;
130 CancelPermissionRequestsForFrame(render_frame_host);
133 bool GeolocationDispatcherHost::OnMessageReceived(
134 const IPC::Message& msg, RenderFrameHost* render_frame_host) {
135 bool handled = true;
136 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg,
137 render_frame_host)
138 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission,
139 OnRequestPermission)
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()
144 return handled;
147 void GeolocationDispatcherHost::OnLocationUpdate(
148 const Geoposition& geoposition) {
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
151 RecordGeopositionErrorCode(geoposition.error_code);
152 if (paused_)
153 return;
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,
170 web_contents(),
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,
180 int bridge_id,
181 const GURL& requesting_origin,
182 bool user_gesture) {
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,
192 web_contents(),
193 bridge_id,
194 requesting_origin,
195 user_gesture,
196 base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse,
197 weak_factory_.GetWeakPtr(),
198 render_process_id,
199 render_frame_id,
200 bridge_id));
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();
238 return;
241 bool high_accuracy = false;
242 for (std::map<RenderFrameHost*, bool>::iterator i =
243 updating_frames_.begin(); i != updating_frames_.end(); ++i) {
244 if (i->second) {
245 high_accuracy = true;
246 break;
249 geolocation_subscription_ = GeolocationProvider::GetInstance()->
250 AddLocationUpdateCallback(
251 base::Bind(&GeolocationDispatcherHost::OnLocationUpdate,
252 base::Unretained(this)),
253 high_accuracy);
256 void GeolocationDispatcherHost::SendGeolocationPermissionResponse(
257 int render_process_id,
258 int render_frame_id,
259 int bridge_id,
260 bool allowed) {
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));
272 if (allowed) {
273 GeolocationProviderImpl::GetInstance()->
274 UserDidOptIntoLocationServices();
277 pending_permissions_.erase(pending_permissions_.begin() + i);
278 return;
282 NOTREACHED();
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,
295 web_contents(),
296 pending_permissions_[i].bridge_id,
297 pending_permissions_[i].origin);
298 pending_permissions_.erase(pending_permissions_.begin() + i);
303 } // namespace content