OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/android/record_user_action.h" | 5 #include "base/android/record_user_action.h" |
6 | 6 |
7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/callback.h" |
8 #include "base/metrics/user_metrics.h" | 10 #include "base/metrics/user_metrics.h" |
9 #include "jni/RecordUserAction_jni.h" | 11 #include "jni/RecordUserAction_jni.h" |
10 | 12 |
| 13 namespace { |
| 14 |
| 15 struct ActionCallbackWrapper { |
| 16 base::ActionCallback action_callback; |
| 17 }; |
| 18 |
| 19 } // namespace |
| 20 |
11 namespace base { | 21 namespace base { |
12 namespace android { | 22 namespace android { |
13 | 23 |
14 static void RecordUserAction(JNIEnv* env, | 24 static void RecordUserAction(JNIEnv* env, |
15 const JavaParamRef<jclass>& clazz, | 25 const JavaParamRef<jclass>& clazz, |
16 const JavaParamRef<jstring>& j_action) { | 26 const JavaParamRef<jstring>& j_action) { |
17 RecordComputedAction(ConvertJavaStringToUTF8(env, j_action)); | 27 RecordComputedAction(ConvertJavaStringToUTF8(env, j_action)); |
18 } | 28 } |
19 | 29 |
| 30 static void OnActionRecorded(const JavaRef<jobject>& callback, |
| 31 const std::string& action) { |
| 32 JNIEnv* env = AttachCurrentThread(); |
| 33 Java_UserActionCallback_onActionRecorded( |
| 34 env, callback, ConvertUTF8ToJavaString(env, action)); |
| 35 } |
| 36 |
| 37 static jlong AddActionCallbackForTesting( |
| 38 JNIEnv* env, |
| 39 const JavaParamRef<jclass>& clazz, |
| 40 const JavaParamRef<jobject>& callback) { |
| 41 // Create a wrapper for the ActionCallback, so it can life on the heap until |
| 42 // RemoveActionCallbackForTesting() is called. |
| 43 auto* wrapper = new ActionCallbackWrapper{base::Bind( |
| 44 &OnActionRecorded, ScopedJavaGlobalRef<jobject>(env, callback))}; |
| 45 base::AddActionCallback(wrapper->action_callback); |
| 46 return reinterpret_cast<intptr_t>(wrapper); |
| 47 } |
| 48 |
| 49 static void RemoveActionCallbackForTesting(JNIEnv* env, |
| 50 const JavaParamRef<jclass>& clazz, |
| 51 jlong callback_id) { |
| 52 DCHECK(callback_id); |
| 53 auto* wrapper = reinterpret_cast<ActionCallbackWrapper*>(callback_id); |
| 54 base::RemoveActionCallback(wrapper->action_callback); |
| 55 delete wrapper; |
| 56 } |
| 57 |
20 // Register native methods | 58 // Register native methods |
21 bool RegisterRecordUserAction(JNIEnv* env) { | 59 bool RegisterRecordUserAction(JNIEnv* env) { |
22 return RegisterNativesImpl(env); | 60 return RegisterNativesImpl(env); |
23 } | 61 } |
24 | 62 |
25 } // namespace android | 63 } // namespace android |
26 } // namespace base | 64 } // namespace base |
OLD | NEW |