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