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" |
10 #include "content/browser/frame_host/render_frame_host_impl.h" | 11 #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" |
13 #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" |
14 #include "content/public/browser/content_browser_client.h" | 17 #include "content/public/browser/content_browser_client.h" |
| 18 #include "content/public/common/geoposition.h" |
15 #include "content/common/geolocation_messages.h" | 19 #include "content/common/geolocation_messages.h" |
16 | 20 |
17 namespace content { | 21 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 |
18 | 68 |
19 GeolocationDispatcherHost::PendingPermission::PendingPermission( | 69 GeolocationDispatcherHost::PendingPermission::PendingPermission( |
20 int render_frame_id, | 70 int render_frame_id, |
21 int render_process_id, | 71 int render_process_id, |
22 int bridge_id, | 72 int bridge_id, |
23 const GURL& origin) | 73 const GURL& origin) |
24 : render_frame_id(render_frame_id), | 74 : render_frame_id(render_frame_id), |
25 render_process_id(render_process_id), | 75 render_process_id(render_process_id), |
26 bridge_id(bridge_id), | 76 bridge_id(bridge_id), |
27 origin(origin) { | 77 origin(origin) { |
28 } | 78 } |
29 | 79 |
30 GeolocationDispatcherHost::PendingPermission::~PendingPermission() { | 80 GeolocationDispatcherHost::PendingPermission::~PendingPermission() { |
31 } | 81 } |
32 | 82 |
33 GeolocationDispatcherHost::GeolocationDispatcherHost( | 83 GeolocationDispatcherHost::GeolocationDispatcherHost( |
34 WebContents* web_contents) | 84 WebContents* web_contents) |
35 : WebContentsObserver(web_contents), | 85 : WebContentsObserver(web_contents), |
| 86 paused_(false), |
36 weak_factory_(this) { | 87 weak_factory_(this) { |
37 // This is initialized by WebContentsImpl. Do not add any non-trivial | 88 // This is initialized by WebContentsImpl. Do not add any non-trivial |
38 // initialization here, defer to OnStartUpdating which is triggered whenever | 89 // initialization here, defer to OnStartUpdating which is triggered whenever |
39 // a javascript geolocation object is actually initialized. | 90 // a javascript geolocation object is actually initialized. |
40 } | 91 } |
41 | 92 |
42 GeolocationDispatcherHost::~GeolocationDispatcherHost() { | 93 GeolocationDispatcherHost::~GeolocationDispatcherHost() { |
43 } | 94 } |
44 | 95 |
| 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 |
45 void GeolocationDispatcherHost::RenderFrameDeleted( | 108 void GeolocationDispatcherHost::RenderFrameDeleted( |
46 RenderFrameHost* render_frame_host) { | 109 RenderFrameHost* render_frame_host) { |
| 110 OnStopUpdating(render_frame_host); |
| 111 |
47 CancelPermissionRequestsForFrame(render_frame_host); | 112 CancelPermissionRequestsForFrame(render_frame_host); |
48 } | 113 } |
49 | 114 |
| 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 |
50 void GeolocationDispatcherHost::DidNavigateAnyFrame( | 123 void GeolocationDispatcherHost::DidNavigateAnyFrame( |
51 RenderFrameHost* render_frame_host, | 124 RenderFrameHost* render_frame_host, |
52 const LoadCommittedDetails& details, | 125 const LoadCommittedDetails& details, |
53 const FrameNavigateParams& params) { | 126 const FrameNavigateParams& params) { |
54 if (details.is_in_page) | 127 if (details.is_in_page) |
55 return; | 128 return; |
56 | 129 |
57 CancelPermissionRequestsForFrame(render_frame_host); | 130 CancelPermissionRequestsForFrame(render_frame_host); |
58 } | 131 } |
59 | 132 |
60 bool GeolocationDispatcherHost::OnMessageReceived( | 133 bool GeolocationDispatcherHost::OnMessageReceived( |
61 const IPC::Message& msg, RenderFrameHost* render_frame_host) { | 134 const IPC::Message& msg, RenderFrameHost* render_frame_host) { |
62 bool handled = true; | 135 bool handled = true; |
63 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg, | 136 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg, |
64 render_frame_host) | 137 render_frame_host) |
65 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, | 138 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, |
66 OnRequestPermission) | 139 OnRequestPermission) |
| 140 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) |
| 141 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) |
67 IPC_MESSAGE_UNHANDLED(handled = false) | 142 IPC_MESSAGE_UNHANDLED(handled = false) |
68 IPC_END_MESSAGE_MAP() | 143 IPC_END_MESSAGE_MAP() |
69 return handled; | 144 return handled; |
70 } | 145 } |
71 | 146 |
| 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()->RegisterPermissionUsage( |
| 169 content::PERMISSION_GEOLOCATION, |
| 170 web_contents(), |
| 171 frame->GetLastCommittedURL().GetOrigin(), |
| 172 top_frame->GetLastCommittedURL().GetOrigin()); |
| 173 |
| 174 frame->Send(new GeolocationMsg_PositionUpdated( |
| 175 frame->GetRoutingID(), geoposition)); |
| 176 } |
| 177 |
72 void GeolocationDispatcherHost::OnRequestPermission( | 178 void GeolocationDispatcherHost::OnRequestPermission( |
73 RenderFrameHost* render_frame_host, | 179 RenderFrameHost* render_frame_host, |
74 int bridge_id, | 180 int bridge_id, |
75 const GURL& requesting_origin, | 181 const GURL& requesting_origin, |
76 bool user_gesture) { | 182 bool user_gesture) { |
77 int render_process_id = render_frame_host->GetProcess()->GetID(); | 183 int render_process_id = render_frame_host->GetProcess()->GetID(); |
78 int render_frame_id = render_frame_host->GetRoutingID(); | 184 int render_frame_id = render_frame_host->GetRoutingID(); |
79 | 185 |
80 PendingPermission pending_permission( | 186 PendingPermission pending_permission( |
81 render_frame_id, render_process_id, bridge_id, requesting_origin); | 187 render_frame_id, render_process_id, bridge_id, requesting_origin); |
82 pending_permissions_.push_back(pending_permission); | 188 pending_permissions_.push_back(pending_permission); |
83 | 189 |
84 GetContentClient()->browser()->RequestPermission( | 190 GetContentClient()->browser()->RequestPermission( |
85 content::PERMISSION_GEOLOCATION, | 191 content::PERMISSION_GEOLOCATION, |
86 web_contents(), | 192 web_contents(), |
87 bridge_id, | 193 bridge_id, |
88 requesting_origin, | 194 requesting_origin, |
89 user_gesture, | 195 user_gesture, |
90 base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse, | 196 base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse, |
91 weak_factory_.GetWeakPtr(), | 197 weak_factory_.GetWeakPtr(), |
92 render_process_id, | 198 render_process_id, |
93 render_frame_id, | 199 render_frame_id, |
94 bridge_id)); | 200 bridge_id)); |
95 } | 201 } |
96 | 202 |
| 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); |
| 212 |
| 213 updating_frames_[render_frame_host] = enable_high_accuracy; |
| 214 RefreshGeolocationOptions(); |
| 215 if (geoposition_override_.get()) |
| 216 UpdateGeoposition(render_frame_host, *geoposition_override_); |
| 217 } |
| 218 |
| 219 void GeolocationDispatcherHost::OnStopUpdating( |
| 220 RenderFrameHost* render_frame_host) { |
| 221 updating_frames_.erase(render_frame_host); |
| 222 RefreshGeolocationOptions(); |
| 223 } |
| 224 |
| 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_); |
| 231 } |
| 232 |
| 233 void GeolocationDispatcherHost::RefreshGeolocationOptions() { |
| 234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 235 |
| 236 if (updating_frames_.empty() || paused_ || geoposition_override_.get()) { |
| 237 geolocation_subscription_.reset(); |
| 238 return; |
| 239 } |
| 240 |
| 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; |
| 247 } |
| 248 } |
| 249 geolocation_subscription_ = GeolocationProvider::GetInstance()-> |
| 250 AddLocationUpdateCallback( |
| 251 base::Bind(&GeolocationDispatcherHost::OnLocationUpdate, |
| 252 base::Unretained(this)), |
| 253 high_accuracy); |
| 254 } |
| 255 |
97 void GeolocationDispatcherHost::SendGeolocationPermissionResponse( | 256 void GeolocationDispatcherHost::SendGeolocationPermissionResponse( |
98 int render_process_id, | 257 int render_process_id, |
99 int render_frame_id, | 258 int render_frame_id, |
100 int bridge_id, | 259 int bridge_id, |
101 bool allowed) { | 260 bool allowed) { |
102 for (size_t i = 0; i < pending_permissions_.size(); ++i) { | 261 for (size_t i = 0; i < pending_permissions_.size(); ++i) { |
103 if (pending_permissions_[i].render_process_id == render_process_id && | 262 if (pending_permissions_[i].render_process_id == render_process_id && |
104 pending_permissions_[i].render_frame_id == render_frame_id && | 263 pending_permissions_[i].render_frame_id == render_frame_id && |
105 pending_permissions_[i].bridge_id == bridge_id) { | 264 pending_permissions_[i].bridge_id == bridge_id) { |
106 RenderFrameHost* render_frame_host = | 265 RenderFrameHost* render_frame_host = |
(...skipping 28 matching lines...) Expand all Loading... |
135 content::PERMISSION_GEOLOCATION, | 294 content::PERMISSION_GEOLOCATION, |
136 web_contents(), | 295 web_contents(), |
137 pending_permissions_[i].bridge_id, | 296 pending_permissions_[i].bridge_id, |
138 pending_permissions_[i].origin); | 297 pending_permissions_[i].origin); |
139 pending_permissions_.erase(pending_permissions_.begin() + i); | 298 pending_permissions_.erase(pending_permissions_.begin() + i); |
140 } | 299 } |
141 } | 300 } |
142 } | 301 } |
143 | 302 |
144 } // namespace content | 303 } // namespace content |
OLD | NEW |