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