OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/geolocation_dispatcher.h" | 5 #include "content/renderer/geolocation_dispatcher.h" |
6 | 6 |
7 #include "content/common/geolocation_messages.h" | 7 #include "content/common/geolocation_messages.h" |
8 #include "content/renderer/render_view_impl.h" | 8 #include "content/renderer/render_view_impl.h" |
9 #include "third_party/WebKit/public/platform/WebString.h" | 9 #include "third_party/WebKit/public/platform/WebString.h" |
10 #include "third_party/WebKit/public/web/WebGeolocationPermissionRequest.h" | 10 #include "third_party/WebKit/public/web/WebGeolocationPermissionRequest.h" |
11 #include "third_party/WebKit/public/web/WebGeolocationPermissionRequestManager.h
" | 11 #include "third_party/WebKit/public/web/WebGeolocationPermissionRequestManager.h
" |
12 #include "third_party/WebKit/public/web/WebGeolocationClient.h" | 12 #include "third_party/WebKit/public/web/WebGeolocationClient.h" |
13 #include "third_party/WebKit/public/web/WebGeolocationPosition.h" | 13 #include "third_party/WebKit/public/web/WebGeolocationPosition.h" |
14 #include "third_party/WebKit/public/web/WebGeolocationError.h" | 14 #include "third_party/WebKit/public/web/WebGeolocationError.h" |
15 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 15 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
16 | 16 |
17 using blink::WebGeolocationController; | 17 using blink::WebGeolocationController; |
18 using blink::WebGeolocationError; | 18 using blink::WebGeolocationError; |
19 using blink::WebGeolocationPermissionRequest; | 19 using blink::WebGeolocationPermissionRequest; |
20 using blink::WebGeolocationPermissionRequestManager; | 20 using blink::WebGeolocationPermissionRequestManager; |
21 using blink::WebGeolocationPosition; | 21 using blink::WebGeolocationPosition; |
22 | 22 |
23 namespace content { | 23 namespace content { |
24 | 24 |
25 GeolocationDispatcher::GeolocationDispatcher(RenderFrame* render_frame) | 25 GeolocationDispatcher::GeolocationDispatcher(RenderFrame* render_frame) |
26 : RenderFrameObserver(render_frame), | 26 : RenderFrameObserver(render_frame), |
27 pending_permissions_(new WebGeolocationPermissionRequestManager()), | 27 pending_permissions_(new WebGeolocationPermissionRequestManager()), |
28 enable_high_accuracy_(false) { | 28 enable_high_accuracy_(false), |
| 29 updating_(false) { |
29 } | 30 } |
30 | 31 |
31 GeolocationDispatcher::~GeolocationDispatcher() {} | 32 GeolocationDispatcher::~GeolocationDispatcher() {} |
32 | 33 |
33 bool GeolocationDispatcher::OnMessageReceived(const IPC::Message& message) { | 34 bool GeolocationDispatcher::OnMessageReceived(const IPC::Message& message) { |
34 bool handled = true; | 35 bool handled = true; |
35 IPC_BEGIN_MESSAGE_MAP(GeolocationDispatcher, message) | 36 IPC_BEGIN_MESSAGE_MAP(GeolocationDispatcher, message) |
36 IPC_MESSAGE_HANDLER(GeolocationMsg_PermissionSet, OnPermissionSet) | 37 IPC_MESSAGE_HANDLER(GeolocationMsg_PermissionSet, OnPermissionSet) |
| 38 IPC_MESSAGE_HANDLER(GeolocationMsg_PositionUpdated, OnPositionUpdated) |
37 IPC_MESSAGE_UNHANDLED(handled = false) | 39 IPC_MESSAGE_UNHANDLED(handled = false) |
38 IPC_END_MESSAGE_MAP() | 40 IPC_END_MESSAGE_MAP() |
39 return handled; | 41 return handled; |
40 } | 42 } |
41 | 43 |
42 void GeolocationDispatcher::startUpdating() { | 44 void GeolocationDispatcher::startUpdating() { |
43 if (!geolocation_service_.get()) { | 45 GURL url; |
44 render_frame()->GetServiceRegistry()->ConnectToRemoteService( | 46 Send(new GeolocationHostMsg_StartUpdating( |
45 &geolocation_service_); | 47 routing_id(), url, enable_high_accuracy_)); |
46 geolocation_service_.set_client(this); | 48 updating_ = true; |
47 } | |
48 if (enable_high_accuracy_) | |
49 geolocation_service_->SetHighAccuracy(true); | |
50 } | 49 } |
51 | 50 |
52 void GeolocationDispatcher::stopUpdating() { | 51 void GeolocationDispatcher::stopUpdating() { |
53 geolocation_service_.reset(); | 52 Send(new GeolocationHostMsg_StopUpdating(routing_id())); |
| 53 updating_ = false; |
54 } | 54 } |
55 | 55 |
56 void GeolocationDispatcher::setEnableHighAccuracy(bool enable_high_accuracy) { | 56 void GeolocationDispatcher::setEnableHighAccuracy(bool enable_high_accuracy) { |
57 // GeolocationController calls setEnableHighAccuracy(true) before | 57 // GeolocationController calls setEnableHighAccuracy(true) before |
58 // startUpdating in response to the first high-accuracy Geolocation | 58 // startUpdating in response to the first high-accuracy Geolocation |
59 // subscription. When the last high-accuracy Geolocation unsubscribes | 59 // subscription. When the last high-accuracy Geolocation unsubscribes |
60 // it calls setEnableHighAccuracy(false) after stopUpdating. | 60 // it calls setEnableHighAccuracy(false) after stopUpdating. |
61 bool has_changed = enable_high_accuracy_ != enable_high_accuracy; | 61 bool has_changed = enable_high_accuracy_ != enable_high_accuracy; |
62 enable_high_accuracy_ = enable_high_accuracy; | 62 enable_high_accuracy_ = enable_high_accuracy; |
63 // We have a different accuracy requirement. Request browser to update. | 63 // We have a different accuracy requirement. Request browser to update. |
64 if (geolocation_service_.get() && has_changed) | 64 if (updating_ && has_changed) |
65 geolocation_service_->SetHighAccuracy(enable_high_accuracy_); | 65 startUpdating(); |
66 } | 66 } |
67 | 67 |
68 void GeolocationDispatcher::setController( | 68 void GeolocationDispatcher::setController( |
69 WebGeolocationController* controller) { | 69 WebGeolocationController* controller) { |
70 controller_.reset(controller); | 70 controller_.reset(controller); |
71 } | 71 } |
72 | 72 |
73 bool GeolocationDispatcher::lastPosition(WebGeolocationPosition&) { | 73 bool GeolocationDispatcher::lastPosition(WebGeolocationPosition&) { |
74 // The latest position is stored in the browser, not the renderer, so we | 74 // The latest position is stored in the browser, not the renderer, so we |
75 // would have to fetch it synchronously to give a good value here. The | 75 // would have to fetch it synchronously to give a good value here. The |
(...skipping 20 matching lines...) Expand all Loading... |
96 } | 96 } |
97 | 97 |
98 // Permission for using geolocation has been set. | 98 // Permission for using geolocation has been set. |
99 void GeolocationDispatcher::OnPermissionSet(int bridge_id, bool is_allowed) { | 99 void GeolocationDispatcher::OnPermissionSet(int bridge_id, bool is_allowed) { |
100 WebGeolocationPermissionRequest permissionRequest; | 100 WebGeolocationPermissionRequest permissionRequest; |
101 if (!pending_permissions_->remove(bridge_id, permissionRequest)) | 101 if (!pending_permissions_->remove(bridge_id, permissionRequest)) |
102 return; | 102 return; |
103 permissionRequest.setIsAllowed(is_allowed); | 103 permissionRequest.setIsAllowed(is_allowed); |
104 } | 104 } |
105 | 105 |
106 void GeolocationDispatcher::OnLocationUpdate(MojoGeopositionPtr geoposition) { | 106 // We have an updated geolocation position or error code. |
107 DCHECK(geolocation_service_.get()); | 107 void GeolocationDispatcher::OnPositionUpdated( |
| 108 const Geoposition& geoposition) { |
| 109 // It is possible for the browser process to have queued an update message |
| 110 // before receiving the stop updating message. |
| 111 if (!updating_) |
| 112 return; |
108 | 113 |
109 if (geoposition->valid) { | 114 if (geoposition.Validate()) { |
110 controller_->positionChanged(WebGeolocationPosition( | 115 controller_->positionChanged( |
111 geoposition->timestamp, | 116 WebGeolocationPosition( |
112 geoposition->latitude, | 117 geoposition.timestamp.ToDoubleT(), |
113 geoposition->longitude, | 118 geoposition.latitude, geoposition.longitude, |
114 geoposition->accuracy, | 119 geoposition.accuracy, |
115 // Lowest point on land is at approximately -400 meters. | 120 // Lowest point on land is at approximately -400 meters. |
116 geoposition->altitude > -10000., | 121 geoposition.altitude > -10000., |
117 geoposition->altitude, | 122 geoposition.altitude, |
118 geoposition->altitude_accuracy >= 0., | 123 geoposition.altitude_accuracy >= 0., |
119 geoposition->altitude_accuracy, | 124 geoposition.altitude_accuracy, |
120 geoposition->heading >= 0. && geoposition->heading <= 360., | 125 geoposition.heading >= 0. && geoposition.heading <= 360., |
121 geoposition->heading, | 126 geoposition.heading, |
122 geoposition->speed >= 0., | 127 geoposition.speed >= 0., |
123 geoposition->speed)); | 128 geoposition.speed)); |
124 } else { | 129 } else { |
125 WebGeolocationError::Error code; | 130 WebGeolocationError::Error code; |
126 switch (geoposition->error_code) { | 131 switch (geoposition.error_code) { |
127 case Geoposition::ERROR_CODE_PERMISSION_DENIED: | 132 case Geoposition::ERROR_CODE_PERMISSION_DENIED: |
128 code = WebGeolocationError::ErrorPermissionDenied; | 133 code = WebGeolocationError::ErrorPermissionDenied; |
129 break; | 134 break; |
130 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE: | 135 case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE: |
131 code = WebGeolocationError::ErrorPositionUnavailable; | 136 code = WebGeolocationError::ErrorPositionUnavailable; |
132 break; | 137 break; |
133 default: | 138 default: |
134 NOTREACHED() << geoposition->error_code; | 139 NOTREACHED() << geoposition.error_code; |
135 return; | 140 return; |
136 } | 141 } |
137 controller_->errorOccurred(WebGeolocationError( | 142 controller_->errorOccurred( |
138 code, blink::WebString::fromUTF8(geoposition->error_message))); | 143 WebGeolocationError( |
| 144 code, blink::WebString::fromUTF8(geoposition.error_message))); |
139 } | 145 } |
140 } | 146 } |
141 | 147 |
142 } // namespace content | 148 } // namespace content |
OLD | NEW |