Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / geolocation_dispatcher.cc
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.
4
5 #include "content/renderer/geolocation_dispatcher.h"
6
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"
16
17 using blink::WebGeolocationController;
18 using blink::WebGeolocationError;
19 using blink::WebGeolocationPermissionRequest;
20 using blink::WebGeolocationPermissionRequestManager;
21 using blink::WebGeolocationPosition;
22
23 namespace content {
24
25 GeolocationDispatcher::GeolocationDispatcher(RenderViewImpl* render_view)
26     : RenderViewObserver(render_view),
27       pending_permissions_(new WebGeolocationPermissionRequestManager()),
28       enable_high_accuracy_(false),
29       updating_(false) {
30 }
31
32 GeolocationDispatcher::~GeolocationDispatcher() {}
33
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;
42 }
43
44 void GeolocationDispatcher::geolocationDestroyed() {
45   controller_.reset();
46   DCHECK(!updating_);
47 }
48
49 void GeolocationDispatcher::startUpdating() {
50   GURL url;
51   Send(new GeolocationHostMsg_StartUpdating(
52       routing_id(), url, enable_high_accuracy_));
53   updating_ = true;
54 }
55
56 void GeolocationDispatcher::stopUpdating() {
57   Send(new GeolocationHostMsg_StopUpdating(routing_id()));
58   updating_ = false;
59 }
60
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();
71 }
72
73 void GeolocationDispatcher::setController(
74     WebGeolocationController* controller) {
75   controller_.reset(controller);
76 }
77
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;
84 }
85
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()));
95 }
96
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)));
107 }
108
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);
115 }
116
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;
124
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;
152     }
153     controller_->errorOccurred(
154         WebGeolocationError(
155             code, blink::WebString::fromUTF8(geoposition.error_message)));
156   }
157 }
158
159 }  // namespace content