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