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) { | |
Lei Zhang
2016/10/29 05:10:41
Modern Chromium only supports Win7 and newer.
shalamov
2016/10/31 13:12:57
I removed check. Thanks.
| |
23 VLOG(1) << "Sensor API is only supported on Windows 7 or newer versions."; | |
24 return nullptr; | |
25 } | |
26 | |
27 return base::Singleton< | |
28 PlatformSensorProviderWin, | |
29 base::LeakySingletonTraits<PlatformSensorProviderWin>>::get(); | |
30 } | |
31 | |
32 void PlatformSensorProviderWin::SetSensorManagerForTesing( | |
33 base::win::ScopedComPtr<ISensorManager> sensor_manager) { | |
34 sensor_manager_ = sensor_manager; | |
35 } | |
36 | |
37 PlatformSensorProviderWin::PlatformSensorProviderWin() = default; | |
38 PlatformSensorProviderWin::~PlatformSensorProviderWin() = default; | |
39 | |
40 void PlatformSensorProviderWin::CreateSensorInternal( | |
41 mojom::SensorType type, | |
42 mojo::ScopedSharedBufferMapping mapping, | |
43 const CreateSensorCallback& callback) { | |
44 DCHECK(CalledOnValidThread()); | |
45 if (!StartSensorThread()) { | |
46 callback.Run(nullptr); | |
47 return; | |
48 } | |
49 | |
50 base::PostTaskAndReplyWithResult( | |
51 sensor_thread_->task_runner().get(), FROM_HERE, | |
52 base::Bind(&PlatformSensorProviderWin::CreateSensorReader, | |
53 base::Unretained(this), type), | |
54 base::Bind(&PlatformSensorProviderWin::SensorReaderCreated, | |
55 base::Unretained(this), type, base::Passed(&mapping), | |
56 callback)); | |
57 } | |
58 | |
59 bool PlatformSensorProviderWin::InitializeSensorManager() { | |
60 if (sensor_manager_) | |
61 return true; | |
62 | |
63 HRESULT hr = sensor_manager_.CreateInstance(CLSID_SensorManager); | |
64 return !FAILED(hr) && sensor_manager_; | |
Lei Zhang
2016/10/29 05:10:42
Why not SUCCEEDED(hr)?
shalamov
2016/10/31 13:12:57
Done.
| |
65 } | |
66 | |
67 void PlatformSensorProviderWin::AllSensorsRemoved() { | |
68 StopSensorThread(); | |
69 } | |
70 | |
71 bool PlatformSensorProviderWin::StartSensorThread() { | |
72 if (!sensor_thread_) { | |
73 sensor_thread_.reset(new base::Thread("Windows platform sensor thread")); | |
Lei Zhang
2016/10/29 05:10:41
base::MakeUnique
shalamov
2016/10/31 13:12:57
Done.
| |
74 sensor_thread_->init_com_with_mta(true); | |
75 } | |
76 | |
77 if (!sensor_thread_->IsRunning()) | |
78 return sensor_thread_->Start(); | |
79 return true; | |
80 } | |
81 | |
82 void PlatformSensorProviderWin::StopSensorThread() { | |
83 if (sensor_thread_ && sensor_thread_->IsRunning()) { | |
84 sensor_manager_.Release(); | |
85 sensor_thread_->Stop(); | |
86 } | |
87 } | |
88 | |
89 void PlatformSensorProviderWin::SensorReaderCreated( | |
90 mojom::SensorType type, | |
91 mojo::ScopedSharedBufferMapping mapping, | |
92 const CreateSensorCallback& callback, | |
93 std::unique_ptr<PlatformSensorReaderWin> sensor_reader) { | |
94 DCHECK(CalledOnValidThread()); | |
95 if (!sensor_reader) { | |
96 callback.Run(nullptr); | |
97 if (!HasSensors()) | |
98 StopSensorThread(); | |
99 return; | |
100 } | |
101 | |
102 scoped_refptr<PlatformSensor> sensor = new PlatformSensorWin( | |
103 type, std::move(mapping), this, sensor_thread_->task_runner(), | |
104 std::move(sensor_reader)); | |
105 callback.Run(sensor); | |
106 } | |
107 | |
108 std::unique_ptr<PlatformSensorReaderWin> | |
109 PlatformSensorProviderWin::CreateSensorReader(mojom::SensorType type) { | |
110 DCHECK(sensor_thread_->task_runner()->BelongsToCurrentThread()); | |
111 if (!InitializeSensorManager()) | |
112 return nullptr; | |
113 return PlatformSensorReaderWin::Create(type, sensor_manager_); | |
114 } | |
115 | |
116 } // namespace device | |
OLD | NEW |