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

Side by Side Diff: Source/WebCore/platform/chromium/support/WebDeviceMotionEventPump.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 next round of comments by Peter 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 unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "WebDeviceMotionEventPump.h"
33
34 #include <public/Platform.h>
35 #include <public/WebDeviceMotionData.h>
36 #include <public/WebDeviceMotionReader.h>
37 #include <wtf/CurrentTime.h>
38
39 namespace WebKit {
40
41 // static
42 WebDeviceMotionEventPump& WebDeviceMotionEventPump::shared()
43 {
44 DEFINE_STATIC_LOCAL(WebDeviceMotionEventPump, webDeviceMotionEventPump, (kPu mpDelayMilliseconds / 1000.0f));
45 return webDeviceMotionEventPump;
46 }
47
48 WebDeviceMotionEventPump::WebDeviceMotionEventPump(double delaySeconds) :
49 m_delaySeconds(delaySeconds),
50 m_lastFireEventTimeMonotonic(0),
51 m_stopped(true),
52 m_lastDeviceMotionData(0),
53 m_pumpTimer(this, &WebDeviceMotionEventPump::fireEvent)
54 {
55 }
56
57 void WebDeviceMotionEventPump::fireEvent(Timer<WebDeviceMotionEventPump>*)
58 {
59 if (m_stopped)
eseidel 2013/04/23 15:21:24 This shoudl be an ASSERT. When you cancel a timer
timvolodine 2013/04/23 18:10:40 Done.
60 return;
61
62 m_lastFireEventTimeMonotonic = monotonicallyIncreasingTime();
63 WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionRea der();
64 WebDeviceMotionData data;
65 if (reader->latestDeviceMotionData(data)) {
66 m_lastDeviceMotionData = PassRefPtr<WebCore::DeviceMotionData>(data);
67
68 if (shouldFireEvent()) {
69 Vector<RefPtr<DeviceMotionController> > listenerVector;
70 copyToVector(m_listeners, listenerVector);
71 for (size_t i = 0;i < listenerVector.size(); ++i)
72 listenerVector[i]->didChangeDeviceMotion(m_lastDeviceMotionData. get());
73 }
74 }
75
76 scheduleFireEvent();
77 }
78
79 void WebDeviceMotionEventPump::scheduleFireEvent()
80 {
81 if (m_pumpTimer.isActive())
eseidel 2013/04/23 15:21:24 This can be an ASSERT I believe.
timvolodine 2013/04/23 18:10:40 Done.
82 return;
83
84 double scheduleDelay = std::max<double>(m_delaySeconds - (monotonicallyIncre asingTime() - m_lastFireEventTimeMonotonic), 0);
85 m_pumpTimer.startOneShot(scheduleDelay);
86 }
87
88 WebCore::DeviceMotionData* WebDeviceMotionEventPump::latestDeviceMotionData()
89 {
90 return m_lastDeviceMotionData.get();
91 }
92
93 bool WebDeviceMotionEventPump::shouldFireEvent()
94 {
95 // FIXME motion event should be fired only when values are different from th e values
96 // of the previously fired event.
97 return true;
98 }
99
100 void WebDeviceMotionEventPump::addListener(DeviceMotionController* controller)
101 {
102 bool wasEmpty = m_listeners.isEmpty();
103 m_listeners.add(controller);
104 if (wasEmpty)
105 start();
106 }
107
108 void WebDeviceMotionEventPump::removeListener(DeviceMotionController* controller )
109 {
110 m_listeners.remove(controller);
111 if (m_listeners.isEmpty())
112 stop();
113 }
114
115 void WebDeviceMotionEventPump::start()
116 {
117 WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionRea der();
118 ASSERT(reader);
119 reader->startUpdating();
120 m_stopped = false;
121 scheduleFireEvent();
122 }
123
124 void WebDeviceMotionEventPump::stop()
125 {
126 WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionRea der();
127 ASSERT(reader);
128 reader->stopUpdating();
129 m_stopped = true;
eseidel 2013/04/23 15:21:24 You should just cancel the timer here.
timvolodine 2013/04/23 18:10:40 Done.
130 }
131
132 } // namespace WebKit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698