platform_sensor_provider_win.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include "services/device/generic_sensor/platform_sensor_provider_win.h"
  5. #include <comdef.h>
  6. #include <objbase.h>
  7. #include <iomanip>
  8. #include "base/bind.h"
  9. #include "base/task/task_runner_util.h"
  10. #include "base/task/task_traits.h"
  11. #include "base/task/thread_pool.h"
  12. #include "base/threading/thread.h"
  13. #include "services/device/generic_sensor/gravity_fusion_algorithm_using_accelerometer.h"
  14. #include "services/device/generic_sensor/linear_acceleration_fusion_algorithm_using_accelerometer.h"
  15. #include "services/device/generic_sensor/orientation_euler_angles_fusion_algorithm_using_quaternion.h"
  16. #include "services/device/generic_sensor/platform_sensor_fusion.h"
  17. #include "services/device/generic_sensor/platform_sensor_win.h"
  18. namespace device {
  19. PlatformSensorProviderWin::PlatformSensorProviderWin()
  20. : com_sta_task_runner_(base::ThreadPool::CreateCOMSTATaskRunner(
  21. {base::TaskPriority::USER_VISIBLE})) {}
  22. PlatformSensorProviderWin::~PlatformSensorProviderWin() = default;
  23. void PlatformSensorProviderWin::SetSensorManagerForTesting(
  24. Microsoft::WRL::ComPtr<ISensorManager> sensor_manager) {
  25. sensor_manager_ = sensor_manager;
  26. }
  27. scoped_refptr<base::SingleThreadTaskRunner>
  28. PlatformSensorProviderWin::GetComStaTaskRunnerForTesting() {
  29. return com_sta_task_runner_;
  30. }
  31. void PlatformSensorProviderWin::CreateSensorInternal(
  32. mojom::SensorType type,
  33. SensorReadingSharedBuffer* reading_buffer,
  34. CreateSensorCallback callback) {
  35. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  36. if (sensor_manager_) {
  37. OnInitSensorManager(type, reading_buffer, std::move(callback));
  38. } else {
  39. com_sta_task_runner_->PostTaskAndReply(
  40. FROM_HERE,
  41. base::BindOnce(&PlatformSensorProviderWin::InitSensorManager,
  42. base::Unretained(this)),
  43. base::BindOnce(&PlatformSensorProviderWin::OnInitSensorManager,
  44. base::Unretained(this), type, reading_buffer,
  45. std::move(callback)));
  46. }
  47. }
  48. void PlatformSensorProviderWin::InitSensorManager() {
  49. DCHECK(com_sta_task_runner_->RunsTasksInCurrentSequence());
  50. HRESULT hr = ::CoCreateInstance(CLSID_SensorManager, nullptr, CLSCTX_ALL,
  51. IID_PPV_ARGS(&sensor_manager_));
  52. if (FAILED(hr)) {
  53. // Only log this error the first time.
  54. static bool logged_failure = false;
  55. if (!logged_failure) {
  56. LOG(ERROR) << "Unable to create instance of SensorManager: "
  57. << _com_error(hr).ErrorMessage() << " (0x" << std::hex
  58. << std::uppercase << std::setfill('0') << std::setw(8) << hr
  59. << ")";
  60. logged_failure = true;
  61. }
  62. }
  63. }
  64. void PlatformSensorProviderWin::OnInitSensorManager(
  65. mojom::SensorType type,
  66. SensorReadingSharedBuffer* reading_buffer,
  67. CreateSensorCallback callback) {
  68. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  69. if (!sensor_manager_) {
  70. std::move(callback).Run(nullptr);
  71. return;
  72. }
  73. switch (type) {
  74. // Fusion sensors.
  75. case mojom::SensorType::LINEAR_ACCELERATION: {
  76. auto linear_acceleration_fusion_algorithm = std::make_unique<
  77. LinearAccelerationFusionAlgorithmUsingAccelerometer>();
  78. // If this PlatformSensorFusion object is successfully initialized,
  79. // |callback| will be run with a reference to this object.
  80. PlatformSensorFusion::Create(
  81. reading_buffer, this, std::move(linear_acceleration_fusion_algorithm),
  82. std::move(callback));
  83. break;
  84. }
  85. case mojom::SensorType::GRAVITY: {
  86. auto gravity_fusion_algorithm =
  87. std::make_unique<GravityFusionAlgorithmUsingAccelerometer>();
  88. // If this PlatformSensorFusion object is successfully initialized,
  89. // |callback| will be run with a reference to this object.
  90. PlatformSensorFusion::Create(reading_buffer, this,
  91. std::move(gravity_fusion_algorithm),
  92. std::move(callback));
  93. break;
  94. }
  95. // Try to create low-level sensors by default.
  96. default: {
  97. base::PostTaskAndReplyWithResult(
  98. com_sta_task_runner_.get(), FROM_HERE,
  99. base::BindOnce(&PlatformSensorProviderWin::CreateSensorReader,
  100. base::Unretained(this), type),
  101. base::BindOnce(&PlatformSensorProviderWin::SensorReaderCreated,
  102. base::Unretained(this), type, reading_buffer,
  103. std::move(callback)));
  104. break;
  105. }
  106. }
  107. }
  108. void PlatformSensorProviderWin::SensorReaderCreated(
  109. mojom::SensorType type,
  110. SensorReadingSharedBuffer* reading_buffer,
  111. CreateSensorCallback callback,
  112. std::unique_ptr<PlatformSensorReaderWinBase> sensor_reader) {
  113. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  114. if (!sensor_reader) {
  115. // Fallback options for sensors that can be implemented using sensor
  116. // fusion. Note that it is important not to generate a cycle by adding a
  117. // fallback here that depends on one of the other fallbacks provided.
  118. switch (type) {
  119. case mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES: {
  120. auto algorithm = std::make_unique<
  121. OrientationEulerAnglesFusionAlgorithmUsingQuaternion>(
  122. true /* absolute */);
  123. PlatformSensorFusion::Create(reading_buffer, this, std::move(algorithm),
  124. std::move(callback));
  125. return;
  126. }
  127. default:
  128. std::move(callback).Run(nullptr);
  129. return;
  130. }
  131. }
  132. scoped_refptr<PlatformSensor> sensor =
  133. new PlatformSensorWin(type, reading_buffer, this, com_sta_task_runner_,
  134. std::move(sensor_reader));
  135. std::move(callback).Run(sensor);
  136. }
  137. std::unique_ptr<PlatformSensorReaderWinBase>
  138. PlatformSensorProviderWin::CreateSensorReader(mojom::SensorType type) {
  139. DCHECK(com_sta_task_runner_->RunsTasksInCurrentSequence());
  140. if (!sensor_manager_)
  141. return nullptr;
  142. return PlatformSensorReaderWin32::Create(type, sensor_manager_);
  143. }
  144. } // namespace device