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..9f4038cd30ba848209f4cb8b40b2711ad3484292 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/DeviceMotionDispatcher.h" |
#include "modules/device_orientation/DeviceMotionEvent.h" |
-#include "core/page/Page.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); |
} |
const char* DeviceMotionController::supplementName() |
@@ -71,21 +86,36 @@ 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) |
+ return; |
+ |
+ DeviceMotionDispatcher::shared().addController(this); |
+ m_isActive = true; |
+ |
+ if (hasLastData() && !m_timer.isActive()) |
+ // Make sure to fire the device motion data as soon as possible. |
+ m_timer.startOneShot(0); |
} |
-void provideDeviceMotionTo(Page* page, DeviceMotionClient* client) |
+void DeviceMotionController::stopUpdating() |
{ |
- DeviceMotionController::provideTo(page, DeviceMotionController::supplementName(), DeviceMotionController::create(client)); |
+ if (!m_isActive) |
+ return; |
+ |
+ DeviceMotionDispatcher::shared().removeController(this); |
+ m_isActive = false; |
} |
} // namespace WebCore |