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 "core/page/Frame.h" | 23 #include "core/page/Frame.h" |
| 24 #include "core/page/Navigator.h" |
24 #include "core/page/Page.h" | 25 #include "core/page/Page.h" |
| 26 #include "core/page/PageVisibilityState.h" |
25 #include "public/platform/Platform.h" | 27 #include "public/platform/Platform.h" |
26 | 28 |
27 namespace WebCore { | 29 namespace WebCore { |
28 | 30 |
29 // Maximum duration of a vibration is 10 seconds. | 31 // Maximum duration of a vibration is 10 seconds. |
30 const unsigned kVibrationDurationMax = 10000; | 32 const unsigned kVibrationDurationMax = 10000; |
31 | 33 |
32 // Maximum number of entries in a vibration pattern. | 34 // Maximum number of entries in a vibration pattern. |
33 const unsigned kVibrationPatternLengthMax = 99; | 35 const unsigned kVibrationPatternLengthMax = 99; |
34 | 36 |
35 NavigatorVibration::NavigatorVibration() | 37 NavigatorVibration::NavigatorVibration(Page* page) |
36 : m_timerStart(this, &NavigatorVibration::timerStartFired) | 38 : PageLifecycleObserver(page) |
| 39 , m_page(page) |
| 40 , m_timerStart(this, &NavigatorVibration::timerStartFired) |
37 , m_timerStop(this, &NavigatorVibration::timerStopFired) | 41 , m_timerStop(this, &NavigatorVibration::timerStopFired) |
38 , m_isVibrating(false) | 42 , m_isVibrating(false) |
39 { | 43 { |
40 } | 44 } |
41 | 45 |
42 NavigatorVibration::~NavigatorVibration() | 46 NavigatorVibration::~NavigatorVibration() |
43 { | 47 { |
44 if (m_isVibrating) | 48 if (m_isVibrating) |
45 cancelVibration(); | 49 cancelVibration(); |
46 } | 50 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 void NavigatorVibration::cancelVibration() | 94 void NavigatorVibration::cancelVibration() |
91 { | 95 { |
92 m_pattern.clear(); | 96 m_pattern.clear(); |
93 if (m_isVibrating) { | 97 if (m_isVibrating) { |
94 WebKit::Platform::current()->cancelVibration(); | 98 WebKit::Platform::current()->cancelVibration(); |
95 m_isVibrating = false; | 99 m_isVibrating = false; |
96 m_timerStop.stop(); | 100 m_timerStop.stop(); |
97 } | 101 } |
98 } | 102 } |
99 | 103 |
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) | 104 void NavigatorVibration::timerStartFired(Timer<NavigatorVibration>* timer) |
118 { | 105 { |
119 ASSERT_UNUSED(timer, timer == &m_timerStart); | 106 ASSERT_UNUSED(timer, timer == &m_timerStart); |
120 | 107 |
121 if (m_pattern.size()) { | 108 if (m_pattern.size()) { |
122 m_isVibrating = true; | 109 m_isVibrating = true; |
123 WebKit::Platform::current()->vibrate(m_pattern[0]); | 110 WebKit::Platform::current()->vibrate(m_pattern[0]); |
124 m_timerStop.startOneShot(m_pattern[0] / 1000.0); | 111 m_timerStop.startOneShot(m_pattern[0] / 1000.0); |
125 m_pattern.remove(0); | 112 m_pattern.remove(0); |
126 } | 113 } |
127 } | 114 } |
128 | 115 |
129 void NavigatorVibration::timerStopFired(Timer<NavigatorVibration>* timer) | 116 void NavigatorVibration::timerStopFired(Timer<NavigatorVibration>* timer) |
130 { | 117 { |
131 ASSERT_UNUSED(timer, timer == &m_timerStop); | 118 ASSERT_UNUSED(timer, timer == &m_timerStop); |
132 | 119 |
133 m_isVibrating = false; | 120 m_isVibrating = false; |
134 | 121 |
135 if (m_pattern.size()) { | 122 if (m_pattern.size()) { |
136 m_timerStart.startOneShot(m_pattern[0] / 1000.0); | 123 m_timerStart.startOneShot(m_pattern[0] / 1000.0); |
137 m_pattern.remove(0); | 124 m_pattern.remove(0); |
138 } | 125 } |
139 } | 126 } |
140 | 127 |
| 128 void NavigatorVibration::pageVisibilityChanged() |
| 129 { |
| 130 if (m_page->visibilityState() != PageVisibilityStateVisible) |
| 131 cancelVibration(); |
| 132 } |
| 133 |
141 bool NavigatorVibration::vibrate(Navigator* navigator, unsigned time) | 134 bool NavigatorVibration::vibrate(Navigator* navigator, unsigned time) |
142 { | 135 { |
143 VibrationPattern pattern; | 136 VibrationPattern pattern; |
144 pattern.append(time); | 137 pattern.append(time); |
145 return NavigatorVibration::vibrate(navigator, pattern); | 138 return NavigatorVibration::vibrate(navigator, pattern); |
146 } | 139 } |
147 | 140 |
148 bool NavigatorVibration::vibrate(Navigator* navigator, const VibrationPattern& p
attern) | 141 bool NavigatorVibration::vibrate(Navigator* navigator, const VibrationPattern& p
attern) |
149 { | 142 { |
150 if (!navigator->frame()->page()) | 143 Page* page = navigator->frame()->page(); |
| 144 if (!page) |
151 return false; | 145 return false; |
152 | 146 |
153 if (navigator->frame()->page()->visibilityState() != PageVisibilityStateVisi
ble) | 147 if (page->visibilityState() != PageVisibilityStateVisible) |
154 return false; | 148 return false; |
155 | 149 |
156 return NavigatorVibration::from(navigator)->vibrate(pattern); | 150 return NavigatorVibration::from(page)->vibrate(pattern); |
157 } | 151 } |
158 | 152 |
159 NavigatorVibration* NavigatorVibration::from(Navigator* navigator) | 153 NavigatorVibration* NavigatorVibration::from(Page* page) |
160 { | 154 { |
161 NavigatorVibration* navigatorVibration = static_cast<NavigatorVibration*>(Su
pplement<Navigator>::from(navigator, supplementName())); | 155 NavigatorVibration* navigatorVibration = static_cast<NavigatorVibration*>(Su
pplement<Page>::from(page, supplementName())); |
162 if (!navigatorVibration) { | 156 if (!navigatorVibration) { |
163 navigatorVibration = new NavigatorVibration(); | 157 navigatorVibration = new NavigatorVibration(page); |
164 Supplement<Navigator>::provideTo(navigator, supplementName(), adoptPtr(n
avigatorVibration)); | 158 Supplement<Page>::provideTo(page, supplementName(), adoptPtr(navigatorVi
bration)); |
165 } | 159 } |
166 return navigatorVibration; | 160 return navigatorVibration; |
167 } | 161 } |
168 | 162 |
169 const char* NavigatorVibration::supplementName() | 163 const char* NavigatorVibration::supplementName() |
170 { | 164 { |
171 return "NavigatorVibration"; | 165 return "NavigatorVibration"; |
172 } | 166 } |
173 | 167 |
174 } // namespace WebCore | 168 } // namespace WebCore |
OLD | NEW |