cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / renderer / geolocation_dispatcher.cc
bloba099a3d96d5741dea16c27ff41ec233d0fb55df2
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;
23 namespace content {
25 GeolocationDispatcher::GeolocationDispatcher(RenderFrame* render_frame)
26 : RenderFrameObserver(render_frame),
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::startUpdating() {
45 GURL url;
46 Send(new GeolocationHostMsg_StartUpdating(
47 routing_id(), url, enable_high_accuracy_));
48 updating_ = true;
51 void GeolocationDispatcher::stopUpdating() {
52 Send(new GeolocationHostMsg_StopUpdating(routing_id()));
53 updating_ = false;
56 void GeolocationDispatcher::setEnableHighAccuracy(bool enable_high_accuracy) {
57 // GeolocationController calls setEnableHighAccuracy(true) before
58 // startUpdating in response to the first high-accuracy Geolocation
59 // subscription. When the last high-accuracy Geolocation unsubscribes
60 // it calls setEnableHighAccuracy(false) after stopUpdating.
61 bool has_changed = enable_high_accuracy_ != enable_high_accuracy;
62 enable_high_accuracy_ = enable_high_accuracy;
63 // We have a different accuracy requirement. Request browser to update.
64 if (updating_ && has_changed)
65 startUpdating();
68 void GeolocationDispatcher::setController(
69 WebGeolocationController* controller) {
70 controller_.reset(controller);
73 bool GeolocationDispatcher::lastPosition(WebGeolocationPosition&) {
74 // The latest position is stored in the browser, not the renderer, so we
75 // would have to fetch it synchronously to give a good value here. The
76 // WebCore::GeolocationController already caches the last position it
77 // receives, so there is not much benefit to more position caching here.
78 return false;
81 // TODO(jknotten): Change the messages to use a security origin, so no
82 // conversion is necessary.
83 void GeolocationDispatcher::requestPermission(
84 const WebGeolocationPermissionRequest& permissionRequest) {
85 int bridge_id = pending_permissions_->add(permissionRequest);
86 base::string16 origin = permissionRequest.securityOrigin().toString();
87 Send(new GeolocationHostMsg_RequestPermission(
88 routing_id(), bridge_id, GURL(origin),
89 blink::WebUserGestureIndicator::isProcessingUserGesture()));
92 void GeolocationDispatcher::cancelPermissionRequest(
93 const WebGeolocationPermissionRequest& permissionRequest) {
94 int bridge_id;
95 pending_permissions_->remove(permissionRequest, bridge_id);
98 // Permission for using geolocation has been set.
99 void GeolocationDispatcher::OnPermissionSet(int bridge_id, bool is_allowed) {
100 WebGeolocationPermissionRequest permissionRequest;
101 if (!pending_permissions_->remove(bridge_id, permissionRequest))
102 return;
103 permissionRequest.setIsAllowed(is_allowed);
106 // We have an updated geolocation position or error code.
107 void GeolocationDispatcher::OnPositionUpdated(
108 const Geoposition& geoposition) {
109 // It is possible for the browser process to have queued an update message
110 // before receiving the stop updating message.
111 if (!updating_)
112 return;
114 if (geoposition.Validate()) {
115 controller_->positionChanged(
116 WebGeolocationPosition(
117 geoposition.timestamp.ToDoubleT(),
118 geoposition.latitude, geoposition.longitude,
119 geoposition.accuracy,
120 // Lowest point on land is at approximately -400 meters.
121 geoposition.altitude > -10000.,
122 geoposition.altitude,
123 geoposition.altitude_accuracy >= 0.,
124 geoposition.altitude_accuracy,
125 geoposition.heading >= 0. && geoposition.heading <= 360.,
126 geoposition.heading,
127 geoposition.speed >= 0.,
128 geoposition.speed));
129 } else {
130 WebGeolocationError::Error code;
131 switch (geoposition.error_code) {
132 case Geoposition::ERROR_CODE_PERMISSION_DENIED:
133 code = WebGeolocationError::ErrorPermissionDenied;
134 break;
135 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE:
136 code = WebGeolocationError::ErrorPositionUnavailable;
137 break;
138 default:
139 NOTREACHED() << geoposition.error_code;
140 return;
142 controller_->errorOccurred(
143 WebGeolocationError(
144 code, blink::WebString::fromUTF8(geoposition.error_message)));
148 } // namespace content