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..7b327a0b4a1126ae8d5c78101df62f4e17f835c5 |
--- /dev/null |
+++ b/content/browser/vibration/vibration_message_filter.cc |
@@ -0,0 +1,60 @@ |
+// 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 int32 kMaximumVibrationDurationMs = 10000; |
+ |
+VibrationMessageFilter::VibrationMessageFilter() { |
+} |
+ |
+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(int32 milliseconds) { |
+ if (!vibration_service_.get()) { |
+ vibration_service_.reset(VibrationService::Create().release()); |
+ } |
+ if (vibration_service_.get()) { |
+ // Though the Blink implementation already sanitizes vibration times, don't |
+ // trust any values passed from the renderer. |
+ milliseconds = |
+ std::max(1, std::min(milliseconds, kMaximumVibrationDurationMs)); |
+ vibration_service_->Vibrate(milliseconds); |
+ } |
+} |
+ |
+void VibrationMessageFilter::OnCancelVibration() { |
+ if (!vibration_service_.get()) { |
+ vibration_service_.reset(VibrationService::Create().release()); |
+ } |
+ if (vibration_service_.get()) |
+ vibration_service_->CancelVibration(); |
+} |
+ |
+} // namespace content |