OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/android/vibration_message_filter.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/safe_numerics.h" |
| 10 #include "content/common/view_messages.h" |
| 11 #include "jni/VibrationMessageFilter_jni.h" |
| 12 #include "third_party/WebKit/public/platform/WebVibration.h" |
| 13 |
| 14 using base::android::AttachCurrentThread; |
| 15 |
| 16 namespace content { |
| 17 |
| 18 // Minimum duration of a vibration is 1 millisecond. |
| 19 const int64 kMinimumVibrationDurationMs = 1; |
| 20 |
| 21 VibrationMessageFilter::VibrationMessageFilter() { |
| 22 } |
| 23 |
| 24 VibrationMessageFilter::~VibrationMessageFilter() { |
| 25 } |
| 26 |
| 27 // static |
| 28 bool VibrationMessageFilter::Register(JNIEnv* env) { |
| 29 return RegisterNativesImpl(env); |
| 30 } |
| 31 |
| 32 bool VibrationMessageFilter::OnMessageReceived( |
| 33 const IPC::Message& message, |
| 34 bool* message_was_ok) { |
| 35 bool handled = true; |
| 36 IPC_BEGIN_MESSAGE_MAP_EX(VibrationMessageFilter, |
| 37 message, |
| 38 *message_was_ok) |
| 39 IPC_MESSAGE_HANDLER(ViewHostMsg_Vibrate, OnVibrate) |
| 40 IPC_MESSAGE_HANDLER(ViewHostMsg_CancelVibration, OnCancelVibration) |
| 41 IPC_MESSAGE_UNHANDLED(handled = false) |
| 42 IPC_END_MESSAGE_MAP_EX() |
| 43 return handled; |
| 44 } |
| 45 |
| 46 void VibrationMessageFilter::OnVibrate(int64 milliseconds) { |
| 47 // Though the Blink implementation already sanitizes vibration times, don't |
| 48 // trust any values passed from the renderer. |
| 49 milliseconds = std::max(kMinimumVibrationDurationMs, |
| 50 std::min(milliseconds, |
| 51 base::checked_numeric_cast<int64>(WebKit::kVibrationDurationMax))); |
| 52 |
| 53 if (j_vibration_message_filter_.is_null()) { |
| 54 j_vibration_message_filter_.Reset( |
| 55 Java_VibrationMessageFilter_create( |
| 56 AttachCurrentThread(), |
| 57 base::android::GetApplicationContext())); |
| 58 } |
| 59 Java_VibrationMessageFilter_vibrate(AttachCurrentThread(), |
| 60 j_vibration_message_filter_.obj(), |
| 61 milliseconds); |
| 62 } |
| 63 |
| 64 void VibrationMessageFilter::OnCancelVibration() { |
| 65 Java_VibrationMessageFilter_cancelVibration(AttachCurrentThread(), |
| 66 j_vibration_message_filter_.obj()); |
| 67 } |
| 68 |
| 69 } // namespace content |
OLD | NEW |