Don't pass a null ServiceWorker registration with OK status code.
[chromium-blink-merge.git] / content / renderer / geolocation_dispatcher.cc
blob7b48ca5da9b2e62196948385e7aa0f8ca55d4807
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(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 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) {
101 int bridge_id;
102 if (!pending_permissions_->remove(permissionRequest, bridge_id))
103 return;
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))
113 return;
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.
122 if (!updating_)
123 return;
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.,
137 geoposition.heading,
138 geoposition.speed >= 0.,
139 geoposition.speed));
140 } else {
141 WebGeolocationError::Error code;
142 switch (geoposition.error_code) {
143 case Geoposition::ERROR_CODE_PERMISSION_DENIED:
144 code = WebGeolocationError::ErrorPermissionDenied;
145 break;
146 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE:
147 code = WebGeolocationError::ErrorPositionUnavailable;
148 break;
149 default:
150 NOTREACHED() << geoposition.error_code;
151 return;
153 controller_->errorOccurred(
154 WebGeolocationError(
155 code, blink::WebString::fromUTF8(geoposition.error_message)));
159 } // namespace content