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

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: 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/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/events/Event.h"
12
13 #include "modules/sensor/AmbientLightSensorDispatcher.h"
14 #include "modules/sensor/AmbientLightSensorReading.h"
15 #include "modules/sensor/SensorReadingEvent.h"
16
17 namespace blink {
18
19 // static
20 AmbientLightSensor* AmbientLightSensor::create(ExecutionContext* context, Sensor Options sensorOptions, ExceptionState& exceptionState)
21 {
22 AmbientLightSensor* sensor = new AmbientLightSensor(context, sensorOptions);
23 sensor->suspendIfNeeded();
24 return sensor;
25 }
26
27 // static
28 AmbientLightSensor* AmbientLightSensor::create(ExecutionContext* context, Except ionState& exceptionState)
29 {
30 return create(context, SensorOptions(), exceptionState);
31 }
32
33 AmbientLightSensor::~AmbientLightSensor()
34 {
35 stopUpdating();
36 }
37
38 AmbientLightSensor::AmbientLightSensor(ExecutionContext* executionContext, Senso rOptions sensorOptions)
39 : Sensor(executionContext, sensorOptions)
40 {
41 }
42
43 AmbientLightSensorReading& AmbientLightSensor::reading()
44 {
45 return *m_lightReading.get();
46 }
47
48 void AmbientLightSensor::didUpdateData()
49 {
50 if (!m_lightReading)
51 m_lightReading = AmbientLightSensorReading::create();
52
53 AmbientLightSensorReadingProxy oldReading(m_lightReading.get()->timeStamp(), m_lightReading.get()->illuminance());
54 AmbientLightSensorReadingProxy currentReading = *AmbientLightSensorDispatche r::instance().latestData();
55
56 Document* document = toDocument(getExecutionContext());
57 DCHECK(document);
58
59 if (document->activeDOMObjectsAreSuspended() || document->activeDOMObjectsAr eStopped())
60 return;
61
62 m_lightReading.get()->setTimeStamp(currentReading.timeStamp);
63 m_lightReading.get()->setIlluminance(currentReading.illuminance);
64
65 // TODO(riju) : Implement threshold when it is specs.
66 if (currentReading.illuminance != oldReading.illuminance)
67 dispatchEvent(SensorReadingEvent::create(EventTypeNames::change, *m_ligh tReading.get()));
68 }
69
70 void AmbientLightSensor::registerWithDispatcher()
71 {
72 AmbientLightSensorDispatcher::instance().addController(this);
73 }
74
75 void AmbientLightSensor::unregisterWithDispatcher()
76 {
77 AmbientLightSensorDispatcher::instance().removeController(this);
78 }
79
80 bool AmbientLightSensor::hasLastData()
81 {
82 return AmbientLightSensorDispatcher::instance().latestData();
83 }
84
85 DEFINE_TRACE(AmbientLightSensor)
86 {
87 ActiveDOMObject::trace(visitor);
88 Sensor::trace(visitor);
89 visitor->trace(m_lightReading);
90 }
91
92 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698