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

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 mikhail's comments 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
21 Sensor::Sensor(ScriptState* scriptState, 22 Sensor::Sensor(ScriptState* scriptState,
22 const SensorOptions& sensorOptions, 23 const SensorOptions& sensorOptions,
23 ExceptionState& exceptionState, 24 ExceptionState& exceptionState,
24 SensorType type) 25 SensorType type)
25 : ActiveScriptWrappable(this), 26 : ActiveScriptWrappable(this),
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 66
66 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) { 67 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) {
67 if (m_state != Sensor::SensorState::IDLE && 68 if (m_state != Sensor::SensorState::IDLE &&
68 m_state != Sensor::SensorState::ERRORED) { 69 m_state != Sensor::SensorState::ERRORED) {
69 exceptionState.throwDOMException( 70 exceptionState.throwDOMException(
70 InvalidStateError, 71 InvalidStateError,
71 "Cannot start because SensorState is not idle or errored"); 72 "Cannot start because SensorState is not idle or errored");
72 return; 73 return;
73 } 74 }
74 75
76 // If the algorithm is not allowed to show a popup, throw SecurityError.
77 if (!UserGestureIndicator::consumeUserGesture()) {
shalamov 2016/11/16 11:10:22 I have two comments for this line: 1. Since it do
riju_ 2016/11/16 12:38:25 I am removing it now, as getting "sensor" informat
78 exceptionState.throwDOMException(
79 SecurityError,
80 "Must be handling a user gesture to show a permission request.");
81 return;
82 }
83
75 initSensorProxyIfNeeded(); 84 initSensorProxyIfNeeded();
76 85
77 if (!m_sensorProxy) { 86 if (!m_sensorProxy) {
78 exceptionState.throwDOMException( 87 exceptionState.throwDOMException(
79 InvalidStateError, "The Sensor is no longer associated to a frame."); 88 InvalidStateError, "The Sensor is no longer associated to a frame.");
80 return; 89 return;
81 } 90 }
82 91
83 startListening(); 92 startListening();
84 } 93 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 return; 169 return;
161 170
162 Document* document = toDocument(getExecutionContext()); 171 Document* document = toDocument(getExecutionContext());
163 if (!document || !document->frame()) 172 if (!document || !document->frame())
164 return; 173 return;
165 174
166 auto provider = SensorProviderProxy::from(document->frame()); 175 auto provider = SensorProviderProxy::from(document->frame());
167 m_sensorProxy = provider->getSensor(m_type); 176 m_sensorProxy = provider->getSensor(m_type);
168 177
169 if (!m_sensorProxy) { 178 if (!m_sensorProxy) {
170 m_sensorProxy = 179 m_sensorProxy = provider->createSensor(m_type, getExecutionContext(),
171 provider->createSensor(m_type, createSensorReadingFactory()); 180 createSensorReadingFactory());
172 } 181 }
173 } 182 }
174 183
175 void Sensor::contextDestroyed() { 184 void Sensor::contextDestroyed() {
176 if (m_state == Sensor::SensorState::ACTIVE || 185 if (m_state == Sensor::SensorState::ACTIVE ||
177 m_state == Sensor::SensorState::ACTIVATING) 186 m_state == Sensor::SensorState::ACTIVATING)
178 stopListening(); 187 stopListening();
179 } 188 }
180 189
181 void Sensor::onSensorInitialized() { 190 void Sensor::onSensorInitialized() {
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 void Sensor::notifyStateChanged() { 357 void Sensor::notifyStateChanged() {
349 dispatchEvent(Event::create(EventTypeNames::statechange)); 358 dispatchEvent(Event::create(EventTypeNames::statechange));
350 } 359 }
351 360
352 void Sensor::notifyError(DOMException* error) { 361 void Sensor::notifyError(DOMException* error) {
353 dispatchEvent( 362 dispatchEvent(
354 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); 363 SensorErrorEvent::create(EventTypeNames::error, std::move(error)));
355 } 364 }
356 365
357 } // namespace blink 366 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698