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 "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, const SensorOptions& sensorOptions, Exc eptionState& exceptionState, SensorType type) |
21 : ActiveScriptWrappable(this) | 21 : ActiveScriptWrappable(this) |
22 , ContextLifecycleObserver(executionContext) | 22 , ContextLifecycleObserver(scriptState->getExecutionContext()) |
23 , PageVisibilityObserver(toDocument(executionContext)->page()) | 23 , PageVisibilityObserver(toDocument(scriptState->getExecutionContext())->pag e()) |
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 (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) { | |
32 exceptionState.throwDOMException(SecurityError, errorMessage); | |
33 return; | |
34 } | |
35 | |
36 // Check top-level browsing context. | |
37 if (!scriptState->domWindow() || !scriptState->domWindow()->frame() || !scri ptState->domWindow()->frame()->isMainFrame()) { | |
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."); | |
47 return; | |
48 } | |
49 // Cap to 60.0 Hz | |
50 const double kMaxAllowedFrequency = 60.0; | |
timvolodine
2016/09/19 16:52:13
think this should be a more 'global' constant some
Mikhail
2016/09/20 13:29:37
Done.
| |
51 if (frequency > kMaxAllowedFrequency) | |
52 m_sensorOptions.setFrequency(kMaxAllowedFrequency); | |
53 } | |
timvolodine
2016/09/19 16:52:13
don't know what the spec says here, maybe output s
Mikhail
2016/09/20 13:29:37
Done.
| |
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 Loading... | |
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 |
OLD | NEW |