Chromium Code Reviews| Index: device/generic_sensor/platform_sensor_provider_win.cc |
| diff --git a/device/generic_sensor/platform_sensor_provider_win.cc b/device/generic_sensor/platform_sensor_provider_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a6e059b95b52cf792271da45ef8b45549faa7395 |
| --- /dev/null |
| +++ b/device/generic_sensor/platform_sensor_provider_win.cc |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/generic_sensor/platform_sensor_provider_win.h" |
| + |
| +#include "base/memory/singleton.h" |
| +#include "base/task_runner_util.h" |
| +#include "base/threading/thread.h" |
| +#include "base/win/windows_version.h" |
| +#include "device/generic_sensor/platform_sensor_win.h" |
| + |
| +namespace device { |
| + |
| +// static |
| +PlatformSensorProvider* PlatformSensorProvider::GetInstance() { |
| + return PlatformSensorProviderWin::GetInstance(); |
| +} |
| + |
| +// static |
| +PlatformSensorProviderWin* PlatformSensorProviderWin::GetInstance() { |
| + if (base::win::GetVersion() < base::win::VERSION_WIN7) { |
| + VLOG(1) << "Sensor API is only supported on Windows 7 or newer versions."; |
| + return nullptr; |
| + } |
| + |
| + return base::Singleton< |
| + PlatformSensorProviderWin, |
| + base::LeakySingletonTraits<PlatformSensorProviderWin>>::get(); |
| +} |
| + |
| +void PlatformSensorProviderWin::SetSensorManagerForTesing( |
| + base::win::ScopedComPtr<ISensorManager> sensor_manager) { |
| + m_sensor_manager_ = sensor_manager; |
| +} |
| + |
| +PlatformSensorProviderWin::PlatformSensorProviderWin() = default; |
| +PlatformSensorProviderWin::~PlatformSensorProviderWin() = default; |
| + |
| +void PlatformSensorProviderWin::CreateSensorInternal( |
| + mojom::SensorType type, |
| + mojo::ScopedSharedBufferMapping mapping, |
| + const CreateSensorCallback& callback) { |
| + DCHECK(CalledOnValidThread()); |
| + if (!StartSensorThread()) { |
| + callback.Run(nullptr); |
| + return; |
| + } |
| + |
| + base::PostTaskAndReplyWithResult( |
| + sensor_thread_->task_runner().get(), FROM_HERE, |
| + base::Bind(&PlatformSensorProviderWin::CreateSensorReader, |
| + base::Unretained(this), type), |
| + base::Bind(&PlatformSensorProviderWin::SensorReaderCreated, |
| + base::Unretained(this), type, base::Passed(&mapping), |
| + callback)); |
| +} |
| + |
| +bool PlatformSensorProviderWin::InitializeSensorManager() { |
| + if (m_sensor_manager_) |
| + return true; |
| + |
| + HRESULT hr = m_sensor_manager_.CreateInstance(CLSID_SensorManager); |
| + return (!FAILED(hr) && m_sensor_manager_); |
|
Reilly Grant (use Gerrit)
2016/10/27 00:59:19
nit: parens around the return value are unnecessar
shalamov
2016/10/27 13:51:07
Done.
|
| +} |
| + |
| +void PlatformSensorProviderWin::AllSensorsRemoved() { |
| + StopSensorThread(); |
| +} |
| + |
| +bool PlatformSensorProviderWin::StartSensorThread() { |
| + if (!sensor_thread_) { |
| + sensor_thread_.reset(new base::Thread("Windows platform sensor thread")); |
| + sensor_thread_->init_com_with_mta(true); |
| + } |
| + |
| + if (!sensor_thread_->IsRunning()) |
| + return sensor_thread_->Start(); |
| + return true; |
| +} |
| + |
| +void PlatformSensorProviderWin::StopSensorThread() { |
| + if (sensor_thread_ && sensor_thread_->IsRunning()) { |
| + m_sensor_manager_.Release(); |
| + sensor_thread_->Stop(); |
| + } |
| +} |
| + |
| +void PlatformSensorProviderWin::SensorReaderCreated( |
| + mojom::SensorType type, |
| + mojo::ScopedSharedBufferMapping mapping, |
| + const CreateSensorCallback& callback, |
| + std::unique_ptr<PlatformSensorReaderWin> sensor_reader) { |
| + DCHECK(CalledOnValidThread()); |
| + if (!sensor_reader) { |
| + callback.Run(nullptr); |
| + if (!HasSensors()) |
|
Reilly Grant (use Gerrit)
2016/10/27 00:59:19
I don't see this function defined anywhere.
shalamov
2016/10/27 13:51:07
It is in the base class https://cs.chromium.org/ch
|
| + StopSensorThread(); |
| + return; |
| + } |
| + |
| + scoped_refptr<PlatformSensor> sensor = new PlatformSensorWin( |
| + type, std::move(mapping), this, sensor_thread_->task_runner(), |
| + std::move(sensor_reader)); |
| + callback.Run(sensor); |
| +} |
| + |
| +std::unique_ptr<PlatformSensorReaderWin> |
| +PlatformSensorProviderWin::CreateSensorReader(mojom::SensorType type) { |
| + DCHECK(sensor_thread_->task_runner()->BelongsToCurrentThread()); |
| + if (!InitializeSensorManager()) |
| + return nullptr; |
| + return PlatformSensorReaderWin::Create(type, m_sensor_manager_); |
| +} |
| + |
| +} // namespace device |