| 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..f1501efd3fbfffe7451458adffe5873f788c0837 100644
|
| --- a/Source/modules/device_orientation/DeviceMotionController.cpp
|
| +++ b/Source/modules/device_orientation/DeviceMotionController.cpp
|
| @@ -27,23 +27,22 @@
|
| #include "config.h"
|
| #include "modules/device_orientation/DeviceMotionController.h"
|
|
|
| -#include "modules/device_orientation/DeviceMotionClient.h"
|
| +#include "core/dom/Document.h"
|
| +#include "core/page/Page.h"
|
| #include "modules/device_orientation/DeviceMotionData.h"
|
| #include "modules/device_orientation/DeviceMotionEvent.h"
|
| -#include "core/page/Page.h"
|
| +#include "modules/device_orientation/WebDeviceMotionDispatcher.h"
|
|
|
| namespace WebCore {
|
|
|
| -DeviceMotionController::DeviceMotionController(DeviceMotionClient* client)
|
| - : DeviceController(client)
|
| +PassRefPtr<DeviceMotionController> DeviceMotionController::create()
|
| {
|
| - ASSERT(m_client);
|
| - deviceMotionClient()->setController(this);
|
| + return adoptRef(new DeviceMotionController());
|
| }
|
|
|
| -PassOwnPtr<DeviceMotionController> DeviceMotionController::create(DeviceMotionClient* client)
|
| +DeviceMotionController::~DeviceMotionController()
|
| {
|
| - return adoptPtr(new DeviceMotionController(client));
|
| + WebKit::WebDeviceMotionDispatcher::shared().removeController(this);
|
| }
|
|
|
| void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotionData)
|
| @@ -51,19 +50,77 @@ void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotio
|
| dispatchDeviceEvent(DeviceMotionEvent::create(eventNames().devicemotionEvent, deviceMotionData));
|
| }
|
|
|
| -DeviceMotionClient* DeviceMotionController::deviceMotionClient()
|
| +bool DeviceMotionController::hasLastData()
|
| {
|
| - return static_cast<DeviceMotionClient*>(m_client);
|
| + return WebKit::WebDeviceMotionDispatcher::shared().latestDeviceMotionData();
|
| }
|
|
|
| -bool DeviceMotionController::hasLastData()
|
| +PassRefPtr<Event> DeviceMotionController::getLastEvent()
|
| {
|
| - return deviceMotionClient()->lastMotion();
|
| + return DeviceMotionEvent::create(eventNames().devicemotionEvent,
|
| + WebKit::WebDeviceMotionDispatcher::shared().latestDeviceMotionData());
|
| }
|
|
|
| -PassRefPtr<Event> DeviceMotionController::getLastEvent()
|
| +void DeviceMotionController::addDeviceEventListener(DOMWindow* window)
|
| +{
|
| + bool wasEmpty = m_listeners.isEmpty();
|
| + m_listeners.add(window);
|
| +
|
| + if (hasLastData()) {
|
| + m_lastEventListeners.add(window);
|
| + if (!m_timer.isActive())
|
| + m_timer.startOneShot(0);
|
| + }
|
| +
|
| + if (wasEmpty)
|
| + startUpdating();
|
| +}
|
| +
|
| +void DeviceMotionController::removeDeviceEventListener(DOMWindow* window)
|
| +{
|
| + m_listeners.remove(window);
|
| + m_lastEventListeners.remove(window);
|
| + if (m_listeners.isEmpty())
|
| + stopUpdating();
|
| +}
|
| +
|
| +void DeviceMotionController::removeAllDeviceEventListeners(DOMWindow* window)
|
| +{
|
| + m_listeners.removeAll(window);
|
| + m_lastEventListeners.removeAll(window);
|
| + if (m_listeners.isEmpty())
|
| + stopUpdating();
|
| +}
|
| +
|
| +void DeviceMotionController::dispatchDeviceEvent(const PassRefPtr<Event> event)
|
| +{
|
| + Vector<RefPtr<DOMWindow> > listenerVector;
|
| + copyToVector(m_listeners, listenerVector);
|
| + dispatchEventToActiveDocuments(listenerVector, event);
|
| +}
|
| +
|
| +void DeviceMotionController::fireDeviceEvent(Timer<DeviceMotionController>* timer)
|
| {
|
| - return DeviceMotionEvent::create(eventNames().devicemotionEvent, deviceMotionClient()->lastMotion());
|
| + ASSERT_UNUSED(timer, timer == &m_timer);
|
| + ASSERT(hasLastData());
|
| +
|
| + m_timer.stop();
|
| + Vector<RefPtr<DOMWindow> > listenerVector;
|
| + copyToVector(m_lastEventListeners, listenerVector);
|
| + m_lastEventListeners.clear();
|
| + dispatchEventToActiveDocuments(listenerVector, getLastEvent());
|
| +}
|
| +
|
| +void DeviceMotionController::dispatchEventToActiveDocuments(Vector<RefPtr<DOMWindow> >& listeners,
|
| + const PassRefPtr<Event> prpEvent)
|
| +{
|
| + RefPtr<Event> event = prpEvent;
|
| + for (size_t i = 0; i < listeners.size(); ++i) {
|
| + if (listeners[i]->document()
|
| + && !listeners[i]->document()->activeDOMObjectsAreSuspended()
|
| + && !listeners[i]->document()->activeDOMObjectsAreStopped())
|
| + listeners[i]->dispatchEvent(event);
|
| + }
|
| }
|
|
|
| const char* DeviceMotionController::supplementName()
|
| @@ -73,7 +130,7 @@ const char* DeviceMotionController::supplementName()
|
|
|
| DeviceMotionController* DeviceMotionController::from(Page* page)
|
| {
|
| - return static_cast<DeviceMotionController*>(Supplement<Page>::from(page, supplementName()));
|
| + return static_cast<DeviceMotionController*>(RefCountedSupplement<Page, DeviceMotionController>::from(page, supplementName()));
|
| }
|
|
|
| bool DeviceMotionController::isActiveAt(Page* page)
|
| @@ -83,9 +140,19 @@ bool DeviceMotionController::isActiveAt(Page* page)
|
| return false;
|
| }
|
|
|
| -void provideDeviceMotionTo(Page* page, DeviceMotionClient* client)
|
| +void DeviceMotionController::startUpdating()
|
| +{
|
| + WebKit::WebDeviceMotionDispatcher::shared().addController(this);
|
| +}
|
| +
|
| +void DeviceMotionController::stopUpdating()
|
| +{
|
| + WebKit::WebDeviceMotionDispatcher::shared().removeController(this);
|
| +}
|
| +
|
| +void provideDeviceMotionTo(Page* page)
|
| {
|
| - DeviceMotionController::provideTo(page, DeviceMotionController::supplementName(), DeviceMotionController::create(client));
|
| + DeviceMotionController::provideTo(page, DeviceMotionController::supplementName(), DeviceMotionController::create());
|
| }
|
|
|
| } // namespace WebCore
|
|
|