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" | |
bulach
2010/12/13 14:49:50
\n
| |
8 #include "chrome/renderer/render_view.h" | |
9 #include "ipc/ipc_message.h" | |
10 #include "third_party/WebKit/WebKit/chromium/public/WebGeolocationPermissionRequ est.h" | |
11 #include "third_party/WebKit/WebKit/chromium/public/WebGeolocationPermissionRequ estManager.h" | |
12 #include "third_party/WebKit/WebKit/chromium/public/WebGeolocationClient.h" | |
13 #include "third_party/WebKit/WebKit/chromium/public/WebGeolocationPosition.h" | |
14 #include "third_party/WebKit/WebKit/chromium/public/WebGeolocationError.h" | |
15 #include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h" | |
16 | |
17 using namespace WebKit; | |
18 | |
19 GeolocationDispatcher::GeolocationDispatcher(RenderView* render_view) | |
20 : render_view_(render_view), | |
21 pending_permissions_(new WebGeolocationPermissionRequestManager()), | |
22 enable_high_accuracy_(false), | |
23 updating_(false) { | |
24 } | |
25 | |
26 GeolocationDispatcher::~GeolocationDispatcher() {} | |
27 | |
28 bool GeolocationDispatcher::OnMessageReceived(const IPC::Message& message) { | |
29 bool handled = true; | |
30 IPC_BEGIN_MESSAGE_MAP(GeolocationDispatcher, message) | |
31 IPC_MESSAGE_HANDLER(ViewMsg_Geolocation_PermissionSet, | |
32 OnGeolocationPermissionSet) | |
33 IPC_MESSAGE_HANDLER(ViewMsg_Geolocation_PositionUpdated, | |
34 OnGeolocationPositionUpdated) | |
35 IPC_MESSAGE_UNHANDLED(handled = false) | |
36 IPC_END_MESSAGE_MAP() | |
37 return handled; | |
38 } | |
39 | |
40 void GeolocationDispatcher::geolocationDestroyed() { | |
41 controller_.reset(); | |
42 DCHECK(!updating_); | |
43 } | |
44 | |
45 void GeolocationDispatcher::startUpdating() { | |
46 // TODO(jknotten): Remove url and bridge_id from StartUpdating message | |
47 // once we have switched over to client-based geolocation. | |
48 GURL url; | |
49 render_view_->Send(new ViewHostMsg_Geolocation_StartUpdating( | |
50 render_view_->routing_id(), -1, url, enable_high_accuracy_)); | |
51 updating_ = true; | |
52 } | |
53 | |
54 void GeolocationDispatcher::stopUpdating() { | |
55 // TODO(jknotten): Remove url and bridge_id from StopUpdating message | |
56 // once we have switched over to client-based geolocation. | |
57 render_view_->Send(new ViewHostMsg_Geolocation_StopUpdating( | |
58 render_view_->routing_id(), -1)); | |
59 updating_ = false; | |
60 } | |
61 | |
62 void GeolocationDispatcher::setEnableHighAccuracy(bool enable_high_accuracy) { | |
63 // GeolocationController calls setEnableHighAccuracy(true) before | |
64 // startUpdating in response to the first high-accuracy Geolocation | |
65 // subscription. When the last high-accuracy Geolocation unsubscribes | |
66 // it calls setEnableHighAccuracy(false) after stopUpdating. | |
67 enable_high_accuracy_ = enable_high_accuracy; | |
68 | |
69 // If we are already updating, send update the browser with the new | |
70 // accuracy requirements. | |
bulach
2010/12/13 14:49:50
maybe:
bool has_changed = enable_high_accuracy_ !
| |
71 if (updating_) | |
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 |