Chromium Code Reviews| 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 // this is a no-op if the controller has not registered with the Device Moti on pump. |
|
Peter Beverloo
2013/04/18 13:56:06
nit: spelling (capital T). There also seems to be
timvolodine
2013/04/18 14:55:37
Done.
| |
| 48 DEFINE_STATIC_LOCAL(DeviceMotionController, deviceMotionController, ()); | 45 unregisterForDeviceMotionUpdates(this); |
| 49 return deviceMotionController; | |
| 50 } | 46 } |
| 51 | 47 |
| 52 void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotio nData) | 48 void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotio nData) |
| 53 { | 49 { |
| 54 dispatchDeviceEvent(DeviceMotionEvent::create(eventNames().devicemotionEvent , deviceMotionData)); | 50 dispatchDeviceEvent(DeviceMotionEvent::create(eventNames().devicemotionEvent , deviceMotionData)); |
| 55 } | 51 } |
| 56 | 52 |
| 57 bool DeviceMotionController::hasLastData() | 53 bool DeviceMotionController::hasLastData() |
| 58 { | 54 { |
| 59 return lastDeviceMotionData(); | 55 return latestDeviceMotionData(); |
| 60 } | 56 } |
| 61 | 57 |
| 62 PassRefPtr<Event> DeviceMotionController::getLastEvent() | 58 PassRefPtr<Event> DeviceMotionController::getLastEvent() |
| 63 { | 59 { |
| 64 return DeviceMotionEvent::create(eventNames().devicemotionEvent, lastDeviceM otionData()); | 60 return DeviceMotionEvent::create(eventNames().devicemotionEvent, latestDevic eMotionData()); |
| 65 } | 61 } |
| 66 | 62 |
| 67 | |
| 68 void DeviceMotionController::addDeviceEventListener(DOMWindow* window) | 63 void DeviceMotionController::addDeviceEventListener(DOMWindow* window) |
| 69 { | 64 { |
| 70 bool wasEmpty = m_listeners.isEmpty(); | 65 bool wasEmpty = m_listeners.isEmpty(); |
| 71 m_listeners.add(window); | 66 m_listeners.add(window); |
| 72 m_activePages.add(window->page()); | |
| 73 | 67 |
| 74 if (hasLastData()) { | 68 if (hasLastData()) { |
| 75 m_lastEventListeners.add(window); | 69 m_lastEventListeners.add(window); |
| 76 if (!m_timer.isActive()) | 70 if (!m_timer.isActive()) |
| 77 m_timer.startOneShot(0); | 71 m_timer.startOneShot(0); |
| 78 } | 72 } |
| 79 | 73 |
| 80 if (wasEmpty) | 74 if (wasEmpty) |
| 81 startUpdating(); | 75 startUpdating(); |
| 82 } | 76 } |
| 83 | 77 |
| 84 void DeviceMotionController::removeDeviceEventListener(DOMWindow* window) | 78 void DeviceMotionController::removeDeviceEventListener(DOMWindow* window) |
| 85 { | 79 { |
| 86 m_listeners.remove(window); | 80 m_listeners.remove(window); |
| 87 m_activePages.remove(window->page()); | |
| 88 m_lastEventListeners.remove(window); | 81 m_lastEventListeners.remove(window); |
| 89 if (m_listeners.isEmpty()) | 82 if (m_listeners.isEmpty()) |
| 90 stopUpdating(); | 83 stopUpdating(); |
| 91 } | 84 } |
| 92 | 85 |
| 93 void DeviceMotionController::removeAllDeviceEventListeners(DOMWindow* window) | 86 void DeviceMotionController::removeAllDeviceEventListeners(DOMWindow* window) |
| 94 { | 87 { |
| 95 m_listeners.removeAll(window); | 88 m_listeners.removeAll(window); |
| 96 m_activePages.removeAll(window->page()); | |
| 97 m_lastEventListeners.removeAll(window); | 89 m_lastEventListeners.removeAll(window); |
| 98 if (m_listeners.isEmpty()) | 90 if (m_listeners.isEmpty()) |
| 99 stopUpdating(); | 91 stopUpdating(); |
| 100 } | 92 } |
| 101 | 93 |
| 94 void DeviceMotionController::dispatchDeviceEvent(const PassRefPtr<Event> prpEven t) | |
| 95 { | |
| 96 Vector<RefPtr<DOMWindow> > listenerVector; | |
| 97 copyToVector(m_listeners, listenerVector); | |
| 98 dispatchEventToActiveDocuments(listenerVector, prpEvent); | |
| 99 } | |
| 100 | |
| 101 void DeviceMotionController::fireDeviceEvent(Timer<DeviceMotionController>* time r) | |
| 102 { | |
| 103 ASSERT_UNUSED(timer, timer == &m_timer); | |
| 104 ASSERT(hasLastData()); | |
| 105 | |
| 106 m_timer.stop(); | |
| 107 Vector<RefPtr<DOMWindow> > listenerVector; | |
| 108 copyToVector(m_lastEventListeners, listenerVector); | |
| 109 m_lastEventListeners.clear(); | |
| 110 dispatchEventToActiveDocuments(listenerVector, getLastEvent()); | |
| 111 } | |
| 112 | |
| 113 void DeviceMotionController::dispatchEventToActiveDocuments(Vector<RefPtr<DOMWin dow> >& listeners, | |
| 114 const PassRefPtr<Event> prpEvent) | |
| 115 { | |
| 116 RefPtr<Event> event = prpEvent; | |
| 117 for (size_t i = 0; i < listeners.size(); ++i) { | |
| 118 if (listeners[i]->document() | |
| 119 && !listeners[i]->document()->activeDOMObjectsAreSuspended() | |
| 120 && !listeners[i]->document()->activeDOMObjectsAreStopped()) | |
| 121 listeners[i]->dispatchEvent(event); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 const char* DeviceMotionController::supplementName() | |
| 126 { | |
| 127 return "DeviceMotionController"; | |
| 128 } | |
| 129 | |
| 130 DeviceMotionController* DeviceMotionController::from(Page* page) | |
| 131 { | |
| 132 return static_cast<DeviceMotionController*>(RefCountedSupplement<Page, Devic eMotionController>::from(page, supplementName())); | |
| 133 } | |
| 134 | |
| 135 bool DeviceMotionController::isActiveAt(Page* page) | |
| 136 { | |
| 137 if (DeviceMotionController* self = DeviceMotionController::from(page)) | |
| 138 return self->isActive(); | |
| 139 return false; | |
| 140 } | |
| 141 | |
| 102 void DeviceMotionController::startUpdating() | 142 void DeviceMotionController::startUpdating() |
| 103 { | 143 { |
| 104 startMonitoringDeviceMotion(); | 144 registerForDeviceMotionUpdates(this); |
| 105 } | 145 } |
| 106 | 146 |
| 107 void DeviceMotionController::stopUpdating() | 147 void DeviceMotionController::stopUpdating() |
| 108 { | 148 { |
| 109 stopMonitoringDeviceMotion(); | 149 unregisterForDeviceMotionUpdates(this); |
| 150 } | |
| 151 | |
| 152 void provideDeviceMotionTo(Page* page) | |
| 153 { | |
| 154 DeviceMotionController::provideTo(page, DeviceMotionController::supplementNa me(), DeviceMotionController::create()); | |
| 110 } | 155 } |
| 111 | 156 |
| 112 } // namespace WebCore | 157 } // namespace WebCore |
| OLD | NEW |