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

Unified Diff: Source/WebCore/dom/DeviceMotionController.cpp

Issue 13866007: WebKit & WebCore part of the device motion implementation using the platform layer. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@devicemotion-webcore
Patch Set: fixed Eric's comments. Created 7 years, 8 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/WebCore/dom/DeviceMotionController.cpp
diff --git a/Source/WebCore/dom/DeviceMotionController.cpp b/Source/WebCore/dom/DeviceMotionController.cpp
index 8cf04a691023ec470bcdde50d65135c657bec7fd..50486c33e6da466988236189c14505dd78ff9bb6 100644
--- a/Source/WebCore/dom/DeviceMotionController.cpp
+++ b/Source/WebCore/dom/DeviceMotionController.cpp
@@ -1,29 +1,25 @@
/*
- * Copyright (C) 2013 Google Inc. All rights reserved.
+ * Copyright 2010 Apple Inc. All rights reserved.
+ * Copyright (C) 2012 Samsung Electronics. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
*
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
@@ -33,20 +29,21 @@
#include "DeviceMotionData.h"
#include "DeviceMotionEvent.h"
+#include "Document.h"
#include "Page.h"
-
#include "PlatformDeviceMotion.h"
namespace WebCore {
-DeviceMotionController::DeviceMotionController() : DeviceController()
+PassRefPtr<DeviceMotionController> DeviceMotionController::create()
{
+ return adoptRef(new DeviceMotionController());
}
-DeviceMotionController& DeviceMotionController::shared()
+DeviceMotionController::~DeviceMotionController()
{
- DEFINE_STATIC_LOCAL(DeviceMotionController, deviceMotionController, ());
- return deviceMotionController;
+ // This is a no-op if the controller has not registered with the device motion pump.
+ unregisterForDeviceMotionUpdates(this);
}
void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotionData)
@@ -56,20 +53,18 @@ void DeviceMotionController::didChangeDeviceMotion(DeviceMotionData* deviceMotio
bool DeviceMotionController::hasLastData()
{
- return lastDeviceMotionData();
+ return latestDeviceMotionData();
}
PassRefPtr<Event> DeviceMotionController::getLastEvent()
{
- return DeviceMotionEvent::create(eventNames().devicemotionEvent, lastDeviceMotionData());
+ return DeviceMotionEvent::create(eventNames().devicemotionEvent, latestDeviceMotionData());
}
-
void DeviceMotionController::addDeviceEventListener(DOMWindow* window)
{
bool wasEmpty = m_listeners.isEmpty();
m_listeners.add(window);
- m_activePages.add(window->page());
if (hasLastData()) {
m_lastEventListeners.add(window);
@@ -84,7 +79,6 @@ void DeviceMotionController::addDeviceEventListener(DOMWindow* window)
void DeviceMotionController::removeDeviceEventListener(DOMWindow* window)
{
m_listeners.remove(window);
- m_activePages.remove(window->page());
m_lastEventListeners.remove(window);
if (m_listeners.isEmpty())
stopUpdating();
@@ -93,20 +87,72 @@ void DeviceMotionController::removeDeviceEventListener(DOMWindow* window)
void DeviceMotionController::removeAllDeviceEventListeners(DOMWindow* window)
{
m_listeners.removeAll(window);
- m_activePages.removeAll(window->page());
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)
+{
+ 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()
+{
+ return "DeviceMotionController";
+}
+
+DeviceMotionController* DeviceMotionController::from(Page* page)
+{
+ return static_cast<DeviceMotionController*>(RefCountedSupplement<Page, DeviceMotionController>::from(page, supplementName()));
+}
+
+bool DeviceMotionController::isActiveAt(Page* page)
+{
+ if (DeviceMotionController* self = DeviceMotionController::from(page))
+ return self->isActive();
+ return false;
+}
+
void DeviceMotionController::startUpdating()
{
- startMonitoringDeviceMotion();
+ registerForDeviceMotionUpdates(this);
}
void DeviceMotionController::stopUpdating()
{
- stopMonitoringDeviceMotion();
+ unregisterForDeviceMotionUpdates(this);
+}
+
+void provideDeviceMotionTo(Page* page)
+{
+ DeviceMotionController::provideTo(page, DeviceMotionController::supplementName(), DeviceMotionController::create());
}
} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698