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

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

Issue 2353493002: [Sensors] Allow Sensor API only on secure top-level browsing contexts and add frequency checks (Closed)
Patch Set: Created 4 years, 3 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
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 "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" 9 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h"
10 #include "modules/sensor/SensorErrorEvent.h" 10 #include "modules/sensor/SensorErrorEvent.h"
11 #include "modules/sensor/SensorPollingStrategy.h" 11 #include "modules/sensor/SensorPollingStrategy.h"
12 #include "modules/sensor/SensorProviderProxy.h" 12 #include "modules/sensor/SensorProviderProxy.h"
13 #include "modules/sensor/SensorReading.h" 13 #include "modules/sensor/SensorReading.h"
14 #include "modules/sensor/SensorReadingEvent.h" 14 #include "modules/sensor/SensorReadingEvent.h"
15 15
16 using namespace device::mojom::blink; 16 using namespace device::mojom::blink;
17 17
18 namespace blink { 18 namespace blink {
19 19
20 Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOp tions, SensorType type) 20 Sensor::Sensor(ScriptState* scriptState, ExecutionContext* executionContext, con st SensorOptions& sensorOptions, ExceptionState& exceptionState, SensorType type )
21 : ActiveScriptWrappable(this) 21 : ActiveScriptWrappable(this)
22 , ContextLifecycleObserver(executionContext) 22 , ContextLifecycleObserver(executionContext)
23 , PageVisibilityObserver(toDocument(executionContext)->page()) 23 , PageVisibilityObserver(toDocument(executionContext)->page())
24 , m_sensorOptions(sensorOptions) 24 , m_sensorOptions(sensorOptions)
25 , m_type(type) 25 , m_type(type)
26 , m_state(Sensor::SensorState::IDLE) 26 , m_state(Sensor::SensorState::IDLE)
27 , m_storedData() 27 , m_storedData()
28 { 28 {
29 // Check secure context.
30 String errorMessage;
31 if (!executionContext->isSecureContext(errorMessage)) {
32 exceptionState.throwDOMException(SecurityError, errorMessage);
33 return;
34 }
35
36 // Check top-level browsing context.
37 if (!scriptState->domWindow()->frame() || !scriptState->domWindow()->frame() ->isMainFrame()) {
haraken 2016/09/19 13:09:41 You need to add '!scriptState->domWindow() ||' as
Mikhail 2016/09/19 14:18:02 Done.
38 exceptionState.throwSecurityError("Must be in a top-level browsing conte xt");
39 return;
40 }
41
42 // Check the given frequency value.
43 if (m_sensorOptions.hasFrequency()) {
44 double frequency = m_sensorOptions.frequency();
45 if (frequency <= 0.0) {
46 exceptionState.throwRangeError("Frequency must be positive");
shalamov 2016/09/19 13:11:42 nit: Dot at the end of error message.
Mikhail 2016/09/19 14:18:02 Done.
47 return;
48 }
49 // Cap to 60.0 Hz
50 const double kMaxAllowedFrequency = 60.0;
51 if (frequency > kMaxAllowedFrequency)
52 m_sensorOptions.setFrequency(kMaxAllowedFrequency);
53 }
29 } 54 }
30 55
31 Sensor::~Sensor() = default; 56 Sensor::~Sensor() = default;
32 57
33 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) 58 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState)
34 { 59 {
35 if (m_state != Sensor::SensorState::IDLE && m_state != Sensor::SensorState:: ERRORED) { 60 if (m_state != Sensor::SensorState::IDLE && m_state != Sensor::SensorState:: ERRORED) {
36 exceptionState.throwDOMException(InvalidStateError, "Cannot start becaus e SensorState is not idle or errored"); 61 exceptionState.throwDOMException(InvalidStateError, "Cannot start becaus e SensorState is not idle or errored");
37 return; 62 return;
38 } 63 }
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 288
264 if (m_state != Sensor::SensorState::ACTIVE 289 if (m_state != Sensor::SensorState::ACTIVE
265 || page()->visibilityState() != PageVisibilityStateVisible) { 290 || page()->visibilityState() != PageVisibilityStateVisible) {
266 m_polling->stopPolling(); 291 m_polling->stopPolling();
267 } else { 292 } else {
268 m_polling->startPolling(); 293 m_polling->startPolling();
269 } 294 }
270 } 295 }
271 296
272 } // namespace blink 297 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698