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

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

Issue 2675483003: [Sensors] Introduce 'unconnected' sensor state (Closed)
Patch Set: rebased Created 3 years, 10 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/dom/ExecutionContextTask.h" 9 #include "core/dom/ExecutionContextTask.h"
10 #include "core/dom/TaskRunnerHelper.h" 10 #include "core/dom/TaskRunnerHelper.h"
11 #include "core/inspector/ConsoleMessage.h" 11 #include "core/inspector/ConsoleMessage.h"
12 #include "core/timing/DOMWindowPerformance.h" 12 #include "core/timing/DOMWindowPerformance.h"
13 #include "core/timing/Performance.h" 13 #include "core/timing/Performance.h"
14 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h" 14 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h"
15 #include "modules/sensor/SensorErrorEvent.h" 15 #include "modules/sensor/SensorErrorEvent.h"
16 #include "modules/sensor/SensorProviderProxy.h" 16 #include "modules/sensor/SensorProviderProxy.h"
17 17
18 using namespace device::mojom::blink; 18 using namespace device::mojom::blink;
19 19
20 namespace blink { 20 namespace blink {
21 21
22 Sensor::Sensor(ExecutionContext* executionContext, 22 Sensor::Sensor(ExecutionContext* executionContext,
23 const SensorOptions& sensorOptions, 23 const SensorOptions& sensorOptions,
24 ExceptionState& exceptionState, 24 ExceptionState& exceptionState,
25 SensorType type) 25 SensorType type)
26 : ContextLifecycleObserver(executionContext), 26 : ContextLifecycleObserver(executionContext),
27 m_sensorOptions(sensorOptions), 27 m_sensorOptions(sensorOptions),
28 m_type(type), 28 m_type(type),
29 m_state(Sensor::SensorState::Idle), 29 m_state(Sensor::SensorState::Unconnected),
30 m_lastUpdateTimestamp(0.0) { 30 m_lastUpdateTimestamp(0.0) {
31 // Check secure context. 31 // Check secure context.
32 String errorMessage; 32 String errorMessage;
33 if (!executionContext->isSecureContext(errorMessage)) { 33 if (!executionContext->isSecureContext(errorMessage)) {
34 exceptionState.throwDOMException(SecurityError, errorMessage); 34 exceptionState.throwDOMException(SecurityError, errorMessage);
35 return; 35 return;
36 } 36 }
37 37
38 // Check top-level browsing context. 38 // Check top-level browsing context.
39 if (!toDocument(executionContext)->domWindow()->frame() || 39 if (!toDocument(executionContext)->domWindow()->frame() ||
(...skipping 16 matching lines...) Expand all
56 ConsoleMessage* consoleMessage = ConsoleMessage::create( 56 ConsoleMessage* consoleMessage = ConsoleMessage::create(
57 JSMessageSource, InfoMessageLevel, "Frequency is limited to 60 Hz."); 57 JSMessageSource, InfoMessageLevel, "Frequency is limited to 60 Hz.");
58 executionContext->addConsoleMessage(consoleMessage); 58 executionContext->addConsoleMessage(consoleMessage);
59 } 59 }
60 } 60 }
61 } 61 }
62 62
63 Sensor::~Sensor() = default; 63 Sensor::~Sensor() = default;
64 64
65 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) { 65 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) {
66 if (m_state != Sensor::SensorState::Idle && 66 if (m_state != Sensor::SensorState::Unconnected &&
67 m_state != Sensor::SensorState::Idle &&
67 m_state != Sensor::SensorState::Errored) 68 m_state != Sensor::SensorState::Errored)
68 return; 69 return;
69 70
70 initSensorProxyIfNeeded(); 71 initSensorProxyIfNeeded();
71 72
72 if (!m_sensorProxy) { 73 if (!m_sensorProxy) {
73 exceptionState.throwDOMException( 74 exceptionState.throwDOMException(
74 InvalidStateError, "The Sensor is no longer associated to a frame."); 75 InvalidStateError, "The Sensor is no longer associated to a frame.");
75 return; 76 return;
76 } 77 }
77 m_lastUpdateTimestamp = WTF::monotonicallyIncreasingTime(); 78 m_lastUpdateTimestamp = WTF::monotonicallyIncreasingTime();
78 startListening(); 79 startListening();
79 } 80 }
80 81
81 void Sensor::stop(ScriptState*, ExceptionState& exceptionState) { 82 void Sensor::stop(ScriptState*, ExceptionState& exceptionState) {
82 if (m_state == Sensor::SensorState::Idle || 83 if (m_state == Sensor::SensorState::Unconnected ||
84 m_state == Sensor::SensorState::Idle ||
83 m_state == Sensor::SensorState::Errored) 85 m_state == Sensor::SensorState::Errored)
84 return; 86 return;
85 87
86 stopListening(); 88 stopListening();
87 } 89 }
88 90
89 static String ToString(Sensor::SensorState state) { 91 static String ToString(Sensor::SensorState state) {
90 switch (state) { 92 switch (state) {
91 case Sensor::SensorState::Idle: 93 case Sensor::SensorState::Unconnected:
92 return "idle"; 94 return "unconnected";
93 case Sensor::SensorState::Activating: 95 case Sensor::SensorState::Activating:
94 return "activating"; 96 return "activating";
95 case Sensor::SensorState::Activated: 97 case Sensor::SensorState::Activated:
96 return "activated"; 98 return "activated";
99 case Sensor::SensorState::Idle:
100 return "idle";
97 case Sensor::SensorState::Errored: 101 case Sensor::SensorState::Errored:
98 return "errored"; 102 return "errored";
99 default: 103 default:
100 NOTREACHED(); 104 NOTREACHED();
101 } 105 }
102 return "idle"; 106 return "idle";
103 } 107 }
104 108
105 // Getters 109 // Getters
106 String Sensor::state() const { 110 String Sensor::state() const {
(...skipping 23 matching lines...) Expand all
130 } 134 }
131 135
132 DEFINE_TRACE(Sensor) { 136 DEFINE_TRACE(Sensor) {
133 visitor->trace(m_sensorProxy); 137 visitor->trace(m_sensorProxy);
134 ActiveScriptWrappable::trace(visitor); 138 ActiveScriptWrappable::trace(visitor);
135 ContextLifecycleObserver::trace(visitor); 139 ContextLifecycleObserver::trace(visitor);
136 EventTargetWithInlineData::trace(visitor); 140 EventTargetWithInlineData::trace(visitor);
137 } 141 }
138 142
139 bool Sensor::hasPendingActivity() const { 143 bool Sensor::hasPendingActivity() const {
140 if (m_state == Sensor::SensorState::Idle || 144 if (m_state == Sensor::SensorState::Unconnected ||
145 m_state == Sensor::SensorState::Idle ||
141 m_state == Sensor::SensorState::Errored) 146 m_state == Sensor::SensorState::Errored)
142 return false; 147 return false;
143 return getExecutionContext() && hasEventListeners(); 148 return getExecutionContext() && hasEventListeners();
144 } 149 }
145 150
146 auto Sensor::createSensorConfig() -> SensorConfigurationPtr { 151 auto Sensor::createSensorConfig() -> SensorConfigurationPtr {
147 auto result = SensorConfiguration::New(); 152 auto result = SensorConfiguration::New();
148 153
149 double defaultFrequency = m_sensorProxy->defaultConfig()->frequency; 154 double defaultFrequency = m_sensorProxy->defaultConfig()->frequency;
150 double maximumFrequency = m_sensorProxy->maximumFrequency(); 155 double maximumFrequency = m_sensorProxy->maximumFrequency();
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 } 321 }
317 322
318 bool Sensor::canReturnReadings() const { 323 bool Sensor::canReturnReadings() const {
319 if (m_state != Sensor::SensorState::Activated) 324 if (m_state != Sensor::SensorState::Activated)
320 return false; 325 return false;
321 DCHECK(m_sensorProxy); 326 DCHECK(m_sensorProxy);
322 return m_sensorProxy->reading().timestamp != 0.0; 327 return m_sensorProxy->reading().timestamp != 0.0;
323 } 328 }
324 329
325 } // namespace blink 330 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/sensor/Sensor.h ('k') | third_party/WebKit/Source/modules/sensor/Sensor.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698