| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "modules/device_light/DeviceLightController.h" | 6 #include "modules/device_light/DeviceLightController.h" |
| 7 | 7 |
| 8 #include "RuntimeEnabledFeatures.h" | 8 #include "RuntimeEnabledFeatures.h" |
| 9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
| 10 #include "core/frame/DOMWindow.h" | 10 #include "core/frame/DOMWindow.h" |
| 11 #include "core/page/Page.h" | 11 #include "core/page/Page.h" |
| 12 #include "modules/device_light/DeviceLightDispatcher.h" | 12 #include "modules/device_light/DeviceLightDispatcher.h" |
| 13 #include "modules/device_light/DeviceLightEvent.h" | 13 #include "modules/device_light/DeviceLightEvent.h" |
| 14 | 14 |
| 15 namespace WebCore { | 15 namespace WebCore { |
| 16 | 16 |
| 17 DeviceLightController::DeviceLightController(Document& document) | 17 DeviceLightController::DeviceLightController(Document& document) |
| 18 : DeviceSensorEventController(document.page()) | 18 : DeviceSingleWindowEventController(document) |
| 19 , DOMWindowLifecycleObserver(document.domWindow()) | |
| 20 , m_document(document) | |
| 21 { | 19 { |
| 22 } | 20 } |
| 23 | 21 |
| 24 DeviceLightController::~DeviceLightController() | 22 DeviceLightController::~DeviceLightController() |
| 25 { | 23 { |
| 26 stopUpdating(); | 24 stopUpdating(); |
| 27 } | 25 } |
| 28 | 26 |
| 29 void DeviceLightController::didChangeDeviceLight(double value) | |
| 30 { | |
| 31 dispatchDeviceEvent(DeviceLightEvent::create(EventTypeNames::devicelight, va
lue)); | |
| 32 } | |
| 33 | |
| 34 const char* DeviceLightController::supplementName() | 27 const char* DeviceLightController::supplementName() |
| 35 { | 28 { |
| 36 return "DeviceLightController"; | 29 return "DeviceLightController"; |
| 37 } | 30 } |
| 38 | 31 |
| 39 DeviceLightController& DeviceLightController::from(Document& document) | 32 DeviceLightController& DeviceLightController::from(Document& document) |
| 40 { | 33 { |
| 41 DeviceLightController* controller = static_cast<DeviceLightController*>(Docu
mentSupplement::from(document, supplementName())); | 34 DeviceLightController* controller = static_cast<DeviceLightController*>(Docu
mentSupplement::from(document, supplementName())); |
| 42 if (!controller) { | 35 if (!controller) { |
| 43 controller = new DeviceLightController(document); | 36 controller = new DeviceLightController(document); |
| 44 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBe
Noop(controller)); | 37 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBe
Noop(controller)); |
| 45 } | 38 } |
| 46 return *controller; | 39 return *controller; |
| 47 } | 40 } |
| 48 | 41 |
| 49 bool DeviceLightController::hasLastData() | 42 bool DeviceLightController::hasLastData() |
| 50 { | 43 { |
| 51 return DeviceLightDispatcher::instance().latestDeviceLightData() >= 0; | 44 return DeviceLightDispatcher::instance().latestDeviceLightData() >= 0; |
| 52 } | 45 } |
| 53 | 46 |
| 54 PassRefPtrWillBeRawPtr<Event> DeviceLightController::getLastEvent() | 47 void DeviceLightController::registerWithDispatcher() |
| 48 { |
| 49 DeviceLightDispatcher::instance().addController(this); |
| 50 } |
| 51 |
| 52 void DeviceLightController::unregisterWithDispatcher() |
| 53 { |
| 54 DeviceLightDispatcher::instance().removeController(this); |
| 55 } |
| 56 |
| 57 PassRefPtrWillBeRawPtr<Event> DeviceLightController::lastEvent() const |
| 55 { | 58 { |
| 56 return DeviceLightEvent::create(EventTypeNames::devicelight, | 59 return DeviceLightEvent::create(EventTypeNames::devicelight, |
| 57 DeviceLightDispatcher::instance().latestDeviceLightData()); | 60 DeviceLightDispatcher::instance().latestDeviceLightData()); |
| 58 } | 61 } |
| 59 | 62 |
| 60 void DeviceLightController::registerWithDispatcher() | 63 bool DeviceLightController::isNullEvent(Event* event) const |
| 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 { | 64 { |
| 72 DeviceLightEvent* lightEvent = toDeviceLightEvent(event); | 65 DeviceLightEvent* lightEvent = toDeviceLightEvent(event); |
| 73 return lightEvent->value() == std::numeric_limits<double>::infinity(); | 66 return lightEvent->value() == std::numeric_limits<double>::infinity(); |
| 74 } | 67 } |
| 75 | 68 |
| 76 Document* DeviceLightController::document() | 69 const AtomicString& DeviceLightController::eventTypeName() const |
| 77 { | 70 { |
| 78 return &m_document; | 71 return EventTypeNames::devicelight; |
| 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 } | 72 } |
| 104 | 73 |
| 105 } // namespace WebCore | 74 } // namespace WebCore |
| OLD | NEW |