OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/generic_sensor/platform_sensor_provider_win.h" |
| 6 |
| 7 #include "base/memory/singleton.h" |
| 8 #include "base/task_runner_util.h" |
| 9 #include "base/threading/thread.h" |
| 10 #include "device/generic_sensor/platform_sensor_win.h" |
| 11 |
| 12 namespace device { |
| 13 |
| 14 // static |
| 15 PlatformSensorProviderWin* PlatformSensorProviderWin::GetInstance() { |
| 16 return base::Singleton< |
| 17 PlatformSensorProviderWin, |
| 18 base::LeakySingletonTraits<PlatformSensorProviderWin>>::get(); |
| 19 } |
| 20 |
| 21 void PlatformSensorProviderWin::SetSensorManagerForTesting( |
| 22 base::win::ScopedComPtr<ISensorManager> sensor_manager) { |
| 23 sensor_manager_ = sensor_manager; |
| 24 } |
| 25 |
| 26 PlatformSensorProviderWin::PlatformSensorProviderWin() = default; |
| 27 PlatformSensorProviderWin::~PlatformSensorProviderWin() = default; |
| 28 |
| 29 void PlatformSensorProviderWin::CreateSensorInternal( |
| 30 mojom::SensorType type, |
| 31 mojo::ScopedSharedBufferMapping mapping, |
| 32 const CreateSensorCallback& callback) { |
| 33 DCHECK(CalledOnValidThread()); |
| 34 if (!StartSensorThread()) { |
| 35 callback.Run(nullptr); |
| 36 return; |
| 37 } |
| 38 |
| 39 base::PostTaskAndReplyWithResult( |
| 40 sensor_thread_->task_runner().get(), FROM_HERE, |
| 41 base::Bind(&PlatformSensorProviderWin::CreateSensorReader, |
| 42 base::Unretained(this), type), |
| 43 base::Bind(&PlatformSensorProviderWin::SensorReaderCreated, |
| 44 base::Unretained(this), type, base::Passed(&mapping), |
| 45 callback)); |
| 46 } |
| 47 |
| 48 bool PlatformSensorProviderWin::InitializeSensorManager() { |
| 49 if (sensor_manager_) |
| 50 return true; |
| 51 |
| 52 HRESULT hr = sensor_manager_.CreateInstance(CLSID_SensorManager); |
| 53 return SUCCEEDED(hr); |
| 54 } |
| 55 |
| 56 void PlatformSensorProviderWin::AllSensorsRemoved() { |
| 57 StopSensorThread(); |
| 58 } |
| 59 |
| 60 bool PlatformSensorProviderWin::StartSensorThread() { |
| 61 if (!sensor_thread_) { |
| 62 sensor_thread_ = base::MakeUnique<base::Thread>("Sensor thread"); |
| 63 sensor_thread_->init_com_with_mta(true); |
| 64 } |
| 65 |
| 66 if (!sensor_thread_->IsRunning()) |
| 67 return sensor_thread_->Start(); |
| 68 return true; |
| 69 } |
| 70 |
| 71 void PlatformSensorProviderWin::StopSensorThread() { |
| 72 if (sensor_thread_ && sensor_thread_->IsRunning()) { |
| 73 sensor_manager_.Release(); |
| 74 sensor_thread_->Stop(); |
| 75 } |
| 76 } |
| 77 |
| 78 void PlatformSensorProviderWin::SensorReaderCreated( |
| 79 mojom::SensorType type, |
| 80 mojo::ScopedSharedBufferMapping mapping, |
| 81 const CreateSensorCallback& callback, |
| 82 std::unique_ptr<PlatformSensorReaderWin> sensor_reader) { |
| 83 DCHECK(CalledOnValidThread()); |
| 84 if (!sensor_reader) { |
| 85 callback.Run(nullptr); |
| 86 if (!HasSensors()) |
| 87 StopSensorThread(); |
| 88 return; |
| 89 } |
| 90 |
| 91 scoped_refptr<PlatformSensor> sensor = new PlatformSensorWin( |
| 92 type, std::move(mapping), this, sensor_thread_->task_runner(), |
| 93 std::move(sensor_reader)); |
| 94 callback.Run(sensor); |
| 95 } |
| 96 |
| 97 std::unique_ptr<PlatformSensorReaderWin> |
| 98 PlatformSensorProviderWin::CreateSensorReader(mojom::SensorType type) { |
| 99 DCHECK(sensor_thread_->task_runner()->BelongsToCurrentThread()); |
| 100 if (!InitializeSensorManager()) |
| 101 return nullptr; |
| 102 return PlatformSensorReaderWin::Create(type, sensor_manager_); |
| 103 } |
| 104 |
| 105 } // namespace device |
OLD | NEW |