DeviceOrientationController.cpp [plain text]
#include "config.h"
#include "DeviceOrientationController.h"
#include "DeviceOrientationClient.h"
#include "DeviceOrientationData.h"
#include "DeviceOrientationEvent.h"
#include "EventNames.h"
#include "Page.h"
namespace WebCore {
DeviceOrientationController::DeviceOrientationController(DeviceOrientationClient* client)
: DeviceController(client)
{
#if PLATFORM(IOS)
if (m_client)
deviceOrientationClient()->setController(this);
#else
ASSERT(m_client);
deviceOrientationClient()->setController(this);
#endif
}
void DeviceOrientationController::didChangeDeviceOrientation(DeviceOrientationData* orientation)
{
dispatchDeviceEvent(DeviceOrientationEvent::create(eventNames().deviceorientationEvent, orientation));
}
DeviceOrientationClient* DeviceOrientationController::deviceOrientationClient()
{
return static_cast<DeviceOrientationClient*>(m_client);
}
#if PLATFORM(IOS)
void DeviceOrientationController::suspendUpdates()
{
if (m_client)
m_client->stopUpdating();
}
void DeviceOrientationController::resumeUpdates()
{
if (m_client && !m_listeners.isEmpty())
m_client->startUpdating();
}
#else
bool DeviceOrientationController::hasLastData()
{
return deviceOrientationClient()->lastOrientation();
}
RefPtr<Event> DeviceOrientationController::getLastEvent()
{
return DeviceOrientationEvent::create(eventNames().deviceorientationEvent, deviceOrientationClient()->lastOrientation());
}
#endif // PLATFORM(IOS)
const char* DeviceOrientationController::supplementName()
{
return "DeviceOrientationController";
}
DeviceOrientationController* DeviceOrientationController::from(Page* page)
{
return static_cast<DeviceOrientationController*>(Supplement<Page>::from(page, supplementName()));
}
bool DeviceOrientationController::isActiveAt(Page* page)
{
if (DeviceOrientationController* self = DeviceOrientationController::from(page))
return self->isActive();
return false;
}
void provideDeviceOrientationTo(Page* page, DeviceOrientationClient* client)
{
DeviceOrientationController::provideTo(page, DeviceOrientationController::supplementName(), std::make_unique<DeviceOrientationController>(client));
}
}