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..e054f77329c62f451cce302be1c2bbc055a7339d |
| --- /dev/null |
| +++ b/Source/WebCore/platform/chromium/support/WebDeviceMotionEventPump.cpp |
| @@ -0,0 +1,131 @@ |
| +/* |
| + * 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> |
| + |
| +namespace WebKit { |
| + |
| +// static |
| +WebDeviceMotionEventPump& WebDeviceMotionEventPump::shared() |
| +{ |
| + DEFINE_STATIC_LOCAL(WebDeviceMotionEventPump, webDeviceMotionEventPump, (kPumpDelayMilliSeconds / 1000.0f)); |
| + return webDeviceMotionEventPump; |
| +} |
| + |
| +WebDeviceMotionEventPump::WebDeviceMotionEventPump(double delay_seconds) : |
|
Peter Beverloo
2013/04/17 19:08:23
nit: WebKit uses camelCase, not hacker_case (for d
timvolodine
2013/04/18 12:37:38
Done.
|
| + m_delaySeconds(delay_seconds), |
| + m_lastFireEventTimeMonotonic(0), |
| + m_stopped(true), |
| + m_lastDeviceMotionData(0), |
| + m_pumpTimer(this, &WebDeviceMotionEventPump::fireEvent) |
| +{ |
| +} |
| + |
| +void WebDeviceMotionEventPump::fireEvent(Timer<WebDeviceMotionEventPump>*) |
| +{ |
| + if (m_stopped) |
| + return; |
| + |
| + m_lastFireEventTimeMonotonic = monotonicallyIncreasingTime(); |
| + WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionReader(); |
| + WebDeviceMotionData data; |
| + reader->lastDeviceMotionData(data); |
|
Peter Beverloo
2013/04/17 19:08:23
hmm. Hypothetical situation: what if there is no l
timvolodine
2013/04/18 12:37:38
Done.
|
| + 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()) |
| + 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. |
|
Peter Beverloo
2013/04/17 19:08:23
Is that so? In either case, I'd argue that this is
timvolodine
2013/04/18 12:37:38
hmm, I was looking at the semantics for orientatio
Peter Beverloo
2013/04/18 13:56:06
Where does the Device Orientation specification sa
timvolodine
2013/04/18 14:55:37
ok the spec does not say anything about firing the
|
| + 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; |
| +} |
| + |
| +} // namespace WebKit |