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/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
;
25 GeolocationDispatcher::GeolocationDispatcher(RenderFrame
* render_frame
)
26 : RenderFrameObserver(render_frame
),
27 pending_permissions_(new WebGeolocationPermissionRequestManager()),
28 enable_high_accuracy_(false),
32 GeolocationDispatcher::~GeolocationDispatcher() {}
34 bool GeolocationDispatcher::OnMessageReceived(const IPC::Message
& message
) {
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)
44 void GeolocationDispatcher::geolocationDestroyed() {
49 void GeolocationDispatcher::startUpdating() {
51 Send(new GeolocationHostMsg_StartUpdating(
52 routing_id(), url
, enable_high_accuracy_
));
56 void GeolocationDispatcher::stopUpdating() {
57 Send(new GeolocationHostMsg_StopUpdating(routing_id()));
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
)
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.
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 base::string16 origin
= permissionRequest
.securityOrigin().toString();
92 Send(new GeolocationHostMsg_RequestPermission(
93 routing_id(), bridge_id
, GURL(origin
),
94 blink::WebUserGestureIndicator::isProcessingUserGesture()));
97 // TODO(jknotten): Change the messages to use a security origin, so no
98 // conversion is necessary.
99 void GeolocationDispatcher::cancelPermissionRequest(
100 const WebGeolocationPermissionRequest
& permissionRequest
) {
102 if (!pending_permissions_
->remove(permissionRequest
, bridge_id
))
104 base::string16 origin
= permissionRequest
.securityOrigin().toString();
105 Send(new GeolocationHostMsg_CancelPermissionRequest(
106 routing_id(), bridge_id
, GURL(origin
)));
109 // Permission for using geolocation has been set.
110 void GeolocationDispatcher::OnPermissionSet(int bridge_id
, bool is_allowed
) {
111 WebGeolocationPermissionRequest permissionRequest
;
112 if (!pending_permissions_
->remove(bridge_id
, permissionRequest
))
114 permissionRequest
.setIsAllowed(is_allowed
);
117 // We have an updated geolocation position or error code.
118 void GeolocationDispatcher::OnPositionUpdated(
119 const Geoposition
& geoposition
) {
120 // It is possible for the browser process to have queued an update message
121 // before receiving the stop updating message.
125 if (geoposition
.Validate()) {
126 controller_
->positionChanged(
127 WebGeolocationPosition(
128 geoposition
.timestamp
.ToDoubleT(),
129 geoposition
.latitude
, geoposition
.longitude
,
130 geoposition
.accuracy
,
131 // Lowest point on land is at approximately -400 meters.
132 geoposition
.altitude
> -10000.,
133 geoposition
.altitude
,
134 geoposition
.altitude_accuracy
>= 0.,
135 geoposition
.altitude_accuracy
,
136 geoposition
.heading
>= 0. && geoposition
.heading
<= 360.,
138 geoposition
.speed
>= 0.,
141 WebGeolocationError::Error code
;
142 switch (geoposition
.error_code
) {
143 case Geoposition::ERROR_CODE_PERMISSION_DENIED
:
144 code
= WebGeolocationError::ErrorPermissionDenied
;
146 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE
:
147 code
= WebGeolocationError::ErrorPositionUnavailable
;
150 NOTREACHED() << geoposition
.error_code
;
153 controller_
->errorOccurred(
155 code
, blink::WebString::fromUTF8(geoposition
.error_message
)));
159 } // namespace content