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

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

Issue 1942663003: [sensors]: Introduce the Generic Sensor API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase + add Tim as owner Created 4 years, 7 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 "bindings/core/v8/ScriptPromise.h"
8 #include "bindings/core/v8/ScriptPromiseResolver.h"
9 #include "core/dom/Document.h"
10 #include "core/dom/ExceptionCode.h"
11
12 #include "modules/sensor/SensorReading.h"
13 #include "modules/sensor/sensor_state_type.h"
14
15 namespace blink {
16
17 Sensor::~Sensor()
18 {
19 }
20
21 Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOp tions)
22 : ActiveScriptWrappable(this)
23 , ActiveDOMObject(executionContext)
24 , PlatformEventController(toDocument(executionContext)->page())
25 , m_sensorState(SensorState::Idle)
26 , m_sensorOptions(sensorOptions)
27 {
28 }
29
30 // Getters
31 String Sensor::state() const
32 {
33 switch (m_sensorState) {
34 case SensorState::Idle:
35 return "idle";
36 case SensorState::Activating:
37 return "activating";
38 case SensorState::Active:
39 return "active";
40 case SensorState::Errored:
41 return "errored";
42 }
43 NOTREACHED();
44 return "idle";
45 }
46
47 SensorReading* Sensor::reading() const
48 {
49 return m_sensorReading.get();
50 }
51
52 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState)
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 updateState(SensorState::Activating);
58
59 // TODO(riju) : Add Permissions stuff later.
60
61 m_hasEventListener = true;
62 startUpdating();
63 }
64
65 void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState)
66 {
67 if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Erro red)
68 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is either idle or errored");
69
70 m_hasEventListener = false;
71 stopUpdating();
72 updateState(SensorState::Idle);
73 }
74
75 void Sensor::updateState(SensorState newState)
76 {
77 m_sensorState = newState;
78 dispatchEvent(Event::create(EventTypeNames::statechange));
timvolodine 2016/05/24 15:30:35 this may need a bit of thinking regarding re-entra
riju_ 2016/05/25 15:07:17 Adding, if (m_sensorState == newState) return;
timvolodine 2016/05/25 15:40:22 don't think this is sufficient. if I am not overlo
riju_ 2016/05/26 13:21:01 Yes, you are right. Now I have checked with adding
79 }
80
81 void Sensor::suspend()
82 {
83 m_hasEventListener = false;
84 stopUpdating();
85 }
86
87 void Sensor::resume()
88 {
89 m_hasEventListener = true;
90 startUpdating();
91 }
92
93 void Sensor::stop()
94 {
95 m_hasEventListener = false;
96 stopUpdating();
timvolodine 2016/05/24 15:30:35 clear m_sensorReading?
riju_ 2016/05/25 15:07:17 for some sensors say geolocation, it is useful to
timvolodine 2016/05/25 15:40:22 caching is nice, but I was thinking in terms of us
riju_ 2016/05/26 13:21:01 Ok, I am clearing the reading in sensor.stop() now
97 }
98
99 bool Sensor::hasPendingActivity() const
100 {
101 // Prevent V8 from garbage collecting the wrapper object if there are
102 // event listeners attached to it.
103 return hasEventListeners();
104 }
105
106 DEFINE_TRACE(Sensor)
107 {
108 ActiveDOMObject::trace(visitor);
109 EventTargetWithInlineData::trace(visitor);
110 PlatformEventController::trace(visitor);
111 visitor->trace(m_sensorReading);
112 }
113
114 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698