OLD | NEW |
| (Empty) |
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/public/common/geoposition.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 | |
16 using blink::WebGeolocationController; | |
17 using blink::WebGeolocationError; | |
18 using blink::WebGeolocationPermissionRequest; | |
19 using blink::WebGeolocationPermissionRequestManager; | |
20 using blink::WebGeolocationPosition; | |
21 | |
22 namespace content { | |
23 | |
24 GeolocationDispatcher::GeolocationDispatcher(RenderFrame* render_frame) | |
25 : RenderFrameObserver(render_frame), | |
26 pending_permissions_(new WebGeolocationPermissionRequestManager()), | |
27 enable_high_accuracy_(false) { | |
28 } | |
29 | |
30 GeolocationDispatcher::~GeolocationDispatcher() {} | |
31 | |
32 void GeolocationDispatcher::startUpdating() { | |
33 if (!geolocation_service_) { | |
34 render_frame()->GetServiceRegistry()->ConnectToRemoteService( | |
35 mojo::GetProxy(&geolocation_service_)); | |
36 } | |
37 if (enable_high_accuracy_) | |
38 geolocation_service_->SetHighAccuracy(true); | |
39 QueryNextPosition(); | |
40 } | |
41 | |
42 void GeolocationDispatcher::stopUpdating() { | |
43 geolocation_service_.reset(); | |
44 } | |
45 | |
46 void GeolocationDispatcher::setEnableHighAccuracy(bool enable_high_accuracy) { | |
47 // GeolocationController calls setEnableHighAccuracy(true) before | |
48 // startUpdating in response to the first high-accuracy Geolocation | |
49 // subscription. When the last high-accuracy Geolocation unsubscribes | |
50 // it calls setEnableHighAccuracy(false) after stopUpdating. | |
51 bool has_changed = enable_high_accuracy_ != enable_high_accuracy; | |
52 enable_high_accuracy_ = enable_high_accuracy; | |
53 // We have a different accuracy requirement. Request browser to update. | |
54 if (geolocation_service_ && has_changed) | |
55 geolocation_service_->SetHighAccuracy(enable_high_accuracy_); | |
56 } | |
57 | |
58 void GeolocationDispatcher::setController( | |
59 WebGeolocationController* controller) { | |
60 controller_.reset(controller); | |
61 } | |
62 | |
63 bool GeolocationDispatcher::lastPosition(WebGeolocationPosition&) { | |
64 // The latest position is stored in the browser, not the renderer, so we | |
65 // would have to fetch it synchronously to give a good value here. The | |
66 // WebCore::GeolocationController already caches the last position it | |
67 // receives, so there is not much benefit to more position caching here. | |
68 return false; | |
69 } | |
70 | |
71 // TODO(jknotten): Change the messages to use a security origin, so no | |
72 // conversion is necessary. | |
73 void GeolocationDispatcher::requestPermission( | |
74 const WebGeolocationPermissionRequest& permissionRequest) { | |
75 num_pending_permission_requests_++; | |
76 if (!permission_service_.get()) { | |
77 render_frame()->GetServiceRegistry()->ConnectToRemoteService( | |
78 mojo::GetProxy(&permission_service_)); | |
79 } | |
80 | |
81 int permission_request_id = pending_permissions_->add(permissionRequest); | |
82 | |
83 permission_service_->RequestPermission( | |
84 blink::mojom::PermissionName::GEOLOCATION, | |
85 permissionRequest.getSecurityOrigin().toString().utf8(), | |
86 base::Bind(&GeolocationDispatcher::OnPermissionSet, | |
87 base::Unretained(this), permission_request_id)); | |
88 } | |
89 | |
90 void GeolocationDispatcher::cancelPermissionRequest( | |
91 const blink::WebGeolocationPermissionRequest& permissionRequest) { | |
92 int permission_request_id; | |
93 pending_permissions_->remove(permissionRequest, permission_request_id); | |
94 if (--num_pending_permission_requests_ == 0) | |
95 permission_service_.reset(); | |
96 } | |
97 | |
98 // Permission for using geolocation has been set. | |
99 void GeolocationDispatcher::OnPermissionSet( | |
100 int permission_request_id, | |
101 blink::mojom::PermissionStatus status) { | |
102 WebGeolocationPermissionRequest permissionRequest; | |
103 if (!pending_permissions_->remove(permission_request_id, permissionRequest)) | |
104 return; | |
105 | |
106 if (--num_pending_permission_requests_ == 0) | |
107 permission_service_.reset(); | |
108 | |
109 permissionRequest.setIsAllowed(status == | |
110 blink::mojom::PermissionStatus::GRANTED); | |
111 } | |
112 | |
113 void GeolocationDispatcher::QueryNextPosition() { | |
114 DCHECK(geolocation_service_); | |
115 geolocation_service_->QueryNextPosition( | |
116 base::Bind(&GeolocationDispatcher::OnPositionUpdate, | |
117 base::Unretained(this))); | |
118 } | |
119 | |
120 void GeolocationDispatcher::OnPositionUpdate( | |
121 blink::mojom::GeopositionPtr geoposition) { | |
122 QueryNextPosition(); | |
123 | |
124 if (geoposition->valid) { | |
125 controller_->positionChanged(WebGeolocationPosition( | |
126 geoposition->timestamp, | |
127 geoposition->latitude, | |
128 geoposition->longitude, | |
129 geoposition->accuracy, | |
130 // Lowest point on land is at approximately -400 meters. | |
131 geoposition->altitude > -10000., | |
132 geoposition->altitude, | |
133 geoposition->altitude_accuracy >= 0., | |
134 geoposition->altitude_accuracy, | |
135 geoposition->heading >= 0. && geoposition->heading <= 360., | |
136 geoposition->heading, | |
137 geoposition->speed >= 0., | |
138 geoposition->speed)); | |
139 } else { | |
140 WebGeolocationError::Error code; | |
141 switch (geoposition->error_code) { | |
142 case blink::mojom::Geoposition::ErrorCode::PERMISSION_DENIED: | |
143 code = WebGeolocationError::ErrorPermissionDenied; | |
144 break; | |
145 case blink::mojom::Geoposition::ErrorCode::POSITION_UNAVAILABLE: | |
146 code = WebGeolocationError::ErrorPositionUnavailable; | |
147 break; | |
148 default: | |
149 NOTREACHED() << geoposition->error_code; | |
150 return; | |
151 } | |
152 controller_->errorOccurred(WebGeolocationError( | |
153 code, blink::WebString::fromUTF8(geoposition->error_message))); | |
154 } | |
155 } | |
156 | |
157 } // namespace content | |
OLD | NEW |