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

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

Issue 1892083002: Generic Sensor API : Ambient Light Sensor API. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: re entrancy fix Created 4 years, 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "modules/sensor/Sensor.h"
6
7 #include "core/dom/Document.h"
8 #include "core/dom/ExceptionCode.h"
9 #include "core/events/Event.h"
10
11 #include "modules/sensor/SensorReading.h"
12
13 namespace blink {
14
15 Sensor::~Sensor()
16 {
17 }
18
19 Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOp tions)
20 : ActiveScriptWrappable(this)
21 , ActiveDOMObject(executionContext)
22 , PlatformEventController(toDocument(executionContext)->page())
23 , m_sensorState(SensorState::Idle)
24 , m_sensorReading(nullptr)
25 , m_sensorOptions(sensorOptions)
26 {
27 }
28
29 // Getters
30 String Sensor::state() const
31 {
32 switch (m_sensorState) {
33 case SensorState::Idle:
34 return "idle";
35 case SensorState::Activating:
36 return "activating";
37 case SensorState::Active:
38 return "active";
39 case SensorState::Errored:
40 return "errored";
41 }
42 NOTREACHED();
43 return "idle";
44 }
45
46 SensorReading* Sensor::reading() const
47 {
48 return m_sensorReading.get();
49 }
50
51 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState)
52 {
53
54 if (m_sensorState != SensorState::Idle && m_sensorState != SensorState::Erro red) {
55 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is not idle or errored");
56
57 // Avoid re-entrancy. sensor.start() inside 'onstatechange' event handle r.
58 m_hasEventListener = false;
59 stopUpdating();
60
61 return;
62 }
63
64 updateState(SensorState::Activating);
65
66 // TODO(riju) : Add Permissions stuff later.
67
68 m_hasEventListener = true;
69 startUpdating();
70 updateState(SensorState::Active);
71
72 }
73
74 void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState)
75 {
76 if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Erro red) {
77 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is either idle or errored");
78 return;
79 }
80
81 // Avoid re-entrancy. sensor.stop() inside 'onstatechange' event handler.
82 if (m_sensorState == SensorState::Activating) {
83 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState Activating");
84 return;
85 }
86
87 m_hasEventListener = false;
88 stopUpdating();
89
90 m_sensorReading.clear();
91 updateState(SensorState::Idle);
92 }
93
94 void Sensor::updateState(SensorState newState)
95 {
96 if (m_sensorState == newState)
97 return;
98
99 m_sensorState = newState;
100 dispatchEvent(Event::create(EventTypeNames::statechange));
101 }
102
103 void Sensor::suspend()
104 {
105 m_hasEventListener = false;
106 stopUpdating();
107 }
108
109 void Sensor::resume()
110 {
111 m_hasEventListener = true;
112 startUpdating();
113 }
114
115 void Sensor::stop()
116 {
117 m_hasEventListener = false;
118 stopUpdating();
119 }
120
121 bool Sensor::hasPendingActivity() const
122 {
123 // Prevent V8 from garbage collecting the wrapper object if there are
124 // event listeners attached to it.
125 return hasEventListeners();
126 }
127
128 DEFINE_TRACE(Sensor)
129 {
130 ActiveDOMObject::trace(visitor);
131 EventTargetWithInlineData::trace(visitor);
132 PlatformEventController::trace(visitor);
133 visitor->trace(m_sensorReading);
134 }
135
136 } // 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