Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 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 | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 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 | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 #include "WebDeviceMotionEventPump.h" | |
| 33 | |
| 34 #include <public/Platform.h> | |
| 35 #include <public/WebDeviceMotionData.h> | |
| 36 #include <public/WebDeviceMotionReader.h> | |
| 37 #include <wtf/CurrentTime.h> | |
| 38 | |
| 39 namespace WebKit { | |
| 40 | |
| 41 // static | |
| 42 WebDeviceMotionEventPump& WebDeviceMotionEventPump::shared() | |
| 43 { | |
| 44 DEFINE_STATIC_LOCAL(WebDeviceMotionEventPump, webDeviceMotionEventPump, (kPu mpDelayMilliSeconds / 1000.0f)); | |
| 45 return webDeviceMotionEventPump; | |
| 46 } | |
| 47 | |
| 48 WebDeviceMotionEventPump::WebDeviceMotionEventPump(double delaySeconds) : | |
| 49 m_delaySeconds(delaySeconds), | |
| 50 m_lastFireEventTimeMonotonic(0), | |
| 51 m_stopped(true), | |
| 52 m_lastDeviceMotionData(0), | |
| 53 m_pumpTimer(this, &WebDeviceMotionEventPump::fireEvent) | |
| 54 { | |
| 55 } | |
| 56 | |
| 57 void WebDeviceMotionEventPump::fireEvent(Timer<WebDeviceMotionEventPump>*) | |
| 58 { | |
| 59 if (m_stopped) | |
| 60 return; | |
| 61 | |
| 62 m_lastFireEventTimeMonotonic = monotonicallyIncreasingTime(); | |
| 63 WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionRea der(); | |
| 64 WebDeviceMotionData data; | |
| 65 if (reader->latestDeviceMotionData(data)) { | |
| 66 m_lastDeviceMotionData = PassRefPtr<WebCore::DeviceMotionData>(data); | |
| 67 | |
| 68 if (shouldFireEvent()) { | |
| 69 Vector<RefPtr<DeviceMotionController> > listenerVector; | |
| 70 copyToVector(m_listeners, listenerVector); | |
| 71 for (size_t i = 0;i < listenerVector.size(); ++i) { | |
|
Peter Beverloo
2013/04/18 13:56:06
nit: don't need brackets around this one-line stat
timvolodine
2013/04/18 14:55:37
Done.
| |
| 72 listenerVector[i]->didChangeDeviceMotion(m_lastDeviceMotionData. get()); | |
| 73 } | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 scheduleFireEvent(); | |
| 78 } | |
| 79 | |
| 80 void WebDeviceMotionEventPump::scheduleFireEvent() | |
| 81 { | |
| 82 if (m_pumpTimer.isActive()) | |
| 83 return; | |
| 84 | |
| 85 double scheduleDelay = std::max<double>(m_delaySeconds - (monotonicallyIncre asingTime() - m_lastFireEventTimeMonotonic), 0); | |
| 86 m_pumpTimer.startOneShot(scheduleDelay); | |
| 87 } | |
| 88 | |
| 89 WebCore::DeviceMotionData* WebDeviceMotionEventPump::latestDeviceMotionData() | |
| 90 { | |
| 91 return m_lastDeviceMotionData.get(); | |
| 92 } | |
| 93 | |
| 94 bool WebDeviceMotionEventPump::shouldFireEvent() | |
| 95 { | |
| 96 // FIXME motion event should be fired only when values are different from th e values | |
| 97 // of the previously fired event. | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 void WebDeviceMotionEventPump::addListener(DeviceMotionController* controller) | |
| 102 { | |
| 103 bool wasEmpty = m_listeners.isEmpty(); | |
| 104 m_listeners.add(controller); | |
| 105 if (wasEmpty) | |
| 106 start(); | |
| 107 } | |
| 108 | |
| 109 void WebDeviceMotionEventPump::removeListener(DeviceMotionController* controller ) | |
| 110 { | |
| 111 m_listeners.remove(controller); | |
| 112 if (m_listeners.isEmpty()) | |
| 113 stop(); | |
| 114 } | |
| 115 | |
| 116 void WebDeviceMotionEventPump::start() | |
| 117 { | |
| 118 WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionRea der(); | |
| 119 ASSERT(reader); | |
| 120 reader->startUpdating(); | |
| 121 m_stopped = false; | |
| 122 scheduleFireEvent(); | |
| 123 } | |
| 124 | |
| 125 void WebDeviceMotionEventPump::stop() | |
| 126 { | |
| 127 WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionRea der(); | |
| 128 ASSERT(reader); | |
| 129 reader->stopUpdating(); | |
| 130 m_stopped = true; | |
| 131 } | |
| 132 | |
| 133 } // namespace WebKit | |
| OLD | NEW |