| 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()->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 | |
| 178 void GeolocationDispatcherHost::OnRequestPermission( | 72 void GeolocationDispatcherHost::OnRequestPermission( |
| 179 RenderFrameHost* render_frame_host, | 73 RenderFrameHost* render_frame_host, |
| 180 int bridge_id, | 74 int bridge_id, |
| 181 const GURL& requesting_origin, | 75 const GURL& requesting_origin, |
| 182 bool user_gesture) { | 76 bool user_gesture) { |
| 183 int render_process_id = render_frame_host->GetProcess()->GetID(); | 77 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 184 int render_frame_id = render_frame_host->GetRoutingID(); | 78 int render_frame_id = render_frame_host->GetRoutingID(); |
| 185 | 79 |
| 186 PendingPermission pending_permission( | 80 PendingPermission pending_permission( |
| 187 render_frame_id, render_process_id, bridge_id, requesting_origin); | 81 render_frame_id, render_process_id, bridge_id, requesting_origin); |
| 188 pending_permissions_.push_back(pending_permission); | 82 pending_permissions_.push_back(pending_permission); |
| 189 | 83 |
| 190 GetContentClient()->browser()->RequestPermission( | 84 GetContentClient()->browser()->RequestPermission( |
| 191 content::PERMISSION_GEOLOCATION, | 85 content::PERMISSION_GEOLOCATION, |
| 192 web_contents(), | 86 web_contents(), |
| 193 bridge_id, | 87 bridge_id, |
| 194 requesting_origin, | 88 requesting_origin, |
| 195 user_gesture, | 89 user_gesture, |
| 196 base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse, | 90 base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse, |
| 197 weak_factory_.GetWeakPtr(), | 91 weak_factory_.GetWeakPtr(), |
| 198 render_process_id, | 92 render_process_id, |
| 199 render_frame_id, | 93 render_frame_id, |
| 200 bridge_id)); | 94 bridge_id)); |
| 201 } | 95 } |
| 202 | 96 |
| 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 | |
| 256 void GeolocationDispatcherHost::SendGeolocationPermissionResponse( | 97 void GeolocationDispatcherHost::SendGeolocationPermissionResponse( |
| 257 int render_process_id, | 98 int render_process_id, |
| 258 int render_frame_id, | 99 int render_frame_id, |
| 259 int bridge_id, | 100 int bridge_id, |
| 260 bool allowed) { | 101 bool allowed) { |
| 261 for (size_t i = 0; i < pending_permissions_.size(); ++i) { | 102 for (size_t i = 0; i < pending_permissions_.size(); ++i) { |
| 262 if (pending_permissions_[i].render_process_id == render_process_id && | 103 if (pending_permissions_[i].render_process_id == render_process_id && |
| 263 pending_permissions_[i].render_frame_id == render_frame_id && | 104 pending_permissions_[i].render_frame_id == render_frame_id && |
| 264 pending_permissions_[i].bridge_id == bridge_id) { | 105 pending_permissions_[i].bridge_id == bridge_id) { |
| 265 RenderFrameHost* render_frame_host = | 106 RenderFrameHost* render_frame_host = |
| (...skipping 28 matching lines...) Expand all Loading... |
| 294 content::PERMISSION_GEOLOCATION, | 135 content::PERMISSION_GEOLOCATION, |
| 295 web_contents(), | 136 web_contents(), |
| 296 pending_permissions_[i].bridge_id, | 137 pending_permissions_[i].bridge_id, |
| 297 pending_permissions_[i].origin); | 138 pending_permissions_[i].origin); |
| 298 pending_permissions_.erase(pending_permissions_.begin() + i); | 139 pending_permissions_.erase(pending_permissions_.begin() + i); |
| 299 } | 140 } |
| 300 } | 141 } |
| 301 } | 142 } |
| 302 | 143 |
| 303 } // namespace content | 144 } // namespace content |
| OLD | NEW |