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();
}
PassOwnPtr<DeviceMotionController> DeviceMotionController::create(DeviceMotionClient* client)
{
return adoptPtr(new DeviceMotionController(client));
}
void DeviceMotionController::timerFired(Timer<DeviceMotionController>* timer)
{
ASSERT_UNUSED(timer, timer == &m_timer);
ASSERT(m_client->currentDeviceMotion());
m_timer.stop();
RefPtr<DeviceMotionData> deviceMotionData = m_client->currentDeviceMotion();
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->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->startUpdating();
}
void DeviceMotionController::removeListener(DOMWindow* window)
{
m_listeners.remove(window);
m_suspendedListeners.remove(window);
m_newListeners.remove(window);
if (m_listeners.isEmpty())
m_client->stopUpdating();
}
void DeviceMotionController::removeAllListeners(DOMWindow* window)
{
if (!m_listeners.contains(window))
return;
m_listeners.removeAll(window);
m_suspendedListeners.removeAll(window);
m_newListeners.remove(window);
if (m_listeners.isEmpty())
m_client->stopUpdating();
}
void DeviceMotionController::suspendEventsForAllListeners(DOMWindow* window)
{
if (!m_listeners.contains(window))
return;
int count = m_listeners.count(window);
removeAllListeners(window);
while (count--)
m_suspendedListeners.add(window);
}
void DeviceMotionController::resumeEventsForAllListeners(DOMWindow* window)
{
if (!m_suspendedListeners.contains(window))
return;
int count = m_suspendedListeners.count(window);
m_suspendedListeners.removeAll(window);
while (count--)
addListener(window);
}
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);
}
const AtomicString& DeviceMotionController::supplementName()
{
DEFINE_STATIC_LOCAL(AtomicString, name, ("DeviceMotionController"));
return name;
}
bool DeviceMotionController::isActiveAt(Page* page)
{
if (DeviceMotionController* self = DeviceMotionController::from(page))
return self->isActive();
return false;
}
void provideDeviceMotionTo(Page* page, DeviceMotionClient* client)
{
DeviceMotionController::provideTo(page, DeviceMotionController::supplementName(), DeviceMotionController::create(client));
}
}