Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: third_party/WebKit/Source/modules/sensor/SensorProxy.cpp

Issue 2395853003: [Sensors] Improvements in shared buffer managing (Closed)
Patch Set: Test compilation fix + comment from Ken Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/modules/sensor/SensorProxy.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "platform/mojo/MojoHelper.h" 9 #include "platform/mojo/MojoHelper.h"
10 10
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 } 92 }
93 93
94 const device::mojom::blink::SensorConfiguration* SensorProxy::defaultConfig() 94 const device::mojom::blink::SensorConfiguration* SensorProxy::defaultConfig()
95 const { 95 const {
96 DCHECK(isInitialized()); 96 DCHECK(isInitialized());
97 return m_defaultConfig.get(); 97 return m_defaultConfig.get();
98 } 98 }
99 99
100 void SensorProxy::updateInternalReading() { 100 void SensorProxy::updateInternalReading() {
101 DCHECK(isInitialized()); 101 DCHECK(isInitialized());
102 Reading* reading = static_cast<Reading*>(m_sharedBuffer.get()); 102 int readAttempts = 0;
103 m_reading = *reading; 103 const int kMaxReadAttemptsCount = 10;
104 while (!tryReadFromBuffer()) {
105 if (++readAttempts == kMaxReadAttemptsCount) {
106 handleSensorError();
107 return;
108 }
109 }
104 } 110 }
105 111
106 void SensorProxy::RaiseError() { 112 void SensorProxy::RaiseError() {
107 handleSensorError(); 113 handleSensorError();
108 } 114 }
109 115
110 void SensorProxy::SensorReadingChanged() { 116 void SensorProxy::SensorReadingChanged() {
111 for (Observer* observer : m_observers) 117 for (Observer* observer : m_observers)
112 observer->onSensorReadingChanged(); 118 observer->onSensorReadingChanged();
113 } 119 }
(...skipping 12 matching lines...) Expand all
126 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage); 132 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage);
127 } 133 }
128 134
129 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, 135 void SensorProxy::onSensorCreated(SensorInitParamsPtr params,
130 SensorClientRequest clientRequest) { 136 SensorClientRequest clientRequest) {
131 DCHECK_EQ(Initializing, m_state); 137 DCHECK_EQ(Initializing, m_state);
132 if (!params) { 138 if (!params) {
133 handleSensorError(NotFoundError, "Sensor is not present on the platform."); 139 handleSensorError(NotFoundError, "Sensor is not present on the platform.");
134 return; 140 return;
135 } 141 }
142 const size_t kReadBufferSize = sizeof(ReadingBuffer);
136 143
137 DCHECK_EQ(0u, params->buffer_offset % SensorInitParams::kReadBufferSize); 144 DCHECK_EQ(0u, params->buffer_offset % kReadBufferSize);
138 145
139 m_mode = params->mode; 146 m_mode = params->mode;
140 m_defaultConfig = std::move(params->default_configuration); 147 m_defaultConfig = std::move(params->default_configuration);
141 if (!m_defaultConfig) { 148 if (!m_defaultConfig) {
142 handleSensorError(); 149 handleSensorError();
143 return; 150 return;
144 } 151 }
145 152
146 DCHECK(m_sensor.is_bound()); 153 DCHECK(m_sensor.is_bound());
147 m_clientBinding.Bind(std::move(clientRequest)); 154 m_clientBinding.Bind(std::move(clientRequest));
148 155
149 m_sharedBufferHandle = std::move(params->memory); 156 m_sharedBufferHandle = std::move(params->memory);
150 DCHECK(!m_sharedBuffer); 157 DCHECK(!m_sharedBuffer);
151 m_sharedBuffer = m_sharedBufferHandle->MapAtOffset( 158 m_sharedBuffer =
152 SensorInitParams::kReadBufferSize, params->buffer_offset); 159 m_sharedBufferHandle->MapAtOffset(kReadBufferSize, params->buffer_offset);
153 160
154 if (!m_sharedBuffer) { 161 if (!m_sharedBuffer) {
155 handleSensorError(); 162 handleSensorError();
156 return; 163 return;
157 } 164 }
158 165
159 auto errorCallback = 166 auto errorCallback =
160 WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this), 167 WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this),
161 UnknownError, String("Internal error"), String()); 168 UnknownError, String("Internal error"), String());
162 m_sensor.set_connection_error_handler( 169 m_sensor.set_connection_error_handler(
163 convertToBaseCallback(std::move(errorCallback))); 170 convertToBaseCallback(std::move(errorCallback)));
164 171
165 m_state = Initialized; 172 m_state = Initialized;
166 for (Observer* observer : m_observers) 173 for (Observer* observer : m_observers)
167 observer->onSensorInitialized(); 174 observer->onSensorInitialized();
168 } 175 }
169 176
177 bool SensorProxy::tryReadFromBuffer() {
178 DCHECK(isInitialized());
179 ReadingBuffer* buffer = static_cast<ReadingBuffer*>(m_sharedBuffer.get());
180 device::OneWriterSeqLock& seqlock = buffer->seqlock.value();
181 auto version = seqlock.ReadBegin();
182 auto reading = buffer->reading;
183 if (seqlock.ReadRetry(version))
184 return false;
185 m_reading = reading;
186 return true;
187 }
188
170 } // namespace blink 189 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/sensor/SensorProxy.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698