mesa gn build: suppress -Wstring-conversion warnings
[chromium-blink-merge.git] / content / renderer / geolocation_dispatcher.cc
blob0660088c3b3d4382bd95cf938ba706d571910b7f
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/child/child_thread.h"
8 #include "content/public/common/geoposition.h"
9 #include "content/renderer/render_view_impl.h"
10 #include "third_party/WebKit/public/platform/WebString.h"
11 #include "third_party/WebKit/public/web/WebGeolocationPermissionRequest.h"
12 #include "third_party/WebKit/public/web/WebGeolocationPermissionRequestManager.h"
13 #include "third_party/WebKit/public/web/WebGeolocationClient.h"
14 #include "third_party/WebKit/public/web/WebGeolocationPosition.h"
15 #include "third_party/WebKit/public/web/WebGeolocationError.h"
16 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
18 using blink::WebGeolocationController;
19 using blink::WebGeolocationError;
20 using blink::WebGeolocationPermissionRequest;
21 using blink::WebGeolocationPermissionRequestManager;
22 using blink::WebGeolocationPosition;
24 namespace content {
26 GeolocationDispatcher::GeolocationDispatcher(RenderFrame* render_frame)
27 : RenderFrameObserver(render_frame),
28 pending_permissions_(new WebGeolocationPermissionRequestManager()),
29 enable_high_accuracy_(false) {
32 GeolocationDispatcher::~GeolocationDispatcher() {}
34 void GeolocationDispatcher::startUpdating() {
35 if (!geolocation_service_.get()) {
36 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
37 &geolocation_service_);
38 geolocation_service_.set_client(this);
40 if (enable_high_accuracy_)
41 geolocation_service_->SetHighAccuracy(true);
44 void GeolocationDispatcher::stopUpdating() {
45 geolocation_service_.reset();
48 void GeolocationDispatcher::setEnableHighAccuracy(bool enable_high_accuracy) {
49 // GeolocationController calls setEnableHighAccuracy(true) before
50 // startUpdating in response to the first high-accuracy Geolocation
51 // subscription. When the last high-accuracy Geolocation unsubscribes
52 // it calls setEnableHighAccuracy(false) after stopUpdating.
53 bool has_changed = enable_high_accuracy_ != enable_high_accuracy;
54 enable_high_accuracy_ = enable_high_accuracy;
55 // We have a different accuracy requirement. Request browser to update.
56 if (geolocation_service_.get() && has_changed)
57 geolocation_service_->SetHighAccuracy(enable_high_accuracy_);
60 void GeolocationDispatcher::setController(
61 WebGeolocationController* controller) {
62 controller_.reset(controller);
65 bool GeolocationDispatcher::lastPosition(WebGeolocationPosition&) {
66 // The latest position is stored in the browser, not the renderer, so we
67 // would have to fetch it synchronously to give a good value here. The
68 // WebCore::GeolocationController already caches the last position it
69 // receives, so there is not much benefit to more position caching here.
70 return false;
73 // TODO(jknotten): Change the messages to use a security origin, so no
74 // conversion is necessary.
75 void GeolocationDispatcher::requestPermission(
76 const WebGeolocationPermissionRequest& permissionRequest) {
77 if (!permission_service_.get()) {
78 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
79 &permission_service_);
82 int permission_request_id = pending_permissions_->add(permissionRequest);
84 permission_service_->RequestPermission(
85 PERMISSION_NAME_GEOLOCATION,
86 permissionRequest.securityOrigin().toString().utf8(),
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::OnLocationUpdate(MojoGeopositionPtr geoposition) {
110 DCHECK(geolocation_service_.get());
112 if (geoposition->valid) {
113 controller_->positionChanged(WebGeolocationPosition(
114 geoposition->timestamp,
115 geoposition->latitude,
116 geoposition->longitude,
117 geoposition->accuracy,
118 // Lowest point on land is at approximately -400 meters.
119 geoposition->altitude > -10000.,
120 geoposition->altitude,
121 geoposition->altitude_accuracy >= 0.,
122 geoposition->altitude_accuracy,
123 geoposition->heading >= 0. && geoposition->heading <= 360.,
124 geoposition->heading,
125 geoposition->speed >= 0.,
126 geoposition->speed));
127 } else {
128 WebGeolocationError::Error code;
129 switch (geoposition->error_code) {
130 case Geoposition::ERROR_CODE_PERMISSION_DENIED:
131 code = WebGeolocationError::ErrorPermissionDenied;
132 break;
133 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE:
134 code = WebGeolocationError::ErrorPositionUnavailable;
135 break;
136 default:
137 NOTREACHED() << geoposition->error_code;
138 return;
140 controller_->errorOccurred(WebGeolocationError(
141 code, blink::WebString::fromUTF8(geoposition->error_message)));
145 } // namespace content