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..a315e589c402253ba7a1bd6c56073880975a4e6b |
--- /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 int kMaximumVibrationDurationMs = 10000; |
+ |
+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 int milliseconds) { |
palmer
2013/06/21 18:17:40
int64_t
Michael van Ouwerkerk
2013/06/24 13:46:57
Done.
|
+ if (!vibration_service_.get()) { |
+ vibration_service_.reset(VibrationService::Create().release()); |
+ } |
+ if (vibration_service_.get()) { |
+ // Though the Blink implementation rejects vibration times that are too |
+ // long, don't trust the values passed from the renderer. |
+ milliseconds = std::min(milliseconds, kMaximumVibrationDurationMs); |
palmer
2013/06/21 18:17:40
You'll want to ensure milliseconds > 0 as well, si
Michael van Ouwerkerk
2013/06/24 13:46:57
Done.
|
+ 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 |