Index: content/browser/vibration/vibration_message_filter.cc |
diff --git a/content/browser/vibration/vibration_message_filter.cc b/content/browser/vibration/vibration_message_filter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c7d8698e90317785b184417359948c9ee9655008 |
--- /dev/null |
+++ b/content/browser/vibration/vibration_message_filter.cc |
@@ -0,0 +1,56 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/vibration/vibration_message_filter.h" |
+ |
+#include <algorithm> |
+ |
+#include "content/common/view_messages.h" |
+ |
+namespace content { |
+ |
+// Maximum duration of a vibration is 10 seconds. |
+const unsigned kMaximumVibrationDurationMs = 10000; |
bulach
2013/06/20 14:21:04
nit: int
Michael van Ouwerkerk
2013/06/20 17:34:09
Why int? Did you mean 'unsigned int'? The vibratio
bulach
2013/06/20 18:11:47
sorry, I should've been clearer: "unsigned int" ra
Michael van Ouwerkerk
2013/06/21 09:53:38
Done in the whole patch.
|
+ |
+VibrationMessageFilter::VibrationMessageFilter( |
+ VibrationService* vibration_service) { |
+ // TODO(mvanouwerkerk): Consider instantiating this lazily - crbug.com/250810 |
+ vibration_service_.reset(vibration_service); |
+} |
+ |
+VibrationMessageFilter::~VibrationMessageFilter() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ if (vibration_service_.get()) |
+ vibration_service_->CancelVibration(); |
+} |
+ |
+bool VibrationMessageFilter::OnMessageReceived( |
+ const IPC::Message& message, |
+ bool* message_was_ok) { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP_EX(VibrationMessageFilter, |
+ message, |
+ *message_was_ok) |
+ IPC_MESSAGE_HANDLER(ViewHostMsg_Vibrate, OnVibrate) |
+ IPC_MESSAGE_HANDLER(ViewHostMsg_CancelVibration, OnCancelVibration) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP_EX() |
+ return handled; |
+} |
+ |
+void VibrationMessageFilter::OnVibrate(unsigned milliseconds) { |
+ // Though the Blink implementation rejects vibration times that are too long, |
+ // don't trust the values passed from the renderer. |
+ if (vibration_service_.get()) { |
+ vibration_service_->Vibrate( |
+ std::min(milliseconds, kMaximumVibrationDurationMs)); |
+ } |
+} |
+ |
+void VibrationMessageFilter::OnCancelVibration() { |
+ if (vibration_service_.get()) |
+ vibration_service_->CancelVibration(); |
+} |
+ |
+} // namespace content |