| 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 "device/generic_sensor/platform_sensor_reader_win.h" | 5 #include "device/generic_sensor/platform_sensor_reader_win.h" |
| 6 | 6 |
| 7 #include <Sensors.h> | 7 #include <Sensors.h> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 } | 337 } |
| 338 | 338 |
| 339 PlatformSensorReaderWin::PlatformSensorReaderWin( | 339 PlatformSensorReaderWin::PlatformSensorReaderWin( |
| 340 base::win::ScopedComPtr<ISensor> sensor, | 340 base::win::ScopedComPtr<ISensor> sensor, |
| 341 std::unique_ptr<ReaderInitParams> params) | 341 std::unique_ptr<ReaderInitParams> params) |
| 342 : init_params_(std::move(params)), | 342 : init_params_(std::move(params)), |
| 343 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 343 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 344 sensor_active_(false), | 344 sensor_active_(false), |
| 345 client_(nullptr), | 345 client_(nullptr), |
| 346 sensor_(sensor), | 346 sensor_(sensor), |
| 347 event_listener_(new EventListener(this)) { | 347 event_listener_(new EventListener(this)), |
| 348 weak_factory_(this) { |
| 348 DCHECK(init_params_); | 349 DCHECK(init_params_); |
| 349 DCHECK(!init_params_->reader_func.is_null()); | 350 DCHECK(!init_params_->reader_func.is_null()); |
| 350 DCHECK(sensor_); | 351 DCHECK(sensor_); |
| 351 } | 352 } |
| 352 | 353 |
| 353 void PlatformSensorReaderWin::SetClient(Client* client) { | 354 void PlatformSensorReaderWin::SetClient(Client* client) { |
| 354 base::AutoLock autolock(lock_); | 355 base::AutoLock autolock(lock_); |
| 355 // Can be null. | 356 // Can be null. |
| 356 client_ = client; | 357 client_ = client; |
| 357 } | 358 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 368 DCHECK(task_runner_->BelongsToCurrentThread()); | 369 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 369 } | 370 } |
| 370 | 371 |
| 371 bool PlatformSensorReaderWin::StartSensor( | 372 bool PlatformSensorReaderWin::StartSensor( |
| 372 const PlatformSensorConfiguration& configuration) { | 373 const PlatformSensorConfiguration& configuration) { |
| 373 base::AutoLock autolock(lock_); | 374 base::AutoLock autolock(lock_); |
| 374 | 375 |
| 375 if (!SetReportingInterval(configuration)) | 376 if (!SetReportingInterval(configuration)) |
| 376 return false; | 377 return false; |
| 377 | 378 |
| 378 // Set event listener. | |
| 379 if (!sensor_active_) { | 379 if (!sensor_active_) { |
| 380 base::win::ScopedComPtr<ISensorEvents> sensor_events; | 380 task_runner_->PostTask( |
| 381 HRESULT hr = event_listener_->QueryInterface(__uuidof(ISensorEvents), | 381 FROM_HERE, base::Bind(&PlatformSensorReaderWin::ListenSensorEvent, |
| 382 sensor_events.ReceiveVoid()); | 382 weak_factory_.GetWeakPtr())); |
| 383 | |
| 384 if (FAILED(hr) || !sensor_events) | |
| 385 return false; | |
| 386 | |
| 387 if (FAILED(sensor_->SetEventSink(sensor_events.get()))) | |
| 388 return false; | |
| 389 | |
| 390 sensor_active_ = true; | 383 sensor_active_ = true; |
| 391 } | 384 } |
| 392 | 385 |
| 393 return true; | 386 return true; |
| 394 } | 387 } |
| 395 | 388 |
| 389 void PlatformSensorReaderWin::ListenSensorEvent() { |
| 390 // Set event listener. |
| 391 if (FAILED(sensor_->SetEventSink(event_listener_.get()))) { |
| 392 SensorError(); |
| 393 StopSensor(); |
| 394 } |
| 395 } |
| 396 |
| 396 bool PlatformSensorReaderWin::SetReportingInterval( | 397 bool PlatformSensorReaderWin::SetReportingInterval( |
| 397 const PlatformSensorConfiguration& configuration) { | 398 const PlatformSensorConfiguration& configuration) { |
| 398 base::win::ScopedComPtr<IPortableDeviceValues> props; | 399 base::win::ScopedComPtr<IPortableDeviceValues> props; |
| 399 if (SUCCEEDED(props.CreateInstance(CLSID_PortableDeviceValues))) { | 400 if (SUCCEEDED(props.CreateInstance(CLSID_PortableDeviceValues))) { |
| 400 unsigned interval = | 401 unsigned interval = |
| 401 (1 / configuration.frequency()) * base::Time::kMillisecondsPerSecond; | 402 (1 / configuration.frequency()) * base::Time::kMillisecondsPerSecond; |
| 402 | 403 |
| 403 HRESULT hr = props->SetUnsignedIntegerValue( | 404 HRESULT hr = props->SetUnsignedIntegerValue( |
| 404 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, interval); | 405 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, interval); |
| 405 | 406 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 427 void PlatformSensorReaderWin::SensorError() { | 428 void PlatformSensorReaderWin::SensorError() { |
| 428 if (client_) | 429 if (client_) |
| 429 client_->OnSensorError(); | 430 client_->OnSensorError(); |
| 430 } | 431 } |
| 431 | 432 |
| 432 unsigned long PlatformSensorReaderWin::GetMinimalReportingIntervalMs() const { | 433 unsigned long PlatformSensorReaderWin::GetMinimalReportingIntervalMs() const { |
| 433 return init_params_->min_reporting_interval_ms; | 434 return init_params_->min_reporting_interval_ms; |
| 434 } | 435 } |
| 435 | 436 |
| 436 } // namespace device | 437 } // namespace device |
| OLD | NEW |