DeviceOrientationController.cpp [plain text]
#include "config.h"
#include "DeviceOrientationController.h"
#include "DeviceOrientation.h"
#include "DeviceOrientationClient.h"
#include "DeviceOrientationEvent.h"
namespace WebCore {
DeviceOrientationController::DeviceOrientationController(Page* page, DeviceOrientationClient* client)
: m_page(page)
, m_client(client)
, m_timer(this, &DeviceOrientationController::timerFired)
{
if (m_client)
m_client->setController(this);
}
void DeviceOrientationController::timerFired(Timer<DeviceOrientationController>* timer)
{
ASSERT_UNUSED(timer, timer == &m_timer);
ASSERT(!m_client || m_client->lastOrientation());
RefPtr<DeviceOrientation> orientation = m_client ? m_client->lastOrientation() : DeviceOrientation::create();
RefPtr<DeviceOrientationEvent> event = DeviceOrientationEvent::create(eventNames().deviceorientationEvent, orientation.get());
Vector<DOMWindow*> listenersVector;
copyToVector(m_newListeners, listenersVector);
m_newListeners.clear();
for (size_t i = 0; i < listenersVector.size(); ++i)
listenersVector[i]->dispatchEvent(event);
}
void DeviceOrientationController::addListener(DOMWindow* window)
{
if (!m_client || m_client->lastOrientation()) {
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 DeviceOrientationController::removeListener(DOMWindow* window)
{
m_listeners.remove(window);
m_newListeners.remove(window);
if (m_listeners.isEmpty() && m_client)
m_client->stopUpdating();
}
void DeviceOrientationController::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 DeviceOrientationController::suspendUpdates()
{
if (m_client)
m_client->stopUpdating();
}
void DeviceOrientationController::resumeUpdates()
{
if (m_client && !m_listeners.isEmpty())
m_client->startUpdating();
}
void DeviceOrientationController::didChangeDeviceOrientation(DeviceOrientation* orientation)
{
RefPtr<DeviceOrientationEvent> event = DeviceOrientationEvent::create(eventNames().deviceorientationEvent, orientation);
Vector<DOMWindow*> listenersVector;
copyToVector(m_listeners, listenersVector);
for (size_t i = 0; i < listenersVector.size(); ++i)
listenersVector[i]->dispatchEvent(event);
}
}