OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "modules/sensor/Sensor.h" | 5 #include "modules/sensor/Sensor.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/ExceptionCode.h" | 8 #include "core/dom/ExceptionCode.h" |
9 #include "core/dom/TaskRunnerHelper.h" | 9 #include "core/dom/TaskRunnerHelper.h" |
10 #include "core/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 ConsoleMessage::Create(kJSMessageSource, kInfoMessageLevel, | 51 ConsoleMessage::Create(kJSMessageSource, kInfoMessageLevel, |
52 "Frequency is limited to 60 Hz."); | 52 "Frequency is limited to 60 Hz."); |
53 execution_context->AddConsoleMessage(console_message); | 53 execution_context->AddConsoleMessage(console_message); |
54 } | 54 } |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 Sensor::~Sensor() = default; | 58 Sensor::~Sensor() = default; |
59 | 59 |
60 void Sensor::start() { | 60 void Sensor::start() { |
61 if (state_ != Sensor::SensorState::kIdle) | |
62 return; | |
63 | |
64 InitSensorProxyIfNeeded(); | |
65 if (!sensor_proxy_) { | |
66 ReportError(kInvalidStateError, | |
67 "The Sensor is no longer associated to a frame."); | |
68 return; | |
69 } | |
70 | |
71 last_update_timestamp_ = WTF::MonotonicallyIncreasingTime(); | |
72 StartListening(); | 61 StartListening(); |
73 } | 62 } |
74 | 63 |
75 void Sensor::stop() { | 64 void Sensor::stop() { |
76 if (state_ == Sensor::SensorState::kIdle) | |
77 return; | |
78 | |
79 StopListening(); | 65 StopListening(); |
80 } | 66 } |
81 | 67 |
82 // Getters | 68 // Getters |
83 bool Sensor::activated() const { | 69 bool Sensor::activated() const { |
84 return state_ == SensorState::kActivated; | 70 return state_ == SensorState::kActivated; |
85 } | 71 } |
86 | 72 |
87 DOMHighResTimeStamp Sensor::timestamp(ScriptState* script_state, | 73 DOMHighResTimeStamp Sensor::timestamp(ScriptState* script_state, |
88 bool& is_null) const { | 74 bool& is_null) const { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 return; | 145 return; |
160 | 146 |
161 auto provider = SensorProviderProxy::From(document->GetFrame()); | 147 auto provider = SensorProviderProxy::From(document->GetFrame()); |
162 sensor_proxy_ = provider->GetSensorProxy(type_); | 148 sensor_proxy_ = provider->GetSensorProxy(type_); |
163 | 149 |
164 if (!sensor_proxy_) | 150 if (!sensor_proxy_) |
165 sensor_proxy_ = provider->CreateSensorProxy(type_, document->GetPage()); | 151 sensor_proxy_ = provider->CreateSensorProxy(type_, document->GetPage()); |
166 } | 152 } |
167 | 153 |
168 void Sensor::ContextDestroyed(ExecutionContext*) { | 154 void Sensor::ContextDestroyed(ExecutionContext*) { |
169 if (state_ == Sensor::SensorState::kActivated || | 155 StopListening(); |
170 state_ == Sensor::SensorState::kActivating) | |
171 StopListening(); | |
172 } | 156 } |
173 | 157 |
174 void Sensor::OnSensorInitialized() { | 158 void Sensor::OnSensorInitialized() { |
175 if (state_ != Sensor::SensorState::kActivating) | 159 if (state_ != Sensor::SensorState::kActivating) |
176 return; | 160 return; |
177 | 161 |
178 StartListening(); | 162 RequestAddConfiguration(); |
179 } | 163 } |
180 | 164 |
181 void Sensor::NotifySensorChanged(double timestamp) { | 165 void Sensor::NotifySensorChanged(double timestamp) { |
182 if (state_ != Sensor::SensorState::kActivated) | 166 if (state_ != Sensor::SensorState::kActivated) |
183 return; | 167 return; |
184 | 168 |
185 DCHECK_GT(configuration_->frequency, 0.0); | 169 DCHECK_GT(configuration_->frequency, 0.0); |
186 double period = 1 / configuration_->frequency; | 170 double period = 1 / configuration_->frequency; |
187 | 171 |
188 if (timestamp - last_update_timestamp_ >= period) { | 172 if (timestamp - last_update_timestamp_ >= period) { |
189 last_update_timestamp_ = timestamp; | 173 last_update_timestamp_ = timestamp; |
190 NotifySensorReadingChanged(); | 174 NotifySensorReadingChanged(); |
191 } | 175 } |
192 } | 176 } |
193 | 177 |
194 void Sensor::OnSensorError(ExceptionCode code, | 178 void Sensor::OnSensorError(ExceptionCode code, |
195 const String& sanitized_message, | 179 const String& sanitized_message, |
196 const String& unsanitized_message) { | 180 const String& unsanitized_message) { |
197 ReportError(code, sanitized_message, unsanitized_message); | 181 HandleError(code, sanitized_message, unsanitized_message); |
198 } | 182 } |
199 | 183 |
200 void Sensor::OnStartRequestCompleted(bool result) { | 184 void Sensor::OnAddConfigurationRequestCompleted(bool result) { |
201 if (state_ != SensorState::kActivating) | 185 if (state_ != SensorState::kActivating) |
202 return; | 186 return; |
203 | 187 |
204 if (!result) { | 188 if (!result) { |
205 ReportError(kNotReadableError, "start() call has failed."); | 189 HandleError(kNotReadableError, "start() call has failed."); |
206 return; | 190 return; |
207 } | 191 } |
208 | 192 |
| 193 // The initial value for m_lastUpdateTimestamp is set to current time, |
| 194 // so that the first reading update will be notified considering the given |
| 195 // frequency hint. |
| 196 last_update_timestamp_ = WTF::MonotonicallyIncreasingTime(); |
| 197 |
209 UpdateState(Sensor::SensorState::kActivated); | 198 UpdateState(Sensor::SensorState::kActivated); |
| 199 |
| 200 if (GetExecutionContext()) { |
| 201 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) |
| 202 ->PostTask(BLINK_FROM_HERE, WTF::Bind(&Sensor::NotifyOnActivate, |
| 203 WrapWeakPersistent(this))); |
| 204 } |
210 } | 205 } |
211 | 206 |
212 void Sensor::StartListening() { | 207 void Sensor::StartListening() { |
213 DCHECK(sensor_proxy_); | 208 if (state_ != SensorState::kIdle) |
214 UpdateState(Sensor::SensorState::kActivating); | 209 return; |
215 | 210 |
216 sensor_proxy_->AddObserver(this); | 211 InitSensorProxyIfNeeded(); |
217 if (!sensor_proxy_->IsInitialized()) { | 212 if (!sensor_proxy_) { |
218 sensor_proxy_->Initialize(); | 213 HandleError(kInvalidStateError, |
| 214 "The Sensor is no longer associated to a frame."); |
219 return; | 215 return; |
220 } | 216 } |
221 | 217 |
| 218 if (sensor_proxy_->IsInitialized()) |
| 219 RequestAddConfiguration(); |
| 220 else |
| 221 sensor_proxy_->Initialize(); |
| 222 |
| 223 sensor_proxy_->AddObserver(this); |
| 224 UpdateState(SensorState::kActivating); |
| 225 } |
| 226 |
| 227 void Sensor::StopListening() { |
| 228 if (state_ == SensorState::kIdle) |
| 229 return; |
| 230 |
| 231 DCHECK(sensor_proxy_); |
| 232 if (sensor_proxy_->IsInitialized()) { |
| 233 DCHECK(configuration_); |
| 234 sensor_proxy_->RemoveConfiguration(configuration_->Clone()); |
| 235 } |
| 236 |
| 237 sensor_proxy_->RemoveObserver(this); |
| 238 UpdateState(Sensor::SensorState::kIdle); |
| 239 } |
| 240 |
| 241 void Sensor::RequestAddConfiguration() { |
222 if (!configuration_) { | 242 if (!configuration_) { |
223 configuration_ = CreateSensorConfig(); | 243 configuration_ = CreateSensorConfig(); |
224 DCHECK(configuration_); | 244 DCHECK(configuration_); |
225 DCHECK(configuration_->frequency > 0 && | 245 DCHECK(configuration_->frequency > 0 && |
226 configuration_->frequency <= | 246 configuration_->frequency <= |
227 SensorConfiguration::kMaxAllowedFrequency); | 247 SensorConfiguration::kMaxAllowedFrequency); |
228 } | 248 } |
229 | 249 |
230 auto start_callback = | |
231 WTF::Bind(&Sensor::OnStartRequestCompleted, WrapWeakPersistent(this)); | |
232 sensor_proxy_->AddConfiguration(configuration_->Clone(), | |
233 std::move(start_callback)); | |
234 } | |
235 | |
236 void Sensor::StopListening() { | |
237 DCHECK(sensor_proxy_); | 250 DCHECK(sensor_proxy_); |
238 UpdateState(Sensor::SensorState::kIdle); | 251 sensor_proxy_->AddConfiguration( |
239 | 252 configuration_->Clone(), |
240 if (sensor_proxy_->IsInitialized()) { | 253 WTF::Bind(&Sensor::OnAddConfigurationRequestCompleted, |
241 DCHECK(configuration_); | 254 WrapWeakPersistent(this))); |
242 sensor_proxy_->RemoveConfiguration(configuration_->Clone()); | |
243 } | |
244 sensor_proxy_->RemoveObserver(this); | |
245 } | 255 } |
246 | 256 |
247 void Sensor::UpdateState(Sensor::SensorState new_state) { | 257 void Sensor::UpdateState(Sensor::SensorState new_state) { |
248 if (new_state == state_) | |
249 return; | |
250 | |
251 if (new_state == SensorState::kActivated && GetExecutionContext()) { | |
252 DCHECK_EQ(SensorState::kActivating, state_); | |
253 // The initial value for m_lastUpdateTimestamp is set to current time, | |
254 // so that the first reading update will be notified considering the given | |
255 // frequency hint. | |
256 last_update_timestamp_ = WTF::MonotonicallyIncreasingTime(); | |
257 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) | |
258 ->PostTask(BLINK_FROM_HERE, WTF::Bind(&Sensor::NotifyOnActivate, | |
259 WrapWeakPersistent(this))); | |
260 } | |
261 | |
262 state_ = new_state; | 258 state_ = new_state; |
263 } | 259 } |
264 | 260 |
265 void Sensor::ReportError(ExceptionCode code, | 261 void Sensor::HandleError(ExceptionCode code, |
266 const String& sanitized_message, | 262 const String& sanitized_message, |
267 const String& unsanitized_message) { | 263 const String& unsanitized_message) { |
268 UpdateState(SensorState::kIdle); | 264 StopListening(); |
| 265 |
269 if (GetExecutionContext()) { | 266 if (GetExecutionContext()) { |
270 auto error = | 267 auto error = |
271 DOMException::Create(code, sanitized_message, unsanitized_message); | 268 DOMException::Create(code, sanitized_message, unsanitized_message); |
272 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) | 269 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) |
273 ->PostTask(BLINK_FROM_HERE, | 270 ->PostTask(BLINK_FROM_HERE, |
274 WTF::Bind(&Sensor::NotifyError, WrapWeakPersistent(this), | 271 WTF::Bind(&Sensor::NotifyError, WrapWeakPersistent(this), |
275 WrapPersistent(error))); | 272 WrapPersistent(error))); |
276 } | 273 } |
277 } | 274 } |
278 | 275 |
(...skipping 16 matching lines...) Expand all Loading... |
295 } | 292 } |
296 | 293 |
297 bool Sensor::CanReturnReadings() const { | 294 bool Sensor::CanReturnReadings() const { |
298 if (!IsActivated()) | 295 if (!IsActivated()) |
299 return false; | 296 return false; |
300 DCHECK(sensor_proxy_); | 297 DCHECK(sensor_proxy_); |
301 return sensor_proxy_->Reading().timestamp != 0.0; | 298 return sensor_proxy_->Reading().timestamp != 0.0; |
302 } | 299 } |
303 | 300 |
304 } // namespace blink | 301 } // namespace blink |
OLD | NEW |