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..c6c7b5fbe70cfd447c87719a530d8d1e7b77c1a8 100644 |
--- a/Source/modules/device_orientation/DeviceMotionController.cpp |
+++ b/Source/modules/device_orientation/DeviceMotionController.cpp |
@@ -27,23 +27,23 @@ |
#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 "core/platform/PlatformDeviceMotion.h" |
#include "modules/device_orientation/DeviceMotionData.h" |
#include "modules/device_orientation/DeviceMotionEvent.h" |
-#include "core/page/Page.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)); |
+ // This is a no-op if the controller has not registered with the device motion pump. |
Peter Beverloo
2013/04/29 17:38:03
There no longer is a pump.
timvolodine
2013/04/30 12:40:18
Done.
|
+ unregisterForDeviceMotionUpdates(this); |
} |
void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotionData) |
@@ -51,19 +51,76 @@ void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotio |
dispatchDeviceEvent(DeviceMotionEvent::create(eventNames().devicemotionEvent, deviceMotionData)); |
} |
-DeviceMotionClient* DeviceMotionController::deviceMotionClient() |
+bool DeviceMotionController::hasLastData() |
{ |
- return static_cast<DeviceMotionClient*>(m_client); |
+ return latestDeviceMotionData(); |
} |
-bool DeviceMotionController::hasLastData() |
+PassRefPtr<Event> DeviceMotionController::getLastEvent() |
{ |
- return deviceMotionClient()->lastMotion(); |
+ return DeviceMotionEvent::create(eventNames().devicemotionEvent, 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() |
+{ |
+ registerForDeviceMotionUpdates(this); |
+} |
+ |
+void DeviceMotionController::stopUpdating() |
+{ |
+ unregisterForDeviceMotionUpdates(this); |
+} |
+ |
+void provideDeviceMotionTo(Page* page) |
{ |
- DeviceMotionController::provideTo(page, DeviceMotionController::supplementName(), DeviceMotionController::create(client)); |
+ DeviceMotionController::provideTo(page, DeviceMotionController::supplementName(), DeviceMotionController::create()); |
} |
} // namespace WebCore |