OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 #if defined(ENABLE_CLIENT_BASED_GEOLOCATION) | |
6 | |
7 #include "chrome/renderer/geolocation_dispatcher.h" | |
8 | |
9 #include "chrome/renderer/render_view.h" | |
10 #include "ipc/ipc_message.h" | |
11 #include "third_party/WebKit/WebKit/chromium/public/WebGeolocationPermissionRequ
est.h" | |
12 #include "third_party/WebKit/WebKit/chromium/public/WebGeolocationPermissionRequ
estManager.h" | |
13 #include "third_party/WebKit/WebKit/chromium/public/WebGeolocationClient.h" | |
14 #include "third_party/WebKit/WebKit/chromium/public/WebGeolocationPosition.h" | |
15 #include "third_party/WebKit/WebKit/chromium/public/WebGeolocationError.h" | |
16 #include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h" | |
17 | |
18 using namespace WebKit; | |
19 | |
20 GeolocationDispatcher::GeolocationDispatcher(RenderView* render_view) | |
21 : render_view_(render_view), | |
22 pending_permissions_(new WebGeolocationPermissionRequestManager()), | |
23 enable_high_accuracy_(false), | |
24 updating_(false) { | |
25 } | |
26 | |
27 GeolocationDispatcher::~GeolocationDispatcher() {} | |
28 | |
29 bool GeolocationDispatcher::OnMessageReceived(const IPC::Message& message) { | |
30 bool handled = true; | |
31 IPC_BEGIN_MESSAGE_MAP(GeolocationDispatcher, message) | |
32 IPC_MESSAGE_HANDLER(ViewMsg_Geolocation_PermissionSet, | |
33 OnGeolocationPermissionSet) | |
34 IPC_MESSAGE_HANDLER(ViewMsg_Geolocation_PositionUpdated, | |
35 OnGeolocationPositionUpdated) | |
36 IPC_MESSAGE_UNHANDLED(handled = false) | |
37 IPC_END_MESSAGE_MAP() | |
38 return handled; | |
39 } | |
40 | |
41 void GeolocationDispatcher::geolocationDestroyed() { | |
42 controller_.reset(); | |
43 DCHECK(!updating_); | |
44 } | |
45 | |
46 void GeolocationDispatcher::startUpdating() { | |
47 // TODO(jknotten): Remove url and bridge_id from StartUpdating message | |
48 // once we have switched over to client-based geolocation. | |
49 GURL url; | |
50 render_view_->Send(new ViewHostMsg_Geolocation_StartUpdating( | |
51 render_view_->routing_id(), -1, url, enable_high_accuracy_)); | |
52 updating_ = true; | |
53 } | |
54 | |
55 void GeolocationDispatcher::stopUpdating() { | |
56 // TODO(jknotten): Remove url and bridge_id from StopUpdating message | |
57 // once we have switched over to client-based geolocation. | |
58 render_view_->Send(new ViewHostMsg_Geolocation_StopUpdating( | |
59 render_view_->routing_id(), -1)); | |
60 updating_ = false; | |
61 } | |
62 | |
63 void GeolocationDispatcher::setEnableHighAccuracy(bool enable_high_accuracy) { | |
64 // GeolocationController calls setEnableHighAccuracy(true) before | |
65 // startUpdating in response to the first high-accuracy Geolocation | |
66 // subscription. When the last high-accuracy Geolocation unsubscribes | |
67 // it calls setEnableHighAccuracy(false) after stopUpdating. | |
68 bool has_changed = enable_high_accuracy_ != enable_high_accuracy; | |
69 enable_high_accuracy_ = enable_high_accuracy; | |
70 // We have a different accuracy requirement. Request browser to update. | |
71 if (updating_ && has_changed) | |
72 startUpdating(); | |
73 } | |
74 | |
75 void GeolocationDispatcher::setController( | |
76 WebGeolocationController* controller) { | |
77 controller_.reset(controller); | |
78 } | |
79 | |
80 bool GeolocationDispatcher::lastPosition(WebGeolocationPosition&) { | |
81 // The latest position is stored in the browser, not the renderer, so we | |
82 // would have to fetch it synchronously to give a good value here. The | |
83 // WebCore::GeolocationController already caches the last position it | |
84 // receives, so there is not much benefit to more position caching here. | |
85 return false; | |
86 } | |
87 | |
88 // TODO(jknotten): Change the messages to use a security origin, so no | |
89 // conversion is necessary. | |
90 void GeolocationDispatcher::requestPermission( | |
91 const WebGeolocationPermissionRequest& permissionRequest) { | |
92 int bridge_id = pending_permissions_->add(permissionRequest); | |
93 string16 origin = permissionRequest.securityOrigin().toString(); | |
94 render_view_->Send(new ViewHostMsg_Geolocation_RequestPermission( | |
95 render_view_->routing_id(), bridge_id, GURL(origin))); | |
96 } | |
97 | |
98 // TODO(jknotten): Change the messages to use a security origin, so no | |
99 // conversion is necessary. | |
100 void GeolocationDispatcher::cancelPermissionRequest( | |
101 const WebGeolocationPermissionRequest& permissionRequest) { | |
102 int bridge_id; | |
103 if (!pending_permissions_->remove(permissionRequest, bridge_id)) | |
104 return; | |
105 string16 origin = permissionRequest.securityOrigin().toString(); | |
106 render_view_->Send(new ViewHostMsg_Geolocation_CancelPermissionRequest( | |
107 render_view_->routing_id(), bridge_id, GURL(origin))); | |
108 } | |
109 | |
110 // Permission for using geolocation has been set. | |
111 void GeolocationDispatcher::OnGeolocationPermissionSet( | |
112 int bridge_id, bool is_allowed) { | |
113 WebGeolocationPermissionRequest permissionRequest; | |
114 if (!pending_permissions_->remove(bridge_id, permissionRequest)) | |
115 return; | |
116 permissionRequest.setIsAllowed(is_allowed); | |
117 } | |
118 | |
119 // We have an updated geolocation position or error code. | |
120 void GeolocationDispatcher::OnGeolocationPositionUpdated( | |
121 const Geoposition& geoposition) { | |
122 DCHECK(updating_); | |
123 DCHECK(geoposition.IsInitialized()); | |
124 if (geoposition.IsValidFix()) { | |
125 controller_->positionChanged( | |
126 WebGeolocationPosition( | |
127 static_cast<int64>(geoposition.timestamp.ToDoubleT() * 1000), | |
128 geoposition.latitude, geoposition.longitude, | |
129 geoposition.accuracy, | |
130 geoposition.is_valid_altitude(), geoposition.altitude, | |
131 geoposition.is_valid_altitude_accuracy(), | |
132 geoposition.altitude_accuracy, | |
133 geoposition.is_valid_heading(), geoposition.heading, | |
134 geoposition.is_valid_speed(), geoposition.speed)); | |
135 } else { | |
136 WebGeolocationError::Error code; | |
137 switch (geoposition.error_code) { | |
138 case Geoposition::ERROR_CODE_PERMISSION_DENIED: | |
139 code = WebGeolocationError::ErrorPermissionDenied; | |
140 break; | |
141 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE: | |
142 code = WebGeolocationError::ErrorPositionUnavailable; | |
143 break; | |
144 default: | |
145 DCHECK(false); | |
146 NOTREACHED() << geoposition.error_code; | |
147 return; | |
148 } | |
149 controller_->errorOccurred( | |
150 WebGeolocationError( | |
151 code, WebKit::WebString::fromUTF8(geoposition.error_message))); | |
152 } | |
153 } | |
154 | |
155 #endif // CLIENT_BASED_GEOLOCATION | |
OLD | NEW |