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..8e2b6c9e79d8502db282f5e3b1f45a4c212f65eb |
| --- /dev/null |
| +++ b/content/browser/vibration/vibration_message_filter.cc |
| @@ -0,0 +1,82 @@ |
| +// 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 "base/safe_numerics.h" |
| +#include "content/common/view_messages.h" |
| +#include "third_party/WebKit/public/platform/WebVibration.h" |
| + |
| +#if defined(OS_ANDROID) |
| +#include "jni/VibrationMessageFilter_jni.h" |
| + |
| +using base::android::AttachCurrentThread; |
| +#endif |
| + |
| +namespace content { |
| + |
| +// Minimum duration of a vibration is 1 millisecond. |
| +const int64 kMinimumVibrationDurationMs = 1; |
|
jam
2013/07/12 00:12:04
should this go with the max value in the webkit he
Michael van Ouwerkerk
2013/07/12 10:49:14
I think not for the moment, as we're using unsigne
|
| + |
| +VibrationMessageFilter::VibrationMessageFilter() { |
| +} |
| + |
| +VibrationMessageFilter::~VibrationMessageFilter() { |
| +#if defined(OS_ANDROID) |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + OnCancelVibration(); |
| +#endif |
| +} |
| + |
| +#if defined(OS_ANDROID) |
| +// static |
| +bool VibrationMessageFilter::Register(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| +#endif |
| + |
| +bool VibrationMessageFilter::OnMessageReceived( |
| + const IPC::Message& message, |
| + bool* message_was_ok) { |
| + bool handled = true; |
| +#if defined(OS_ANDROID) |
| + 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() |
| +#endif |
| + return handled; |
| +} |
| + |
| +#if defined(OS_ANDROID) |
| +void VibrationMessageFilter::OnVibrate(int64 milliseconds) { |
| + // 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))); |
| + |
| + if (j_vibration_message_filter_.is_null()) { |
| + j_vibration_message_filter_.Reset( |
| + Java_VibrationMessageFilter_create( |
| + AttachCurrentThread(), |
| + base::android::GetApplicationContext())); |
| + } |
| + Java_VibrationMessageFilter_vibrate(AttachCurrentThread(), |
| + j_vibration_message_filter_.obj(), |
| + milliseconds); |
| +} |
| + |
| +void VibrationMessageFilter::OnCancelVibration() { |
| + Java_VibrationMessageFilter_cancelVibration(AttachCurrentThread(), |
| + j_vibration_message_filter_.obj()); |
| +} |
| +#endif |
| + |
| +} // namespace content |