| 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 namespace blink { |
| 30 | 31 |
| 31 // Maximum number of entries in a vibration pattern. | 32 // Maximum number of entries in a vibration pattern. |
| 32 const unsigned kVibrationPatternLengthMax = 99; | 33 const unsigned kVibrationPatternLengthMax = 99; |
| 33 | 34 |
| 34 NavigatorVibration::NavigatorVibration(Page& page) | 35 NavigatorVibration::NavigatorVibration(Page& page) |
| 35 : PageLifecycleObserver(&page) | 36 : PageLifecycleObserver(&page) |
| 36 , m_timerStart(this, &NavigatorVibration::timerStartFired) | 37 , m_timerStart(this, &NavigatorVibration::timerStartFired) |
| 37 , m_timerStop(this, &NavigatorVibration::timerStopFired) | 38 , m_timerStop(this, &NavigatorVibration::timerStopFired) |
| 38 , m_isVibrating(false) | 39 , m_isVibrating(false) |
| 39 { | 40 { |
| 40 } | 41 } |
| 41 | 42 |
| 42 NavigatorVibration::~NavigatorVibration() | 43 NavigatorVibration::~NavigatorVibration() |
| 43 { | 44 { |
| 44 if (m_isVibrating) | 45 if (m_isVibrating) |
| 45 cancelVibration(); | 46 cancelVibration(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 bool NavigatorVibration::vibrate(const VibrationPattern& pattern) | 49 bool NavigatorVibration::vibrate(const VibrationPattern& pattern) |
| 49 { | 50 { |
| 50 VibrationPattern sanitized = pattern; | |
| 51 size_t length = sanitized.size(); | |
| 52 | |
| 53 // If the pattern is too long then truncate it. | |
| 54 if (length > kVibrationPatternLengthMax) { | |
| 55 sanitized.shrink(kVibrationPatternLengthMax); | |
| 56 length = kVibrationPatternLengthMax; | |
| 57 } | |
| 58 | |
| 59 // If any pattern entry is too long then truncate it. | |
| 60 for (size_t i = 0; i < length; ++i) { | |
| 61 if (sanitized[i] > kVibrationDurationMax) | |
| 62 sanitized[i] = kVibrationDurationMax; | |
| 63 } | |
| 64 | |
| 65 // If the last item in the pattern is a pause then discard it. | |
| 66 if (length && !(length % 2)) | |
| 67 sanitized.removeLast(); | |
| 68 | |
| 69 // Cancelling clears the stored pattern so do it before setting the new one. | 51 // Cancelling clears the stored pattern so do it before setting the new one. |
| 70 if (m_isVibrating) | 52 if (m_isVibrating) |
| 71 cancelVibration(); | 53 cancelVibration(); |
| 72 | 54 |
| 73 m_pattern = sanitized; | 55 m_pattern = sanitizeVibrationPattern(pattern); |
| 74 | 56 |
| 75 if (m_timerStart.isActive()) | 57 if (m_timerStart.isActive()) |
| 76 m_timerStart.stop(); | 58 m_timerStart.stop(); |
| 77 | 59 |
| 78 if (!m_pattern.size()) | 60 if (!m_pattern.size()) |
| 79 return true; | 61 return true; |
| 80 | 62 |
| 81 if (m_pattern.size() == 1 && !m_pattern[0]) { | 63 if (m_pattern.size() == 1 && !m_pattern[0]) { |
| 82 m_pattern.clear(); | 64 m_pattern.clear(); |
| 83 return true; | 65 return true; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 NavigatorVibration& NavigatorVibration::from(Page& page) | 143 NavigatorVibration& NavigatorVibration::from(Page& page) |
| 162 { | 144 { |
| 163 NavigatorVibration* navigatorVibration = static_cast<NavigatorVibration*>(Wi
llBeHeapSupplement<Page>::from(page, supplementName())); | 145 NavigatorVibration* navigatorVibration = static_cast<NavigatorVibration*>(Wi
llBeHeapSupplement<Page>::from(page, supplementName())); |
| 164 if (!navigatorVibration) { | 146 if (!navigatorVibration) { |
| 165 navigatorVibration = new NavigatorVibration(page); | 147 navigatorVibration = new NavigatorVibration(page); |
| 166 WillBeHeapSupplement<Page>::provideTo(page, supplementName(), adoptPtrWi
llBeNoop(navigatorVibration)); | 148 WillBeHeapSupplement<Page>::provideTo(page, supplementName(), adoptPtrWi
llBeNoop(navigatorVibration)); |
| 167 } | 149 } |
| 168 return *navigatorVibration; | 150 return *navigatorVibration; |
| 169 } | 151 } |
| 170 | 152 |
| 153 NavigatorVibration::VibrationPattern NavigatorVibration::sanitizeVibrationPatter
n(const UnsignedLongOrUnsignedLongSequence& pattern) |
| 154 { |
| 155 VibrationPattern sanitized; |
| 156 |
| 157 if (pattern.isUnsignedLong()) |
| 158 sanitized.append(pattern.getAsUnsignedLong()); |
| 159 else if (pattern.isUnsignedLongSequence()) |
| 160 sanitized = pattern.getAsUnsignedLongSequence(); |
| 161 |
| 162 return sanitizeVibrationPattern(sanitized); |
| 163 } |
| 164 |
| 165 NavigatorVibration::VibrationPattern NavigatorVibration::sanitizeVibrationPatter
n(const VibrationPattern& pattern) |
| 166 { |
| 167 VibrationPattern sanitized = pattern; |
| 168 size_t length = sanitized.size(); |
| 169 |
| 170 // If the pattern is too long then truncate it. |
| 171 if (length > kVibrationPatternLengthMax) { |
| 172 sanitized.shrink(kVibrationPatternLengthMax); |
| 173 length = kVibrationPatternLengthMax; |
| 174 } |
| 175 |
| 176 // If any pattern entry is too long then truncate it. |
| 177 for (size_t i = 0; i < length; ++i) { |
| 178 if (sanitized[i] > kVibrationDurationMax) |
| 179 sanitized[i] = kVibrationDurationMax; |
| 180 } |
| 181 |
| 182 // If the last item in the pattern is a pause then discard it. |
| 183 if (length && !(length % 2)) |
| 184 sanitized.removeLast(); |
| 185 |
| 186 return sanitized; |
| 187 } |
| 188 |
| 171 const char* NavigatorVibration::supplementName() | 189 const char* NavigatorVibration::supplementName() |
| 172 { | 190 { |
| 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 |