Chromium Code Reviews| Index: Source/WebCore/platform/chromium/support/WebDeviceMotionEventPump.cpp |
| diff --git a/Source/WebCore/platform/chromium/support/WebDeviceMotionEventPump.cpp b/Source/WebCore/platform/chromium/support/WebDeviceMotionEventPump.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4f475d2c3701f54b80c9d0a13a1cbdfd48af4fbf |
| --- /dev/null |
| +++ b/Source/WebCore/platform/chromium/support/WebDeviceMotionEventPump.cpp |
| @@ -0,0 +1,132 @@ |
| +/* |
| + * Copyright (C) 2013 Google Inc. All rights reserved. |
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions are |
| + * met: |
| + * |
| + * * Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * * Redistributions in binary form must reproduce the above |
| + * copyright notice, this list of conditions and the following disclaimer |
| + * in the documentation and/or other materials provided with the |
| + * distribution. |
| + * * Neither the name of Google Inc. nor the names of its |
| + * contributors may be used to endorse or promote products derived from |
| + * this software without specific prior written permission. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#include "config.h" |
| +#include "WebDeviceMotionEventPump.h" |
| + |
| +#include <public/Platform.h> |
| +#include <public/WebDeviceMotionData.h> |
| +#include <public/WebDeviceMotionReader.h> |
| +#include <wtf/CurrentTime.h> |
| + |
| +namespace WebKit { |
| + |
| +// static |
| +WebDeviceMotionEventPump& WebDeviceMotionEventPump::shared() |
| +{ |
| + DEFINE_STATIC_LOCAL(WebDeviceMotionEventPump, webDeviceMotionEventPump, (kPumpDelayMilliseconds / 1000.0f)); |
| + return webDeviceMotionEventPump; |
| +} |
| + |
| +WebDeviceMotionEventPump::WebDeviceMotionEventPump(double delaySeconds) : |
| + m_delaySeconds(delaySeconds), |
| + m_lastFireEventTimeMonotonic(0), |
| + m_stopped(true), |
| + m_lastDeviceMotionData(0), |
| + m_pumpTimer(this, &WebDeviceMotionEventPump::fireEvent) |
| +{ |
| +} |
| + |
| +void WebDeviceMotionEventPump::fireEvent(Timer<WebDeviceMotionEventPump>*) |
| +{ |
| + if (m_stopped) |
|
eseidel
2013/04/23 15:21:24
This shoudl be an ASSERT. When you cancel a timer
timvolodine
2013/04/23 18:10:40
Done.
|
| + return; |
| + |
| + m_lastFireEventTimeMonotonic = monotonicallyIncreasingTime(); |
| + WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionReader(); |
| + WebDeviceMotionData data; |
| + if (reader->latestDeviceMotionData(data)) { |
| + m_lastDeviceMotionData = PassRefPtr<WebCore::DeviceMotionData>(data); |
| + |
| + if (shouldFireEvent()) { |
| + Vector<RefPtr<DeviceMotionController> > listenerVector; |
| + copyToVector(m_listeners, listenerVector); |
| + for (size_t i = 0;i < listenerVector.size(); ++i) |
| + listenerVector[i]->didChangeDeviceMotion(m_lastDeviceMotionData.get()); |
| + } |
| + } |
| + |
| + scheduleFireEvent(); |
| +} |
| + |
| +void WebDeviceMotionEventPump::scheduleFireEvent() |
| +{ |
| + if (m_pumpTimer.isActive()) |
|
eseidel
2013/04/23 15:21:24
This can be an ASSERT I believe.
timvolodine
2013/04/23 18:10:40
Done.
|
| + return; |
| + |
| + double scheduleDelay = std::max<double>(m_delaySeconds - (monotonicallyIncreasingTime() - m_lastFireEventTimeMonotonic), 0); |
| + m_pumpTimer.startOneShot(scheduleDelay); |
| +} |
| + |
| +WebCore::DeviceMotionData* WebDeviceMotionEventPump::latestDeviceMotionData() |
| +{ |
| + return m_lastDeviceMotionData.get(); |
| +} |
| + |
| +bool WebDeviceMotionEventPump::shouldFireEvent() |
| +{ |
| + // FIXME motion event should be fired only when values are different from the values |
| + // of the previously fired event. |
| + return true; |
| +} |
| + |
| +void WebDeviceMotionEventPump::addListener(DeviceMotionController* controller) |
| +{ |
| + bool wasEmpty = m_listeners.isEmpty(); |
| + m_listeners.add(controller); |
| + if (wasEmpty) |
| + start(); |
| +} |
| + |
| +void WebDeviceMotionEventPump::removeListener(DeviceMotionController* controller) |
| +{ |
| + m_listeners.remove(controller); |
| + if (m_listeners.isEmpty()) |
| + stop(); |
| +} |
| + |
| +void WebDeviceMotionEventPump::start() |
| +{ |
| + WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionReader(); |
| + ASSERT(reader); |
| + reader->startUpdating(); |
| + m_stopped = false; |
| + scheduleFireEvent(); |
| +} |
| + |
| +void WebDeviceMotionEventPump::stop() |
| +{ |
| + WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionReader(); |
| + ASSERT(reader); |
| + reader->stopUpdating(); |
| + m_stopped = true; |
|
eseidel
2013/04/23 15:21:24
You should just cancel the timer here.
timvolodine
2013/04/23 18:10:40
Done.
|
| +} |
| + |
| +} // namespace WebKit |