| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright 2010 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2012 Samsung Electronics. All rights reserved. |
| 3 * | 4 * |
| 4 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions |
| 6 * met: | 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. |
| 7 * | 13 * |
| 8 * * Redistributions of source code must retain the above copyright | 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 9 * notice, this list of conditions and the following disclaimer. | 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 10 * * Redistributions in binary form must reproduce the above | 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 11 * copyright notice, this list of conditions and the following disclaimer | 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 12 * in the documentation and/or other materials provided with the | 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 13 * distribution. | 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 14 * * Neither the name of Google Inc. nor the names of its | 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 15 * contributors may be used to endorse or promote products derived from | 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 16 * this software without specific prior written permission. | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (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 |
| 28 * 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. |
| 29 */ | 25 */ |
| 30 | 26 |
| 31 #include "config.h" | 27 #include "config.h" |
| 32 #include "DeviceMotionController.h" | 28 #include "DeviceMotionController.h" |
| 33 | 29 |
| 34 #include "DeviceMotionData.h" | 30 #include "DeviceMotionData.h" |
| 35 #include "DeviceMotionEvent.h" | 31 #include "DeviceMotionEvent.h" |
| 32 #include "Document.h" |
| 36 #include "Page.h" | 33 #include "Page.h" |
| 37 | |
| 38 #include "PlatformDeviceMotion.h" | 34 #include "PlatformDeviceMotion.h" |
| 39 | 35 |
| 40 namespace WebCore { | 36 namespace WebCore { |
| 41 | 37 |
| 42 DeviceMotionController::DeviceMotionController() : DeviceController() | 38 PassRefPtr<DeviceMotionController> DeviceMotionController::create() |
| 43 { | 39 { |
| 40 return adoptRef(new DeviceMotionController()); |
| 44 } | 41 } |
| 45 | 42 |
| 46 DeviceMotionController& DeviceMotionController::shared() | 43 DeviceMotionController::~DeviceMotionController() |
| 47 { | 44 { |
| 48 DEFINE_STATIC_LOCAL(DeviceMotionController, deviceMotionController, ()); | 45 // This is a no-op if the controller has not registered with the device moti
on pump. |
| 49 return deviceMotionController; | 46 unregisterForDeviceMotionUpdates(this); |
| 50 } | 47 } |
| 51 | 48 |
| 52 void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotio
nData) | 49 void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotio
nData) |
| 53 { | 50 { |
| 54 dispatchDeviceEvent(DeviceMotionEvent::create(eventNames().devicemotionEvent
, deviceMotionData)); | 51 dispatchDeviceEvent(DeviceMotionEvent::create(eventNames().devicemotionEvent
, deviceMotionData)); |
| 55 } | 52 } |
| 56 | 53 |
| 57 bool DeviceMotionController::hasLastData() | 54 bool DeviceMotionController::hasLastData() |
| 58 { | 55 { |
| 59 return lastDeviceMotionData(); | 56 return latestDeviceMotionData(); |
| 60 } | 57 } |
| 61 | 58 |
| 62 PassRefPtr<Event> DeviceMotionController::getLastEvent() | 59 PassRefPtr<Event> DeviceMotionController::getLastEvent() |
| 63 { | 60 { |
| 64 return DeviceMotionEvent::create(eventNames().devicemotionEvent, lastDeviceM
otionData()); | 61 return DeviceMotionEvent::create(eventNames().devicemotionEvent, latestDevic
eMotionData()); |
| 65 } | 62 } |
| 66 | 63 |
| 67 | |
| 68 void DeviceMotionController::addDeviceEventListener(DOMWindow* window) | 64 void DeviceMotionController::addDeviceEventListener(DOMWindow* window) |
| 69 { | 65 { |
| 70 bool wasEmpty = m_listeners.isEmpty(); | 66 bool wasEmpty = m_listeners.isEmpty(); |
| 71 m_listeners.add(window); | 67 m_listeners.add(window); |
| 72 m_activePages.add(window->page()); | |
| 73 | 68 |
| 74 if (hasLastData()) { | 69 if (hasLastData()) { |
| 75 m_lastEventListeners.add(window); | 70 m_lastEventListeners.add(window); |
| 76 if (!m_timer.isActive()) | 71 if (!m_timer.isActive()) |
| 77 m_timer.startOneShot(0); | 72 m_timer.startOneShot(0); |
| 78 } | 73 } |
| 79 | 74 |
| 80 if (wasEmpty) | 75 if (wasEmpty) |
| 81 startUpdating(); | 76 startUpdating(); |
| 82 } | 77 } |
| 83 | 78 |
| 84 void DeviceMotionController::removeDeviceEventListener(DOMWindow* window) | 79 void DeviceMotionController::removeDeviceEventListener(DOMWindow* window) |
| 85 { | 80 { |
| 86 m_listeners.remove(window); | 81 m_listeners.remove(window); |
| 87 m_activePages.remove(window->page()); | |
| 88 m_lastEventListeners.remove(window); | 82 m_lastEventListeners.remove(window); |
| 89 if (m_listeners.isEmpty()) | 83 if (m_listeners.isEmpty()) |
| 90 stopUpdating(); | 84 stopUpdating(); |
| 91 } | 85 } |
| 92 | 86 |
| 93 void DeviceMotionController::removeAllDeviceEventListeners(DOMWindow* window) | 87 void DeviceMotionController::removeAllDeviceEventListeners(DOMWindow* window) |
| 94 { | 88 { |
| 95 m_listeners.removeAll(window); | 89 m_listeners.removeAll(window); |
| 96 m_activePages.removeAll(window->page()); | |
| 97 m_lastEventListeners.removeAll(window); | 90 m_lastEventListeners.removeAll(window); |
| 98 if (m_listeners.isEmpty()) | 91 if (m_listeners.isEmpty()) |
| 99 stopUpdating(); | 92 stopUpdating(); |
| 100 } | 93 } |
| 101 | 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 } |
| 124 } |
| 125 |
| 126 const char* DeviceMotionController::supplementName() |
| 127 { |
| 128 return "DeviceMotionController"; |
| 129 } |
| 130 |
| 131 DeviceMotionController* DeviceMotionController::from(Page* page) |
| 132 { |
| 133 return static_cast<DeviceMotionController*>(RefCountedSupplement<Page, Devic
eMotionController>::from(page, supplementName())); |
| 134 } |
| 135 |
| 136 bool DeviceMotionController::isActiveAt(Page* page) |
| 137 { |
| 138 if (DeviceMotionController* self = DeviceMotionController::from(page)) |
| 139 return self->isActive(); |
| 140 return false; |
| 141 } |
| 142 |
| 102 void DeviceMotionController::startUpdating() | 143 void DeviceMotionController::startUpdating() |
| 103 { | 144 { |
| 104 startMonitoringDeviceMotion(); | 145 registerForDeviceMotionUpdates(this); |
| 105 } | 146 } |
| 106 | 147 |
| 107 void DeviceMotionController::stopUpdating() | 148 void DeviceMotionController::stopUpdating() |
| 108 { | 149 { |
| 109 stopMonitoringDeviceMotion(); | 150 unregisterForDeviceMotionUpdates(this); |
| 151 } |
| 152 |
| 153 void provideDeviceMotionTo(Page* page) |
| 154 { |
| 155 DeviceMotionController::provideTo(page, DeviceMotionController::supplementNa
me(), DeviceMotionController::create()); |
| 110 } | 156 } |
| 111 | 157 |
| 112 } // namespace WebCore | 158 } // namespace WebCore |
| OLD | NEW |