Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / device_orientation / DeviceOrientationController.cpp
1 /*
2  * Copyright 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2012 Samsung Electronics. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26
27 #include "config.h"
28 #include "modules/device_orientation/DeviceOrientationController.h"
29
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"
36
37 namespace WebCore {
38
39 DeviceOrientationController::DeviceOrientationController(Document& document)
40     : DeviceSensorEventController(document.page())
41     , DOMWindowLifecycleObserver(document.domWindow())
42     , m_document(document)
43 {
44 }
45
46 DeviceOrientationController::~DeviceOrientationController()
47 {
48     stopUpdating();
49 }
50
51 void DeviceOrientationController::didChangeDeviceOrientation(DeviceOrientationData* deviceOrientationData)
52 {
53     if (m_overrideOrientationData)
54         return;
55     dispatchDeviceEvent(DeviceOrientationEvent::create(EventTypeNames::deviceorientation, deviceOrientationData));
56 }
57
58 const char* DeviceOrientationController::supplementName()
59 {
60     return "DeviceOrientationController";
61 }
62
63 DeviceOrientationController& DeviceOrientationController::from(Document& document)
64 {
65     DeviceOrientationController* controller = static_cast<DeviceOrientationController*>(DocumentSupplement::from(document, supplementName()));
66     if (!controller) {
67         controller = new DeviceOrientationController(document);
68         DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBeNoop(controller));
69     }
70     return *controller;
71 }
72
73 DeviceOrientationData* DeviceOrientationController::lastData()
74 {
75     return m_overrideOrientationData ? m_overrideOrientationData.get() : DeviceOrientationDispatcher::instance().latestDeviceOrientationData();
76 }
77
78 bool DeviceOrientationController::hasLastData()
79 {
80     return lastData();
81 }
82
83 PassRefPtrWillBeRawPtr<Event> DeviceOrientationController::getLastEvent()
84 {
85     return DeviceOrientationEvent::create(EventTypeNames::deviceorientation, lastData());
86 }
87
88 void DeviceOrientationController::registerWithDispatcher()
89 {
90     DeviceOrientationDispatcher::instance().addDeviceOrientationController(this);
91 }
92
93 void DeviceOrientationController::unregisterWithDispatcher()
94 {
95     DeviceOrientationDispatcher::instance().removeDeviceOrientationController(this);
96 }
97
98 bool DeviceOrientationController::isNullEvent(Event* event)
99 {
100     DeviceOrientationEvent* orientationEvent = toDeviceOrientationEvent(event);
101     return !orientationEvent->orientation()->canProvideEventData();
102 }
103
104 Document* DeviceOrientationController::document()
105 {
106     return &m_document;
107 }
108
109 void DeviceOrientationController::didAddEventListener(DOMWindow* window, const AtomicString& eventType)
110 {
111     if (eventType != EventTypeNames::deviceorientation)
112         return;
113
114     if (page() && page()->visibilityState() == PageVisibilityStateVisible)
115         startUpdating();
116
117     m_hasEventListener = true;
118 }
119
120 void DeviceOrientationController::didRemoveEventListener(DOMWindow* window, const AtomicString& eventType)
121 {
122     if (eventType != EventTypeNames::deviceorientation || window->hasEventListeners(EventTypeNames::deviceorientation))
123         return;
124
125     stopUpdating();
126     m_hasEventListener = false;
127 }
128
129 void DeviceOrientationController::didRemoveAllEventListeners(DOMWindow* window)
130 {
131     stopUpdating();
132     m_hasEventListener = false;
133 }
134
135 void DeviceOrientationController::setOverride(DeviceOrientationData* deviceOrientationData)
136 {
137     m_overrideOrientationData.clear();
138     didChangeDeviceOrientation(deviceOrientationData);
139     m_overrideOrientationData = deviceOrientationData;
140 }
141
142 void DeviceOrientationController::clearOverride()
143 {
144     if (!m_overrideOrientationData)
145         return;
146     m_overrideOrientationData.clear();
147     DeviceOrientationData* orientation = lastData();
148     if (orientation)
149         didChangeDeviceOrientation(orientation);
150 }
151
152 void DeviceOrientationController::trace(Visitor* visitor)
153 {
154     visitor->trace(m_overrideOrientationData);
155     DocumentSupplement::trace(visitor);
156 }
157
158 } // namespace WebCore