Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3099)

Unified Diff: base/android/record_user_action.cc

Issue 2956283002: Expose Add/RemoveActionCallback to java (Closed)
Patch Set: Use assert instead of an exception Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/android/record_user_action.cc
diff --git a/base/android/record_user_action.cc b/base/android/record_user_action.cc
index 1452341d82399d67464ca961f6314527b50ea430..e375f8efb463d2ba19035decf86f1f74546827a4 100644
--- a/base/android/record_user_action.cc
+++ b/base/android/record_user_action.cc
@@ -5,9 +5,19 @@
#include "base/android/record_user_action.h"
#include "base/android/jni_string.h"
+#include "base/bind.h"
+#include "base/callback.h"
#include "base/metrics/user_metrics.h"
#include "jni/RecordUserAction_jni.h"
+namespace {
+
+struct ActionCallbackWrapper {
+ base::ActionCallback action_callback;
+};
+
+} // namespace
+
namespace base {
namespace android {
@@ -17,6 +27,34 @@ static void RecordUserAction(JNIEnv* env,
RecordComputedAction(ConvertJavaStringToUTF8(env, j_action));
}
+static void OnActionRecorded(const JavaRef<jobject>& callback,
+ const std::string& action) {
+ JNIEnv* env = AttachCurrentThread();
+ Java_UserActionCallback_onActionRecorded(
+ env, callback, ConvertUTF8ToJavaString(env, action));
+}
+
+static jlong AddActionCallbackForTesting(
+ JNIEnv* env,
+ const JavaParamRef<jclass>& clazz,
+ const JavaParamRef<jobject>& callback) {
+ // Create a wrapper for the ActionCallback, so it can life on the heap until
+ // RemoveActionCallbackForTesting() is called.
+ auto* wrapper = new ActionCallbackWrapper{base::Bind(
+ &OnActionRecorded, ScopedJavaGlobalRef<jobject>(env, callback))};
+ base::AddActionCallback(wrapper->action_callback);
+ return reinterpret_cast<intptr_t>(wrapper);
+}
+
+static void RemoveActionCallbackForTesting(JNIEnv* env,
+ const JavaParamRef<jclass>& clazz,
+ jlong callback_id) {
+ DCHECK(callback_id);
+ auto* wrapper = reinterpret_cast<ActionCallbackWrapper*>(callback_id);
+ base::RemoveActionCallback(wrapper->action_callback);
+ delete wrapper;
+}
+
// Register native methods
bool RegisterRecordUserAction(JNIEnv* env) {
return RegisterNativesImpl(env);

Powered by Google App Engine
This is Rietveld 408576698