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

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

Issue 2458453002: [sensors] Add Permission guard to the generic sensor apis.
Patch Set: Fix comments + rebase Created 4 years, 1 month 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
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/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 "platform/UserGestureIndicator.h"
16 17
17 using namespace device::mojom::blink; 18 using namespace device::mojom::blink;
18 19
19 namespace blink { 20 namespace blink {
20 21
22 using mojom::blink::PermissionStatus;
23
21 Sensor::Sensor(ScriptState* scriptState, 24 Sensor::Sensor(ScriptState* scriptState,
22 const SensorOptions& sensorOptions, 25 const SensorOptions& sensorOptions,
23 ExceptionState& exceptionState, 26 ExceptionState& exceptionState,
24 SensorType type) 27 SensorType type)
25 : ActiveScriptWrappable(this), 28 : ActiveScriptWrappable(this),
26 ContextLifecycleObserver(scriptState->getExecutionContext()), 29 ContextLifecycleObserver(scriptState->getExecutionContext()),
27 PageVisibilityObserver( 30 PageVisibilityObserver(
28 toDocument(scriptState->getExecutionContext())->page()), 31 toDocument(scriptState->getExecutionContext())->page()),
29 m_sensorOptions(sensorOptions), 32 m_sensorOptions(sensorOptions),
30 m_type(type), 33 m_type(type),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 68
66 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) { 69 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) {
67 if (m_state != Sensor::SensorState::IDLE && 70 if (m_state != Sensor::SensorState::IDLE &&
68 m_state != Sensor::SensorState::ERRORED) { 71 m_state != Sensor::SensorState::ERRORED) {
69 exceptionState.throwDOMException( 72 exceptionState.throwDOMException(
70 InvalidStateError, 73 InvalidStateError,
71 "Cannot start because SensorState is not idle or errored"); 74 "Cannot start because SensorState is not idle or errored");
72 return; 75 return;
73 } 76 }
74 77
78 // If the algorithm is not allowed to show a popup, throw SecurityError.
79 if (!UserGestureIndicator::consumeUserGesture()) {
80 exceptionState.throwDOMException(
81 SecurityError,
82 "Must be handling a user gesture to show a permission request.");
83 return;
84 }
85
75 initSensorProxyIfNeeded(); 86 initSensorProxyIfNeeded();
76
77 if (!m_sensorProxy) { 87 if (!m_sensorProxy) {
78 exceptionState.throwDOMException( 88 exceptionState.throwDOMException(
79 InvalidStateError, "The Sensor is no longer associated to a frame."); 89 InvalidStateError, "The Sensor is no longer associated to a frame.");
80 return; 90 return;
81 } 91 }
82 92
83 startListening(); 93 m_sensorProxy->addObserver(this);
94 if (!m_sensorProxy->isInitialized()) {
95 m_sensorProxy->initialize();
96 return;
97 }
98
99 // Check the permission status.
100 auto permissionStatus = m_sensorProxy->getPermissionStatus();
101
102 if (permissionStatus == PermissionStatus::ASK) {
103 m_sensorProxy->setStartPendingPermission(true);
104 m_sensorProxy->requestPermission(scriptState->getExecutionContext(),
105 permissionStatus);
106 return;
107 }
108 if (permissionStatus == PermissionStatus::GRANTED) {
109 startListening();
110 } else if (permissionStatus == PermissionStatus::DENIED) {
111 stopListening();
Mikhail 2016/11/10 09:30:48 how can be sensor listening in 'idle' or 'errored'
riju_ 2016/11/14 14:00:06 Done.
112
113 ConsoleMessage* consoleMessage = ConsoleMessage::create(
114 JSMessageSource, InfoMessageLevel, "Permission Denied.");
115 scriptState->getExecutionContext()->addConsoleMessage(consoleMessage);
116
117 exceptionState.throwDOMException(NotAllowedError, "Permission Denied.");
118 }
84 } 119 }
85 120
86 void Sensor::stop(ScriptState*, ExceptionState& exceptionState) { 121 void Sensor::stop(ScriptState*, ExceptionState& exceptionState) {
87 if (m_state == Sensor::SensorState::IDLE || 122 if (m_state == Sensor::SensorState::IDLE ||
88 m_state == Sensor::SensorState::ERRORED) { 123 m_state == Sensor::SensorState::ERRORED) {
89 exceptionState.throwDOMException( 124 exceptionState.throwDOMException(
90 InvalidStateError, 125 InvalidStateError,
91 "Cannot stop because SensorState is either idle or errored"); 126 "Cannot stop because SensorState is either idle or errored");
92 return; 127 return;
93 } 128 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 return; 178 return;
144 179
145 Document* document = toDocument(getExecutionContext()); 180 Document* document = toDocument(getExecutionContext());
146 if (!document || !document->frame()) 181 if (!document || !document->frame())
147 return; 182 return;
148 183
149 auto provider = SensorProviderProxy::from(document->frame()); 184 auto provider = SensorProviderProxy::from(document->frame());
150 m_sensorProxy = provider->getSensor(m_type); 185 m_sensorProxy = provider->getSensor(m_type);
151 186
152 if (!m_sensorProxy) { 187 if (!m_sensorProxy) {
153 m_sensorProxy = 188 m_sensorProxy = provider->createSensor(m_type, getExecutionContext(),
154 provider->createSensor(m_type, createSensorReadingFactory()); 189 createSensorReadingFactory());
155 } 190 }
156 } 191 }
157 192
158 void Sensor::contextDestroyed() { 193 void Sensor::contextDestroyed() {
159 if (m_state == Sensor::SensorState::ACTIVE || 194 if (m_state == Sensor::SensorState::ACTIVE ||
160 m_state == Sensor::SensorState::ACTIVATING) 195 m_state == Sensor::SensorState::ACTIVATING)
161 stopListening(); 196 stopListening();
197 m_sensorProxy->resetPermissionService();
Mikhail 2016/11/10 09:30:48 should be handled by proxy itself
riju_ 2016/11/14 14:00:06 Done.
162 } 198 }
163 199
164 void Sensor::onSensorInitialized() { 200 void Sensor::onSensorInitialized() {
165 if (m_state != Sensor::SensorState::ACTIVATING) 201 if (m_state != Sensor::SensorState::ACTIVATING)
166 return; 202 return;
167 203
168 startListening(); 204 startListening();
169 } 205 }
170 206
171 void Sensor::onSensorReadingChanged() { 207 void Sensor::onSensorReadingChanged() {
172 if (m_polling) 208 if (m_polling)
173 m_polling->onSensorReadingChanged(); 209 m_polling->onSensorReadingChanged();
174 } 210 }
175 211
212 void Sensor::onSensorPermissionChanged() {
Mikhail 2016/11/10 09:30:48 all this logic should be inside proxy itself
riju_ 2016/11/14 14:00:06 Done.
213 // Check the permission status.
214 auto permissionStatus = m_sensorProxy->getPermissionStatus();
215
216 if (permissionStatus != PermissionStatus::GRANTED) {
217 stopListening();
218 reportError(NotAllowedError,
219 "start/stop() call has failed as permission was denied.");
220 } else {
221 if (m_sensorProxy->getStartPendingPermission()) {
222 startListening();
223 m_sensorProxy->setStartPendingPermission(false);
224 }
225 }
226 }
227
176 void Sensor::onSensorError(ExceptionCode code, 228 void Sensor::onSensorError(ExceptionCode code,
177 const String& sanitizedMessage, 229 const String& sanitizedMessage,
178 const String& unsanitizedMessage) { 230 const String& unsanitizedMessage) {
179 reportError(code, sanitizedMessage, unsanitizedMessage); 231 reportError(code, sanitizedMessage, unsanitizedMessage);
180 } 232 }
181 233
182 void Sensor::onStartRequestCompleted(bool result) { 234 void Sensor::onStartRequestCompleted(bool result) {
183 if (m_state != Sensor::SensorState::ACTIVATING) 235 if (m_state != Sensor::SensorState::ACTIVATING)
184 return; 236 return;
185 237
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 m_sensorProxy->suspend(); 273 m_sensorProxy->suspend();
222 } else { 274 } else {
223 m_sensorProxy->resume(); 275 m_sensorProxy->resume();
224 } 276 }
225 } 277 }
226 278
227 void Sensor::startListening() { 279 void Sensor::startListening() {
228 DCHECK(m_sensorProxy); 280 DCHECK(m_sensorProxy);
229 updateState(Sensor::SensorState::ACTIVATING); 281 updateState(Sensor::SensorState::ACTIVATING);
230 282
231 m_sensorProxy->addObserver(this);
232 if (!m_sensorProxy->isInitialized()) {
233 m_sensorProxy->initialize();
234 return;
235 }
236
237 if (!m_configuration) { 283 if (!m_configuration) {
238 m_configuration = 284 m_configuration =
239 createSensorConfig(m_sensorOptions, *m_sensorProxy->defaultConfig()); 285 createSensorConfig(m_sensorOptions, *m_sensorProxy->defaultConfig());
240 DCHECK(m_configuration); 286 DCHECK(m_configuration);
241 } 287 }
242 288
243 auto startCallback = 289 auto startCallback =
244 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this)); 290 WTF::bind(&Sensor::onStartRequestCompleted, wrapWeakPersistent(this));
245 m_sensorProxy->addConfiguration(m_configuration->Clone(), 291 m_sensorProxy->addConfiguration(m_configuration->Clone(),
246 std::move(startCallback)); 292 std::move(startCallback));
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 void Sensor::notifyStateChanged() { 376 void Sensor::notifyStateChanged() {
331 dispatchEvent(Event::create(EventTypeNames::statechange)); 377 dispatchEvent(Event::create(EventTypeNames::statechange));
332 } 378 }
333 379
334 void Sensor::notifyError(DOMException* error) { 380 void Sensor::notifyError(DOMException* error) {
335 dispatchEvent( 381 dispatchEvent(
336 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); 382 SensorErrorEvent::create(EventTypeNames::error, std::move(error)));
337 } 383 }
338 384
339 } // namespace blink 385 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698