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

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: Comments from Tim 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 "core/inspector/ConsoleMessage.h"
9 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" 10 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h"
10 #include "modules/sensor/SensorErrorEvent.h" 11 #include "modules/sensor/SensorErrorEvent.h"
11 #include "modules/sensor/SensorPollingStrategy.h" 12 #include "modules/sensor/SensorPollingStrategy.h"
12 #include "modules/sensor/SensorProviderProxy.h" 13 #include "modules/sensor/SensorProviderProxy.h"
13 #include "modules/sensor/SensorReading.h" 14 #include "modules/sensor/SensorReading.h"
14 #include "modules/sensor/SensorReadingEvent.h" 15 #include "modules/sensor/SensorReadingEvent.h"
15 16
16 using namespace device::mojom::blink; 17 using namespace device::mojom::blink;
17 18
18 namespace blink { 19 namespace blink {
19 20
20 Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOp tions, SensorType type) 21 Sensor::Sensor(ScriptState* scriptState, const SensorOptions& sensorOptions, Exc eptionState& exceptionState, SensorType type)
21 : ActiveScriptWrappable(this) 22 : ActiveScriptWrappable(this)
22 , ContextLifecycleObserver(executionContext) 23 , ContextLifecycleObserver(scriptState->getExecutionContext())
23 , PageVisibilityObserver(toDocument(executionContext)->page()) 24 , PageVisibilityObserver(toDocument(scriptState->getExecutionContext())->pag e())
24 , m_sensorOptions(sensorOptions) 25 , m_sensorOptions(sensorOptions)
25 , m_type(type) 26 , m_type(type)
26 , m_state(Sensor::SensorState::IDLE) 27 , m_state(Sensor::SensorState::IDLE)
27 , m_storedData() 28 , m_storedData()
28 { 29 {
30 // Check secure context.
31 String errorMessage;
32 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) {
33 exceptionState.throwDOMException(SecurityError, errorMessage);
34 return;
35 }
36
37 // Check top-level browsing context.
38 if (!scriptState->domWindow() || !scriptState->domWindow()->frame() || !scri ptState->domWindow()->frame()->isMainFrame()) {
39 exceptionState.throwSecurityError("Must be in a top-level browsing conte xt");
40 return;
41 }
42
43 // Check the given frequency value.
44 if (m_sensorOptions.hasFrequency()) {
45 double frequency = m_sensorOptions.frequency();
46 if (frequency <= 0.0) {
47 exceptionState.throwRangeError("Frequency must be positive.");
48 return;
49 }
50
51 if (frequency > SensorConfiguration::kMaxAllowedFrequency) {
52 m_sensorOptions.setFrequency(SensorConfiguration::kMaxAllowedFrequen cy);
53 ConsoleMessage* consoleMessage = ConsoleMessage::create(JSMessageSou rce, InfoMessageLevel, "Frequency is limited to 60 Hz.");
54 scriptState->getExecutionContext()->addConsoleMessage(consoleMessage );
55 }
56 }
29 } 57 }
30 58
31 Sensor::~Sensor() = default; 59 Sensor::~Sensor() = default;
32 60
33 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) 61 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState)
34 { 62 {
35 if (m_state != Sensor::SensorState::IDLE && m_state != Sensor::SensorState:: ERRORED) { 63 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"); 64 exceptionState.throwDOMException(InvalidStateError, "Cannot start becaus e SensorState is not idle or errored");
37 return; 65 return;
38 } 66 }
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 291
264 if (m_state != Sensor::SensorState::ACTIVE 292 if (m_state != Sensor::SensorState::ACTIVE
265 || page()->visibilityState() != PageVisibilityStateVisible) { 293 || page()->visibilityState() != PageVisibilityStateVisible) {
266 m_polling->stopPolling(); 294 m_polling->stopPolling();
267 } else { 295 } else {
268 m_polling->startPolling(); 296 m_polling->startPolling();
269 } 297 }
270 } 298 }
271 299
272 } // namespace blink 300 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698