Chromium Code Reviews| 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..b4318b95aa4ba5de38c19815c65e0c5da11a2478 |
| --- /dev/null |
| +++ b/content/browser/vibration/vibration_message_filter.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_message_filter.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 VibrationMessageFilterImpl |
|
Michael van Ouwerkerk
2013/11/08 14:41:07
It looks like you don't need the a separate Impl c
ostap
2013/11/08 20:33:41
Done.
|
| + : public VibrationMessageFilter { |
| + public: |
| + VibrationMessageFilterImpl(); |
| + |
| + // BrowserMessageFilter implementation. |
| + virtual bool OnMessageReceived(const IPC::Message& message, |
| + bool* message_was_ok) OVERRIDE; |
| + |
| + void OnVibrate(int64 milliseconds); |
| + void OnCancelVibration(); |
| + |
| + private: |
| + virtual ~VibrationMessageFilterImpl(); |
| + |
| + VibrationProvider* provider_; |
| + DISALLOW_COPY_AND_ASSIGN(VibrationMessageFilterImpl); |
| +}; |
| + |
| +// Minimum duration of a vibration is 1 millisecond. |
| +const int64 kMinimumVibrationDurationMs = 1; |
| + |
| +VibrationMessageFilterImpl::VibrationMessageFilterImpl() { |
| + provider_ = GetContentClient()->browser()->OverrideVibrationProvider(); |
| +#if defined(OS_ANDROID) |
| + if (!provider_) |
| + provider_ = new VibrationProviderAndroid(); |
| +#endif |
| +} |
| + |
| +VibrationMessageFilterImpl::~VibrationMessageFilterImpl() { |
| +} |
| + |
| +bool VibrationMessageFilterImpl::OnMessageReceived( |
| + const IPC::Message& message, |
| + bool* message_was_ok) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP_EX(VibrationMessageFilterImpl, |
| + 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 VibrationMessageFilterImpl::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 VibrationMessageFilterImpl::OnCancelVibration() { |
| + if (!provider_) return; |
| + |
| + provider_->OnCancelVibration(); |
| +} |
| + |
| +// static |
| +VibrationMessageFilter* VibrationMessageFilter::New() { |
|
Michael van Ouwerkerk
2013/11/08 14:41:07
It looks like you don't need this method, you can
|
| + return new VibrationMessageFilterImpl(); |
| +} |
| + |
| +VibrationMessageFilter::VibrationMessageFilter() { |
| +} |
| + |
| +VibrationMessageFilter::~VibrationMessageFilter() { |
| +} |
| + |
| +} // namespace content |