OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 Apple Inc. All rights reserved. | 2 * Copyright 2010 Apple Inc. All rights reserved. |
3 * Copyright (C) 2012 Samsung Electronics. All rights reserved. | 3 * Copyright (C) 2012 Samsung Electronics. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above copyright | 10 * * Redistributions in binary form must reproduce the above copyright |
11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
13 * | 13 * |
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY | 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 */ | 25 */ |
26 | 26 |
27 #include "config.h" | 27 #include "config.h" |
28 #include "modules/device_orientation/DeviceMotionController.h" | 28 #include "modules/device_orientation/DeviceMotionController.h" |
29 | 29 |
30 #include "modules/device_orientation/DeviceMotionClient.h" | 30 #include "core/dom/Document.h" |
| 31 #include "core/page/Page.h" |
31 #include "modules/device_orientation/DeviceMotionData.h" | 32 #include "modules/device_orientation/DeviceMotionData.h" |
32 #include "modules/device_orientation/DeviceMotionEvent.h" | 33 #include "modules/device_orientation/DeviceMotionEvent.h" |
33 #include "core/page/Page.h" | 34 #include "modules/device_orientation/WebDeviceMotionDispatcher.h" |
34 | 35 |
35 namespace WebCore { | 36 namespace WebCore { |
36 | 37 |
37 DeviceMotionController::DeviceMotionController(DeviceMotionClient* client) | 38 PassRefPtr<DeviceMotionController> DeviceMotionController::create() |
38 : DeviceController(client) | |
39 { | 39 { |
40 ASSERT(m_client); | 40 return adoptRef(new DeviceMotionController()); |
41 deviceMotionClient()->setController(this); | |
42 } | 41 } |
43 | 42 |
44 PassOwnPtr<DeviceMotionController> DeviceMotionController::create(DeviceMotionCl
ient* client) | 43 DeviceMotionController::~DeviceMotionController() |
45 { | 44 { |
46 return adoptPtr(new DeviceMotionController(client)); | 45 WebKit::WebDeviceMotionDispatcher::shared().removeController(this); |
47 } | 46 } |
48 | 47 |
49 void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotio
nData) | 48 void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotio
nData) |
50 { | 49 { |
51 dispatchDeviceEvent(DeviceMotionEvent::create(eventNames().devicemotionEvent
, deviceMotionData)); | 50 dispatchDeviceEvent(DeviceMotionEvent::create(eventNames().devicemotionEvent
, deviceMotionData)); |
52 } | 51 } |
53 | 52 |
54 DeviceMotionClient* DeviceMotionController::deviceMotionClient() | |
55 { | |
56 return static_cast<DeviceMotionClient*>(m_client); | |
57 } | |
58 | |
59 bool DeviceMotionController::hasLastData() | 53 bool DeviceMotionController::hasLastData() |
60 { | 54 { |
61 return deviceMotionClient()->lastMotion(); | 55 return WebKit::WebDeviceMotionDispatcher::shared().latestDeviceMotionData(); |
62 } | 56 } |
63 | 57 |
64 PassRefPtr<Event> DeviceMotionController::getLastEvent() | 58 PassRefPtr<Event> DeviceMotionController::getLastEvent() |
65 { | 59 { |
66 return DeviceMotionEvent::create(eventNames().devicemotionEvent, deviceMotio
nClient()->lastMotion()); | 60 return DeviceMotionEvent::create(eventNames().devicemotionEvent, |
| 61 WebKit::WebDeviceMotionDispatcher::shared().latestDeviceMotionData()); |
| 62 } |
| 63 |
| 64 void DeviceMotionController::addDeviceEventListener(DOMWindow* window) |
| 65 { |
| 66 bool wasEmpty = m_listeners.isEmpty(); |
| 67 m_listeners.add(window); |
| 68 |
| 69 if (hasLastData()) { |
| 70 m_lastEventListeners.add(window); |
| 71 if (!m_timer.isActive()) |
| 72 m_timer.startOneShot(0); |
| 73 } |
| 74 |
| 75 if (wasEmpty) |
| 76 startUpdating(); |
| 77 } |
| 78 |
| 79 void DeviceMotionController::removeDeviceEventListener(DOMWindow* window) |
| 80 { |
| 81 m_listeners.remove(window); |
| 82 m_lastEventListeners.remove(window); |
| 83 if (m_listeners.isEmpty()) |
| 84 stopUpdating(); |
| 85 } |
| 86 |
| 87 void DeviceMotionController::removeAllDeviceEventListeners(DOMWindow* window) |
| 88 { |
| 89 m_listeners.removeAll(window); |
| 90 m_lastEventListeners.removeAll(window); |
| 91 if (m_listeners.isEmpty()) |
| 92 stopUpdating(); |
| 93 } |
| 94 |
| 95 void DeviceMotionController::dispatchDeviceEvent(const PassRefPtr<Event> event) |
| 96 { |
| 97 Vector<RefPtr<DOMWindow> > listenerVector; |
| 98 copyToVector(m_listeners, listenerVector); |
| 99 dispatchEventToActiveDocuments(listenerVector, event); |
| 100 } |
| 101 |
| 102 void DeviceMotionController::fireDeviceEvent(Timer<DeviceMotionController>* time
r) |
| 103 { |
| 104 ASSERT_UNUSED(timer, timer == &m_timer); |
| 105 ASSERT(hasLastData()); |
| 106 |
| 107 m_timer.stop(); |
| 108 Vector<RefPtr<DOMWindow> > listenerVector; |
| 109 copyToVector(m_lastEventListeners, listenerVector); |
| 110 m_lastEventListeners.clear(); |
| 111 dispatchEventToActiveDocuments(listenerVector, getLastEvent()); |
| 112 } |
| 113 |
| 114 void DeviceMotionController::dispatchEventToActiveDocuments(Vector<RefPtr<DOMWin
dow> >& listeners, |
| 115 const PassRefPtr<Event> prpEvent) |
| 116 { |
| 117 RefPtr<Event> event = prpEvent; |
| 118 for (size_t i = 0; i < listeners.size(); ++i) { |
| 119 if (listeners[i]->document() |
| 120 && !listeners[i]->document()->activeDOMObjectsAreSuspended() |
| 121 && !listeners[i]->document()->activeDOMObjectsAreStopped()) |
| 122 listeners[i]->dispatchEvent(event); |
| 123 } |
67 } | 124 } |
68 | 125 |
69 const char* DeviceMotionController::supplementName() | 126 const char* DeviceMotionController::supplementName() |
70 { | 127 { |
71 return "DeviceMotionController"; | 128 return "DeviceMotionController"; |
72 } | 129 } |
73 | 130 |
74 DeviceMotionController* DeviceMotionController::from(Page* page) | 131 DeviceMotionController* DeviceMotionController::from(Page* page) |
75 { | 132 { |
76 return static_cast<DeviceMotionController*>(Supplement<Page>::from(page, sup
plementName())); | 133 return static_cast<DeviceMotionController*>(RefCountedSupplement<Page, Devic
eMotionController>::from(page, supplementName())); |
77 } | 134 } |
78 | 135 |
79 bool DeviceMotionController::isActiveAt(Page* page) | 136 bool DeviceMotionController::isActiveAt(Page* page) |
80 { | 137 { |
81 if (DeviceMotionController* self = DeviceMotionController::from(page)) | 138 if (DeviceMotionController* self = DeviceMotionController::from(page)) |
82 return self->isActive(); | 139 return self->isActive(); |
83 return false; | 140 return false; |
84 } | 141 } |
85 | 142 |
86 void provideDeviceMotionTo(Page* page, DeviceMotionClient* client) | 143 void DeviceMotionController::startUpdating() |
87 { | 144 { |
88 DeviceMotionController::provideTo(page, DeviceMotionController::supplementNa
me(), DeviceMotionController::create(client)); | 145 WebKit::WebDeviceMotionDispatcher::shared().addController(this); |
| 146 } |
| 147 |
| 148 void DeviceMotionController::stopUpdating() |
| 149 { |
| 150 WebKit::WebDeviceMotionDispatcher::shared().removeController(this); |
| 151 } |
| 152 |
| 153 void provideDeviceMotionTo(Page* page) |
| 154 { |
| 155 DeviceMotionController::provideTo(page, DeviceMotionController::supplementNa
me(), DeviceMotionController::create()); |
89 } | 156 } |
90 | 157 |
91 } // namespace WebCore | 158 } // namespace WebCore |
OLD | NEW |