| 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 WebDeviceMotionEventPump::~WebDeviceMotionEventPump() |
| 58 { |
| 59 } |
| 60 |
| 61 void WebDeviceMotionEventPump::fireEvent(Timer<WebDeviceMotionEventPump>*) |
| 62 { |
| 63 ASSERT(!m_stopped); |
| 64 |
| 65 m_lastFireEventTimeMonotonic = monotonicallyIncreasingTime(); |
| 66 WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionRea
der(); |
| 67 WebDeviceMotionData data; |
| 68 if (reader->latestDeviceMotionData(data)) { |
| 69 m_lastDeviceMotionData = PassRefPtr<WebCore::DeviceMotionData>(data); |
| 70 |
| 71 if (shouldFireEvent(m_lastDeviceMotionData.get())) { |
| 72 Vector<RefPtr<DeviceMotionController> > listenerVector; |
| 73 copyToVector(m_listeners, listenerVector); |
| 74 for (size_t i = 0;i < listenerVector.size(); ++i) |
| 75 listenerVector[i]->didChangeDeviceMotion(m_lastDeviceMotionData.
get()); |
| 76 } |
| 77 } |
| 78 |
| 79 scheduleFireEvent(); |
| 80 } |
| 81 |
| 82 void WebDeviceMotionEventPump::scheduleFireEvent() |
| 83 { |
| 84 ASSERT(!m_pumpTimer.isActive()); |
| 85 |
| 86 double scheduleDelay = std::max<double>(m_delaySeconds - (monotonicallyIncre
asingTime() - m_lastFireEventTimeMonotonic), 0); |
| 87 m_pumpTimer.startOneShot(scheduleDelay); |
| 88 } |
| 89 |
| 90 WebCore::DeviceMotionData* WebDeviceMotionEventPump::latestDeviceMotionData() |
| 91 { |
| 92 return m_lastDeviceMotionData.get(); |
| 93 } |
| 94 |
| 95 bool WebDeviceMotionEventPump::shouldFireEvent(DeviceMotionData* data) |
| 96 { |
| 97 // Only fire if the event actually can provide any data. |
| 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 m_pumpTimer.stop(); |
| 130 m_stopped = true; |
| 131 reader->stopUpdating(); |
| 132 } |
| 133 |
| 134 } // namespace WebKit |
| OLD | NEW |