2 * Copyright 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Samsung Electronics. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "modules/device_orientation/DeviceOrientationController.h"
30 #include "core/dom/Document.h"
31 #include "core/frame/DOMWindow.h"
32 #include "core/page/Page.h"
33 #include "modules/device_orientation/DeviceOrientationData.h"
34 #include "modules/device_orientation/DeviceOrientationDispatcher.h"
35 #include "modules/device_orientation/DeviceOrientationEvent.h"
39 DeviceOrientationController::DeviceOrientationController(Document& document)
40 : DeviceSensorEventController(document.page())
41 , DOMWindowLifecycleObserver(document.domWindow())
42 , m_document(document)
46 DeviceOrientationController::~DeviceOrientationController()
51 void DeviceOrientationController::didChangeDeviceOrientation(DeviceOrientationData* deviceOrientationData)
53 if (m_overrideOrientationData)
55 dispatchDeviceEvent(DeviceOrientationEvent::create(EventTypeNames::deviceorientation, deviceOrientationData));
58 const char* DeviceOrientationController::supplementName()
60 return "DeviceOrientationController";
63 DeviceOrientationController& DeviceOrientationController::from(Document& document)
65 DeviceOrientationController* controller = static_cast<DeviceOrientationController*>(DocumentSupplement::from(document, supplementName()));
67 controller = new DeviceOrientationController(document);
68 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBeNoop(controller));
73 DeviceOrientationData* DeviceOrientationController::lastData()
75 return m_overrideOrientationData ? m_overrideOrientationData.get() : DeviceOrientationDispatcher::instance().latestDeviceOrientationData();
78 bool DeviceOrientationController::hasLastData()
83 PassRefPtrWillBeRawPtr<Event> DeviceOrientationController::getLastEvent()
85 return DeviceOrientationEvent::create(EventTypeNames::deviceorientation, lastData());
88 void DeviceOrientationController::registerWithDispatcher()
90 DeviceOrientationDispatcher::instance().addDeviceOrientationController(this);
93 void DeviceOrientationController::unregisterWithDispatcher()
95 DeviceOrientationDispatcher::instance().removeDeviceOrientationController(this);
98 bool DeviceOrientationController::isNullEvent(Event* event)
100 DeviceOrientationEvent* orientationEvent = toDeviceOrientationEvent(event);
101 return !orientationEvent->orientation()->canProvideEventData();
104 Document* DeviceOrientationController::document()
109 void DeviceOrientationController::didAddEventListener(DOMWindow* window, const AtomicString& eventType)
111 if (eventType != EventTypeNames::deviceorientation)
114 if (page() && page()->visibilityState() == PageVisibilityStateVisible)
117 m_hasEventListener = true;
120 void DeviceOrientationController::didRemoveEventListener(DOMWindow* window, const AtomicString& eventType)
122 if (eventType != EventTypeNames::deviceorientation || window->hasEventListeners(EventTypeNames::deviceorientation))
126 m_hasEventListener = false;
129 void DeviceOrientationController::didRemoveAllEventListeners(DOMWindow* window)
132 m_hasEventListener = false;
135 void DeviceOrientationController::setOverride(DeviceOrientationData* deviceOrientationData)
137 m_overrideOrientationData.clear();
138 didChangeDeviceOrientation(deviceOrientationData);
139 m_overrideOrientationData = deviceOrientationData;
142 void DeviceOrientationController::clearOverride()
144 if (!m_overrideOrientationData)
146 m_overrideOrientationData.clear();
147 DeviceOrientationData* orientation = lastData();
149 didChangeDeviceOrientation(orientation);
152 void DeviceOrientationController::trace(Visitor* visitor)
154 visitor->trace(m_overrideOrientationData);
155 DocumentSupplement::trace(visitor);
158 } // namespace WebCore