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/Sensor.h" | 5 #include "modules/sensor/Sensor.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/ExceptionCode.h" | 8 #include "core/dom/ExceptionCode.h" |
9 #include "core/dom/ExecutionContextTask.h" | 9 #include "core/dom/ExecutionContextTask.h" |
10 #include "core/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" | 11 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" |
12 #include "modules/sensor/SensorErrorEvent.h" | 12 #include "modules/sensor/SensorErrorEvent.h" |
13 #include "modules/sensor/SensorPollingStrategy.h" | 13 #include "modules/sensor/SensorPollingStrategy.h" |
14 #include "modules/sensor/SensorProviderProxy.h" | 14 #include "modules/sensor/SensorProviderProxy.h" |
15 #include "modules/sensor/SensorReading.h" | 15 #include "modules/sensor/SensorReading.h" |
16 #include "modules/sensor/SensorReadingEvent.h" | 16 #include "modules/sensor/SensorReadingEvent.h" |
17 #include "platform/UserGestureIndicator.h" | |
17 | 18 |
18 using namespace device::mojom::blink; | 19 using namespace device::mojom::blink; |
19 | 20 |
20 namespace blink { | 21 namespace blink { |
21 | 22 |
23 using mojom::blink::PermissionStatus; | |
24 | |
22 Sensor::Sensor(ScriptState* scriptState, | 25 Sensor::Sensor(ScriptState* scriptState, |
23 const SensorOptions& sensorOptions, | 26 const SensorOptions& sensorOptions, |
24 ExceptionState& exceptionState, | 27 ExceptionState& exceptionState, |
25 SensorType type) | 28 SensorType type) |
26 : ActiveScriptWrappable(this), | 29 : ActiveScriptWrappable(this), |
27 ContextLifecycleObserver(scriptState->getExecutionContext()), | 30 ContextLifecycleObserver(scriptState->getExecutionContext()), |
28 PageVisibilityObserver( | 31 PageVisibilityObserver( |
29 toDocument(scriptState->getExecutionContext())->page()), | 32 toDocument(scriptState->getExecutionContext())->page()), |
30 m_sensorOptions(sensorOptions), | 33 m_sensorOptions(sensorOptions), |
31 m_type(type), | 34 m_type(type), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 | 70 |
68 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) { | 71 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) { |
69 if (m_state != Sensor::SensorState::IDLE && | 72 if (m_state != Sensor::SensorState::IDLE && |
70 m_state != Sensor::SensorState::ERRORED) { | 73 m_state != Sensor::SensorState::ERRORED) { |
71 exceptionState.throwDOMException( | 74 exceptionState.throwDOMException( |
72 InvalidStateError, | 75 InvalidStateError, |
73 "Cannot start because SensorState is not idle or errored"); | 76 "Cannot start because SensorState is not idle or errored"); |
74 return; | 77 return; |
75 } | 78 } |
76 | 79 |
77 initSensorProxyIfNeeded(); | 80 initSensorProxyIfNeeded(); |
shalamov
2016/11/04 19:00:20
This will request sensor from SensorProvider and i
riju_
2016/11/09 10:30:34
If there is no proxy, we can't call proxy->somePer
| |
78 | 81 |
79 if (!m_sensorProxy) { | 82 if (!m_sensorProxy) { |
80 exceptionState.throwDOMException( | 83 exceptionState.throwDOMException( |
81 InvalidStateError, "The Sensor is no longer associated to a frame."); | 84 InvalidStateError, "The Sensor is no longer associated to a frame."); |
82 return; | 85 return; |
83 } | 86 } |
84 | 87 |
85 startListening(); | 88 // If the algorithm is not allowed to show a popup, throw SecurityError. |
89 if (!UserGestureIndicator::consumeUserGesture()) { | |
Mikhail
2016/11/07 08:02:02
must be moved to the beginning.
riju_
2016/11/09 10:30:34
Done.
| |
90 exceptionState.throwDOMException( | |
91 SecurityError, | |
92 "Must be handling a user gesture to show a permission request."); | |
93 return; | |
94 } | |
95 | |
96 // Check the permission status first. | |
97 auto permissionStatus = m_sensorProxy->getPermissionStatus(); | |
98 | |
99 if (permissionStatus == PermissionStatus::ASK) { | |
100 m_sensorProxy->requestPermission(scriptState->getExecutionContext(), | |
Mikhail
2016/11/07 08:02:02
return here?
riju_
2016/11/09 10:30:34
Done.
| |
101 permissionStatus); | |
102 } else if (permissionStatus == PermissionStatus::DENIED) { | |
103 stopListening(); | |
104 | |
105 ConsoleMessage* consoleMessage = ConsoleMessage::create( | |
106 JSMessageSource, InfoMessageLevel, "Permission Rejected."); | |
107 scriptState->getExecutionContext()->addConsoleMessage(consoleMessage); | |
108 | |
109 reportError(PermissionDeniedError, | |
110 "start() call has failed as permission was denied."); | |
111 } else { | |
112 startListening(); | |
113 } | |
114 | |
115 // Keep listening to changes. | |
116 m_sensorProxy->getNextPermissionChange(scriptState->getExecutionContext(), | |
shalamov
2016/11/04 19:00:20
This can be moved after HasPermission is completed
riju_
2016/11/09 10:30:34
Done.
| |
117 permissionStatus); | |
86 } | 118 } |
87 | 119 |
88 void Sensor::stop(ScriptState*, ExceptionState& exceptionState) { | 120 void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState) { |
89 if (m_state == Sensor::SensorState::IDLE || | 121 if (m_state == Sensor::SensorState::IDLE || |
90 m_state == Sensor::SensorState::ERRORED) { | 122 m_state == Sensor::SensorState::ERRORED) { |
91 exceptionState.throwDOMException( | 123 exceptionState.throwDOMException( |
92 InvalidStateError, | 124 InvalidStateError, |
93 "Cannot stop because SensorState is either idle or errored"); | 125 "Cannot stop because SensorState is either idle or errored"); |
94 return; | 126 return; |
95 } | 127 } |
96 | 128 |
97 stopListening(); | 129 stopListening(); |
130 | |
131 // Check the permission status first. | |
132 auto permissionStatus = m_sensorProxy->getPermissionStatus(); | |
shalamov
2016/11/04 19:00:20
Is this required for stop()? Should we create issu
riju_
2016/11/09 10:30:34
Will remove.
https://github.com/w3c/sensors/issues
| |
133 | |
134 if (permissionStatus != PermissionStatus::GRANTED) { | |
Mikhail
2016/11/07 08:02:02
is it possible that we had permissions to start bu
riju_
2016/11/09 10:30:34
My mistake, now spec says we don't need permission
| |
135 ConsoleMessage* consoleMessage = ConsoleMessage::create( | |
136 JSMessageSource, InfoMessageLevel, "Permission Rejected."); | |
137 scriptState->getExecutionContext()->addConsoleMessage(consoleMessage); | |
138 | |
139 reportError(PermissionDeniedError, | |
140 "stop() call has failed as permission was not granted."); | |
141 } | |
142 | |
143 // Keep listening to changes. | |
144 m_sensorProxy->getNextPermissionChange(scriptState->getExecutionContext(), | |
Mikhail
2016/11/07 08:02:02
this class already has 'getExecutionContext()'
riju_
2016/11/09 10:30:34
Moving this to the onPermissionUpdate callback, as
| |
145 permissionStatus); | |
98 } | 146 } |
99 | 147 |
100 static String ToString(Sensor::SensorState state) { | 148 static String ToString(Sensor::SensorState state) { |
101 switch (state) { | 149 switch (state) { |
102 case Sensor::SensorState::IDLE: | 150 case Sensor::SensorState::IDLE: |
103 return "idle"; | 151 return "idle"; |
104 case Sensor::SensorState::ACTIVATING: | 152 case Sensor::SensorState::ACTIVATING: |
105 return "activating"; | 153 return "activating"; |
106 case Sensor::SensorState::ACTIVE: | 154 case Sensor::SensorState::ACTIVE: |
107 return "active"; | 155 return "active"; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 } | 187 } |
140 | 188 |
141 void Sensor::initSensorProxyIfNeeded() { | 189 void Sensor::initSensorProxyIfNeeded() { |
142 if (m_sensorProxy) | 190 if (m_sensorProxy) |
143 return; | 191 return; |
144 | 192 |
145 Document* document = toDocument(getExecutionContext()); | 193 Document* document = toDocument(getExecutionContext()); |
146 if (!document || !document->frame()) | 194 if (!document || !document->frame()) |
147 return; | 195 return; |
148 | 196 |
149 m_sensorProxy = | 197 m_sensorProxy = SensorProviderProxy::from(document->frame()) |
150 SensorProviderProxy::from(document->frame())->getOrCreateSensor(m_type); | 198 ->getOrCreateSensor(m_type, getExecutionContext()); |
151 } | 199 } |
152 | 200 |
153 void Sensor::contextDestroyed() { | 201 void Sensor::contextDestroyed() { |
154 if (m_state == Sensor::SensorState::ACTIVE || | 202 if (m_state == Sensor::SensorState::ACTIVE || |
155 m_state == Sensor::SensorState::ACTIVATING) | 203 m_state == Sensor::SensorState::ACTIVATING) |
156 stopListening(); | 204 stopListening(); |
205 m_sensorProxy->resetPermissionService(); | |
157 } | 206 } |
158 | 207 |
159 void Sensor::onSensorInitialized() { | 208 void Sensor::onSensorInitialized() { |
160 if (m_state != Sensor::SensorState::ACTIVATING) | 209 if (m_state != Sensor::SensorState::ACTIVATING) |
161 return; | 210 return; |
162 | 211 |
163 startListening(); | 212 startListening(); |
164 } | 213 } |
165 | 214 |
166 void Sensor::onSensorReadingChanged() { | 215 void Sensor::onSensorReadingChanged() { |
167 if (m_polling) | 216 if (m_polling) |
168 m_polling->onSensorReadingChanged(); | 217 m_polling->onSensorReadingChanged(); |
169 } | 218 } |
170 | 219 |
220 void Sensor::onSensorPermissionChanged() { | |
221 // Check the permission status first. | |
222 auto permissionStatus = m_sensorProxy->getPermissionStatus(); | |
223 | |
224 if (permissionStatus != PermissionStatus::GRANTED) { | |
225 stopListening(); | |
226 reportError(PermissionDeniedError, | |
227 "start/stop() call has failed as permission was denied."); | |
228 } | |
Mikhail
2016/11/07 08:02:02
what if permissions are granted? we probably shoul
riju_
2016/11/09 10:30:34
Yes, provided a start() was waiting for permission
| |
229 } | |
230 | |
171 void Sensor::onSensorError(ExceptionCode code, | 231 void Sensor::onSensorError(ExceptionCode code, |
172 const String& sanitizedMessage, | 232 const String& sanitizedMessage, |
173 const String& unsanitizedMessage) { | 233 const String& unsanitizedMessage) { |
174 reportError(code, sanitizedMessage, unsanitizedMessage); | 234 reportError(code, sanitizedMessage, unsanitizedMessage); |
175 } | 235 } |
176 | 236 |
177 void Sensor::onStartRequestCompleted(bool result) { | 237 void Sensor::onStartRequestCompleted(bool result) { |
178 if (m_state != Sensor::SensorState::ACTIVATING) | 238 if (m_state != Sensor::SensorState::ACTIVATING) |
179 return; | 239 return; |
180 | 240 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
331 void Sensor::notifyStateChanged() { | 391 void Sensor::notifyStateChanged() { |
332 dispatchEvent(Event::create(EventTypeNames::statechange)); | 392 dispatchEvent(Event::create(EventTypeNames::statechange)); |
333 } | 393 } |
334 | 394 |
335 void Sensor::notifyError(DOMException* error) { | 395 void Sensor::notifyError(DOMException* error) { |
336 dispatchEvent( | 396 dispatchEvent( |
337 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); | 397 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
338 } | 398 } |
339 | 399 |
340 } // namespace blink | 400 } // namespace blink |
OLD | NEW |