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