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

Side by Side Diff: third_party/WebKit/Source/modules/vibration/VibrationController.cpp

Issue 2755363002: [DeviceService] Port VibrationManager to be hosted in Device Service (Closed)
Patch Set: Rebase only Created 3 years, 9 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
« no previous file with comments | « third_party/WebKit/Source/modules/vibration/VibrationController.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "modules/vibration/VibrationController.h" 20 #include "modules/vibration/VibrationController.h"
21 21
22 #include "bindings/modules/v8/UnsignedLongOrUnsignedLongSequence.h" 22 #include "bindings/modules/v8/UnsignedLongOrUnsignedLongSequence.h"
23 #include "core/dom/Document.h" 23 #include "core/dom/Document.h"
24 #include "core/dom/TaskRunnerHelper.h" 24 #include "core/dom/TaskRunnerHelper.h"
25 #include "core/frame/Navigator.h" 25 #include "core/frame/Navigator.h"
26 #include "core/page/Page.h" 26 #include "core/page/Page.h"
27 #include "platform/mojo/MojoHelper.h" 27 #include "platform/mojo/MojoHelper.h"
28 #include "public/platform/InterfaceProvider.h" 28 #include "public/platform/Connector.h"
29 #include "public/platform/Platform.h"
30 #include "services/device/public/interfaces/constants.mojom-blink.h"
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 // Maximum duration of a vibration is 10 seconds. 35 // Maximum duration of a vibration is 10 seconds.
34 const unsigned kVibrationDurationMsMax = 10000; 36 const unsigned kVibrationDurationMsMax = 10000;
35 37
36 blink::VibrationController::VibrationPattern sanitizeVibrationPatternInternal( 38 blink::VibrationController::VibrationPattern sanitizeVibrationPatternInternal(
37 const blink::VibrationController::VibrationPattern& pattern) { 39 const blink::VibrationController::VibrationPattern& pattern) {
38 blink::VibrationController::VibrationPattern sanitized = pattern; 40 blink::VibrationController::VibrationPattern sanitized = pattern;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 VibrationController::VibrationController(Document& document) 78 VibrationController::VibrationController(Document& document)
77 : ContextLifecycleObserver(&document), 79 : ContextLifecycleObserver(&document),
78 PageVisibilityObserver(document.page()), 80 PageVisibilityObserver(document.page()),
79 m_timerDoVibrate( 81 m_timerDoVibrate(
80 TaskRunnerHelper::get(TaskType::MiscPlatformAPI, &document), 82 TaskRunnerHelper::get(TaskType::MiscPlatformAPI, &document),
81 this, 83 this,
82 &VibrationController::doVibrate), 84 &VibrationController::doVibrate),
83 m_isRunning(false), 85 m_isRunning(false),
84 m_isCallingCancel(false), 86 m_isCallingCancel(false),
85 m_isCallingVibrate(false) { 87 m_isCallingVibrate(false) {
86 document.frame()->interfaceProvider()->getInterface( 88 Platform::current()->connector()->bindInterface(
87 mojo::MakeRequest(&m_service)); 89 device::mojom::blink::kServiceName,
90 mojo::MakeRequest(&m_vibrationManager));
88 } 91 }
89 92
90 VibrationController::~VibrationController() {} 93 VibrationController::~VibrationController() {}
91 94
92 bool VibrationController::vibrate(const VibrationPattern& pattern) { 95 bool VibrationController::vibrate(const VibrationPattern& pattern) {
93 // Cancel clears the stored pattern and cancels any ongoing vibration. 96 // Cancel clears the stored pattern and cancels any ongoing vibration.
94 cancel(); 97 cancel();
95 98
96 m_pattern = sanitizeVibrationPatternInternal(pattern); 99 m_pattern = sanitizeVibrationPatternInternal(pattern);
97 100
(...skipping 19 matching lines...) Expand all
117 void VibrationController::doVibrate(TimerBase* timer) { 120 void VibrationController::doVibrate(TimerBase* timer) {
118 DCHECK(timer == &m_timerDoVibrate); 121 DCHECK(timer == &m_timerDoVibrate);
119 122
120 if (m_pattern.isEmpty()) 123 if (m_pattern.isEmpty())
121 m_isRunning = false; 124 m_isRunning = false;
122 125
123 if (!m_isRunning || m_isCallingCancel || m_isCallingVibrate || 126 if (!m_isRunning || m_isCallingCancel || m_isCallingVibrate ||
124 !getExecutionContext() || !page()->isPageVisible()) 127 !getExecutionContext() || !page()->isPageVisible())
125 return; 128 return;
126 129
127 if (m_service) { 130 if (m_vibrationManager) {
128 m_isCallingVibrate = true; 131 m_isCallingVibrate = true;
129 m_service->Vibrate(m_pattern[0], convertToBaseCallback(WTF::bind( 132 m_vibrationManager->Vibrate(
130 &VibrationController::didVibrate, 133 m_pattern[0],
131 wrapPersistent(this)))); 134 convertToBaseCallback(
135 WTF::bind(&VibrationController::didVibrate, wrapPersistent(this))));
132 } 136 }
133 } 137 }
134 138
135 void VibrationController::didVibrate() { 139 void VibrationController::didVibrate() {
136 m_isCallingVibrate = false; 140 m_isCallingVibrate = false;
137 141
138 // If the pattern is empty here, it was probably cleared by a fresh call to 142 // If the pattern is empty here, it was probably cleared by a fresh call to
139 // |vibrate| while the mojo call was in flight. 143 // |vibrate| while the mojo call was in flight.
140 if (m_pattern.isEmpty()) 144 if (m_pattern.isEmpty())
141 return; 145 return;
142 146
143 // Use the current vibration entry of the pattern as the initial interval. 147 // Use the current vibration entry of the pattern as the initial interval.
144 unsigned interval = m_pattern[0]; 148 unsigned interval = m_pattern[0];
145 m_pattern.remove(0); 149 m_pattern.remove(0);
146 150
147 // If there is another entry it is for a pause. 151 // If there is another entry it is for a pause.
148 if (!m_pattern.isEmpty()) { 152 if (!m_pattern.isEmpty()) {
149 interval += m_pattern[0]; 153 interval += m_pattern[0];
150 m_pattern.remove(0); 154 m_pattern.remove(0);
151 } 155 }
152 156
153 m_timerDoVibrate.startOneShot(interval / 1000.0, BLINK_FROM_HERE); 157 m_timerDoVibrate.startOneShot(interval / 1000.0, BLINK_FROM_HERE);
154 } 158 }
155 159
156 void VibrationController::cancel() { 160 void VibrationController::cancel() {
157 m_pattern.clear(); 161 m_pattern.clear();
158 m_timerDoVibrate.stop(); 162 m_timerDoVibrate.stop();
159 163
160 if (m_isRunning && !m_isCallingCancel && m_service) { 164 if (m_isRunning && !m_isCallingCancel && m_vibrationManager) {
161 m_isCallingCancel = true; 165 m_isCallingCancel = true;
162 m_service->Cancel(convertToBaseCallback( 166 m_vibrationManager->Cancel(convertToBaseCallback(
163 WTF::bind(&VibrationController::didCancel, wrapPersistent(this)))); 167 WTF::bind(&VibrationController::didCancel, wrapPersistent(this))));
164 } 168 }
165 169
166 m_isRunning = false; 170 m_isRunning = false;
167 } 171 }
168 172
169 void VibrationController::didCancel() { 173 void VibrationController::didCancel() {
170 m_isCallingCancel = false; 174 m_isCallingCancel = false;
171 175
172 // A new vibration pattern may have been set while the mojo call for 176 // A new vibration pattern may have been set while the mojo call for
173 // |cancel| was in flight, so kick the timer to let |doVibrate| process the 177 // |cancel| was in flight, so kick the timer to let |doVibrate| process the
174 // pattern. 178 // pattern.
175 m_timerDoVibrate.startOneShot(0, BLINK_FROM_HERE); 179 m_timerDoVibrate.startOneShot(0, BLINK_FROM_HERE);
176 } 180 }
177 181
178 void VibrationController::contextDestroyed(ExecutionContext*) { 182 void VibrationController::contextDestroyed(ExecutionContext*) {
179 cancel(); 183 cancel();
180 184
181 // If the document context was destroyed, never call the mojo service again. 185 // If the document context was destroyed, never call the mojo service again.
182 m_service.reset(); 186 m_vibrationManager.reset();
183 } 187 }
184 188
185 void VibrationController::pageVisibilityChanged() { 189 void VibrationController::pageVisibilityChanged() {
186 if (!page()->isPageVisible()) 190 if (!page()->isPageVisible())
187 cancel(); 191 cancel();
188 } 192 }
189 193
190 DEFINE_TRACE(VibrationController) { 194 DEFINE_TRACE(VibrationController) {
191 ContextLifecycleObserver::trace(visitor); 195 ContextLifecycleObserver::trace(visitor);
192 PageVisibilityObserver::trace(visitor); 196 PageVisibilityObserver::trace(visitor);
193 } 197 }
194 198
195 } // namespace blink 199 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/vibration/VibrationController.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698