1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
base / android / field_trial_list.cc [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <string>
#include "base/android/jni_string.h"
#include "base/lazy_instance.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_list_including_low_anonymity.h"
#include "base/metrics/field_trial_params.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "base/base_jni/FieldTrialList_jni.h"
namespace {
// Log trials and their groups on activation, for debugging purposes.
class TrialLogger : public base::FieldTrialList::Observer {
public:
TrialLogger() = default;
TrialLogger(const TrialLogger&) = delete;
TrialLogger& operator=(const TrialLogger&) = delete;
void OnFieldTrialGroupFinalized(const base::FieldTrial& trial,
const std::string& group_name) override {
Log(trial.trial_name(), group_name);
}
static void Log(const std::string& trial_name,
const std::string& group_name) {
// Changes to format of the log message below must be accompanied by
// changes to finch smoke tests since they look for this log message
// in the logcat.
LOG(INFO) << "Active field trial \"" << trial_name << "\" in group \""
<< group_name << '"';
}
protected:
~TrialLogger() override = default;
};
base::LazyInstance<TrialLogger>::Leaky g_trial_logger =
LAZY_INSTANCE_INITIALIZER;
} // namespace
static std::string JNI_FieldTrialList_FindFullName(JNIEnv* env,
std::string& trial_name) {
return base::FieldTrialList::FindFullName(trial_name);
}
static jboolean JNI_FieldTrialList_TrialExists(JNIEnv* env,
std::string& trial_name) {
return base::FieldTrialList::TrialExists(trial_name);
}
static std::string JNI_FieldTrialList_GetVariationParameter(
JNIEnv* env,
std::string& trial_name,
std::string& parameter_key) {
std::map<std::string, std::string> parameters;
base::GetFieldTrialParams(trial_name, ¶meters);
return parameters[parameter_key];
}
// JNI_FieldTrialList_LogActiveTrials() is static function, this makes friending
// it a hassle because it must be declared in the file that the friend
// declaration is in, but its declaration can't be included in multiple places
// or things get messy and the linker gets mad. This helper class exists only to
// friend the JNI function and is, in turn, friended by
// FieldTrialListIncludingLowAnonymity which allows for the private
// GetActiveFieldTrialGroups() to be reached.
class AndroidFieldTrialListLogActiveTrialsFriendHelper {
private:
friend void ::JNI_FieldTrialList_LogActiveTrials(JNIEnv* env);
static bool AddObserver(base::FieldTrialList::Observer* observer) {
return base::FieldTrialListIncludingLowAnonymity::AddObserver(observer);
}
static void GetActiveFieldTrialGroups(
base::FieldTrial::ActiveGroups* active_groups) {
base::FieldTrialListIncludingLowAnonymity::GetActiveFieldTrialGroups(
active_groups);
}
};
static void JNI_FieldTrialList_LogActiveTrials(JNIEnv* env) {
DCHECK(!g_trial_logger.IsCreated()); // This need only be called once.
LOG(INFO) << "Logging active field trials...";
AndroidFieldTrialListLogActiveTrialsFriendHelper::AddObserver(
&g_trial_logger.Get());
// Log any trials that were already active before adding the observer.
std::vector<base::FieldTrial::ActiveGroup> active_groups;
AndroidFieldTrialListLogActiveTrialsFriendHelper::GetActiveFieldTrialGroups(
&active_groups);
for (const base::FieldTrial::ActiveGroup& group : active_groups) {
TrialLogger::Log(group.trial_name, group.group_name);
}
}
static jboolean JNI_FieldTrialList_CreateFieldTrial(JNIEnv* env,
std::string& trial_name,
std::string& group_name) {
return base::FieldTrialList::CreateFieldTrial(trial_name, group_name) !=
nullptr;
}