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 // Cancelling clears the pattern so do this before setting the new pattern. |
| 63 if (m_isVibrating) |
| 64 cancelVibration(); |
| 65 |
| 66 // If the last item in the pattern is a pause then discard it. |
| 67 if (length && !(length % 2)) { |
| 68 VibrationPattern tempPattern = pattern; |
| 69 tempPattern.removeLast(); |
| 70 m_pattern = tempPattern; |
| 71 } else { |
| 72 m_pattern = pattern; |
| 73 } |
| 74 |
| 75 if (m_timerStart.isActive()) |
| 76 m_timerStart.stop(); |
| 77 |
| 78 if (!m_pattern.size()) |
| 79 return true; |
| 80 |
| 81 if (m_pattern.size() == 1 && !m_pattern[0]) { |
| 82 m_pattern.clear(); |
| 83 return true; |
| 84 } |
| 85 |
| 86 m_timerStart.startOneShot(0); |
| 87 return true; |
| 88 } |
| 89 |
| 90 void NavigatorVibration::cancelVibration() |
| 91 { |
| 92 m_pattern.clear(); |
| 93 if (m_isVibrating) { |
| 94 WebKit::Platform::current()->cancelVibration(); |
| 95 m_isVibrating = false; |
| 96 m_timerStop.stop(); |
| 97 } |
| 98 } |
| 99 |
| 100 void NavigatorVibration::suspendVibration() |
| 101 { |
| 102 if (!m_isVibrating) |
| 103 return; |
| 104 |
| 105 m_pattern.insert(0, m_timerStop.nextFireInterval()); |
| 106 m_timerStop.stop(); |
| 107 cancelVibration(); |
| 108 } |
| 109 |
| 110 void NavigatorVibration::resumeVibration() |
| 111 { |
| 112 ASSERT(!m_timerStart.isActive()); |
| 113 |
| 114 m_timerStart.startOneShot(0); |
| 115 } |
| 116 |
| 117 void NavigatorVibration::timerStartFired(Timer<NavigatorVibration>* timer) |
| 118 { |
| 119 ASSERT_UNUSED(timer, timer == &m_timerStart); |
| 120 |
| 121 if (m_pattern.size()) { |
| 122 m_isVibrating = true; |
| 123 WebKit::Platform::current()->vibrate(m_pattern[0]); |
| 124 m_timerStop.startOneShot(m_pattern[0] / 1000.0); |
| 125 m_pattern.remove(0); |
| 126 } |
| 127 } |
| 128 |
| 129 void NavigatorVibration::timerStopFired(Timer<NavigatorVibration>* timer) |
| 130 { |
| 131 ASSERT_UNUSED(timer, timer == &m_timerStop); |
| 132 |
| 133 m_isVibrating = false; |
| 134 |
| 135 if (m_pattern.size()) { |
| 136 m_timerStart.startOneShot(m_pattern[0] / 1000.0); |
| 137 m_pattern.remove(0); |
| 138 } |
| 139 } |
| 140 |
| 141 bool NavigatorVibration::vibrate(Navigator* navigator, unsigned time) |
| 142 { |
| 143 VibrationPattern pattern; |
| 144 pattern.append(time); |
| 145 return NavigatorVibration::vibrate(navigator, pattern); |
| 146 } |
| 147 |
| 148 bool NavigatorVibration::vibrate(Navigator* navigator, const VibrationPattern& p
attern) |
43 { | 149 { |
44 if (!navigator->frame()->page()) | 150 if (!navigator->frame()->page()) |
45 return; | 151 return false; |
46 | 152 |
47 if (navigator->frame()->page()->visibilityState() == PageVisibilityStateHidd
en) | 153 if (navigator->frame()->page()->visibilityState() != PageVisibilityStateVisi
ble) |
48 return; | 154 return false; |
49 | 155 |
50 if (!Vibration::isActive(navigator->frame()->page())) { | 156 return NavigatorVibration::from(navigator)->vibrate(pattern); |
51 ec = NOT_SUPPORTED_ERR; | |
52 return; | |
53 } | |
54 | |
55 Vibration::from(navigator->frame()->page())->vibrate(time); | |
56 } | 157 } |
57 | 158 |
58 void NavigatorVibration::vibrate(Navigator* navigator, const VibrationPattern& p
attern, ExceptionCode& ec) | 159 NavigatorVibration* NavigatorVibration::from(Navigator* navigator) |
59 { | 160 { |
60 if (!navigator->frame()->page()) | 161 NavigatorVibration* navigatorVibration = static_cast<NavigatorVibration*>(Su
pplement<Navigator>::from(navigator, supplementName())); |
61 return; | 162 if (!navigatorVibration) { |
| 163 navigatorVibration = new NavigatorVibration(); |
| 164 Supplement<Navigator>::provideTo(navigator, supplementName(), adoptPtr(n
avigatorVibration)); |
| 165 } |
| 166 return navigatorVibration; |
| 167 } |
62 | 168 |
63 if (navigator->frame()->page()->visibilityState() == PageVisibilityStateHidd
en) | 169 const char* NavigatorVibration::supplementName() |
64 return; | 170 { |
65 | 171 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 } | 172 } |
73 | 173 |
74 } // namespace WebCore | 174 } // namespace WebCore |
75 | |
76 #endif // ENABLE(VIBRATION) | |
77 | |
OLD | NEW |