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.
6 #include "modules/device_orientation/DeviceOrientationController.h"
8 #include "core/dom/Document.h"
9 #include "modules/EventModules.h"
10 #include "modules/device_orientation/DeviceOrientationData.h"
11 #include "modules/device_orientation/DeviceOrientationDispatcher.h"
12 #include "modules/device_orientation/DeviceOrientationEvent.h"
16 DeviceOrientationController::DeviceOrientationController(Document& document)
17 : DeviceSingleWindowEventController(document)
21 DeviceOrientationController::~DeviceOrientationController()
26 void DeviceOrientationController::didUpdateData()
28 if (m_overrideOrientationData)
30 dispatchDeviceEvent(lastEvent());
33 const char* DeviceOrientationController::supplementName()
35 return "DeviceOrientationController";
38 DeviceOrientationController& DeviceOrientationController::from(Document& document)
40 DeviceOrientationController* controller = static_cast<DeviceOrientationController*>(DocumentSupplement::from(document, supplementName()));
42 controller = new DeviceOrientationController(document);
43 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBeNoop(controller));
48 DeviceOrientationData* DeviceOrientationController::lastData() const
50 return m_overrideOrientationData ? m_overrideOrientationData.get() : DeviceOrientationDispatcher::instance().latestDeviceOrientationData();
53 bool DeviceOrientationController::hasLastData()
58 void DeviceOrientationController::registerWithDispatcher()
60 DeviceOrientationDispatcher::instance().addController(this);
63 void DeviceOrientationController::unregisterWithDispatcher()
65 DeviceOrientationDispatcher::instance().removeController(this);
68 PassRefPtrWillBeRawPtr<Event> DeviceOrientationController::lastEvent() const
70 return DeviceOrientationEvent::create(eventTypeName(), lastData());
73 bool DeviceOrientationController::isNullEvent(Event* event) const
75 DeviceOrientationEvent* orientationEvent = toDeviceOrientationEvent(event);
76 return !orientationEvent->orientation()->canProvideEventData();
79 const AtomicString& DeviceOrientationController::eventTypeName() const
81 return EventTypeNames::deviceorientation;
84 void DeviceOrientationController::setOverride(DeviceOrientationData* deviceOrientationData)
86 ASSERT(deviceOrientationData);
87 m_overrideOrientationData = deviceOrientationData;
88 dispatchDeviceEvent(lastEvent());
91 void DeviceOrientationController::clearOverride()
93 if (!m_overrideOrientationData)
95 m_overrideOrientationData.clear();
100 void DeviceOrientationController::trace(Visitor* visitor)
102 visitor->trace(m_overrideOrientationData);
103 DocumentSupplement::trace(visitor);
106 } // namespace WebCore