DeviceMotionController.cpp [plain text]
#include "config.h"
#include "DeviceMotionController.h"
#include "DeviceMotionClient.h"
#include "DeviceMotionData.h"
#include "DeviceMotionEvent.h"
namespace WebCore {
DeviceMotionController::DeviceMotionController(DeviceMotionClient* client)
: m_client(client)
, m_timer(this, &DeviceMotionController::timerFired)
{
ASSERT(m_client);
m_client->setController(this);
}
DeviceMotionController::~DeviceMotionController()
{
m_client->deviceMotionControllerDestroyed();
}
void DeviceMotionController::timerFired(Timer<DeviceMotionController>* timer)
{
ASSERT_UNUSED(timer, timer == &m_timer);
ASSERT(!m_client || m_client->currentDeviceMotion());
m_timer.stop();
RefPtr<DeviceMotionData> deviceMotionData = m_client ? m_client->currentDeviceMotion() : DeviceMotionData::create();
RefPtr<DeviceMotionEvent> event = DeviceMotionEvent::create(eventNames().devicemotionEvent, deviceMotionData.get());
Vector<RefPtr<DOMWindow> > listenersVector;
copyToVector(m_newListeners, listenersVector);
m_newListeners.clear();
for (size_t i = 0; i < listenersVector.size(); ++i)
listenersVector[i]->dispatchEvent(event);
}
void DeviceMotionController::addListener(DOMWindow* window)
{
if (!m_client || m_client->currentDeviceMotion()) {
m_newListeners.add(window);
if (!m_timer.isActive())
m_timer.startOneShot(0);
}
bool wasEmpty = m_listeners.isEmpty();
m_listeners.add(window);
if (wasEmpty && m_client)
m_client->startUpdating();
}
void DeviceMotionController::removeListener(DOMWindow* window)
{
m_listeners.remove(window);
m_newListeners.remove(window);
if (m_listeners.isEmpty() && m_client)
m_client->stopUpdating();
}
void DeviceMotionController::removeAllListeners(DOMWindow* window)
{
if (!m_listeners.contains(window))
return;
m_listeners.removeAll(window);
m_newListeners.remove(window);
if (m_listeners.isEmpty() && m_client)
m_client->stopUpdating();
}
void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotionData)
{
RefPtr<DeviceMotionEvent> event = DeviceMotionEvent::create(eventNames().devicemotionEvent, deviceMotionData);
Vector<RefPtr<DOMWindow> > listenersVector;
copyToVector(m_listeners, listenersVector);
for (size_t i = 0; i < listenersVector.size(); ++i)
listenersVector[i]->dispatchEvent(event);
}
}