Index: content/browser/geolocation/geolocation_service_impl.cc |
diff --git a/content/browser/geolocation/geolocation_service_impl.cc b/content/browser/geolocation/geolocation_service_impl.cc |
index aae95ac01b6bf465564048bee3e7b197d2cc70b8..55df1d87540bd5ef0a8878ae44e8fd72524e878c 100644 |
--- a/content/browser/geolocation/geolocation_service_impl.cc |
+++ b/content/browser/geolocation/geolocation_service_impl.cc |
@@ -7,10 +7,15 @@ |
#include "base/bind.h" |
#include "base/metrics/histogram.h" |
#include "content/browser/geolocation/geolocation_service_context.h" |
-#include "content/public/common/mojo_geoposition.mojom.h" |
+#include "content/public/browser/browser_context.h" |
+#include "content/public/browser/geolocation_provider.h" |
+#include "content/public/browser/permission_manager.h" |
+#include "content/public/browser/permission_type.h" |
+#include "content/public/browser/render_frame_host.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "content/public/common/geoposition.h" |
namespace content { |
- |
namespace { |
// Geoposition error codes for reporting in UMA. |
@@ -51,47 +56,32 @@ void RecordGeopositionErrorCode(Geoposition::ErrorCode error_code) { |
code = GEOPOSITION_ERROR_CODE_TIMEOUT; |
break; |
} |
- UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode", |
- code, |
+ UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode", code, |
GEOPOSITION_ERROR_CODE_COUNT); |
} |
} // namespace |
GeolocationServiceImpl::GeolocationServiceImpl( |
- mojo::InterfaceRequest<GeolocationService> request, |
GeolocationServiceContext* context, |
- const base::Closure& update_callback) |
- : binding_(this, request.Pass()), |
- context_(context), |
- update_callback_(update_callback), |
- high_accuracy_(false), |
- has_position_to_report_(false) { |
+ RenderFrameHost* render_frame_host) |
+ : context_(context), |
+ render_process_id_(render_frame_host->GetProcess()->GetID()), |
+ render_frame_id_(render_frame_host->GetRoutingID()), |
+ high_accuracy_(false) { |
DCHECK(context_); |
- binding_.set_connection_error_handler( |
- base::Bind(&GeolocationServiceImpl::OnConnectionError, |
- base::Unretained(this))); |
} |
-GeolocationServiceImpl::~GeolocationServiceImpl() { |
- // Make sure to respond to any pending callback even without a valid position. |
- if (!position_callback_.is_null()) { |
- if (!current_position_.valid) { |
- current_position_.error_code = MojoGeoposition::ErrorCode( |
- GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE); |
- current_position_.error_message = mojo::String(""); |
- } |
- ReportCurrentPosition(); |
- } |
-} |
+GeolocationServiceImpl::~GeolocationServiceImpl() = default; |
void GeolocationServiceImpl::PauseUpdates() { |
geolocation_subscription_.reset(); |
} |
void GeolocationServiceImpl::ResumeUpdates() { |
- if (position_override_.Validate()) { |
- OnLocationUpdate(position_override_); |
+ if (context_->position_override() && |
+ context_->position_override()->Validate()) { |
+ OnLocationUpdate(*context_->position_override()); |
return; |
} |
@@ -112,8 +102,9 @@ void GeolocationServiceImpl::SetHighAccuracy(bool high_accuracy) { |
high_accuracy); |
high_accuracy_ = high_accuracy; |
- if (position_override_.Validate()) { |
- OnLocationUpdate(position_override_); |
+ if (context_->position_override() && |
+ context_->position_override()->Validate()) { |
+ OnLocationUpdate(*context_->position_override()); |
return; |
} |
@@ -124,39 +115,30 @@ void GeolocationServiceImpl::QueryNextPosition( |
const PositionCallback& callback) { |
if (!position_callback_.is_null()) { |
DVLOG(1) << "Overlapped call to QueryNextPosition!"; |
- OnConnectionError(); // Simulate a connection error. |
+ context_->DestroyService(this); |
return; |
} |
position_callback_ = callback; |
- if (has_position_to_report_) |
+ if (current_position_) |
ReportCurrentPosition(); |
} |
-void GeolocationServiceImpl::SetOverride(const Geoposition& position) { |
- position_override_ = position; |
- if (!position_override_.Validate()) { |
+void GeolocationServiceImpl::OnOverrideSet() { |
+ if (!context_->position_override()->Validate()) { |
ResumeUpdates(); |
} |
geolocation_subscription_.reset(); |
- OnLocationUpdate(position_override_); |
+ OnLocationUpdate(*context_->position_override()); |
} |
void GeolocationServiceImpl::ClearOverride() { |
- position_override_ = Geoposition(); |
StartListeningForUpdates(); |
} |
-void GeolocationServiceImpl::OnConnectionError() { |
- context_->ServiceHadConnectionError(this); |
- |
- // The above call deleted this instance, so the only safe thing to do is |
- // return. |
-} |
- |
void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) { |
RecordGeopositionErrorCode(position.error_code); |
DCHECK(context_); |
@@ -164,31 +146,51 @@ void GeolocationServiceImpl::OnLocationUpdate(const Geoposition& position) { |
if (context_->paused()) |
return; |
- update_callback_.Run(); |
- |
- current_position_.valid = position.Validate(); |
- current_position_.latitude = position.latitude; |
- current_position_.longitude = position.longitude; |
- current_position_.altitude = position.altitude; |
- current_position_.accuracy = position.accuracy; |
- current_position_.altitude_accuracy = position.altitude_accuracy; |
- current_position_.heading = position.heading; |
- current_position_.speed = position.speed; |
- current_position_.timestamp = position.timestamp.ToDoubleT(); |
- current_position_.error_code = |
+ ReportPermissionUsage(); |
+ |
+ if (!current_position_) |
+ current_position_ = MojoGeoposition::New(); |
+ current_position_->valid = position.Validate(); |
+ current_position_->latitude = position.latitude; |
+ current_position_->longitude = position.longitude; |
+ current_position_->altitude = position.altitude; |
+ current_position_->accuracy = position.accuracy; |
+ current_position_->altitude_accuracy = position.altitude_accuracy; |
+ current_position_->heading = position.heading; |
+ current_position_->speed = position.speed; |
+ current_position_->timestamp = position.timestamp.ToDoubleT(); |
+ current_position_->error_code = |
MojoGeoposition::ErrorCode(position.error_code); |
- current_position_.error_message = position.error_message; |
- |
- has_position_to_report_ = true; |
+ current_position_->error_message = position.error_message; |
if (!position_callback_.is_null()) |
ReportCurrentPosition(); |
} |
void GeolocationServiceImpl::ReportCurrentPosition() { |
- position_callback_.Run(current_position_.Clone()); |
+ position_callback_.Run(std::move(current_position_)); |
position_callback_.reset(); |
- has_position_to_report_ = false; |
+} |
+ |
+void GeolocationServiceImpl::ReportPermissionUsage() { |
+ RenderFrameHost* render_frame_host = |
+ RenderFrameHost::FromID(render_process_id_, render_frame_id_); |
+ if (!render_frame_host) |
+ return; |
+ |
+ PermissionManager* permission_manager = render_frame_host->GetProcess() |
+ ->GetBrowserContext() |
+ ->GetPermissionManager(); |
+ if (!permission_manager) |
+ return; |
+ |
+ permission_manager->RegisterPermissionUsage( |
+ PermissionType::GEOLOCATION, |
+ render_frame_host->GetLastCommittedURL().GetOrigin(), |
+ WebContents::FromRenderFrameHost(render_frame_host) |
+ ->GetMainFrame() |
+ ->GetLastCommittedURL() |
+ .GetOrigin()); |
} |
} // namespace content |