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/vibration/vibration_message_filter.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/safe_numerics.h" | |
10 #include "content/common/view_messages.h" | |
11 #include "third_party/WebKit/public/platform/WebVibration.h" | |
12 | |
13 #if defined(OS_ANDROID) | |
14 #include "jni/VibrationMessageFilter_jni.h" | |
15 | |
16 using base::android::AttachCurrentThread; | |
17 #endif | |
18 | |
19 namespace content { | |
20 | |
21 // Minimum duration of a vibration is 1 millisecond. | |
22 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
| |
23 | |
24 VibrationMessageFilter::VibrationMessageFilter() { | |
25 } | |
26 | |
27 VibrationMessageFilter::~VibrationMessageFilter() { | |
28 #if defined(OS_ANDROID) | |
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
30 OnCancelVibration(); | |
31 #endif | |
32 } | |
33 | |
34 #if defined(OS_ANDROID) | |
35 // static | |
36 bool VibrationMessageFilter::Register(JNIEnv* env) { | |
37 return RegisterNativesImpl(env); | |
38 } | |
39 #endif | |
40 | |
41 bool VibrationMessageFilter::OnMessageReceived( | |
42 const IPC::Message& message, | |
43 bool* message_was_ok) { | |
44 bool handled = true; | |
45 #if defined(OS_ANDROID) | |
46 IPC_BEGIN_MESSAGE_MAP_EX(VibrationMessageFilter, | |
47 message, | |
48 *message_was_ok) | |
49 IPC_MESSAGE_HANDLER(ViewHostMsg_Vibrate, OnVibrate) | |
50 IPC_MESSAGE_HANDLER(ViewHostMsg_CancelVibration, OnCancelVibration) | |
51 IPC_MESSAGE_UNHANDLED(handled = false) | |
52 IPC_END_MESSAGE_MAP_EX() | |
53 #endif | |
54 return handled; | |
55 } | |
56 | |
57 #if defined(OS_ANDROID) | |
58 void VibrationMessageFilter::OnVibrate(int64 milliseconds) { | |
59 // Though the Blink implementation already sanitizes vibration times, don't | |
60 // trust any values passed from the renderer. | |
61 milliseconds = std::max(kMinimumVibrationDurationMs, | |
62 std::min(milliseconds, | |
63 base::checked_numeric_cast<int64>(WebKit::kVibrationDurationMax))); | |
64 | |
65 if (j_vibration_message_filter_.is_null()) { | |
66 j_vibration_message_filter_.Reset( | |
67 Java_VibrationMessageFilter_create( | |
68 AttachCurrentThread(), | |
69 base::android::GetApplicationContext())); | |
70 } | |
71 Java_VibrationMessageFilter_vibrate(AttachCurrentThread(), | |
72 j_vibration_message_filter_.obj(), | |
73 milliseconds); | |
74 } | |
75 | |
76 void VibrationMessageFilter::OnCancelVibration() { | |
77 Java_VibrationMessageFilter_cancelVibration(AttachCurrentThread(), | |
78 j_vibration_message_filter_.obj()); | |
79 } | |
80 #endif | |
81 | |
82 } // namespace content | |
OLD | NEW |