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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "base/base_switches.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/prefs/pref_service.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/tracing/background_tracing_field_trial.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/metrics/metrics_pref_names.h"
#include "content/public/browser/background_tracing_config.h"
#include "content/public/browser/background_tracing_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_utils.h"
namespace {
class ChromeTracingDelegateBrowserTest : public InProcessBrowserTest {
public:
ChromeTracingDelegateBrowserTest()
: receive_count_(0),
started_finalizations_count_(0),
last_on_started_finalizing_success_(false) {}
#if !defined(OS_CHROMEOS)
void SetUpOnMainThread() override {
PrefService* local_state = g_browser_process->local_state();
DCHECK(local_state);
local_state->SetBoolean(metrics::prefs::kMetricsReportingEnabled, true);
}
#endif
bool StartPreemptiveScenario(
const base::Closure& on_upload_callback,
content::BackgroundTracingManager::DataFiltering data_filtering) {
on_upload_callback_ = on_upload_callback;
base::DictionaryValue dict;
dict.SetString("mode", "PREEMPTIVE_TRACING_MODE");
dict.SetString("category", "BENCHMARK");
scoped_ptr<base::ListValue> rules_list(new base::ListValue());
{
scoped_ptr<base::DictionaryValue> rules_dict(new base::DictionaryValue());
rules_dict->SetString("rule", "MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED");
rules_dict->SetString("trigger_name", "test");
rules_list->Append(std::move(rules_dict));
}
dict.Set("configs", std::move(rules_list));
scoped_ptr<content::BackgroundTracingConfig> config(
content::BackgroundTracingConfig::FromDict(&dict));
DCHECK(config);
content::BackgroundTracingManager::ReceiveCallback receive_callback =
base::Bind(&ChromeTracingDelegateBrowserTest::OnUpload,
base::Unretained(this));
return content::BackgroundTracingManager::GetInstance()->SetActiveScenario(
std::move(config), receive_callback, data_filtering);
}
void TriggerPreemptiveScenario(
const base::Closure& on_started_finalization_callback) {
on_started_finalization_callback_ = on_started_finalization_callback;
trigger_handle_ =
content::BackgroundTracingManager::GetInstance()->RegisterTriggerType(
"test");
content::BackgroundTracingManager::StartedFinalizingCallback
started_finalizing_callback =
base::Bind(&ChromeTracingDelegateBrowserTest::OnStartedFinalizing,
base::Unretained(this));
content::BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
trigger_handle_, started_finalizing_callback);
}
int get_receive_count() const { return receive_count_; }
bool get_started_finalizations() const {
return started_finalizations_count_;
}
bool get_last_started_finalization_success() const {
return last_on_started_finalizing_success_;
}
private:
void OnUpload(const scoped_refptr<base::RefCountedString>& file_contents,
scoped_ptr<const base::DictionaryValue> metadata,
base::Callback<void()> done_callback) {
receive_count_ += 1;
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
base::Bind(done_callback));
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
base::Bind(on_upload_callback_));
}
void OnStartedFinalizing(bool success) {
started_finalizations_count_++;
last_on_started_finalizing_success_ = success;
if (!on_started_finalization_callback_.is_null()) {
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(on_started_finalization_callback_));
}
}
base::Closure on_upload_callback_;
base::Closure on_started_finalization_callback_;
int receive_count_;
int started_finalizations_count_;
content::BackgroundTracingManager::TriggerHandle trigger_handle_;
bool last_on_started_finalizing_success_;
};
IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest,
BackgroundTracingTimeThrottled) {
base::RunLoop wait_for_upload;
EXPECT_TRUE(StartPreemptiveScenario(
wait_for_upload.QuitClosure(),
content::BackgroundTracingManager::NO_DATA_FILTERING));
TriggerPreemptiveScenario(base::Closure());
wait_for_upload.Run();
EXPECT_TRUE(get_receive_count() == 1);
PrefService* local_state = g_browser_process->local_state();
DCHECK(local_state);
const base::Time last_upload_time = base::Time::FromInternalValue(
local_state->GetInt64(prefs::kBackgroundTracingLastUpload));
EXPECT_FALSE(last_upload_time.is_null());
// We should not be able to start a new reactive scenario immediately after
// a previous one gets uploaded.
EXPECT_FALSE(StartPreemptiveScenario(
base::Closure(), content::BackgroundTracingManager::NO_DATA_FILTERING));
}
IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest,
BackgroundTracingThrottleTimeElapsed) {
base::RunLoop wait_for_upload;
EXPECT_TRUE(StartPreemptiveScenario(
wait_for_upload.QuitClosure(),
content::BackgroundTracingManager::NO_DATA_FILTERING));
TriggerPreemptiveScenario(base::Closure());
wait_for_upload.Run();
EXPECT_TRUE(get_receive_count() == 1);
PrefService* local_state = g_browser_process->local_state();
DCHECK(local_state);
const base::Time last_upload_time = base::Time::FromInternalValue(
local_state->GetInt64(prefs::kBackgroundTracingLastUpload));
EXPECT_FALSE(last_upload_time.is_null());
// We move the last upload time to eight days in the past,
// and at that point should be able to start a scenario again.
base::Time new_upload_time = last_upload_time - base::TimeDelta::FromDays(8);
local_state->SetInt64(prefs::kBackgroundTracingLastUpload,
new_upload_time.ToInternalValue());
EXPECT_TRUE(StartPreemptiveScenario(
base::Closure(), content::BackgroundTracingManager::NO_DATA_FILTERING));
}
// If we need a PII-stripped trace, any existing OTR session should block the
// trace.
IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest,
ExistingIncognitoSessionBlockingTraceStart) {
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_NEW_INCOGNITO_WINDOW));
EXPECT_TRUE(BrowserList::IsOffTheRecordSessionActive());
EXPECT_FALSE(StartPreemptiveScenario(
base::Closure(), content::BackgroundTracingManager::ANONYMIZE_DATA));
}
// If we need a PII-stripped trace, any new OTR session during tracing should
// block the finalization of the trace.
IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest,
NewIncognitoSessionBlockingTraceFinalization) {
EXPECT_TRUE(StartPreemptiveScenario(
base::Closure(), content::BackgroundTracingManager::ANONYMIZE_DATA));
EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_NEW_INCOGNITO_WINDOW));
EXPECT_TRUE(BrowserList::IsOffTheRecordSessionActive());
base::RunLoop wait_for_finalization_start;
TriggerPreemptiveScenario(wait_for_finalization_start.QuitClosure());
wait_for_finalization_start.Run();
EXPECT_TRUE(get_started_finalizations() == 1);
EXPECT_FALSE(get_last_started_finalization_success());
}
class ChromeTracingDelegateBrowserTestOnStartup
: public ChromeTracingDelegateBrowserTest {
protected:
ChromeTracingDelegateBrowserTestOnStartup() {}
static void FieldTrialConfigTextFilter(std::string* config_text) {
ASSERT_TRUE(config_text);
// We need to replace the config JSON with the full one here, as we can't
// pass JSON through the fieldtrial switch parsing.
if (*config_text == "default_config_for_testing") {
*config_text =
"{\"mode\":\"PREEMPTIVE_TRACING_MODE\", \"category\": "
"\"BENCHMARK\",\"configs\": [{\"rule\": "
"\"MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED\",\"trigger_name\":"
"\"test\"}]}";
}
}
void SetUpCommandLine(base::CommandLine* command_line) override {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kForceFieldTrials, "BackgroundTracing/TestGroup/");
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kForceFieldTrialParams,
"BackgroundTracing.TestGroup:config/default_config_for_testing");
tracing::SetConfigTextFilterForTesting(&FieldTrialConfigTextFilter);
}
};
#if !defined(OS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTestOnStartup,
PRE_ScenarioSetFromFieldtrial) {
// At this point the metrics pref is not set.
EXPECT_FALSE(
content::BackgroundTracingManager::GetInstance()->HasActiveScenario());
}
#endif // OS_CHROMEOS
IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTestOnStartup,
ScenarioSetFromFieldtrial) {
// We should reach this point without crashing.
EXPECT_TRUE(
content::BackgroundTracingManager::GetInstance()->HasActiveScenario());
}
#if !defined(OS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTestOnStartup,
PRE_PRE_StartupTracingThrottle) {
// At this point the metrics pref is not set.
EXPECT_FALSE(
content::BackgroundTracingManager::GetInstance()->HasActiveScenario());
}
#endif // OS_CHROMEOS
IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTestOnStartup,
PRE_StartupTracingThrottle) {
EXPECT_TRUE(
content::BackgroundTracingManager::GetInstance()->HasActiveScenario());
// Simulate a trace upload.
PrefService* local_state = g_browser_process->local_state();
DCHECK(local_state);
local_state->SetInt64(prefs::kBackgroundTracingLastUpload,
base::Time::Now().ToInternalValue());
}
IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTestOnStartup,
StartupTracingThrottle) {
// The startup scenario should *not* be started, since not enough
// time has elapsed since the last upload (set in the PRE_ above).
EXPECT_FALSE(
content::BackgroundTracingManager::GetInstance()->HasActiveScenario());
}
} // namespace
|