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

Unified Diff: Source/modules/device_light/DeviceLightController.cpp

Issue 143823004: Implement DeviceLight (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: nits Created 6 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 side-by-side diff with in-line comments
Download patch
Index: Source/modules/device_light/DeviceLightController.cpp
diff --git a/Source/modules/device_light/DeviceLightController.cpp b/Source/modules/device_light/DeviceLightController.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..36e09cedfe518ea105e169d6c7817ec3389a9775
--- /dev/null
+++ b/Source/modules/device_light/DeviceLightController.cpp
@@ -0,0 +1,105 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "modules/device_light/DeviceLightController.h"
+
+#include "RuntimeEnabledFeatures.h"
+#include "core/dom/Document.h"
+#include "core/frame/DOMWindow.h"
+#include "core/page/Page.h"
+#include "modules/device_light/DeviceLightDispatcher.h"
+#include "modules/device_light/DeviceLightEvent.h"
+
+namespace WebCore {
+
+DeviceLightController::DeviceLightController(Document& document)
+ : DeviceSensorEventController(document.page())
+ , DOMWindowLifecycleObserver(document.domWindow())
+ , m_document(document)
+{
+}
+
+DeviceLightController::~DeviceLightController()
+{
+ stopUpdating();
+}
+
+void DeviceLightController::didChangeDeviceLight(double value)
+{
+ dispatchDeviceEvent(DeviceLightEvent::create(EventTypeNames::devicelight, value));
+}
+
+const char* DeviceLightController::supplementName()
+{
+ return "DeviceLightController";
+}
+
+DeviceLightController& DeviceLightController::from(Document& document)
+{
+ DeviceLightController* controller = static_cast<DeviceLightController*>(DocumentSupplement::from(document, supplementName()));
+ if (!controller) {
+ controller = new DeviceLightController(document);
+ DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBeNoop(controller));
+ }
+ return *controller;
+}
+
+bool DeviceLightController::hasLastData()
+{
+ return DeviceLightDispatcher::instance().latestDeviceLightData() >= 0;
+}
+
+PassRefPtrWillBeRawPtr<Event> DeviceLightController::getLastEvent()
+{
+ return DeviceLightEvent::create(EventTypeNames::devicelight,
+ DeviceLightDispatcher::instance().latestDeviceLightData());
+}
+
+void DeviceLightController::registerWithDispatcher()
+{
+ DeviceLightDispatcher::instance().addDeviceLightController(this);
+}
+
+void DeviceLightController::unregisterWithDispatcher()
+{
+ DeviceLightDispatcher::instance().removeDeviceLightController(this);
+}
+
+bool DeviceLightController::isNullEvent(Event* event)
+{
+ DeviceLightEvent* lightEvent = toDeviceLightEvent(event);
+ return lightEvent->value() == std::numeric_limits<double>::infinity();
+}
+
+Document* DeviceLightController::document()
+{
+ return &m_document;
+}
+
+void DeviceLightController::didAddEventListener(DOMWindow* window, const AtomicString& eventType)
+{
+ if (eventType == EventTypeNames::devicelight && RuntimeEnabledFeatures::deviceLightEnabled()) {
+ if (page() && page()->visibilityState() == PageVisibilityStateVisible)
+ startUpdating();
+ m_hasEventListener = true;
+ }
+}
+
+void DeviceLightController::didRemoveEventListener(DOMWindow* window, const AtomicString& eventType)
+{
+ if (eventType != EventTypeNames::devicelight || window->hasEventListeners(EventTypeNames::devicelight))
+ return;
+
+ stopUpdating();
+ m_hasEventListener = false;
+}
+
+void DeviceLightController::didRemoveAllEventListeners(DOMWindow* window)
+{
+ stopUpdating();
+ m_hasEventListener = false;
+}
+
+} // namespace WebCore
« no previous file with comments | « Source/modules/device_light/DeviceLightController.h ('k') | Source/modules/device_light/DeviceLightDispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698