OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "config.h" |
| 6 #include "modules/device_light/DeviceLightController.h" |
| 7 |
| 8 #include "RuntimeEnabledFeatures.h" |
| 9 #include "core/dom/Document.h" |
| 10 #include "core/frame/DOMWindow.h" |
| 11 #include "core/page/Page.h" |
| 12 #include "modules/device_light/DeviceLightDispatcher.h" |
| 13 #include "modules/device_light/DeviceLightEvent.h" |
| 14 |
| 15 namespace WebCore { |
| 16 |
| 17 DeviceLightController::DeviceLightController(Document& document) |
| 18 : DeviceSensorEventController(document.page()) |
| 19 , DOMWindowLifecycleObserver(document.domWindow()) |
| 20 , m_document(document) |
| 21 { |
| 22 } |
| 23 |
| 24 DeviceLightController::~DeviceLightController() |
| 25 { |
| 26 stopUpdating(); |
| 27 } |
| 28 |
| 29 void DeviceLightController::didChangeDeviceLight(double value) |
| 30 { |
| 31 dispatchDeviceEvent(DeviceLightEvent::create(EventTypeNames::devicelight, va
lue)); |
| 32 } |
| 33 |
| 34 const char* DeviceLightController::supplementName() |
| 35 { |
| 36 return "DeviceLightController"; |
| 37 } |
| 38 |
| 39 DeviceLightController& DeviceLightController::from(Document& document) |
| 40 { |
| 41 DeviceLightController* controller = static_cast<DeviceLightController*>(Docu
mentSupplement::from(document, supplementName())); |
| 42 if (!controller) { |
| 43 controller = new DeviceLightController(document); |
| 44 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBe
Noop(controller)); |
| 45 } |
| 46 return *controller; |
| 47 } |
| 48 |
| 49 bool DeviceLightController::hasLastData() |
| 50 { |
| 51 return DeviceLightDispatcher::instance().latestDeviceLightData() >= 0; |
| 52 } |
| 53 |
| 54 PassRefPtrWillBeRawPtr<Event> DeviceLightController::getLastEvent() |
| 55 { |
| 56 return DeviceLightEvent::create(EventTypeNames::devicelight, |
| 57 DeviceLightDispatcher::instance().latestDeviceLightData()); |
| 58 } |
| 59 |
| 60 void DeviceLightController::registerWithDispatcher() |
| 61 { |
| 62 DeviceLightDispatcher::instance().addDeviceLightController(this); |
| 63 } |
| 64 |
| 65 void DeviceLightController::unregisterWithDispatcher() |
| 66 { |
| 67 DeviceLightDispatcher::instance().removeDeviceLightController(this); |
| 68 } |
| 69 |
| 70 bool DeviceLightController::isNullEvent(Event* event) |
| 71 { |
| 72 DeviceLightEvent* lightEvent = toDeviceLightEvent(event); |
| 73 return lightEvent->value() == std::numeric_limits<double>::infinity(); |
| 74 } |
| 75 |
| 76 Document* DeviceLightController::document() |
| 77 { |
| 78 return &m_document; |
| 79 } |
| 80 |
| 81 void DeviceLightController::didAddEventListener(DOMWindow* window, const AtomicS
tring& eventType) |
| 82 { |
| 83 if (eventType == EventTypeNames::devicelight && RuntimeEnabledFeatures::devi
ceLightEnabled()) { |
| 84 if (page() && page()->visibilityState() == PageVisibilityStateVisible) |
| 85 startUpdating(); |
| 86 m_hasEventListener = true; |
| 87 } |
| 88 } |
| 89 |
| 90 void DeviceLightController::didRemoveEventListener(DOMWindow* window, const Atom
icString& eventType) |
| 91 { |
| 92 if (eventType != EventTypeNames::devicelight || window->hasEventListeners(Ev
entTypeNames::devicelight)) |
| 93 return; |
| 94 |
| 95 stopUpdating(); |
| 96 m_hasEventListener = false; |
| 97 } |
| 98 |
| 99 void DeviceLightController::didRemoveAllEventListeners(DOMWindow* window) |
| 100 { |
| 101 stopUpdating(); |
| 102 m_hasEventListener = false; |
| 103 } |
| 104 |
| 105 } // namespace WebCore |
OLD | NEW |