Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2076)

Unified Diff: Source/modules/device_orientation/DeviceMotionController.cpp

Issue 14460010: Implement the Blink part of the Device Motion API. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: removed files incorrectly listed in WebKit.gyp Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a094268e7ba84f08348dcc969679887f6e5b8722 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::instance().latestDeviceMotionData();
}
-bool DeviceMotionController::hasLastData()
+PassRefPtr<Event> DeviceMotionController::getLastEvent()
{
- return deviceMotionClient()->lastMotion();
+ return DeviceMotionEvent::create(eventNames().devicemotionEvent,
+ DeviceMotionDispatcher::instance().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(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::instance().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::instance().removeController(this);
+ m_isActive = false;
}
} // namespace WebCore
« no previous file with comments | « Source/modules/device_orientation/DeviceMotionController.h ('k') | Source/modules/device_orientation/DeviceMotionData.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698