Chromium Code Reviews| 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 PlatformSensorProvider* PlatformSensorProvider::GetInstance() { | |
| 16 return PlatformSensorProviderWin::GetInstance(); | |
| 17 } | |
| 18 | |
| 19 // static | |
| 20 PlatformSensorProviderWin* PlatformSensorProviderWin::GetInstance() { | |
| 21 return base::Singleton< | |
| 22 PlatformSensorProviderWin, | |
| 23 base::LeakySingletonTraits<PlatformSensorProviderWin>>::get(); | |
| 24 } | |
| 25 | |
| 26 void PlatformSensorProviderWin::SetSensorManagerForTesting( | |
| 27 base::win::ScopedComPtr<ISensorManager> sensor_manager) { | |
| 28 sensor_manager_ = sensor_manager; | |
| 29 } | |
| 30 | |
| 31 PlatformSensorProviderWin::PlatformSensorProviderWin() = default; | |
| 32 PlatformSensorProviderWin::~PlatformSensorProviderWin() = default; | |
| 33 | |
| 34 void PlatformSensorProviderWin::CreateSensorInternal( | |
| 35 mojom::SensorType type, | |
| 36 mojo::ScopedSharedBufferMapping mapping, | |
| 37 const CreateSensorCallback& callback) { | |
| 38 DCHECK(CalledOnValidThread()); | |
| 39 if (!StartSensorThread()) { | |
| 40 callback.Run(nullptr); | |
| 41 return; | |
| 42 } | |
| 43 | |
| 44 base::PostTaskAndReplyWithResult( | |
| 45 sensor_thread_->task_runner().get(), FROM_HERE, | |
| 46 base::Bind(&PlatformSensorProviderWin::CreateSensorReader, | |
| 47 base::Unretained(this), type), | |
| 48 base::Bind(&PlatformSensorProviderWin::SensorReaderCreated, | |
| 49 base::Unretained(this), type, base::Passed(&mapping), | |
| 50 callback)); | |
| 51 } | |
| 52 | |
| 53 bool PlatformSensorProviderWin::InitializeSensorManager() { | |
| 54 if (sensor_manager_) | |
| 55 return true; | |
| 56 | |
| 57 HRESULT hr = sensor_manager_.CreateInstance(CLSID_SensorManager); | |
| 58 return SUCCEEDED(hr); | |
| 59 } | |
| 60 | |
| 61 void PlatformSensorProviderWin::AllSensorsRemoved() { | |
| 62 StopSensorThread(); | |
| 63 } | |
| 64 | |
| 65 bool PlatformSensorProviderWin::StartSensorThread() { | |
| 66 if (!sensor_thread_) { | |
| 67 sensor_thread_ = | |
| 68 base::MakeUnique<base::Thread>("Windows platform sensor thread"); | |
|
Reilly Grant (use Gerrit)
2016/10/31 21:49:43
As with the Linux patch, you might as well call th
shalamov
2016/11/01 11:00:02
Done.
| |
| 69 sensor_thread_->init_com_with_mta(true); | |
| 70 } | |
| 71 | |
| 72 if (!sensor_thread_->IsRunning()) | |
| 73 return sensor_thread_->Start(); | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 void PlatformSensorProviderWin::StopSensorThread() { | |
| 78 if (sensor_thread_ && sensor_thread_->IsRunning()) { | |
| 79 sensor_manager_.Release(); | |
| 80 sensor_thread_->Stop(); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void PlatformSensorProviderWin::SensorReaderCreated( | |
| 85 mojom::SensorType type, | |
| 86 mojo::ScopedSharedBufferMapping mapping, | |
| 87 const CreateSensorCallback& callback, | |
| 88 std::unique_ptr<PlatformSensorReaderWin> sensor_reader) { | |
| 89 DCHECK(CalledOnValidThread()); | |
| 90 if (!sensor_reader) { | |
| 91 callback.Run(nullptr); | |
| 92 if (!HasSensors()) | |
| 93 StopSensorThread(); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 scoped_refptr<PlatformSensor> sensor = new PlatformSensorWin( | |
| 98 type, std::move(mapping), this, sensor_thread_->task_runner(), | |
| 99 std::move(sensor_reader)); | |
| 100 callback.Run(sensor); | |
| 101 } | |
| 102 | |
| 103 std::unique_ptr<PlatformSensorReaderWin> | |
| 104 PlatformSensorProviderWin::CreateSensorReader(mojom::SensorType type) { | |
| 105 DCHECK(sensor_thread_->task_runner()->BelongsToCurrentThread()); | |
| 106 if (!InitializeSensorManager()) | |
| 107 return nullptr; | |
| 108 return PlatformSensorReaderWin::Create(type, sensor_manager_); | |
| 109 } | |
| 110 | |
| 111 } // namespace device | |
| OLD | NEW |