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 "base/win/windows_version.h" | |
| 11 #include "device/generic_sensor/platform_sensor_win.h" | |
| 12 | |
| 13 namespace device { | |
| 14 | |
| 15 // static | |
| 16 PlatformSensorProvider* PlatformSensorProvider::GetInstance() { | |
| 17 return PlatformSensorProviderWin::GetInstance(); | |
| 18 } | |
| 19 | |
| 20 // static | |
| 21 PlatformSensorProviderWin* PlatformSensorProviderWin::GetInstance() { | |
| 22 if (base::win::GetVersion() < base::win::VERSION_WIN7) | |
| 23 return nullptr; | |
|
Mikhail
2016/10/26 06:40:15
maybe logging here?
shalamov
2016/10/26 12:09:43
Done.
| |
| 24 return base::Singleton< | |
| 25 PlatformSensorProviderWin, | |
| 26 base::LeakySingletonTraits<PlatformSensorProviderWin>>::get(); | |
| 27 } | |
| 28 | |
| 29 void PlatformSensorProviderWin::SetSensorManagerForTesing( | |
| 30 base::win::ScopedComPtr<ISensorManager> sensor_manager) { | |
| 31 m_sensor_manager_ = sensor_manager; | |
| 32 } | |
| 33 | |
| 34 PlatformSensorProviderWin::PlatformSensorProviderWin() = default; | |
| 35 PlatformSensorProviderWin::~PlatformSensorProviderWin() = default; | |
| 36 | |
| 37 void PlatformSensorProviderWin::CreateSensorInternal( | |
| 38 mojom::SensorType type, | |
| 39 mojo::ScopedSharedBufferMapping mapping, | |
| 40 const CreateSensorCallback& callback) { | |
| 41 DCHECK(CalledOnValidThread()); | |
| 42 if (!StartSensorThread()) { | |
| 43 callback.Run(nullptr); | |
| 44 return; | |
| 45 } | |
| 46 | |
| 47 base::PostTaskAndReplyWithResult( | |
| 48 sensor_thread_->task_runner().get(), FROM_HERE, | |
| 49 base::Bind(&PlatformSensorProviderWin::CreateSensorReader, | |
| 50 base::Unretained(this), type), | |
| 51 base::Bind(&PlatformSensorProviderWin::SensorReaderCreated, | |
| 52 base::Unretained(this), type, base::Passed(&mapping), | |
| 53 callback)); | |
| 54 } | |
| 55 | |
| 56 bool PlatformSensorProviderWin::InitializeSensorManager() { | |
| 57 if (m_sensor_manager_) | |
| 58 return true; | |
| 59 | |
| 60 HRESULT hr = m_sensor_manager_.CreateInstance(CLSID_SensorManager); | |
| 61 if (FAILED(hr) || !m_sensor_manager_) | |
|
Mikhail
2016/10/26 06:40:15
return (!FAILED(hr) && m_sensor_manager_);
shalamov
2016/10/26 12:09:43
Done.
| |
| 62 return false; | |
| 63 | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 void PlatformSensorProviderWin::AllSensorsRemoved() { | |
| 68 m_sensor_manager_.Release(); | |
| 69 StopSensorThread(); | |
| 70 } | |
| 71 | |
| 72 bool PlatformSensorProviderWin::StartSensorThread() { | |
| 73 if (!sensor_thread_) { | |
| 74 sensor_thread_.reset(new base::Thread("Windows platform sensor thread")); | |
| 75 sensor_thread_->init_com_with_mta(true); | |
| 76 } | |
| 77 | |
| 78 if (!sensor_thread_->IsRunning()) | |
| 79 return sensor_thread_->Start(); | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 void PlatformSensorProviderWin::StopSensorThread() { | |
| 84 if (sensor_thread_ && sensor_thread_->IsRunning()) | |
| 85 sensor_thread_->Stop(); | |
|
Mikhail
2016/10/26 06:40:15
sensor_thread_.reset() ?
shalamov
2016/10/26 12:09:43
Thread can be reused after Stop is called.
| |
| 86 } | |
| 87 | |
| 88 void PlatformSensorProviderWin::SensorReaderCreated( | |
| 89 mojom::SensorType type, | |
| 90 mojo::ScopedSharedBufferMapping mapping, | |
| 91 const CreateSensorCallback& callback, | |
| 92 std::unique_ptr<PlatformSensorReaderWin> sensor_reader) { | |
| 93 DCHECK(CalledOnValidThread()); | |
| 94 if (!sensor_reader) { | |
| 95 callback.Run(nullptr); | |
| 96 if (!HasSensors()) | |
| 97 StopSensorThread(); | |
| 98 return; | |
| 99 } | |
| 100 | |
| 101 scoped_refptr<PlatformSensor> sensor = new PlatformSensorWin( | |
| 102 type, std::move(mapping), this, sensor_thread_->task_runner(), | |
| 103 std::move(sensor_reader)); | |
| 104 callback.Run(sensor); | |
| 105 } | |
| 106 | |
| 107 std::unique_ptr<PlatformSensorReaderWin> | |
| 108 PlatformSensorProviderWin::CreateSensorReader(mojom::SensorType type) { | |
| 109 DCHECK(sensor_thread_->task_runner()->BelongsToCurrentThread()); | |
| 110 if (!InitializeSensorManager()) | |
| 111 return nullptr; | |
| 112 return PlatformSensorReaderWin::Create(type, m_sensor_manager_); | |
| 113 } | |
| 114 | |
| 115 } // namespace device | |
| OLD | NEW |