DeviceMotionController.cpp [plain text]
#include "config.h"
#include "DeviceMotionController.h"
#include "DeviceMotionClient.h"
#include "DeviceMotionData.h"
#include "DeviceMotionEvent.h"
#include "Page.h"
namespace WebCore {
DeviceMotionController::DeviceMotionController(DeviceMotionClient* client)
: DeviceController(client)
{
ASSERT(m_client);
deviceMotionClient()->setController(this);
}
#if PLATFORM(IOS)
void DeviceMotionController::suspendUpdates()
{
if (m_client)
m_client->stopUpdating();
}
void DeviceMotionController::resumeUpdates()
{
if (m_client && !m_listeners.isEmpty())
m_client->startUpdating();
}
#endif
void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotionData)
{
dispatchDeviceEvent(DeviceMotionEvent::create(eventNames().devicemotionEvent, deviceMotionData));
}
DeviceMotionClient* DeviceMotionController::deviceMotionClient()
{
return static_cast<DeviceMotionClient*>(m_client);
}
bool DeviceMotionController::hasLastData()
{
return deviceMotionClient()->lastMotion();
}
PassRefPtr<Event> DeviceMotionController::getLastEvent()
{
return DeviceMotionEvent::create(eventNames().devicemotionEvent, deviceMotionClient()->lastMotion());
}
const char* DeviceMotionController::supplementName()
{
return "DeviceMotionController";
}
DeviceMotionController* DeviceMotionController::from(Page* page)
{
return static_cast<DeviceMotionController*>(Supplement<Page>::from(page, supplementName()));
}
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(), std::make_unique<DeviceMotionController>(client));
}
}