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 noop if the controller is not registered for updates. |
|
Peter Beverloo
2013/04/17 19:08:23
nit: "This is a no-op if the controller has not re
timvolodine
2013/04/18 12:37:38
Done.
| |
| 48 DEFINE_STATIC_LOCAL(DeviceMotionController, deviceMotionController, ()); | 45 unRegisterForDeviceMotionUpdates(this); |
|
Peter Beverloo
2013/04/17 19:08:23
"Unregister" is one word, so no need to capitalize
timvolodine
2013/04/18 12:37:38
Done.
| |
| 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(PassRefPtr<Event> prpEvent) | |
|
Peter Beverloo
2013/04/17 19:08:23
Please don't use acronyms and spell out the "prpEv
timvolodine
2013/04/18 12:37:38
I believe this is a guideline from WebKit on how t
Peter Beverloo
2013/04/18 13:56:06
Since you store it in a normal RefPtr on line 96 i
timvolodine
2013/04/18 14:55:37
Done.
| |
| 95 { | |
| 96 RefPtr<Event> event = prpEvent; | |
| 97 Vector<RefPtr<DOMWindow> > listenerVector; | |
| 98 copyToVector(m_listeners, listenerVector); | |
| 99 for (size_t i = 0; i < listenerVector.size(); ++i) { | |
| 100 if (listenerVector[i]->document() | |
| 101 && !listenerVector[i]->document()->activeDOMObjectsAreSuspended() | |
|
eseidel
2013/04/17 01:51:16
When are active dom objects stopped?
timvolodine
2013/04/17 17:47:25
What I can see from the code the three reasons to
| |
| 102 && !listenerVector[i]->document()->activeDOMObjectsAreStopped()) | |
| 103 listenerVector[i]->dispatchEvent(event); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void DeviceMotionController::fireDeviceEvent(Timer<DeviceMotionController>* time r) | |
| 108 { | |
| 109 ASSERT_UNUSED(timer, timer == &m_timer); | |
| 110 ASSERT(hasLastData()); | |
| 111 | |
| 112 m_timer.stop(); | |
| 113 Vector<RefPtr<DOMWindow> > listenerVector; | |
| 114 copyToVector(m_lastEventListeners, listenerVector); | |
| 115 m_lastEventListeners.clear(); | |
| 116 for (size_t i = 0; i < listenerVector.size(); ++i) { | |
| 117 if (listenerVector[i]->document() | |
| 118 && !listenerVector[i]->document()->activeDOMObjectsAreSuspended() | |
| 119 && !listenerVector[i]->document()->activeDOMObjectsAreStopped()) | |
| 120 listenerVector[i]->dispatchEvent(getLastEvent()); | |
|
Peter Beverloo
2013/04/17 19:08:23
Since we have two paths for dispatching events, do
timvolodine
2013/04/18 12:37:38
Done.
| |
| 121 } | |
| 122 } | |
| 123 | |
| 124 const char* DeviceMotionController::supplementName() | |
| 125 { | |
| 126 return "DeviceMotionController"; | |
| 127 } | |
| 128 | |
| 129 DeviceMotionController* DeviceMotionController::from(Page* page) | |
| 130 { | |
| 131 return static_cast<DeviceMotionController*>(RefCountedSupplement<Page, Devic eMotionController>::from(page, supplementName())); | |
| 132 } | |
| 133 | |
| 134 bool DeviceMotionController::isActiveAt(Page* page) | |
| 135 { | |
| 136 if (DeviceMotionController* self = DeviceMotionController::from(page)) | |
| 137 return self->isActive(); | |
| 138 return false; | |
| 139 } | |
| 140 | |
| 102 void DeviceMotionController::startUpdating() | 141 void DeviceMotionController::startUpdating() |
| 103 { | 142 { |
| 104 startMonitoringDeviceMotion(); | 143 registerForDeviceMotionUpdates(this); |
| 105 } | 144 } |
| 106 | 145 |
| 107 void DeviceMotionController::stopUpdating() | 146 void DeviceMotionController::stopUpdating() |
| 108 { | 147 { |
| 109 stopMonitoringDeviceMotion(); | 148 unRegisterForDeviceMotionUpdates(this); |
| 149 } | |
| 150 | |
| 151 void provideDeviceMotionTo(Page* page) | |
| 152 { | |
| 153 DeviceMotionController::provideTo(page, DeviceMotionController::supplementNa me(), DeviceMotionController::create()); | |
| 110 } | 154 } |
| 111 | 155 |
| 112 } // namespace WebCore | 156 } // namespace WebCore |
| OLD | NEW |