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

Unified Diff: content/browser/vibration/vibration_dispatcher_host.cc

Issue 23496051: Make VibrationMessageFilter available to other platforms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased patch Created 7 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/vibration/vibration_dispatcher_host.cc
diff --git a/content/browser/vibration/vibration_dispatcher_host.cc b/content/browser/vibration/vibration_dispatcher_host.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bac09b799c1448ce5cb41607d553702957e2588d
--- /dev/null
+++ b/content/browser/vibration/vibration_dispatcher_host.cc
@@ -0,0 +1,97 @@
+// Copyright 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_dispatcher_host.h"
+
+#include <algorithm>
+
+#include "base/safe_numerics.h"
+#include "content/common/view_messages.h"
+#include "content/port/browser/vibration_provider.h"
+#include "content/public/browser/content_browser_client.h"
+#include "content/public/common/content_client.h"
+#include "third_party/WebKit/public/platform/WebVibration.h"
+
+#if defined(OS_ANDROID)
+#include "content/browser/vibration/vibration_provider_android.h"
+#endif
+
+namespace content {
+
+class VibrationDispatcherHostImpl
+ : public VibrationDispatcherHost {
+ public:
+ VibrationDispatcherHostImpl();
+
+ // BrowserMessageFilter implementation.
+ virtual bool OnMessageReceived(const IPC::Message& message,
+ bool* message_was_ok) OVERRIDE;
+
+ void OnVibrate(int64 milliseconds);
+ void OnCancelVibration();
+
+ private:
+ virtual ~VibrationDispatcherHostImpl();
+
+ VibrationProvider* provider_;
+ DISALLOW_COPY_AND_ASSIGN(VibrationDispatcherHostImpl);
+};
+
+// Minimum duration of a vibration is 1 millisecond.
+const int64 kMinimumVibrationDurationMs = 1;
+
+VibrationDispatcherHostImpl::VibrationDispatcherHostImpl() {
+ provider_ = GetContentClient()->browser()->OverrideVibrationProvider();
+#if defined(OS_ANDROID)
+ if (!provider_)
+ provider_ = new VibrationProviderAndroid();
+#endif
+}
+
+VibrationDispatcherHostImpl::~VibrationDispatcherHostImpl() {
+}
+
+bool VibrationDispatcherHostImpl::OnMessageReceived(
+ const IPC::Message& message,
+ bool* message_was_ok) {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP_EX(VibrationDispatcherHostImpl,
+ 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 VibrationDispatcherHostImpl::OnVibrate(int64 milliseconds) {
+ if (!provider_) return;
+
+ // Though the Blink implementation already sanitizes vibration times, don't
+ // trust any values passed from the renderer.
+ milliseconds = std::max(kMinimumVibrationDurationMs, std::min(milliseconds,
+ base::checked_numeric_cast<int64>(WebKit::kVibrationDurationMax)));
+
+ provider_->OnVibrate(milliseconds);
+}
+
+void VibrationDispatcherHostImpl::OnCancelVibration() {
+ if (!provider_) return;
+
+ provider_->OnCancelVibration();
+}
+
+// static
+VibrationDispatcherHost* VibrationDispatcherHost::New() {
+ return new VibrationDispatcherHostImpl();
+}
+
+VibrationDispatcherHost::VibrationDispatcherHost() {
+}
+
+VibrationDispatcherHost::~VibrationDispatcherHost() {
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698