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

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

Issue 1892083002: Generic Sensor API : Ambient Light Sensor API. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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/AmbientLightSensor.h"
6
7 #include "bindings/core/v8/ScriptPromise.h"
8 #include "bindings/core/v8/ScriptPromiseResolver.h"
9 #include "core/dom/DOMException.h"
10 #include "core/dom/Document.h"
11 #include "core/dom/ExceptionCode.h"
12 #include "core/events/Event.h"
13
14 #include "modules/sensor/AmbientLightSensorDispatcher.h"
15 #include "modules/sensor/AmbientLightSensorReading.h"
16 #include "modules/sensor/SensorReadingEvent.h"
17 #include "modules/sensor/sensor_state_type.h"
18
19 namespace blink {
20
21 // static
22 AmbientLightSensor* AmbientLightSensor::create(ExecutionContext* context, Sensor Options sensorOptions, ExceptionState& exceptionState)
23 {
24 AmbientLightSensor* sensor = new AmbientLightSensor(context, sensorOptions);
25 sensor->suspendIfNeeded();
26 return sensor;
27 }
28
29 // static
30 AmbientLightSensor* AmbientLightSensor::create(ExecutionContext* context, Except ionState& exceptionState)
31 {
32 return create(context, SensorOptions(), exceptionState);
33 }
34
35 AmbientLightSensor::~AmbientLightSensor()
36 {
37 stopUpdating();
38 }
39
40 AmbientLightSensor::AmbientLightSensor(ExecutionContext* executionContext, Senso rOptions sensorOptions)
41 : Sensor(executionContext, sensorOptions)
42 , PlatformEventController(toDocument(executionContext)->page())
43 {
44 }
45
46 void AmbientLightSensor::start(ScriptState* scriptState, ExceptionState& excepti onState)
47 {
48 if (m_sensorState != SensorState::Idle && m_sensorState != SensorState::Erro red)
49 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is not idle or errored");
50
51 updateState(SensorState::Activating);
52
53 // TODO(riju) : Add Permissions stuff later.
54
55 m_hasEventListener = true;
56 startUpdating();
57 }
58
59 void AmbientLightSensor::stop(ScriptState* scriptState, ExceptionState& exceptio nState)
60 {
61 if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Erro red)
62 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens orState is either idle or errored");
63
64 m_hasEventListener = false;
65 stopUpdating();
66 updateState(SensorState::Idle);
67 }
68
69 void AmbientLightSensor::updateState(SensorState newState)
70 {
71 m_sensorState = newState;
72 dispatchEvent(Event::create(EventTypeNames::statechange));
73 }
74
75 AmbientLightSensorReading& AmbientLightSensor::reading()
76 {
77 return *m_lightReading.get();
78 }
79
80 void AmbientLightSensor::didUpdateData()
81 {
82 if (!m_lightReading)
83 m_lightReading = AmbientLightSensorReading::create();
84
85 AmbientLightSensorReadingProxy oldReading(m_lightReading.get()->timeStamp(), m_lightReading.get()->illuminance());
86 AmbientLightSensorReadingProxy currentReading = *AmbientLightSensorDispatche r::instance().latestData();
87
88 Document* document = toDocument(getExecutionContext());
89 ASSERT(document);
90
91 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped())
92 return;
93
94 m_lightReading.get()->setTimeStamp(currentReading.timeStamp);
95 m_lightReading.get()->setIlluminance(currentReading.illuminance);
96
97 // TODO(riju) : Implement threshold when it is specs.
98 if (currentReading.illuminance != oldReading.illuminance)
99 dispatchEvent(SensorReadingEvent::create(EventTypeNames::change, *m_ligh tReading.get()));
100 }
101
102 void AmbientLightSensor::registerWithDispatcher()
103 {
104 AmbientLightSensorDispatcher::instance().addController(this);
105 }
106
107 void AmbientLightSensor::unregisterWithDispatcher()
108 {
109 AmbientLightSensorDispatcher::instance().removeController(this);
110 }
111
112 bool AmbientLightSensor::hasLastData()
113 {
114 return AmbientLightSensorDispatcher::instance().latestData();
115 }
116
117 void AmbientLightSensor::suspend()
118 {
119 m_hasEventListener = false;
timvolodine 2016/05/06 17:05:41 could this go to the Sensor superclass?
riju_ 2016/05/16 16:58:51 I could add the that to Sensor superclass, But th
120 stopUpdating();
121 }
122
123 void AmbientLightSensor::resume()
124 {
125 m_hasEventListener = true;
126 startUpdating();
127 }
128
129 void AmbientLightSensor::stop()
130 {
131 m_hasEventListener = false;
132 stopUpdating();
133 }
134
135 bool AmbientLightSensor::hasPendingActivity() const
136 {
137 // Prevent V8 from garbage collecting the wrapper object if there are
138 // event listeners attached to it.
139 return hasEventListeners();
140 }
141
142 DEFINE_TRACE(AmbientLightSensor)
143 {
144 PlatformEventController::trace(visitor);
145 ActiveDOMObject::trace(visitor);
146 Sensor::trace(visitor);
147 visitor->trace(m_lightReading);
148 }
149
150 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698