Chromium Code Reviews| 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 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 } | 235 } |
| 236 | 236 |
| 237 PlatformSensorReaderWin::PlatformSensorReaderWin( | 237 PlatformSensorReaderWin::PlatformSensorReaderWin( |
| 238 base::win::ScopedComPtr<ISensor> sensor, | 238 base::win::ScopedComPtr<ISensor> sensor, |
| 239 std::unique_ptr<ReaderInitParams> params) | 239 std::unique_ptr<ReaderInitParams> params) |
| 240 : init_params_(std::move(params)), | 240 : init_params_(std::move(params)), |
| 241 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 241 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 242 sensor_active_(false), | 242 sensor_active_(false), |
| 243 client_(nullptr), | 243 client_(nullptr), |
| 244 sensor_(sensor), | 244 sensor_(sensor), |
| 245 event_listener_(new EventListener(this)) { | 245 event_listener_(new EventListener(this)), |
| 246 weak_factory_(this) { | |
| 246 DCHECK(init_params_); | 247 DCHECK(init_params_); |
| 247 DCHECK(!init_params_->reader_func.is_null()); | 248 DCHECK(!init_params_->reader_func.is_null()); |
| 248 DCHECK(sensor_); | 249 DCHECK(sensor_); |
| 249 } | 250 } |
| 250 | 251 |
| 251 void PlatformSensorReaderWin::SetClient(Client* client) { | 252 void PlatformSensorReaderWin::SetClient(Client* client) { |
| 252 base::AutoLock autolock(lock_); | 253 base::AutoLock autolock(lock_); |
| 253 // Can be null. | 254 // Can be null. |
| 254 client_ = client; | 255 client_ = client; |
| 255 } | 256 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 266 DCHECK(task_runner_->BelongsToCurrentThread()); | 267 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 267 } | 268 } |
| 268 | 269 |
| 269 bool PlatformSensorReaderWin::StartSensor( | 270 bool PlatformSensorReaderWin::StartSensor( |
| 270 const PlatformSensorConfiguration& configuration) { | 271 const PlatformSensorConfiguration& configuration) { |
| 271 base::AutoLock autolock(lock_); | 272 base::AutoLock autolock(lock_); |
| 272 | 273 |
| 273 if (!SetReportingInterval(configuration)) | 274 if (!SetReportingInterval(configuration)) |
| 274 return false; | 275 return false; |
| 275 | 276 |
| 276 // Set event listener. | |
| 277 if (!sensor_active_) { | 277 if (!sensor_active_) { |
| 278 base::win::ScopedComPtr<ISensorEvents> sensor_events; | 278 task_runner_->PostTask( |
| 279 HRESULT hr = event_listener_->QueryInterface(__uuidof(ISensorEvents), | 279 FROM_HERE, base::Bind(&PlatformSensorReaderWin::ListenSensorEvent, |
| 280 sensor_events.ReceiveVoid()); | 280 weak_factory_.GetWeakPtr())); |
| 281 | |
| 282 if (FAILED(hr) || !sensor_events) | |
| 283 return false; | |
| 284 | |
| 285 if (FAILED(sensor_->SetEventSink(sensor_events.get()))) | |
| 286 return false; | |
| 287 | |
| 288 sensor_active_ = true; | |
| 289 } | 281 } |
| 290 | 282 |
| 291 return true; | 283 return true; |
| 292 } | 284 } |
| 293 | 285 |
| 286 void PlatformSensorReaderWin::ListenSensorEvent() { | |
| 287 base::AutoLock autolock(lock_); | |
|
shalamov
2016/11/15 07:31:19
If it is accessed only on sensor thread, I think y
| |
| 288 event_listener_->AddRef(); | |
| 289 // Set event listener. | |
| 290 if (FAILED(sensor_->SetEventSink(event_listener_.get()))) { | |
| 291 SensorError(); | |
| 292 StopSensor(); | |
| 293 } | |
| 294 sensor_active_ = true; | |
|
shalamov
2016/11/15 07:31:19
Should this be moved to:
if (!sensor_active_) {
| |
| 295 } | |
| 296 | |
| 294 bool PlatformSensorReaderWin::SetReportingInterval( | 297 bool PlatformSensorReaderWin::SetReportingInterval( |
| 295 const PlatformSensorConfiguration& configuration) { | 298 const PlatformSensorConfiguration& configuration) { |
| 296 base::win::ScopedComPtr<IPortableDeviceValues> props; | 299 base::win::ScopedComPtr<IPortableDeviceValues> props; |
| 297 if (SUCCEEDED(props.CreateInstance(CLSID_PortableDeviceValues))) { | 300 if (SUCCEEDED(props.CreateInstance(CLSID_PortableDeviceValues))) { |
| 298 unsigned interval = | 301 unsigned interval = |
| 299 (1 / configuration.frequency()) * base::Time::kMillisecondsPerSecond; | 302 (1 / configuration.frequency()) * base::Time::kMillisecondsPerSecond; |
| 300 | 303 |
| 301 HRESULT hr = props->SetUnsignedIntegerValue( | 304 HRESULT hr = props->SetUnsignedIntegerValue( |
| 302 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, interval); | 305 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, interval); |
| 303 | 306 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 329 | 332 |
| 330 unsigned long PlatformSensorReaderWin::GetMinimalReportingIntervalMs() const { | 333 unsigned long PlatformSensorReaderWin::GetMinimalReportingIntervalMs() const { |
| 331 return init_params_->min_reporting_interval_ms; | 334 return init_params_->min_reporting_interval_ms; |
| 332 } | 335 } |
| 333 | 336 |
| 334 mojom::ReportingMode PlatformSensorReaderWin::GetReportingMode() const { | 337 mojom::ReportingMode PlatformSensorReaderWin::GetReportingMode() const { |
| 335 return init_params_->reporting_mode; | 338 return init_params_->reporting_mode; |
| 336 } | 339 } |
| 337 | 340 |
| 338 } // namespace device | 341 } // namespace device |
| OLD | NEW |