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; | 281 sensor_active_ = true; |
289 } | 282 } |
290 | 283 |
291 return true; | 284 return true; |
292 } | 285 } |
293 | 286 |
287 void PlatformSensorReaderWin::ListenSensorEvent() { | |
288 event_listener_->AddRef(); | |
Mikhail
2016/11/21 11:17:40
why is that needed?
| |
289 // Set event listener. | |
290 if (FAILED(sensor_->SetEventSink(event_listener_.get()))) { | |
291 SensorError(); | |
292 StopSensor(); | |
293 } | |
294 } | |
295 | |
294 bool PlatformSensorReaderWin::SetReportingInterval( | 296 bool PlatformSensorReaderWin::SetReportingInterval( |
295 const PlatformSensorConfiguration& configuration) { | 297 const PlatformSensorConfiguration& configuration) { |
296 base::win::ScopedComPtr<IPortableDeviceValues> props; | 298 base::win::ScopedComPtr<IPortableDeviceValues> props; |
297 if (SUCCEEDED(props.CreateInstance(CLSID_PortableDeviceValues))) { | 299 if (SUCCEEDED(props.CreateInstance(CLSID_PortableDeviceValues))) { |
298 unsigned interval = | 300 unsigned interval = |
299 (1 / configuration.frequency()) * base::Time::kMillisecondsPerSecond; | 301 (1 / configuration.frequency()) * base::Time::kMillisecondsPerSecond; |
300 | 302 |
301 HRESULT hr = props->SetUnsignedIntegerValue( | 303 HRESULT hr = props->SetUnsignedIntegerValue( |
302 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, interval); | 304 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, interval); |
303 | 305 |
(...skipping 25 matching lines...) Expand all Loading... | |
329 | 331 |
330 unsigned long PlatformSensorReaderWin::GetMinimalReportingIntervalMs() const { | 332 unsigned long PlatformSensorReaderWin::GetMinimalReportingIntervalMs() const { |
331 return init_params_->min_reporting_interval_ms; | 333 return init_params_->min_reporting_interval_ms; |
332 } | 334 } |
333 | 335 |
334 mojom::ReportingMode PlatformSensorReaderWin::GetReportingMode() const { | 336 mojom::ReportingMode PlatformSensorReaderWin::GetReportingMode() const { |
335 return init_params_->reporting_mode; | 337 return init_params_->reporting_mode; |
336 } | 338 } |
337 | 339 |
338 } // namespace device | 340 } // namespace device |
OLD | NEW |