ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / tracing / background_tracing_field_trial.cc
blobfbfa247220dec7845627c5c0647a23af6ad00bdb
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
3 // found in the LICENSE file.
5 #include "chrome/browser/tracing/background_tracing_field_trial.h"
7 #include <string>
9 #include "base/json/json_reader.h"
10 #include "base/metrics/field_trial.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/tracing/crash_service_uploader.h"
13 #include "chrome/common/variations/variations_util.h"
14 #include "components/variations/variations_associated_data.h"
15 #include "content/public/browser/background_tracing_config.h"
16 #include "content/public/browser/background_tracing_manager.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "url/gurl.h"
20 namespace tracing {
22 namespace {
24 const char kBackgroundTracingFieldTrial[] = "BackgroundTracing";
25 const char kBackgroundTracingConfig[] = "config";
26 const char kBackgroundTracingUploadUrl[] = "upload_url";
28 ConfigTextFilterForTesting g_config_text_filter_for_testing = nullptr;
30 void OnUploadComplete(TraceCrashServiceUploader* uploader,
31 const base::Closure& done_callback,
32 bool success,
33 const std::string& feedback) {
34 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
35 done_callback.Run();
38 void UploadCallback(const std::string& upload_url,
39 const scoped_refptr<base::RefCountedString>& file_contents,
40 scoped_ptr<base::DictionaryValue> metadata,
41 base::Closure callback) {
42 TraceCrashServiceUploader* uploader = new TraceCrashServiceUploader(
43 g_browser_process->system_request_context());
45 if (GURL(upload_url).is_valid())
46 uploader->SetUploadURL(upload_url);
48 uploader->DoUpload(
49 file_contents->data(), content::TraceUploader::UNCOMPRESSED_UPLOAD,
50 metadata.Pass(), content::TraceUploader::UploadProgressCallback(),
51 base::Bind(&OnUploadComplete, base::Owned(uploader), callback));
54 } // namespace
56 void SetConfigTextFilterForTesting(ConfigTextFilterForTesting predicate) {
57 g_config_text_filter_for_testing = predicate;
60 void SetupBackgroundTracingFieldTrial() {
61 std::string config_text = variations::GetVariationParamValue(
62 kBackgroundTracingFieldTrial, kBackgroundTracingConfig);
63 std::string upload_url = variations::GetVariationParamValue(
64 kBackgroundTracingFieldTrial, kBackgroundTracingUploadUrl);
66 if (config_text.empty())
67 return;
69 if (g_config_text_filter_for_testing)
70 (*g_config_text_filter_for_testing)(&config_text);
72 scoped_ptr<base::Value> value = base::JSONReader::Read(config_text);
73 if (!value)
74 return;
76 const base::DictionaryValue* dict = nullptr;
77 if (!value->GetAsDictionary(&dict))
78 return;
80 scoped_ptr<content::BackgroundTracingConfig> config =
81 content::BackgroundTracingConfig::FromDict(dict);
82 if (!config)
83 return;
85 content::BackgroundTracingManager::GetInstance()->SetActiveScenario(
86 config.Pass(), base::Bind(&UploadCallback, upload_url),
87 content::BackgroundTracingManager::ANONYMIZE_DATA);
90 } // namespace tracing