| 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..e870a65d3a5e9ea434703d6d92ae377bf7f77371
|
| --- /dev/null
|
| +++ b/Source/WebCore/platform/chromium/support/WebDeviceMotionEventPump.cpp
|
| @@ -0,0 +1,134 @@
|
| +/*
|
| + * 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)
|
| +{
|
| +}
|
| +
|
| +WebDeviceMotionEventPump::~WebDeviceMotionEventPump()
|
| +{
|
| +}
|
| +
|
| +void WebDeviceMotionEventPump::fireEvent(Timer<WebDeviceMotionEventPump>*)
|
| +{
|
| + ASSERT(!m_stopped);
|
| +
|
| + m_lastFireEventTimeMonotonic = monotonicallyIncreasingTime();
|
| + WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionReader();
|
| + WebDeviceMotionData data;
|
| + if (reader->latestDeviceMotionData(data)) {
|
| + m_lastDeviceMotionData = PassRefPtr<WebCore::DeviceMotionData>(data);
|
| +
|
| + if (shouldFireEvent(m_lastDeviceMotionData.get())) {
|
| + 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()
|
| +{
|
| + ASSERT(!m_pumpTimer.isActive());
|
| +
|
| + 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(DeviceMotionData* data)
|
| +{
|
| + // Only fire if the event actually can provide any data.
|
| + 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);
|
| + m_pumpTimer.stop();
|
| + m_stopped = true;
|
| + reader->stopUpdating();
|
| +}
|
| +
|
| +} // namespace WebKit
|
|
|