Fix build break
[chromium-blink-merge.git] / content / renderer / geolocation_dispatcher.cc
blobca9aa5326f7ad2c0f580abf15b38bb17ed27abb0
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/common/geolocation_messages.h"
8 #include "content/renderer/render_view_impl.h"
9 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebGeolocationPermissionRequest.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebGeolocationPermissionRequestManager.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebGeolocationClient.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebGeolocationPosition.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebGeolocationError.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
17 using WebKit::WebGeolocationController;
18 using WebKit::WebGeolocationError;
19 using WebKit::WebGeolocationPermissionRequest;
20 using WebKit::WebGeolocationPermissionRequestManager;
21 using WebKit::WebGeolocationPosition;
23 namespace content {
25 GeolocationDispatcher::GeolocationDispatcher(RenderViewImpl* render_view)
26 : RenderViewObserver(render_view),
27 pending_permissions_(new WebGeolocationPermissionRequestManager()),
28 enable_high_accuracy_(false),
29 updating_(false) {
32 GeolocationDispatcher::~GeolocationDispatcher() {}
34 bool GeolocationDispatcher::OnMessageReceived(const IPC::Message& message) {
35 bool handled = true;
36 IPC_BEGIN_MESSAGE_MAP(GeolocationDispatcher, message)
37 IPC_MESSAGE_HANDLER(GeolocationMsg_PermissionSet, OnPermissionSet)
38 IPC_MESSAGE_HANDLER(GeolocationMsg_PositionUpdated, OnPositionUpdated)
39 IPC_MESSAGE_UNHANDLED(handled = false)
40 IPC_END_MESSAGE_MAP()
41 return handled;
44 void GeolocationDispatcher::geolocationDestroyed() {
45 controller_.reset();
46 DCHECK(!updating_);
49 void GeolocationDispatcher::startUpdating() {
50 GURL url;
51 Send(new GeolocationHostMsg_StartUpdating(
52 routing_id(), url, enable_high_accuracy_));
53 updating_ = true;
56 void GeolocationDispatcher::stopUpdating() {
57 Send(new GeolocationHostMsg_StopUpdating(routing_id()));
58 updating_ = false;
61 void GeolocationDispatcher::setEnableHighAccuracy(bool enable_high_accuracy) {
62 // GeolocationController calls setEnableHighAccuracy(true) before
63 // startUpdating in response to the first high-accuracy Geolocation
64 // subscription. When the last high-accuracy Geolocation unsubscribes
65 // it calls setEnableHighAccuracy(false) after stopUpdating.
66 bool has_changed = enable_high_accuracy_ != enable_high_accuracy;
67 enable_high_accuracy_ = enable_high_accuracy;
68 // We have a different accuracy requirement. Request browser to update.
69 if (updating_ && has_changed)
70 startUpdating();
73 void GeolocationDispatcher::setController(
74 WebGeolocationController* controller) {
75 controller_.reset(controller);
78 bool GeolocationDispatcher::lastPosition(WebGeolocationPosition&) {
79 // The latest position is stored in the browser, not the renderer, so we
80 // would have to fetch it synchronously to give a good value here. The
81 // WebCore::GeolocationController already caches the last position it
82 // receives, so there is not much benefit to more position caching here.
83 return false;
86 // TODO(jknotten): Change the messages to use a security origin, so no
87 // conversion is necessary.
88 void GeolocationDispatcher::requestPermission(
89 const WebGeolocationPermissionRequest& permissionRequest) {
90 int bridge_id = pending_permissions_->add(permissionRequest);
91 string16 origin = permissionRequest.securityOrigin().toString();
92 Send(new GeolocationHostMsg_RequestPermission(
93 routing_id(), bridge_id, GURL(origin)));
96 // TODO(jknotten): Change the messages to use a security origin, so no
97 // conversion is necessary.
98 void GeolocationDispatcher::cancelPermissionRequest(
99 const WebGeolocationPermissionRequest& permissionRequest) {
100 int bridge_id;
101 if (!pending_permissions_->remove(permissionRequest, bridge_id))
102 return;
103 string16 origin = permissionRequest.securityOrigin().toString();
104 Send(new GeolocationHostMsg_CancelPermissionRequest(
105 routing_id(), bridge_id, GURL(origin)));
108 // Permission for using geolocation has been set.
109 void GeolocationDispatcher::OnPermissionSet(int bridge_id, bool is_allowed) {
110 WebGeolocationPermissionRequest permissionRequest;
111 if (!pending_permissions_->remove(bridge_id, permissionRequest))
112 return;
113 permissionRequest.setIsAllowed(is_allowed);
116 // We have an updated geolocation position or error code.
117 void GeolocationDispatcher::OnPositionUpdated(
118 const Geoposition& geoposition) {
119 // It is possible for the browser process to have queued an update message
120 // before receiving the stop updating message.
121 if (!updating_)
122 return;
124 if (geoposition.Validate()) {
125 controller_->positionChanged(
126 WebGeolocationPosition(
127 geoposition.timestamp.ToDoubleT(),
128 geoposition.latitude, geoposition.longitude,
129 geoposition.accuracy,
130 // Lowest point on land is at approximately -400 meters.
131 geoposition.altitude > -10000.,
132 geoposition.altitude,
133 geoposition.altitude_accuracy >= 0.,
134 geoposition.altitude_accuracy,
135 geoposition.heading >= 0. && geoposition.heading <= 360.,
136 geoposition.heading,
137 geoposition.speed >= 0.,
138 geoposition.speed));
139 } else {
140 WebGeolocationError::Error code;
141 switch (geoposition.error_code) {
142 case Geoposition::ERROR_CODE_PERMISSION_DENIED:
143 code = WebGeolocationError::ErrorPermissionDenied;
144 break;
145 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE:
146 code = WebGeolocationError::ErrorPositionUnavailable;
147 break;
148 default:
149 NOTREACHED() << geoposition.error_code;
150 return;
152 controller_->errorOccurred(
153 WebGeolocationError(
154 code, WebKit::WebString::fromUTF8(geoposition.error_message)));
158 } // namespace content