OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2012 Samsung Electronics | 2 * Copyright (C) 2012 Samsung Electronics |
3 * | 3 * |
4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
8 * | 8 * |
9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU Library General Public License | 14 * You should have received a copy of the GNU Library General Public License |
15 * along with this library; see the file COPYING.LIB. If not, write to | 15 * along with this library; see the file COPYING.LIB. If not, write to |
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
18 */ | 18 */ |
19 | 19 |
20 #include "config.h" | 20 #include "config.h" |
21 #include "modules/vibration/NavigatorVibration.h" | 21 #include "modules/vibration/NavigatorVibration.h" |
22 | 22 |
23 #if ENABLE(VIBRATION) | |
24 | |
25 #include "core/dom/ExceptionCode.h" | |
26 #include "core/page/Frame.h" | 23 #include "core/page/Frame.h" |
27 #include "core/page/Navigator.h" | |
28 #include "core/page/Page.h" | 24 #include "core/page/Page.h" |
29 #include "modules/vibration/Vibration.h" | 25 #include "public/platform/Platform.h" |
30 #include "wtf/Uint32Array.h" | |
31 | 26 |
32 namespace WebCore { | 27 namespace WebCore { |
33 | 28 |
29 // Maximum duration of a vibration is 10 seconds. | |
30 const unsigned kVibrationDurationMax = 10000; | |
31 | |
32 // Maximum number of entries in a vibration pattern. | |
33 const unsigned kVibrationPatternLengthMax = 99; | |
34 | |
34 NavigatorVibration::NavigatorVibration() | 35 NavigatorVibration::NavigatorVibration() |
36 : m_timerStart(this, &NavigatorVibration::timerStartFired) | |
37 , m_timerStop(this, &NavigatorVibration::timerStopFired) | |
38 , m_isVibrating(false) | |
35 { | 39 { |
36 } | 40 } |
37 | 41 |
38 NavigatorVibration::~NavigatorVibration() | 42 NavigatorVibration::~NavigatorVibration() |
39 { | 43 { |
44 if (m_isVibrating) | |
45 cancelVibration(); | |
40 } | 46 } |
41 | 47 |
42 void NavigatorVibration::vibrate(Navigator* navigator, unsigned time, ExceptionC ode& ec) | 48 bool NavigatorVibration::vibrate(const VibrationPattern& pattern) |
49 { | |
50 size_t length = pattern.size(); | |
51 | |
52 // If the pattern is too long then abort. | |
53 if (length > kVibrationPatternLengthMax) | |
54 return false; | |
55 | |
56 // If any pattern entry is too long then abort. | |
57 for (size_t i = 0; i < length; ++i) { | |
58 if (pattern[i] > kVibrationDurationMax) | |
59 return false; | |
60 } | |
61 | |
62 m_pattern = pattern; | |
63 | |
64 // If the last item in the pattern is a pause then discard it. | |
65 if (length && !(length % 2)) | |
66 m_pattern.removeLast(); | |
67 | |
68 if (m_isVibrating) | |
69 cancelVibration(); | |
70 | |
71 if (m_timerStart.isActive()) | |
72 m_timerStart.stop(); | |
73 | |
74 if (!m_pattern.size()) | |
75 return true; | |
76 | |
77 if (m_pattern.size() == 1 && !m_pattern[0]) { | |
78 m_pattern.clear(); | |
79 return true; | |
80 } | |
81 | |
82 m_timerStart.startOneShot(0); | |
83 } | |
84 | |
85 void NavigatorVibration::cancelVibration() | |
86 { | |
87 m_pattern.clear(); | |
88 if (m_isVibrating) { | |
89 WebKit::Platform::current()->cancelVibration(); | |
90 m_isVibrating = false; | |
91 m_timerStop.stop(); | |
92 } | |
93 } | |
94 | |
95 void NavigatorVibration::suspendVibration() | |
96 { | |
97 if (!m_isVibrating) | |
98 return; | |
99 | |
100 m_pattern.insert(0, m_timerStop.nextFireInterval()); | |
101 m_timerStop.stop(); | |
102 cancelVibration(); | |
103 } | |
104 | |
105 void NavigatorVibration::resumeVibration() | |
106 { | |
107 m_timerStart.startOneShot(0); | |
abarth-chromium
2013/06/12 20:41:27
Should we ASSERT that m_timerStart isn't active?
Michael van Ouwerkerk
2013/06/13 18:23:17
Done.
| |
108 } | |
109 | |
110 void NavigatorVibration::timerStartFired(Timer<NavigatorVibration>* timer) | |
111 { | |
112 ASSERT_UNUSED(timer, timer == &m_timerStart); | |
113 | |
114 m_timerStart.stop(); | |
abarth-chromium
2013/06/12 20:41:27
Is this necessary if we only use one-shot timers?
Michael van Ouwerkerk
2013/06/13 18:23:17
It doesn't seem to be. I've removed it here and in
| |
115 | |
116 if (m_pattern.size()) { | |
117 m_isVibrating = true; | |
118 WebKit::Platform::current()->vibrate(m_pattern[0]); | |
119 m_timerStop.startOneShot(m_pattern[0] / 1000.0); | |
120 m_pattern.remove(0); | |
121 } | |
122 } | |
123 | |
124 void NavigatorVibration::timerStopFired(Timer<NavigatorVibration>* timer) | |
125 { | |
126 ASSERT_UNUSED(timer, timer == &m_timerStop); | |
127 | |
128 m_timerStop.stop(); | |
129 m_isVibrating = false; | |
130 | |
131 if (m_pattern.size()) { | |
132 m_timerStart.startOneShot(m_pattern[0] / 1000.0); | |
133 m_pattern.remove(0); | |
134 } | |
135 } | |
136 | |
137 bool NavigatorVibration::vibrate(Navigator* navigator, unsigned time) | |
138 { | |
139 VibrationPattern pattern; | |
140 pattern.append(time); | |
141 return NavigatorVibration::vibrate(navigator, pattern); | |
142 } | |
143 | |
144 bool NavigatorVibration::vibrate(Navigator* navigator, const VibrationPattern& p attern) | |
43 { | 145 { |
44 if (!navigator->frame()->page()) | 146 if (!navigator->frame()->page()) |
45 return; | 147 return false; |
46 | 148 |
47 if (navigator->frame()->page()->visibilityState() == PageVisibilityStateHidd en) | 149 if (navigator->frame()->page()->visibilityState() != PageVisibilityStateVisi ble) |
48 return; | 150 return false; |
49 | 151 |
50 if (!Vibration::isActive(navigator->frame()->page())) { | 152 return NavigatorVibration::from(navigator)->vibrate(pattern); |
51 ec = NOT_SUPPORTED_ERR; | |
52 return; | |
53 } | |
54 | |
55 Vibration::from(navigator->frame()->page())->vibrate(time); | |
56 } | 153 } |
57 | 154 |
58 void NavigatorVibration::vibrate(Navigator* navigator, const VibrationPattern& p attern, ExceptionCode& ec) | 155 NavigatorVibration* NavigatorVibration::from(Navigator* navigator) |
59 { | 156 { |
60 if (!navigator->frame()->page()) | 157 if (!Supplement<Navigator>::from(navigator, supplementName())) |
61 return; | 158 Supplement<Navigator>::provideTo(navigator, supplementName(), adoptPtr(n ew NavigatorVibration())); |
159 return static_cast<NavigatorVibration*>(Supplement<Navigator>::from(navigato r, supplementName())); | |
abarth-chromium
2013/06/12 20:41:27
Please use the normal pattern for these "from" fun
Michael van Ouwerkerk
2013/06/13 18:23:17
Done.
| |
160 } | |
62 | 161 |
63 if (navigator->frame()->page()->visibilityState() == PageVisibilityStateHidd en) | 162 const char* NavigatorVibration::supplementName() |
64 return; | 163 { |
65 | 164 return "NavigatorVibration"; |
66 if (!Vibration::isActive(navigator->frame()->page())) { | |
67 ec = NOT_SUPPORTED_ERR; | |
68 return; | |
69 } | |
70 | |
71 Vibration::from(navigator->frame()->page())->vibrate(pattern); | |
72 } | 165 } |
73 | 166 |
74 } // namespace WebCore | 167 } // namespace WebCore |
75 | |
76 #endif // ENABLE(VIBRATION) | |
77 | |
OLD | NEW |