Chromium Code Reviews| 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) | |
| 19 , DOMWindowLifecycleObserver(document.domWindow()) | |
| 20 { | |
| 21 } | |
| 22 | |
| 23 DeviceLightController::~DeviceLightController() | |
| 24 { | |
| 25 stopUpdating(); | |
| 26 } | |
| 27 | |
| 28 void DeviceLightController::didChangeDeviceLight(double value) | |
| 29 { | |
| 30 dispatchDeviceEvent(DeviceLightEvent::create(EventTypeNames::devicelight, va lue)); | |
| 31 } | |
| 32 | |
| 33 const char* DeviceLightController::supplementName() | |
| 34 { | |
| 35 return "DeviceLightController"; | |
| 36 } | |
| 37 | |
| 38 DeviceLightController& DeviceLightController::from(Document& document) | |
| 39 { | |
| 40 DeviceLightController* controller = static_cast<DeviceLightController*>(Docu mentSupplement::from(document, supplementName())); | |
| 41 if (!controller) { | |
| 42 controller = new DeviceLightController(document); | |
| 43 DocumentSupplement::provideTo(document, supplementName(), adoptPtr(contr oller)); | |
| 44 } | |
| 45 return *controller; | |
| 46 } | |
| 47 | |
| 48 bool DeviceLightController::hasLastData() | |
| 49 { | |
| 50 return DeviceLightDispatcher::instance().latestDeviceLightData() >= 0; | |
| 51 } | |
| 52 | |
| 53 PassRefPtrWillBeRawPtr<Event> DeviceLightController::getLastEvent() | |
| 54 { | |
| 55 return DeviceLightEvent::create(EventTypeNames::devicelight, | |
| 56 DeviceLightDispatcher::instance().latestDeviceLightData()); | |
| 57 } | |
| 58 | |
| 59 void DeviceLightController::registerWithDispatcher() | |
| 60 { | |
| 61 DeviceLightDispatcher::instance().addDeviceLightController(this); | |
| 62 } | |
| 63 | |
| 64 void DeviceLightController::unregisterWithDispatcher() | |
| 65 { | |
| 66 DeviceLightDispatcher::instance().removeDeviceLightController(this); | |
| 67 } | |
| 68 | |
| 69 bool DeviceLightController::isNullEvent(Event* event) | |
| 70 { | |
| 71 DeviceLightEvent* lightEvent = toDeviceLightEvent(event); | |
| 72 return lightEvent->value() == std::numeric_limits<double>::infinity(); | |
| 73 } | |
| 74 | |
| 75 void DeviceLightController::didAddEventListener(DOMWindow* window, const AtomicS tring& eventType) | |
| 76 { | |
| 77 if (eventType == EventTypeNames::devicelight && RuntimeEnabledFeatures::devi ceLightEnabled()) { | |
| 78 if (page() && page()->visibilityState() == PageVisibilityStateVisible) | |
| 79 startUpdating(); | |
| 80 m_hasEventListener = true; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void DeviceLightController::didRemoveEventListener(DOMWindow* window, const Atom icString& eventType) | |
| 85 { | |
| 86 if (eventType != EventTypeNames::devicelight || window->hasEventListeners(Ev entTypeNames::devicelight)) { | |
|
timvolodine
2014/04/29 14:48:41
the logic here is wrong. please look at e.g. Devic
riju_
2014/04/29 15:59:41
Done.
| |
| 87 stopUpdating(); | |
| 88 m_hasEventListener = false; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void DeviceLightController::didRemoveAllEventListeners(DOMWindow* window) | |
| 93 { | |
| 94 stopUpdating(); | |
| 95 m_hasEventListener = false; | |
| 96 } | |
| 97 | |
| 98 } // namespace WebCore | |
| OLD | NEW |