OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 /* |
2 // Use of this source code is governed by a BSD-style license that can be | 2 * Copyright 2010 Apple Inc. All rights reserved. |
3 // found in the LICENSE file. | 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 */ |
4 | 26 |
5 #include "config.h" | 27 #include "config.h" |
6 #include "modules/device_orientation/DeviceOrientationController.h" | 28 #include "modules/device_orientation/DeviceOrientationController.h" |
7 | 29 |
8 #include "core/dom/Document.h" | 30 #include "core/dom/Document.h" |
9 #include "modules/EventModules.h" | 31 #include "core/frame/DOMWindow.h" |
| 32 #include "core/page/Page.h" |
10 #include "modules/device_orientation/DeviceOrientationData.h" | 33 #include "modules/device_orientation/DeviceOrientationData.h" |
11 #include "modules/device_orientation/DeviceOrientationDispatcher.h" | 34 #include "modules/device_orientation/DeviceOrientationDispatcher.h" |
12 #include "modules/device_orientation/DeviceOrientationEvent.h" | 35 #include "modules/device_orientation/DeviceOrientationEvent.h" |
13 | 36 |
14 namespace WebCore { | 37 namespace WebCore { |
15 | 38 |
16 DeviceOrientationController::DeviceOrientationController(Document& document) | 39 DeviceOrientationController::DeviceOrientationController(Document& document) |
17 : DeviceSingleWindowEventController(document) | 40 : DeviceSensorEventController(document.page()) |
| 41 , DOMWindowLifecycleObserver(document.domWindow()) |
| 42 , m_document(document) |
18 { | 43 { |
19 } | 44 } |
20 | 45 |
21 DeviceOrientationController::~DeviceOrientationController() | 46 DeviceOrientationController::~DeviceOrientationController() |
22 { | 47 { |
23 stopUpdating(); | 48 stopUpdating(); |
24 } | 49 } |
25 | 50 |
26 void DeviceOrientationController::didUpdateData() | 51 void DeviceOrientationController::didChangeDeviceOrientation(DeviceOrientationDa
ta* deviceOrientationData) |
27 { | 52 { |
28 if (m_overrideOrientationData) | 53 if (m_overrideOrientationData) |
29 return; | 54 return; |
30 dispatchDeviceEvent(lastEvent()); | 55 dispatchDeviceEvent(DeviceOrientationEvent::create(EventTypeNames::deviceori
entation, deviceOrientationData)); |
31 } | 56 } |
32 | 57 |
33 const char* DeviceOrientationController::supplementName() | 58 const char* DeviceOrientationController::supplementName() |
34 { | 59 { |
35 return "DeviceOrientationController"; | 60 return "DeviceOrientationController"; |
36 } | 61 } |
37 | 62 |
38 DeviceOrientationController& DeviceOrientationController::from(Document& documen
t) | 63 DeviceOrientationController& DeviceOrientationController::from(Document& documen
t) |
39 { | 64 { |
40 DeviceOrientationController* controller = static_cast<DeviceOrientationContr
oller*>(DocumentSupplement::from(document, supplementName())); | 65 DeviceOrientationController* controller = static_cast<DeviceOrientationContr
oller*>(DocumentSupplement::from(document, supplementName())); |
41 if (!controller) { | 66 if (!controller) { |
42 controller = new DeviceOrientationController(document); | 67 controller = new DeviceOrientationController(document); |
43 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBe
Noop(controller)); | 68 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBe
Noop(controller)); |
44 } | 69 } |
45 return *controller; | 70 return *controller; |
46 } | 71 } |
47 | 72 |
48 DeviceOrientationData* DeviceOrientationController::lastData() const | 73 DeviceOrientationData* DeviceOrientationController::lastData() |
49 { | 74 { |
50 return m_overrideOrientationData ? m_overrideOrientationData.get() : DeviceO
rientationDispatcher::instance().latestDeviceOrientationData(); | 75 return m_overrideOrientationData ? m_overrideOrientationData.get() : DeviceO
rientationDispatcher::instance().latestDeviceOrientationData(); |
51 } | 76 } |
52 | 77 |
53 bool DeviceOrientationController::hasLastData() | 78 bool DeviceOrientationController::hasLastData() |
54 { | 79 { |
55 return lastData(); | 80 return lastData(); |
56 } | 81 } |
57 | 82 |
| 83 PassRefPtrWillBeRawPtr<Event> DeviceOrientationController::getLastEvent() |
| 84 { |
| 85 return DeviceOrientationEvent::create(EventTypeNames::deviceorientation, las
tData()); |
| 86 } |
| 87 |
58 void DeviceOrientationController::registerWithDispatcher() | 88 void DeviceOrientationController::registerWithDispatcher() |
59 { | 89 { |
60 DeviceOrientationDispatcher::instance().addController(this); | 90 DeviceOrientationDispatcher::instance().addDeviceOrientationController(this)
; |
61 } | 91 } |
62 | 92 |
63 void DeviceOrientationController::unregisterWithDispatcher() | 93 void DeviceOrientationController::unregisterWithDispatcher() |
64 { | 94 { |
65 DeviceOrientationDispatcher::instance().removeController(this); | 95 DeviceOrientationDispatcher::instance().removeDeviceOrientationController(th
is); |
66 } | 96 } |
67 | 97 |
68 PassRefPtrWillBeRawPtr<Event> DeviceOrientationController::lastEvent() const | 98 bool DeviceOrientationController::isNullEvent(Event* event) |
69 { | |
70 return DeviceOrientationEvent::create(eventTypeName(), lastData()); | |
71 } | |
72 | |
73 bool DeviceOrientationController::isNullEvent(Event* event) const | |
74 { | 99 { |
75 DeviceOrientationEvent* orientationEvent = toDeviceOrientationEvent(event); | 100 DeviceOrientationEvent* orientationEvent = toDeviceOrientationEvent(event); |
76 return !orientationEvent->orientation()->canProvideEventData(); | 101 return !orientationEvent->orientation()->canProvideEventData(); |
77 } | 102 } |
78 | 103 |
79 const AtomicString& DeviceOrientationController::eventTypeName() const | 104 Document* DeviceOrientationController::document() |
80 { | 105 { |
81 return EventTypeNames::deviceorientation; | 106 return &m_document; |
| 107 } |
| 108 |
| 109 void DeviceOrientationController::didAddEventListener(DOMWindow* window, const A
tomicString& 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, cons
t AtomicString& eventType) |
| 121 { |
| 122 if (eventType != EventTypeNames::deviceorientation || window->hasEventListen
ers(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; |
82 } | 133 } |
83 | 134 |
84 void DeviceOrientationController::setOverride(DeviceOrientationData* deviceOrien
tationData) | 135 void DeviceOrientationController::setOverride(DeviceOrientationData* deviceOrien
tationData) |
85 { | 136 { |
86 ASSERT(deviceOrientationData); | 137 m_overrideOrientationData.clear(); |
| 138 didChangeDeviceOrientation(deviceOrientationData); |
87 m_overrideOrientationData = deviceOrientationData; | 139 m_overrideOrientationData = deviceOrientationData; |
88 dispatchDeviceEvent(lastEvent()); | |
89 } | 140 } |
90 | 141 |
91 void DeviceOrientationController::clearOverride() | 142 void DeviceOrientationController::clearOverride() |
92 { | 143 { |
93 if (!m_overrideOrientationData) | 144 if (!m_overrideOrientationData) |
94 return; | 145 return; |
95 m_overrideOrientationData.clear(); | 146 m_overrideOrientationData.clear(); |
96 if (lastData()) | 147 DeviceOrientationData* orientation = lastData(); |
97 didUpdateData(); | 148 if (orientation) |
| 149 didChangeDeviceOrientation(orientation); |
98 } | 150 } |
99 | 151 |
100 void DeviceOrientationController::trace(Visitor* visitor) | 152 void DeviceOrientationController::trace(Visitor* visitor) |
101 { | 153 { |
102 visitor->trace(m_overrideOrientationData); | 154 visitor->trace(m_overrideOrientationData); |
103 DocumentSupplement::trace(visitor); | 155 DocumentSupplement::trace(visitor); |
104 } | 156 } |
105 | 157 |
106 } // namespace WebCore | 158 } // namespace WebCore |
OLD | NEW |