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

Side by Side Diff: Source/modules/vibration/NavigatorVibration.cpp

Issue 1042513002: Add the vibrate attribute to the Notification object (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 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
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;
timvolodine 2015/04/16 12:38:01 btw this can probably also go into the anonymous n
Peter Beverloo 2015/04/16 12:46:41 (Constants are file-local by default, no need for
Sanghyun Park 2015/04/16 13:17:30 This constants is used in sanitizedVibrationPatter
33 34
34 NavigatorVibration::NavigatorVibration(Page& page) 35 static NavigatorVibration::VibrationPattern sanitizeVibrationPatternInternal(con st NavigatorVibration::VibrationPattern& pattern)
timvolodine 2015/04/16 12:38:01 no need for 'static' here, and why not put this me
Peter Beverloo 2015/04/16 12:46:41 +1, please use an anonymous namespace for the func
Sanghyun Park 2015/04/16 13:17:30 I just want to avoid using same blink namespace in
timvolodine 2015/04/16 13:17:51 yes, and just to clarify: I meant you can drop sta
35 : PageLifecycleObserver(&page)
36 , m_timerStart(this, &NavigatorVibration::timerStartFired)
37 , m_timerStop(this, &NavigatorVibration::timerStopFired)
38 , m_isVibrating(false)
39 { 36 {
40 } 37 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(); 38 size_t length = sanitized.size();
52 39
53 // If the pattern is too long then truncate it. 40 // If the pattern is too long then truncate it.
54 if (length > kVibrationPatternLengthMax) { 41 if (length > kVibrationPatternLengthMax) {
55 sanitized.shrink(kVibrationPatternLengthMax); 42 sanitized.shrink(kVibrationPatternLengthMax);
56 length = kVibrationPatternLengthMax; 43 length = kVibrationPatternLengthMax;
57 } 44 }
58 45
59 // If any pattern entry is too long then truncate it. 46 // If any pattern entry is too long then truncate it.
60 for (size_t i = 0; i < length; ++i) { 47 for (size_t i = 0; i < length; ++i) {
61 if (sanitized[i] > kVibrationDurationMax) 48 if (sanitized[i] > kVibrationDurationMax)
62 sanitized[i] = kVibrationDurationMax; 49 sanitized[i] = kVibrationDurationMax;
63 } 50 }
64 51
65 // If the last item in the pattern is a pause then discard it. 52 // If the last item in the pattern is a pause then discard it.
66 if (length && !(length % 2)) 53 if (length && !(length % 2))
67 sanitized.removeLast(); 54 sanitized.removeLast();
68 55
56 return sanitized;
57 }
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
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
OLDNEW
« no previous file with comments | « Source/modules/vibration/NavigatorVibration.h ('k') | public/platform/modules/notifications/WebNotificationData.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698