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/permissions/PermissionUtils.h" | |
8 #include "modules/sensor/SensorProviderProxy.h" | 9 #include "modules/sensor/SensorProviderProxy.h" |
10 #include "platform/UserGestureIndicator.h" | |
9 #include "platform/mojo/MojoHelper.h" | 11 #include "platform/mojo/MojoHelper.h" |
10 #include "public/platform/Platform.h" | 12 #include "public/platform/Platform.h" |
13 #include "wtf/RefPtr.h" | |
11 | 14 |
12 using namespace device::mojom::blink; | 15 using namespace device::mojom::blink; |
13 | 16 |
14 namespace blink { | 17 namespace blink { |
15 | 18 |
16 SensorProxy::SensorProxy(SensorType sensorType, SensorProviderProxy* provider) | 19 using mojom::blink::PermissionName; |
20 using mojom::blink::PermissionService; | |
21 using mojom::blink::PermissionStatus; | |
22 | |
23 SensorProxy::SensorProxy(SensorType sensorType, | |
24 ExecutionContext* executionContext, | |
25 SensorProviderProxy* provider) | |
17 : m_type(sensorType), | 26 : m_type(sensorType), |
18 m_mode(ReportingMode::CONTINUOUS), | 27 m_mode(ReportingMode::CONTINUOUS), |
19 m_provider(provider), | 28 m_provider(provider), |
20 m_clientBinding(this), | 29 m_clientBinding(this), |
21 m_state(SensorProxy::Uninitialized), | 30 m_state(SensorProxy::Uninitialized), |
22 m_reading(), | 31 m_reading(), |
23 m_suspended(false) {} | 32 m_suspended(false), |
33 m_sensorPermission(PermissionStatus::ASK), | |
34 m_executionContext(executionContext) { | |
35 // Get permission service. | |
shalamov
2016/11/04 19:00:20
Is this needed in constructor? Also, handleSersonE
riju_
2016/11/09 10:30:34
Moving to Initialize
| |
36 PermissionService* permissionService = | |
37 getPermissionService(m_executionContext); | |
38 if (!permissionService) { | |
39 handleSensorError( | |
40 InvalidStateError, | |
41 "In its current state, the global scope can't request permissions"); | |
42 } | |
24 | 43 |
25 SensorProxy::~SensorProxy() {} | 44 // Check permissions. |
45 m_permissionService->HasPermission( | |
46 createPermissionDescriptor(PermissionName::SENSORS), | |
47 m_executionContext->getSecurityOrigin(), | |
48 convertToBaseCallback( | |
49 WTF::bind(&SensorProxy::onPermissionUpdate, wrapPersistent(this)))); | |
50 } | |
51 | |
52 void SensorProxy::getNextPermissionChange(ExecutionContext* executionContext, | |
53 PermissionStatus status) { | |
54 m_permissionService->GetNextPermissionChange( | |
55 createPermissionDescriptor(PermissionName::SENSORS), | |
56 executionContext->getSecurityOrigin(), m_sensorPermission, | |
57 convertToBaseCallback( | |
58 WTF::bind(&SensorProxy::onPermissionUpdate, wrapPersistent(this)))); | |
59 } | |
60 | |
61 void SensorProxy::requestPermission(ExecutionContext* executionContext, | |
62 PermissionStatus status) { | |
63 m_permissionService->RequestPermission( | |
64 createPermissionDescriptor(PermissionName::SENSORS), | |
65 m_executionContext->getSecurityOrigin(), | |
66 UserGestureIndicator::processingUserGesture(), | |
67 convertToBaseCallback( | |
68 WTF::bind(&SensorProxy::onPermissionUpdate, wrapPersistent(this)))); | |
69 } | |
70 | |
71 void SensorProxy::onPermissionUpdate(PermissionStatus status) { | |
72 if (m_sensorPermission != status) { | |
73 m_sensorPermission = status; | |
74 SensorPermissionChanged(); | |
75 } | |
76 } | |
77 | |
78 SensorProxy::~SensorProxy() { | |
79 m_permissionService.reset(); | |
Mikhail
2016/11/07 08:02:02
isn't it automatically reset when destructed?
riju_
2016/11/09 10:30:34
Done.
| |
80 } | |
81 | |
82 void SensorProxy::resetPermissionService() { | |
83 m_permissionService.reset(); | |
84 } | |
85 | |
86 PermissionService* SensorProxy::getPermissionService( | |
87 ExecutionContext* executionContext) { | |
88 if (!m_permissionService && | |
89 connectToPermissionService(executionContext, | |
90 mojo::GetProxy(&m_permissionService))) { | |
91 m_permissionService.set_connection_error_handler(convertToBaseCallback( | |
92 WTF::bind(&SensorProxy::permissionServiceConnectionError, | |
93 wrapWeakPersistent(this)))); | |
94 } | |
95 return m_permissionService.get(); | |
96 } | |
97 | |
98 void SensorProxy::permissionServiceConnectionError() { | |
99 m_permissionService.reset(); | |
100 } | |
26 | 101 |
27 void SensorProxy::dispose() { | 102 void SensorProxy::dispose() { |
28 m_clientBinding.Close(); | 103 m_clientBinding.Close(); |
29 } | 104 } |
30 | 105 |
31 DEFINE_TRACE(SensorProxy) { | 106 DEFINE_TRACE(SensorProxy) { |
32 visitor->trace(m_observers); | 107 visitor->trace(m_observers); |
33 visitor->trace(m_provider); | 108 visitor->trace(m_provider); |
109 visitor->trace(m_executionContext); | |
34 } | 110 } |
35 | 111 |
36 void SensorProxy::addObserver(Observer* observer) { | 112 void SensorProxy::addObserver(Observer* observer) { |
37 if (!m_observers.contains(observer)) | 113 if (!m_observers.contains(observer)) |
38 m_observers.add(observer); | 114 m_observers.add(observer); |
39 } | 115 } |
40 | 116 |
41 void SensorProxy::removeObserver(Observer* observer) { | 117 void SensorProxy::removeObserver(Observer* observer) { |
42 m_observers.remove(observer); | 118 m_observers.remove(observer); |
43 } | 119 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 | 188 |
113 void SensorProxy::RaiseError() { | 189 void SensorProxy::RaiseError() { |
114 handleSensorError(); | 190 handleSensorError(); |
115 } | 191 } |
116 | 192 |
117 void SensorProxy::SensorReadingChanged() { | 193 void SensorProxy::SensorReadingChanged() { |
118 for (Observer* observer : m_observers) | 194 for (Observer* observer : m_observers) |
119 observer->onSensorReadingChanged(); | 195 observer->onSensorReadingChanged(); |
120 } | 196 } |
121 | 197 |
198 void SensorProxy::SensorPermissionChanged() { | |
199 for (Observer* observer : m_observers) | |
200 observer->onSensorPermissionChanged(); | |
201 } | |
202 | |
122 void SensorProxy::handleSensorError(ExceptionCode code, | 203 void SensorProxy::handleSensorError(ExceptionCode code, |
123 const String& sanitizedMessage, | 204 const String& sanitizedMessage, |
124 const String& unsanitizedMessage) { | 205 const String& unsanitizedMessage) { |
125 if (!Platform::current()) { | 206 if (!Platform::current()) { |
126 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. | 207 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. |
127 return; | 208 return; |
128 } | 209 } |
129 m_state = Uninitialized; | 210 m_state = Uninitialized; |
130 m_sensor.reset(); | 211 m_sensor.reset(); |
131 m_sharedBuffer.reset(); | 212 m_sharedBuffer.reset(); |
132 m_sharedBufferHandle.reset(); | 213 m_sharedBufferHandle.reset(); |
133 m_defaultConfig.reset(); | 214 m_defaultConfig.reset(); |
134 m_clientBinding.Close(); | 215 m_clientBinding.Close(); |
216 m_permissionService.reset(); | |
135 | 217 |
136 for (Observer* observer : m_observers) | 218 for (Observer* observer : m_observers) |
137 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage); | 219 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage); |
138 } | 220 } |
139 | 221 |
140 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, | 222 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, |
141 SensorClientRequest clientRequest) { | 223 SensorClientRequest clientRequest) { |
142 DCHECK_EQ(Initializing, m_state); | 224 DCHECK_EQ(Initializing, m_state); |
143 if (!params) { | 225 if (!params) { |
144 handleSensorError(NotFoundError, "Sensor is not present on the platform."); | 226 handleSensorError(NotFoundError, "Sensor is not present on the platform."); |
(...skipping 16 matching lines...) Expand all Loading... | |
161 m_sharedBufferHandle = std::move(params->memory); | 243 m_sharedBufferHandle = std::move(params->memory); |
162 DCHECK(!m_sharedBuffer); | 244 DCHECK(!m_sharedBuffer); |
163 m_sharedBuffer = | 245 m_sharedBuffer = |
164 m_sharedBufferHandle->MapAtOffset(kReadBufferSize, params->buffer_offset); | 246 m_sharedBufferHandle->MapAtOffset(kReadBufferSize, params->buffer_offset); |
165 | 247 |
166 if (!m_sharedBuffer) { | 248 if (!m_sharedBuffer) { |
167 handleSensorError(); | 249 handleSensorError(); |
168 return; | 250 return; |
169 } | 251 } |
170 | 252 |
253 // Check permission now. | |
254 if (m_sensorPermission == PermissionStatus::ASK) | |
255 requestPermission(m_executionContext, m_sensorPermission); | |
Mikhail
2016/11/04 13:05:59
here it's a bit late to check permission as sensor
riju_
2016/11/09 10:30:34
Done.
| |
256 | |
171 auto errorCallback = | 257 auto errorCallback = |
172 WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this), | 258 WTF::bind(&SensorProxy::handleSensorError, wrapWeakPersistent(this), |
173 UnknownError, String("Internal error"), String()); | 259 UnknownError, String("Internal error"), String()); |
174 m_sensor.set_connection_error_handler( | 260 m_sensor.set_connection_error_handler( |
175 convertToBaseCallback(std::move(errorCallback))); | 261 convertToBaseCallback(std::move(errorCallback))); |
176 | 262 |
177 m_state = Initialized; | 263 m_state = Initialized; |
178 for (Observer* observer : m_observers) | 264 for (Observer* observer : m_observers) |
179 observer->onSensorInitialized(); | 265 observer->onSensorInitialized(); |
180 } | 266 } |
181 | 267 |
182 bool SensorProxy::tryReadFromBuffer() { | 268 bool SensorProxy::tryReadFromBuffer() { |
183 DCHECK(isInitialized()); | 269 DCHECK(isInitialized()); |
184 const ReadingBuffer* buffer = | 270 const ReadingBuffer* buffer = |
185 static_cast<const ReadingBuffer*>(m_sharedBuffer.get()); | 271 static_cast<const ReadingBuffer*>(m_sharedBuffer.get()); |
186 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); | 272 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); |
187 auto version = seqlock.ReadBegin(); | 273 auto version = seqlock.ReadBegin(); |
188 auto reading = buffer->reading; | 274 auto reading = buffer->reading; |
189 if (seqlock.ReadRetry(version)) | 275 if (seqlock.ReadRetry(version)) |
190 return false; | 276 return false; |
191 m_reading = reading; | 277 m_reading = reading; |
192 return true; | 278 return true; |
193 } | 279 } |
194 | 280 |
195 } // namespace blink | 281 } // namespace blink |
OLD | NEW |