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..02fc7a57072ddd462d009bdbdf6b3cf37f44ea50 |
| --- /dev/null |
| +++ b/content/browser/vibration/vibration_message_filter.cc |
| @@ -0,0 +1,87 @@ |
| +// 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" |
| + |
| +#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; |
| + |
| +// Maximum duration of a vibration is 10 seconds. |
| +const int64 kMaximumVibrationDurationMs = 10000; |
| + |
| + |
| +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, kMaximumVibrationDurationMs)); |
| + initializeJava(); |
| + Java_VibrationMessageFilter_vibrate(AttachCurrentThread(), |
| + j_vibration_message_filter_.obj(), |
| + milliseconds); |
| +} |
| + |
| +void VibrationMessageFilter::OnCancelVibration() { |
| + initializeJava(); |
| + Java_VibrationMessageFilter_cancelVibration(AttachCurrentThread(), |
| + j_vibration_message_filter_.obj()); |
| +} |
| + |
| +void VibrationMessageFilter::initializeJava() { |
|
jam
2013/07/03 16:51:55
why do you need this method? i.e. won't OnVibrate
Michael van Ouwerkerk
2013/07/10 15:24:26
Done. It does look like it's safe to assume cancel
|
| + if (j_vibration_message_filter_.is_null()) { |
| + j_vibration_message_filter_.Reset( |
| + Java_VibrationMessageFilter_create( |
| + AttachCurrentThread(), |
| + base::android::GetApplicationContext())); |
| + } |
| +} |
| +#endif |
| + |
| +} // namespace content |