OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/sensor/SensorProxy.h" | 5 #include "modules/sensor/SensorProxy.h" |
6 | 6 |
7 #include "core/frame/LocalFrame.h" | 7 #include "core/frame/LocalFrame.h" |
8 #include "modules/sensor/SensorProviderProxy.h" | 8 #include "modules/sensor/SensorProviderProxy.h" |
| 9 #include "modules/sensor/SensorReading.h" |
9 #include "platform/mojo/MojoHelper.h" | 10 #include "platform/mojo/MojoHelper.h" |
10 #include "public/platform/Platform.h" | 11 #include "public/platform/Platform.h" |
11 | 12 |
12 using namespace device::mojom::blink; | 13 using namespace device::mojom::blink; |
13 | 14 |
14 namespace blink { | 15 namespace blink { |
15 | 16 |
16 SensorProxy::SensorProxy(SensorType sensorType, SensorProviderProxy* provider) | 17 SensorProxy::SensorProxy(SensorType sensorType, |
| 18 SensorProviderProxy* provider, |
| 19 std::unique_ptr<SensorReadingFactory> readingFactory) |
17 : m_type(sensorType), | 20 : m_type(sensorType), |
18 m_mode(ReportingMode::CONTINUOUS), | 21 m_mode(ReportingMode::CONTINUOUS), |
19 m_provider(provider), | 22 m_provider(provider), |
20 m_clientBinding(this), | 23 m_clientBinding(this), |
21 m_state(SensorProxy::Uninitialized), | 24 m_state(SensorProxy::Uninitialized), |
22 m_reading(), | 25 m_suspended(false), |
23 m_suspended(false) {} | 26 m_readingFactory(std::move(readingFactory)) {} |
24 | 27 |
25 SensorProxy::~SensorProxy() {} | 28 SensorProxy::~SensorProxy() {} |
26 | 29 |
27 void SensorProxy::dispose() { | 30 void SensorProxy::dispose() { |
28 m_clientBinding.Close(); | 31 m_clientBinding.Close(); |
29 } | 32 } |
30 | 33 |
31 DEFINE_TRACE(SensorProxy) { | 34 DEFINE_TRACE(SensorProxy) { |
| 35 visitor->trace(m_reading); |
32 visitor->trace(m_observers); | 36 visitor->trace(m_observers); |
33 visitor->trace(m_provider); | 37 visitor->trace(m_provider); |
34 } | 38 } |
35 | 39 |
36 void SensorProxy::addObserver(Observer* observer) { | 40 void SensorProxy::addObserver(Observer* observer) { |
37 if (!m_observers.contains(observer)) | 41 if (!m_observers.contains(observer)) |
38 m_observers.add(observer); | 42 m_observers.add(observer); |
39 } | 43 } |
40 | 44 |
41 void SensorProxy::removeObserver(Observer* observer) { | 45 void SensorProxy::removeObserver(Observer* observer) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 m_sensor->Resume(); | 95 m_sensor->Resume(); |
92 m_suspended = false; | 96 m_suspended = false; |
93 } | 97 } |
94 | 98 |
95 const device::mojom::blink::SensorConfiguration* SensorProxy::defaultConfig() | 99 const device::mojom::blink::SensorConfiguration* SensorProxy::defaultConfig() |
96 const { | 100 const { |
97 DCHECK(isInitialized()); | 101 DCHECK(isInitialized()); |
98 return m_defaultConfig.get(); | 102 return m_defaultConfig.get(); |
99 } | 103 } |
100 | 104 |
101 void SensorProxy::updateInternalReading() { | 105 void SensorProxy::updateSensorReading() { |
102 DCHECK(isInitialized()); | 106 DCHECK(isInitialized()); |
| 107 DCHECK(m_readingFactory); |
103 int readAttempts = 0; | 108 int readAttempts = 0; |
104 const int kMaxReadAttemptsCount = 10; | 109 const int kMaxReadAttemptsCount = 10; |
105 while (!tryReadFromBuffer()) { | 110 device::SensorReading readingData; |
| 111 while (!tryReadFromBuffer(readingData)) { |
106 if (++readAttempts == kMaxReadAttemptsCount) { | 112 if (++readAttempts == kMaxReadAttemptsCount) { |
107 handleSensorError(); | 113 handleSensorError(); |
108 return; | 114 return; |
109 } | 115 } |
110 } | 116 } |
| 117 |
| 118 m_reading = m_readingFactory->createSensorReading(readingData); |
111 } | 119 } |
112 | 120 |
113 void SensorProxy::RaiseError() { | 121 void SensorProxy::RaiseError() { |
114 handleSensorError(); | 122 handleSensorError(); |
115 } | 123 } |
116 | 124 |
117 void SensorProxy::SensorReadingChanged() { | 125 void SensorProxy::SensorReadingChanged() { |
118 for (Observer* observer : m_observers) | 126 for (Observer* observer : m_observers) |
119 observer->onSensorReadingChanged(); | 127 observer->onSensorReadingChanged(); |
120 } | 128 } |
121 | 129 |
122 void SensorProxy::handleSensorError(ExceptionCode code, | 130 void SensorProxy::handleSensorError(ExceptionCode code, |
123 String sanitizedMessage, | 131 String sanitizedMessage, |
124 String unsanitizedMessage) { | 132 String unsanitizedMessage) { |
125 if (!Platform::current()) { | 133 if (!Platform::current()) { |
126 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. | 134 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. |
127 return; | 135 return; |
128 } | 136 } |
129 | 137 |
130 m_state = Uninitialized; | 138 m_state = Uninitialized; |
131 // The m_sensor.reset() will release all callbacks and its bound parameters, | 139 // The m_sensor.reset() will release all callbacks and its bound parameters, |
132 // therefore, handleSensorError accepts messages by value. | 140 // therefore, handleSensorError accepts messages by value. |
133 m_sensor.reset(); | 141 m_sensor.reset(); |
134 m_sharedBuffer.reset(); | 142 m_sharedBuffer.reset(); |
135 m_sharedBufferHandle.reset(); | 143 m_sharedBufferHandle.reset(); |
136 m_defaultConfig.reset(); | 144 m_defaultConfig.reset(); |
137 m_clientBinding.Close(); | 145 m_clientBinding.Close(); |
| 146 m_reading = nullptr; |
138 | 147 |
139 for (Observer* observer : m_observers) | 148 for (Observer* observer : m_observers) |
140 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage); | 149 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage); |
141 } | 150 } |
142 | 151 |
143 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, | 152 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, |
144 SensorClientRequest clientRequest) { | 153 SensorClientRequest clientRequest) { |
145 DCHECK_EQ(Initializing, m_state); | 154 DCHECK_EQ(Initializing, m_state); |
146 if (!params) { | 155 if (!params) { |
147 handleSensorError(NotFoundError, "Sensor is not present on the platform."); | 156 handleSensorError(NotFoundError, "Sensor is not present on the platform."); |
(...skipping 27 matching lines...) Expand all Loading... |
175 WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this), | 184 WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this), |
176 UnknownError, String("Internal error"), String()); | 185 UnknownError, String("Internal error"), String()); |
177 m_sensor.set_connection_error_handler( | 186 m_sensor.set_connection_error_handler( |
178 convertToBaseCallback(std::move(errorCallback))); | 187 convertToBaseCallback(std::move(errorCallback))); |
179 | 188 |
180 m_state = Initialized; | 189 m_state = Initialized; |
181 for (Observer* observer : m_observers) | 190 for (Observer* observer : m_observers) |
182 observer->onSensorInitialized(); | 191 observer->onSensorInitialized(); |
183 } | 192 } |
184 | 193 |
185 bool SensorProxy::tryReadFromBuffer() { | 194 bool SensorProxy::tryReadFromBuffer(device::SensorReading& result) { |
186 DCHECK(isInitialized()); | 195 DCHECK(isInitialized()); |
187 const ReadingBuffer* buffer = | 196 const ReadingBuffer* buffer = |
188 static_cast<const ReadingBuffer*>(m_sharedBuffer.get()); | 197 static_cast<const ReadingBuffer*>(m_sharedBuffer.get()); |
189 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); | 198 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); |
190 auto version = seqlock.ReadBegin(); | 199 auto version = seqlock.ReadBegin(); |
191 auto reading = buffer->reading; | 200 auto readingData = buffer->reading; |
192 if (seqlock.ReadRetry(version)) | 201 if (seqlock.ReadRetry(version)) |
193 return false; | 202 return false; |
194 m_reading = reading; | 203 result = readingData; |
195 return true; | 204 return true; |
196 } | 205 } |
197 | 206 |
198 } // namespace blink | 207 } // namespace blink |
OLD | NEW |