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 #include "bindings/modules/v8/UnionTypesModules.h" |
23 #include "core/frame/LocalFrame.h" | 24 #include "core/frame/LocalFrame.h" |
24 #include "core/frame/Navigator.h" | 25 #include "core/frame/Navigator.h" |
25 #include "core/page/PageVisibilityState.h" | 26 #include "core/page/PageVisibilityState.h" |
26 #include "public/platform/Platform.h" | 27 #include "public/platform/Platform.h" |
27 #include "public/platform/WebVibration.h" | 28 #include "public/platform/WebVibration.h" |
28 | 29 |
29 namespace blink { | |
30 | |
31 // Maximum number of entries in a vibration pattern. | 30 // Maximum number of entries in a vibration pattern. |
32 const unsigned kVibrationPatternLengthMax = 99; | 31 const unsigned kVibrationPatternLengthMax = 99; |
33 | 32 |
34 NavigatorVibration::NavigatorVibration(Page& page) | 33 blink::NavigatorVibration::VibrationPattern sanitizeVibrationPatternInternal(con
st blink::NavigatorVibration::VibrationPattern& pattern) |
35 : PageLifecycleObserver(&page) | |
36 , m_timerStart(this, &NavigatorVibration::timerStartFired) | |
37 , m_timerStop(this, &NavigatorVibration::timerStopFired) | |
38 , m_isVibrating(false) | |
39 { | 34 { |
40 } | 35 blink::NavigatorVibration::VibrationPattern sanitized = pattern; |
41 | |
42 NavigatorVibration::~NavigatorVibration() | |
43 { | |
44 if (m_isVibrating) | |
45 cancelVibration(); | |
46 } | |
47 | |
48 bool NavigatorVibration::vibrate(const VibrationPattern& pattern) | |
49 { | |
50 VibrationPattern sanitized = pattern; | |
51 size_t length = sanitized.size(); | 36 size_t length = sanitized.size(); |
52 | 37 |
53 // If the pattern is too long then truncate it. | 38 // If the pattern is too long then truncate it. |
54 if (length > kVibrationPatternLengthMax) { | 39 if (length > kVibrationPatternLengthMax) { |
55 sanitized.shrink(kVibrationPatternLengthMax); | 40 sanitized.shrink(kVibrationPatternLengthMax); |
56 length = kVibrationPatternLengthMax; | 41 length = kVibrationPatternLengthMax; |
57 } | 42 } |
58 | 43 |
59 // If any pattern entry is too long then truncate it. | 44 // If any pattern entry is too long then truncate it. |
60 for (size_t i = 0; i < length; ++i) { | 45 for (size_t i = 0; i < length; ++i) { |
61 if (sanitized[i] > kVibrationDurationMax) | 46 if (sanitized[i] > blink::kVibrationDurationMax) |
62 sanitized[i] = kVibrationDurationMax; | 47 sanitized[i] = blink::kVibrationDurationMax; |
63 } | 48 } |
64 | 49 |
65 // If the last item in the pattern is a pause then discard it. | 50 // If the last item in the pattern is a pause then discard it. |
66 if (length && !(length % 2)) | 51 if (length && !(length % 2)) |
67 sanitized.removeLast(); | 52 sanitized.removeLast(); |
68 | 53 |
| 54 return sanitized; |
| 55 } |
| 56 |
| 57 namespace blink { |
| 58 |
| 59 NavigatorVibration::VibrationPattern NavigatorVibration::sanitizeVibrationPatter
n(const UnsignedLongOrUnsignedLongSequence& pattern) |
| 60 { |
| 61 VibrationPattern sanitized; |
| 62 |
| 63 if (pattern.isUnsignedLong()) |
| 64 sanitized.append(pattern.getAsUnsignedLong()); |
| 65 else if (pattern.isUnsignedLongSequence()) |
| 66 sanitized = pattern.getAsUnsignedLongSequence(); |
| 67 |
| 68 return sanitizeVibrationPatternInternal(sanitized); |
| 69 } |
| 70 |
| 71 NavigatorVibration::NavigatorVibration(Page& page) |
| 72 : PageLifecycleObserver(&page) |
| 73 , m_timerStart(this, &NavigatorVibration::timerStartFired) |
| 74 , m_timerStop(this, &NavigatorVibration::timerStopFired) |
| 75 , m_isVibrating(false) |
| 76 { |
| 77 } |
| 78 |
| 79 NavigatorVibration::~NavigatorVibration() |
| 80 { |
| 81 if (m_isVibrating) |
| 82 cancelVibration(); |
| 83 } |
| 84 |
| 85 bool NavigatorVibration::vibrate(const VibrationPattern& pattern) |
| 86 { |
69 // Cancelling clears the stored pattern so do it before setting the new one. | 87 // Cancelling clears the stored pattern so do it before setting the new one. |
70 if (m_isVibrating) | 88 if (m_isVibrating) |
71 cancelVibration(); | 89 cancelVibration(); |
72 | 90 |
73 m_pattern = sanitized; | 91 m_pattern = sanitizeVibrationPatternInternal(pattern); |
74 | 92 |
75 if (m_timerStart.isActive()) | 93 if (m_timerStart.isActive()) |
76 m_timerStart.stop(); | 94 m_timerStart.stop(); |
77 | 95 |
78 if (!m_pattern.size()) | 96 if (!m_pattern.size()) |
79 return true; | 97 return true; |
80 | 98 |
81 if (m_pattern.size() == 1 && !m_pattern[0]) { | 99 if (m_pattern.size() == 1 && !m_pattern[0]) { |
82 m_pattern.clear(); | 100 m_pattern.clear(); |
83 return true; | 101 return true; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 return "NavigatorVibration"; | 191 return "NavigatorVibration"; |
174 } | 192 } |
175 | 193 |
176 DEFINE_TRACE(NavigatorVibration) | 194 DEFINE_TRACE(NavigatorVibration) |
177 { | 195 { |
178 WillBeHeapSupplement<Page>::trace(visitor); | 196 WillBeHeapSupplement<Page>::trace(visitor); |
179 PageLifecycleObserver::trace(visitor); | 197 PageLifecycleObserver::trace(visitor); |
180 } | 198 } |
181 | 199 |
182 } // namespace blink | 200 } // namespace blink |
OLD | NEW |