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 if (!permission_service_.get()) { | |
76 render_frame()->GetServiceRegistry()->ConnectToRemoteService( | |
77 mojo::GetProxy(&permission_service_)); | |
78 } | |
79 | |
80 int permission_request_id = pending_permissions_->add(permissionRequest); | |
81 | |
82 permission_service_->RequestPermission( | |
83 blink::mojom::PermissionName::GEOLOCATION, | |
84 permissionRequest.getSecurityOrigin().toString().utf8(), | |
85 base::Bind(&GeolocationDispatcher::OnPermissionSet, | |
86 base::Unretained(this), permission_request_id)); | |
87 } | |
88 | |
89 void GeolocationDispatcher::cancelPermissionRequest( | |
90 const blink::WebGeolocationPermissionRequest& permissionRequest) { | |
91 int permission_request_id; | |
92 pending_permissions_->remove(permissionRequest, permission_request_id); | |
93 } | |
94 | |
95 // Permission for using geolocation has been set. | |
96 void GeolocationDispatcher::OnPermissionSet( | |
97 int permission_request_id, | |
98 blink::mojom::PermissionStatus status) { | |
99 WebGeolocationPermissionRequest permissionRequest; | |
100 if (!pending_permissions_->remove(permission_request_id, permissionRequest)) | |
101 return; | |
102 | |
103 permissionRequest.setIsAllowed(status == | |
104 blink::mojom::PermissionStatus::GRANTED); | |
105 } | |
106 | |
107 void GeolocationDispatcher::QueryNextPosition() { | |
108 DCHECK(geolocation_service_); | |
109 geolocation_service_->QueryNextPosition( | |
110 base::Bind(&GeolocationDispatcher::OnPositionUpdate, | |
111 base::Unretained(this))); | |
112 } | |
113 | |
114 void GeolocationDispatcher::OnPositionUpdate( | |
115 blink::mojom::GeopositionPtr geoposition) { | |
116 QueryNextPosition(); | |
117 | |
118 if (geoposition->valid) { | |
119 controller_->positionChanged(WebGeolocationPosition( | |
120 geoposition->timestamp, | |
121 geoposition->latitude, | |
122 geoposition->longitude, | |
123 geoposition->accuracy, | |
124 // Lowest point on land is at approximately -400 meters. | |
125 geoposition->altitude > -10000., | |
126 geoposition->altitude, | |
127 geoposition->altitude_accuracy >= 0., | |
128 geoposition->altitude_accuracy, | |
129 geoposition->heading >= 0. && geoposition->heading <= 360., | |
130 geoposition->heading, | |
131 geoposition->speed >= 0., | |
132 geoposition->speed)); | |
133 } else { | |
134 WebGeolocationError::Error code; | |
135 switch (geoposition->error_code) { | |
136 case blink::mojom::Geoposition::ErrorCode::PERMISSION_DENIED: | |
137 code = WebGeolocationError::ErrorPermissionDenied; | |
138 break; | |
139 case blink::mojom::Geoposition::ErrorCode::POSITION_UNAVAILABLE: | |
140 code = WebGeolocationError::ErrorPositionUnavailable; | |
141 break; | |
142 default: | |
143 NOTREACHED() << geoposition->error_code; | |
144 return; | |
145 } | |
146 controller_->errorOccurred(WebGeolocationError( | |
147 code, blink::WebString::fromUTF8(geoposition->error_message))); | |
148 } | |
149 } | |
150 | |
151 } // namespace content | |
OLD | NEW |