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

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: added unregister in destructor of DeviceMotionController 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
38 namespace WebKit {
39
40 // static
41 WebDeviceMotionEventPump& WebDeviceMotionEventPump::shared()
42 {
43 DEFINE_STATIC_LOCAL(WebDeviceMotionEventPump, webDeviceMotionEventPump, (kPu mpDelayMilliSeconds / 1000.0f));
44 return webDeviceMotionEventPump;
45 }
46
47 WebDeviceMotionEventPump::WebDeviceMotionEventPump(double delay_seconds) :
Peter Beverloo 2013/04/17 19:08:23 nit: WebKit uses camelCase, not hacker_case (for d
timvolodine 2013/04/18 12:37:38 Done.
48 m_delaySeconds(delay_seconds),
49 m_lastFireEventTimeMonotonic(0),
50 m_stopped(true),
51 m_lastDeviceMotionData(0),
52 m_pumpTimer(this, &WebDeviceMotionEventPump::fireEvent)
53 {
54 }
55
56 void WebDeviceMotionEventPump::fireEvent(Timer<WebDeviceMotionEventPump>*)
57 {
58 if (m_stopped)
59 return;
60
61 m_lastFireEventTimeMonotonic = monotonicallyIncreasingTime();
62 WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionRea der();
63 WebDeviceMotionData data;
64 reader->lastDeviceMotionData(data);
Peter Beverloo 2013/04/17 19:08:23 hmm. Hypothetical situation: what if there is no l
timvolodine 2013/04/18 12:37:38 Done.
65 m_lastDeviceMotionData = PassRefPtr<WebCore::DeviceMotionData>(data);
66
67 if (shouldFireEvent()) {
68 Vector<RefPtr<DeviceMotionController> > listenerVector;
69 copyToVector(m_listeners, listenerVector);
70 for (size_t i = 0;i < listenerVector.size(); ++i) {
71 listenerVector[i]->didChangeDeviceMotion(m_lastDeviceMotionData.get( ));
72 }
73 }
74
75 scheduleFireEvent();
76 }
77
78 void WebDeviceMotionEventPump::scheduleFireEvent()
79 {
80 if (m_pumpTimer.isActive())
81 return;
82
83 double scheduleDelay = std::max<double>(m_delaySeconds - (monotonicallyIncre asingTime() - m_lastFireEventTimeMonotonic), 0);
84 m_pumpTimer.startOneShot(scheduleDelay);
85 }
86
87 WebCore::DeviceMotionData* WebDeviceMotionEventPump::latestDeviceMotionData()
88 {
89 return m_lastDeviceMotionData.get();
90 }
91
92 bool WebDeviceMotionEventPump::shouldFireEvent()
93 {
94 // FIXME motion event should be fired only when values are different from th e values
95 // of the previously fired event.
Peter Beverloo 2013/04/17 19:08:23 Is that so? In either case, I'd argue that this is
timvolodine 2013/04/18 12:37:38 hmm, I was looking at the semantics for orientatio
Peter Beverloo 2013/04/18 13:56:06 Where does the Device Orientation specification sa
timvolodine 2013/04/18 14:55:37 ok the spec does not say anything about firing the
96 return true;
97 }
98
99 void WebDeviceMotionEventPump::addListener(DeviceMotionController* controller)
100 {
101 bool wasEmpty = m_listeners.isEmpty();
102 m_listeners.add(controller);
103 if (wasEmpty)
104 start();
105 }
106
107 void WebDeviceMotionEventPump::removeListener(DeviceMotionController* controller )
108 {
109 m_listeners.remove(controller);
110 if (m_listeners.isEmpty())
111 stop();
112 }
113
114 void WebDeviceMotionEventPump::start()
115 {
116 WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionRea der();
117 ASSERT(reader);
118 reader->startUpdating();
119 m_stopped = false;
120 scheduleFireEvent();
121 }
122
123 void WebDeviceMotionEventPump::stop()
124 {
125 WebDeviceMotionReader* reader = WebKit::Platform::current()->deviceMotionRea der();
126 ASSERT(reader);
127 reader->stopUpdating();
128 m_stopped = true;
129 }
130
131 } // namespace WebKit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698