Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: third_party/WebKit/Source/modules/sensor/Sensor.cpp

Issue 2839963004: [Sensors] Error handler in Senor class should stop the platform sensor (Closed)
Patch Set: Added layout test Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 }
Reilly Grant (use Gerrit) 2017/04/26 14:58:45 nit: no braces when both if and else clauses are a
Mikhail 2017/04/27 07:50:34 Done.
223
224 sensor_proxy_->AddObserver(this);
225 UpdateState(SensorState::kActivating);
226 }
227
228 void Sensor::StopListening() {
229 if (state_ == SensorState::kIdle)
230 return;
231
232 DCHECK(sensor_proxy_);
233 if (sensor_proxy_->IsInitialized()) {
234 DCHECK(configuration_);
235 sensor_proxy_->RemoveConfiguration(configuration_->Clone());
236 }
237
238 sensor_proxy_->RemoveObserver(this);
239 UpdateState(Sensor::SensorState::kIdle);
240 }
241
242 void Sensor::RequestAddConfiguration() {
222 if (!configuration_) { 243 if (!configuration_) {
223 configuration_ = CreateSensorConfig(); 244 configuration_ = CreateSensorConfig();
224 DCHECK(configuration_); 245 DCHECK(configuration_);
225 DCHECK(configuration_->frequency > 0 && 246 DCHECK(configuration_->frequency > 0 &&
226 configuration_->frequency <= 247 configuration_->frequency <=
227 SensorConfiguration::kMaxAllowedFrequency); 248 SensorConfiguration::kMaxAllowedFrequency);
228 } 249 }
229 250
230 auto start_callback = 251 auto start_callback = WTF::Bind(&Sensor::OnAddConfigurationRequestCompleted,
Reilly Grant (use Gerrit) 2017/04/26 14:58:45 nit: no need to save the callback in a local varia
Mikhail 2017/04/27 07:50:34 Done.
231 WTF::Bind(&Sensor::OnStartRequestCompleted, WrapWeakPersistent(this)); 252 WrapWeakPersistent(this));
253 DCHECK(sensor_proxy_);
232 sensor_proxy_->AddConfiguration(configuration_->Clone(), 254 sensor_proxy_->AddConfiguration(configuration_->Clone(),
233 std::move(start_callback)); 255 std::move(start_callback));
234 } 256 }
235 257
236 void Sensor::StopListening() {
237 DCHECK(sensor_proxy_);
238 UpdateState(Sensor::SensorState::kIdle);
239
240 if (sensor_proxy_->IsInitialized()) {
241 DCHECK(configuration_);
242 sensor_proxy_->RemoveConfiguration(configuration_->Clone());
243 }
244 sensor_proxy_->RemoveObserver(this);
245 }
246
247 void Sensor::UpdateState(Sensor::SensorState new_state) { 258 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; 259 state_ = new_state;
263 } 260 }
264 261
265 void Sensor::ReportError(ExceptionCode code, 262 void Sensor::HandleError(ExceptionCode code,
266 const String& sanitized_message, 263 const String& sanitized_message,
267 const String& unsanitized_message) { 264 const String& unsanitized_message) {
268 UpdateState(SensorState::kIdle); 265 StopListening();
266
269 if (GetExecutionContext()) { 267 if (GetExecutionContext()) {
270 auto error = 268 auto error =
271 DOMException::Create(code, sanitized_message, unsanitized_message); 269 DOMException::Create(code, sanitized_message, unsanitized_message);
272 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) 270 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext())
273 ->PostTask(BLINK_FROM_HERE, 271 ->PostTask(BLINK_FROM_HERE,
274 WTF::Bind(&Sensor::NotifyError, WrapWeakPersistent(this), 272 WTF::Bind(&Sensor::NotifyError, WrapWeakPersistent(this),
275 WrapPersistent(error))); 273 WrapPersistent(error)));
276 } 274 }
277 } 275 }
278 276
(...skipping 16 matching lines...) Expand all
295 } 293 }
296 294
297 bool Sensor::CanReturnReadings() const { 295 bool Sensor::CanReturnReadings() const {
298 if (!IsActivated()) 296 if (!IsActivated())
299 return false; 297 return false;
300 DCHECK(sensor_proxy_); 298 DCHECK(sensor_proxy_);
301 return sensor_proxy_->Reading().timestamp != 0.0; 299 return sensor_proxy_->Reading().timestamp != 0.0;
302 } 300 }
303 301
304 } // namespace blink 302 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698