Roll src/third_party/WebKit 5376503:96de67d (svn 199879:199889)
[chromium-blink-merge.git] / content / renderer / geolocation_dispatcher.cc
blob9f54473664f3384f3ceafc2a787b691c699b2132
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/renderer/geolocation_dispatcher.h"
7 #include "content/public/common/geoposition.h"
8 #include "content/renderer/render_view_impl.h"
9 #include "third_party/WebKit/public/platform/WebString.h"
10 #include "third_party/WebKit/public/web/WebGeolocationPermissionRequest.h"
11 #include "third_party/WebKit/public/web/WebGeolocationPermissionRequestManager.h"
12 #include "third_party/WebKit/public/web/WebGeolocationClient.h"
13 #include "third_party/WebKit/public/web/WebGeolocationPosition.h"
14 #include "third_party/WebKit/public/web/WebGeolocationError.h"
15 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
17 using blink::WebGeolocationController;
18 using blink::WebGeolocationError;
19 using blink::WebGeolocationPermissionRequest;
20 using blink::WebGeolocationPermissionRequestManager;
21 using blink::WebGeolocationPosition;
23 namespace content {
25 GeolocationDispatcher::GeolocationDispatcher(RenderFrame* render_frame)
26 : RenderFrameObserver(render_frame),
27 pending_permissions_(new WebGeolocationPermissionRequestManager()),
28 enable_high_accuracy_(false) {
31 GeolocationDispatcher::~GeolocationDispatcher() {}
33 void GeolocationDispatcher::startUpdating() {
34 if (!geolocation_service_) {
35 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
36 mojo::GetProxy(&geolocation_service_));
38 if (enable_high_accuracy_)
39 geolocation_service_->SetHighAccuracy(true);
40 QueryNextPosition();
43 void GeolocationDispatcher::stopUpdating() {
44 geolocation_service_.reset();
47 void GeolocationDispatcher::setEnableHighAccuracy(bool enable_high_accuracy) {
48 // GeolocationController calls setEnableHighAccuracy(true) before
49 // startUpdating in response to the first high-accuracy Geolocation
50 // subscription. When the last high-accuracy Geolocation unsubscribes
51 // it calls setEnableHighAccuracy(false) after stopUpdating.
52 bool has_changed = enable_high_accuracy_ != enable_high_accuracy;
53 enable_high_accuracy_ = enable_high_accuracy;
54 // We have a different accuracy requirement. Request browser to update.
55 if (geolocation_service_ && has_changed)
56 geolocation_service_->SetHighAccuracy(enable_high_accuracy_);
59 void GeolocationDispatcher::setController(
60 WebGeolocationController* controller) {
61 controller_.reset(controller);
64 bool GeolocationDispatcher::lastPosition(WebGeolocationPosition&) {
65 // The latest position is stored in the browser, not the renderer, so we
66 // would have to fetch it synchronously to give a good value here. The
67 // WebCore::GeolocationController already caches the last position it
68 // receives, so there is not much benefit to more position caching here.
69 return false;
72 // TODO(jknotten): Change the messages to use a security origin, so no
73 // conversion is necessary.
74 void GeolocationDispatcher::requestPermission(
75 const WebGeolocationPermissionRequest& permissionRequest) {
76 if (!permission_service_.get()) {
77 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
78 mojo::GetProxy(&permission_service_));
81 int permission_request_id = pending_permissions_->add(permissionRequest);
83 permission_service_->RequestPermission(
84 PERMISSION_NAME_GEOLOCATION,
85 permissionRequest.securityOrigin().toString().utf8(),
86 blink::WebUserGestureIndicator::isProcessingUserGesture(),
87 base::Bind(&GeolocationDispatcher::OnPermissionSet,
88 base::Unretained(this),
89 permission_request_id));
92 void GeolocationDispatcher::cancelPermissionRequest(
93 const blink::WebGeolocationPermissionRequest& permissionRequest) {
94 int permission_request_id;
95 pending_permissions_->remove(permissionRequest, permission_request_id);
98 // Permission for using geolocation has been set.
99 void GeolocationDispatcher::OnPermissionSet(
100 int permission_request_id,
101 PermissionStatus status) {
102 WebGeolocationPermissionRequest permissionRequest;
103 if (!pending_permissions_->remove(permission_request_id, permissionRequest))
104 return;
106 permissionRequest.setIsAllowed(status == PERMISSION_STATUS_GRANTED);
109 void GeolocationDispatcher::QueryNextPosition() {
110 DCHECK(geolocation_service_);
111 geolocation_service_->QueryNextPosition(
112 base::Bind(&GeolocationDispatcher::OnPositionUpdate,
113 base::Unretained(this)));
116 void GeolocationDispatcher::OnPositionUpdate(MojoGeopositionPtr geoposition) {
117 QueryNextPosition();
119 if (geoposition->valid) {
120 controller_->positionChanged(WebGeolocationPosition(
121 geoposition->timestamp,
122 geoposition->latitude,
123 geoposition->longitude,
124 geoposition->accuracy,
125 // Lowest point on land is at approximately -400 meters.
126 geoposition->altitude > -10000.,
127 geoposition->altitude,
128 geoposition->altitude_accuracy >= 0.,
129 geoposition->altitude_accuracy,
130 geoposition->heading >= 0. && geoposition->heading <= 360.,
131 geoposition->heading,
132 geoposition->speed >= 0.,
133 geoposition->speed));
134 } else {
135 WebGeolocationError::Error code;
136 switch (geoposition->error_code) {
137 case Geoposition::ERROR_CODE_PERMISSION_DENIED:
138 code = WebGeolocationError::ErrorPermissionDenied;
139 break;
140 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE:
141 code = WebGeolocationError::ErrorPositionUnavailable;
142 break;
143 default:
144 NOTREACHED() << geoposition->error_code;
145 return;
147 controller_->errorOccurred(WebGeolocationError(
148 code, blink::WebString::fromUTF8(geoposition->error_message)));
152 } // namespace content