Chromium Code Reviews| Index: Source/modules/device_orientation/DeviceMotionController.cpp |
| diff --git a/Source/modules/device_orientation/DeviceMotionController.cpp b/Source/modules/device_orientation/DeviceMotionController.cpp |
| index b6b03a14f7bb48ad043efc28914a13ba0f69d8fb..0e9877bf857f709eaaf9a005139e14f1ba2b6359 100644 |
| --- a/Source/modules/device_orientation/DeviceMotionController.cpp |
| +++ b/Source/modules/device_orientation/DeviceMotionController.cpp |
| @@ -27,23 +27,24 @@ |
| #include "config.h" |
| #include "modules/device_orientation/DeviceMotionController.h" |
| -#include "modules/device_orientation/DeviceMotionClient.h" |
| +#include "core/dom/Document.h" |
| +#include "core/page/DOMWindow.h" |
| #include "modules/device_orientation/DeviceMotionData.h" |
| #include "modules/device_orientation/DeviceMotionEvent.h" |
| -#include "core/page/Page.h" |
| +#include "modules/device_orientation/DeviceMotionDispatcher.h" |
| namespace WebCore { |
| -DeviceMotionController::DeviceMotionController(DeviceMotionClient* client) |
| - : DeviceController(client) |
| +DeviceMotionController::DeviceMotionController(Document* document) |
| + : m_document(document) |
| + , m_isActive(false) |
| + , m_timer(this, &DeviceMotionController::fireDeviceEvent) |
| { |
| - ASSERT(m_client); |
| - deviceMotionClient()->setController(this); |
| } |
| -PassOwnPtr<DeviceMotionController> DeviceMotionController::create(DeviceMotionClient* client) |
| +DeviceMotionController::~DeviceMotionController() |
| { |
| - return adoptPtr(new DeviceMotionController(client)); |
| + stopUpdating(); |
| } |
| void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotionData) |
| @@ -51,19 +52,33 @@ void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotio |
| dispatchDeviceEvent(DeviceMotionEvent::create(eventNames().devicemotionEvent, deviceMotionData)); |
| } |
| -DeviceMotionClient* DeviceMotionController::deviceMotionClient() |
| +bool DeviceMotionController::hasLastData() |
| { |
| - return static_cast<DeviceMotionClient*>(m_client); |
| + return DeviceMotionDispatcher::shared().latestDeviceMotionData(); |
| } |
| -bool DeviceMotionController::hasLastData() |
| +PassRefPtr<Event> DeviceMotionController::getLastEvent() |
| { |
| - return deviceMotionClient()->lastMotion(); |
| + return DeviceMotionEvent::create(eventNames().devicemotionEvent, |
| + DeviceMotionDispatcher::shared().latestDeviceMotionData()); |
| } |
| -PassRefPtr<Event> DeviceMotionController::getLastEvent() |
| +void DeviceMotionController::fireDeviceEvent(Timer<DeviceMotionController>* timer) |
| { |
| - return DeviceMotionEvent::create(eventNames().devicemotionEvent, deviceMotionClient()->lastMotion()); |
| + ASSERT_UNUSED(timer, timer == &m_timer); |
| + ASSERT(hasLastData()); |
| + |
| + m_timer.stop(); |
| + dispatchDeviceEvent(getLastEvent()); |
| +} |
| + |
| +void DeviceMotionController::dispatchDeviceEvent(const PassRefPtr<Event> prpEvent) |
| +{ |
| + RefPtr<Event> event = prpEvent; |
| + if (m_document && m_document->domWindow() |
| + && !m_document->activeDOMObjectsAreSuspended() |
| + && !m_document->activeDOMObjectsAreStopped()) |
| + m_document->domWindow()->dispatchEvent(event); |
|
abarth-chromium
2013/05/07 20:10:01
This line is indented 8 spaces from the "if" on li
timvolodine
2013/05/08 12:19:24
Done.
|
| } |
| const char* DeviceMotionController::supplementName() |
| @@ -71,21 +86,34 @@ const char* DeviceMotionController::supplementName() |
| return "DeviceMotionController"; |
| } |
| -DeviceMotionController* DeviceMotionController::from(Page* page) |
| +DeviceMotionController* DeviceMotionController::from(Document* document) |
| { |
| - return static_cast<DeviceMotionController*>(Supplement<Page>::from(page, supplementName())); |
| + DeviceMotionController* controller = static_cast<DeviceMotionController*>(Supplement<ScriptExecutionContext>::from(document, supplementName())); |
| + if (!controller) { |
| + controller = new DeviceMotionController(document); |
| + Supplement<ScriptExecutionContext>::provideTo(document, supplementName(), adoptPtr(controller)); |
| + } |
| + return controller; |
| } |
| -bool DeviceMotionController::isActiveAt(Page* page) |
| +void DeviceMotionController::startUpdating() |
| { |
| - if (DeviceMotionController* self = DeviceMotionController::from(page)) |
| - return self->isActive(); |
| - return false; |
| + if (!m_isActive) { |
| + DeviceMotionDispatcher::shared().addController(this); |
| + m_isActive = true; |
| + |
| + if (hasLastData() && !m_timer.isActive()) |
| + // make sure to fire the motion data asap. |
| + m_timer.startOneShot(0); |
| + } |
| } |
| -void provideDeviceMotionTo(Page* page, DeviceMotionClient* client) |
| +void DeviceMotionController::stopUpdating() |
| { |
| - DeviceMotionController::provideTo(page, DeviceMotionController::supplementName(), DeviceMotionController::create(client)); |
| + if (m_isActive) { |
| + DeviceMotionDispatcher::shared().removeController(this); |
| + m_isActive = false; |
| + } |
| } |
| } // namespace WebCore |