1 // Copyright 2020 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 "weblayer/browser/android/metrics/metrics_test_helper.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/no_destructor.h"
10 #include "content/public/test/test_utils.h"
11 #include "weblayer/browser/profile_impl.h"
12 #include "weblayer/test/weblayer_browsertests_jni/MetricsTestHelper_jni.h"
13
14 namespace weblayer {
15
16 namespace {
17
GetOnLogMetricsCallback()18 OnLogsMetricsCallback& GetOnLogMetricsCallback() {
19 static base::NoDestructor<OnLogsMetricsCallback> s_callback;
20 return *s_callback;
21 }
22
GetProfileByName(const std::string & name)23 ProfileImpl* GetProfileByName(const std::string& name) {
24 for (auto* profile : ProfileImpl::GetAllProfiles()) {
25 if (profile->name() == name)
26 return profile;
27 }
28
29 return nullptr;
30 }
31
32 } // namespace
33
InstallTestGmsBridge(ConsentType consent_type,const OnLogsMetricsCallback on_log_metrics)34 void InstallTestGmsBridge(ConsentType consent_type,
35 const OnLogsMetricsCallback on_log_metrics) {
36 GetOnLogMetricsCallback() = on_log_metrics;
37 Java_MetricsTestHelper_installTestGmsBridge(
38 base::android::AttachCurrentThread(), static_cast<int>(consent_type));
39 }
40
RemoveTestGmsBridge()41 void RemoveTestGmsBridge() {
42 Java_MetricsTestHelper_removeTestGmsBridge(
43 base::android::AttachCurrentThread());
44 GetOnLogMetricsCallback().Reset();
45 }
46
RunConsentCallback(bool has_consent)47 void RunConsentCallback(bool has_consent) {
48 Java_MetricsTestHelper_runConsentCallback(
49 base::android::AttachCurrentThread(), has_consent);
50 }
51
CreateProfile(const std::string & name)52 ProfileImpl* CreateProfile(const std::string& name) {
53 DCHECK(!GetProfileByName(name));
54 JNIEnv* env = base::android::AttachCurrentThread();
55 Java_MetricsTestHelper_createProfile(
56 env, base::android::ConvertUTF8ToJavaString(env, name));
57 ProfileImpl* profile = GetProfileByName(name);
58 // Creating a profile may involve storage partition initialization. Wait for
59 // the initialization to be completed.
60 content::RunAllTasksUntilIdle();
61 return profile;
62 }
DestroyProfile(const std::string & name)63 void DestroyProfile(const std::string& name) {
64 DCHECK(GetProfileByName(name));
65 JNIEnv* env = base::android::AttachCurrentThread();
66 Java_MetricsTestHelper_destroyProfile(
67 env, base::android::ConvertUTF8ToJavaString(env, name));
68 }
69
JNI_MetricsTestHelper_OnLogMetrics(JNIEnv * env,const base::android::JavaParamRef<jbyteArray> & data)70 void JNI_MetricsTestHelper_OnLogMetrics(
71 JNIEnv* env,
72 const base::android::JavaParamRef<jbyteArray>& data) {
73 auto& callback = GetOnLogMetricsCallback();
74 if (!callback)
75 return;
76
77 metrics::ChromeUserMetricsExtension proto;
78 jbyte* src_bytes = env->GetByteArrayElements(data, nullptr);
79 proto.ParseFromArray(src_bytes, env->GetArrayLength(data.obj()));
80 env->ReleaseByteArrayElements(data, src_bytes, JNI_ABORT);
81 callback.Run(proto);
82 }
83
84 } // namespace weblayer
85