Commit e0202790 authored by Oksana Zhuravlova's avatar Oksana Zhuravlova Committed by Commit Bot

[tracing] Move bg tracing config reading into //content

This CL moves functions related to getting background tracing
configuration from field trials out of //chrome into
//content/public/browser (BackgroundTracingManager) so that they can be
reused by other embedders (in this case, android_webview).

Bug: b/170511922
Change-Id: Ia4ffc5f84472d9af00a3b84460d3815b360593eb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2473057
Commit-Queue: Oksana Zhuravlova <oksamyt@chromium.org>
Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarSami Kyöstilä <skyostil@chromium.org>
Reviewed-by: default avataroysteine <oysteine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821363}
parent 0d49ca90
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "chrome/browser/tracing/crash_service_uploader.h" #include "chrome/browser/tracing/crash_service_uploader.h"
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
#include "components/tracing/common/tracing_switches.h" #include "components/tracing/common/tracing_switches.h"
#include "components/variations/variations_associated_data.h"
#include "content/public/browser/background_tracing_config.h" #include "content/public/browser/background_tracing_config.h"
#include "content/public/browser/background_tracing_manager.h" #include "content/public/browser/background_tracing_manager.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
...@@ -32,12 +31,6 @@ namespace tracing { ...@@ -32,12 +31,6 @@ namespace tracing {
namespace { namespace {
const char kBackgroundTracingFieldTrial[] = "BackgroundTracing";
const char kBackgroundTracingConfig[] = "config";
const char kBackgroundTracingUploadUrl[] = "upload_url";
ConfigTextFilterForTesting g_config_text_filter_for_testing = nullptr;
void OnBackgroundTracingUploadComplete( void OnBackgroundTracingUploadComplete(
TraceCrashServiceUploader* uploader, TraceCrashServiceUploader* uploader,
content::BackgroundTracingManager::FinishedProcessingCallback done_callback, content::BackgroundTracingManager::FinishedProcessingCallback done_callback,
...@@ -76,28 +69,6 @@ void BackgroundTracingUploadCallback( ...@@ -76,28 +69,6 @@ void BackgroundTracingUploadCallback(
std::move(callback))); std::move(callback)));
} }
std::unique_ptr<content::BackgroundTracingConfig> GetBackgroundTracingConfig() {
std::string config_text = variations::GetVariationParamValue(
kBackgroundTracingFieldTrial, kBackgroundTracingConfig);
if (config_text.empty())
return nullptr;
if (g_config_text_filter_for_testing)
(*g_config_text_filter_for_testing)(&config_text);
std::unique_ptr<base::Value> value =
base::JSONReader::ReadDeprecated(config_text);
if (!value)
return nullptr;
const base::DictionaryValue* dict = nullptr;
if (!value->GetAsDictionary(&dict))
return nullptr;
return content::BackgroundTracingConfig::FromDict(dict);
}
void SetupBackgroundTracingFromConfigFile(const base::FilePath& config_file, void SetupBackgroundTracingFromConfigFile(const base::FilePath& config_file,
const std::string& upload_url) { const std::string& upload_url) {
std::string config_text; std::string config_text;
...@@ -128,10 +99,6 @@ void SetupBackgroundTracingFromConfigFile(const base::FilePath& config_file, ...@@ -128,10 +99,6 @@ void SetupBackgroundTracingFromConfigFile(const base::FilePath& config_file,
} // namespace } // namespace
void SetConfigTextFilterForTesting(ConfigTextFilterForTesting predicate) {
g_config_text_filter_for_testing = predicate;
}
void SetupBackgroundTracingFieldTrial() { void SetupBackgroundTracingFieldTrial() {
auto* command_line = base::CommandLine::ForCurrentProcess(); auto* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kEnableBackgroundTracing) && if (command_line->HasSwitch(switches::kEnableBackgroundTracing) &&
...@@ -143,13 +110,14 @@ void SetupBackgroundTracingFieldTrial() { ...@@ -143,13 +110,14 @@ void SetupBackgroundTracingFieldTrial() {
} }
std::unique_ptr<content::BackgroundTracingConfig> config = std::unique_ptr<content::BackgroundTracingConfig> config =
GetBackgroundTracingConfig(); content::BackgroundTracingManager::GetInstance()
->GetBackgroundTracingConfig();
std::string upload_url = variations::GetVariationParamValue(
kBackgroundTracingFieldTrial, kBackgroundTracingUploadUrl);
content::BackgroundTracingManager::GetInstance()->SetActiveScenario( content::BackgroundTracingManager::GetInstance()->SetActiveScenario(
std::move(config), std::move(config),
base::BindRepeating(&BackgroundTracingUploadCallback, upload_url), base::BindRepeating(&BackgroundTracingUploadCallback,
content::BackgroundTracingManager::GetInstance()
->GetBackgroundTracingUploadUrl()),
content::BackgroundTracingManager::ANONYMIZE_DATA); content::BackgroundTracingManager::ANONYMIZE_DATA);
} }
......
...@@ -5,15 +5,10 @@ ...@@ -5,15 +5,10 @@
#ifndef CHROME_BROWSER_TRACING_BACKGROUND_TRACING_FIELD_TRIAL_H_ #ifndef CHROME_BROWSER_TRACING_BACKGROUND_TRACING_FIELD_TRIAL_H_
#define CHROME_BROWSER_TRACING_BACKGROUND_TRACING_FIELD_TRIAL_H_ #define CHROME_BROWSER_TRACING_BACKGROUND_TRACING_FIELD_TRIAL_H_
#include <string>
namespace tracing { namespace tracing {
void SetupBackgroundTracingFieldTrial(); void SetupBackgroundTracingFieldTrial();
typedef void (*ConfigTextFilterForTesting)(std::string*);
void SetConfigTextFilterForTesting(ConfigTextFilterForTesting predicate);
} // namespace tracing } // namespace tracing
#endif // CHROME_BROWSER_TRACING_BACKGROUND_TRACING_FIELD_TRIAL_H_ #endif // CHROME_BROWSER_TRACING_BACKGROUND_TRACING_FIELD_TRIAL_H_
...@@ -50,9 +50,10 @@ const char kValidTracingConfig[] = R"( ...@@ -50,9 +50,10 @@ const char kValidTracingConfig[] = R"(
} }
)"; )";
void CheckConfig(std::string* config) { std::string CheckConfig(const std::string& config) {
if (*config == kTestConfig) if (config == kTestConfig)
g_test_config_loaded = true; g_test_config_loaded = true;
return config;
} }
} // namespace } // namespace
...@@ -71,7 +72,8 @@ TEST_F(BackgroundTracingTest, SetupBackgroundTracingFieldTrial) { ...@@ -71,7 +72,8 @@ TEST_F(BackgroundTracingTest, SetupBackgroundTracingFieldTrial) {
// In case it is already set at previous test run. // In case it is already set at previous test run.
g_test_config_loaded = false; g_test_config_loaded = false;
tracing::SetConfigTextFilterForTesting(&CheckConfig); content::BackgroundTracingManager::GetInstance()
->SetConfigTextFilterForTesting(base::BindRepeating(&CheckConfig));
tracing::SetupBackgroundTracingFieldTrial(); tracing::SetupBackgroundTracingFieldTrial();
EXPECT_TRUE(g_test_config_loaded); EXPECT_TRUE(g_test_config_loaded);
......
...@@ -70,7 +70,11 @@ void ChromeTracingDelegate::RegisterPrefs(PrefRegistrySimple* registry) { ...@@ -70,7 +70,11 @@ void ChromeTracingDelegate::RegisterPrefs(PrefRegistrySimple* registry) {
} }
ChromeTracingDelegate::ChromeTracingDelegate() : incognito_launched_(false) { ChromeTracingDelegate::ChromeTracingDelegate() : incognito_launched_(false) {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); // Ensure that this code is called on the UI thread, except for
// tests where a UI thread might not have been initialized at this point.
DCHECK(
content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ||
!content::BrowserThread::IsThreadInitialized(content::BrowserThread::UI));
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
BrowserList::AddObserver(this); BrowserList::AddObserver(this);
#else #else
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h" #include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.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_commands.h"
#include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/browser_list.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
...@@ -248,25 +247,30 @@ class ChromeTracingDelegateBrowserTestOnStartup ...@@ -248,25 +247,30 @@ class ChromeTracingDelegateBrowserTestOnStartup
protected: protected:
ChromeTracingDelegateBrowserTestOnStartup() {} ChromeTracingDelegateBrowserTestOnStartup() {}
static void FieldTrialConfigTextFilter(std::string* config_text) { static std::string FieldTrialConfigTextFilter(
ASSERT_TRUE(config_text); const std::string& config_text) {
// We need to replace the config JSON with the full one here, as we can't // We need to replace the config JSON with the full one here, as we can't
// pass JSON through the fieldtrial switch parsing. // pass JSON through the fieldtrial switch parsing.
if (*config_text == "default_config_for_testing") { if (config_text == "default_config_for_testing") {
*config_text = return "{\"mode\":\"PREEMPTIVE_TRACING_MODE\", \"category\": "
"{\"mode\":\"PREEMPTIVE_TRACING_MODE\", \"category\": " "\"BENCHMARK\",\"configs\": [{\"rule\": "
"\"BENCHMARK\",\"configs\": [{\"rule\": " "\"MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED\",\"trigger_name\":"
"\"MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED\",\"trigger_name\":" "\"test\"}]}";
"\"test\"}]}";
} }
return config_text;
} }
void SetUpCommandLine(base::CommandLine* command_line) override { void SetUpCommandLine(base::CommandLine* command_line) override {
variations::testing::VariationParamsManager::AppendVariationParams( variations::testing::VariationParamsManager::AppendVariationParams(
"BackgroundTracing", "TestGroup", "BackgroundTracing", "TestGroup",
{{"config", "default_config_for_testing"}}, command_line); {{"config", "default_config_for_testing"}}, command_line);
}
tracing::SetConfigTextFilterForTesting(&FieldTrialConfigTextFilter); void CreatedBrowserMainParts(
content::BrowserMainParts* browser_main_parts) override {
content::BackgroundTracingManager::GetInstance()
->SetConfigTextFilterForTesting(
base::BindRepeating(&FieldTrialConfigTextFilter));
} }
}; };
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "base/location.h" #include "base/location.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -19,6 +20,7 @@ ...@@ -19,6 +20,7 @@
#include "base/values.h" #include "base/values.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/tracing/common/trace_startup_config.h" #include "components/tracing/common/trace_startup_config.h"
#include "components/variations/variations_associated_data.h"
#include "content/browser/tracing/background_memory_tracing_observer.h" #include "content/browser/tracing/background_memory_tracing_observer.h"
#include "content/browser/tracing/background_startup_tracing_observer.h" #include "content/browser/tracing/background_startup_tracing_observer.h"
#include "content/browser/tracing/background_tracing_active_scenario.h" #include "content/browser/tracing/background_tracing_active_scenario.h"
...@@ -46,6 +48,10 @@ ...@@ -46,6 +48,10 @@
namespace content { namespace content {
const char kBackgroundTracingFieldTrial[] = "BackgroundTracing";
const char kBackgroundTracingConfig[] = "config";
const char kBackgroundTracingUploadUrl[] = "upload_url";
// static // static
void BackgroundTracingManagerImpl::RecordMetric(Metrics metric) { void BackgroundTracingManagerImpl::RecordMetric(Metrics metric) {
UMA_HISTOGRAM_ENUMERATION("Tracing.Background.ScenarioState", metric, UMA_HISTOGRAM_ENUMERATION("Tracing.Background.ScenarioState", metric,
...@@ -237,7 +243,11 @@ std::string BackgroundTracingManagerImpl::GetLatestTraceToUpload() { ...@@ -237,7 +243,11 @@ std::string BackgroundTracingManagerImpl::GetLatestTraceToUpload() {
void BackgroundTracingManagerImpl::AddEnabledStateObserver( void BackgroundTracingManagerImpl::AddEnabledStateObserver(
EnabledStateObserver* observer) { EnabledStateObserver* observer) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); // Ensure that this code is called on the UI thread, except for
// tests where a UI thread might not have been initialized at this point.
DCHECK(
content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ||
!content::BrowserThread::IsThreadInitialized(content::BrowserThread::UI));
background_tracing_observers_.insert(observer); background_tracing_observers_.insert(observer);
} }
...@@ -304,6 +314,11 @@ void BackgroundTracingManagerImpl::SetTraceToUploadForTesting( ...@@ -304,6 +314,11 @@ void BackgroundTracingManagerImpl::SetTraceToUploadForTesting(
SetTraceToUpload(std::move(trace_data)); SetTraceToUpload(std::move(trace_data));
} }
void BackgroundTracingManagerImpl::SetConfigTextFilterForTesting(
ConfigTextFilterForTesting predicate) {
config_text_filter_for_testing_ = std::move(predicate);
}
void BackgroundTracingManagerImpl::SetTraceToUpload( void BackgroundTracingManagerImpl::SetTraceToUpload(
std::unique_ptr<std::string> trace_data) { std::unique_ptr<std::string> trace_data) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
...@@ -314,6 +329,33 @@ void BackgroundTracingManagerImpl::SetTraceToUpload( ...@@ -314,6 +329,33 @@ void BackgroundTracingManagerImpl::SetTraceToUpload(
} }
} }
std::string BackgroundTracingManagerImpl::GetBackgroundTracingUploadUrl() {
return variations::GetVariationParamValue(kBackgroundTracingFieldTrial,
kBackgroundTracingUploadUrl);
}
std::unique_ptr<content::BackgroundTracingConfig>
BackgroundTracingManagerImpl::GetBackgroundTracingConfig() {
std::string config_text = variations::GetVariationParamValue(
kBackgroundTracingFieldTrial, kBackgroundTracingConfig);
if (config_text.empty())
return nullptr;
if (config_text_filter_for_testing_)
config_text = config_text_filter_for_testing_.Run(config_text);
auto value = base::JSONReader::Read(config_text);
if (!value)
return nullptr;
// TODO(crbug.com/646113): use the new base::Value API.
const base::DictionaryValue* dict = nullptr;
if (!value->GetAsDictionary(&dict))
return nullptr;
return BackgroundTracingConfig::FromDict(dict);
}
void BackgroundTracingManagerImpl::ValidateStartupScenario() { void BackgroundTracingManagerImpl::ValidateStartupScenario() {
if (!active_scenario_ || !delegate_) { if (!active_scenario_ || !delegate_) {
return; return;
......
...@@ -115,6 +115,9 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager { ...@@ -115,6 +115,9 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager {
bool HasTraceToUpload() override; bool HasTraceToUpload() override;
std::string GetLatestTraceToUpload() override; std::string GetLatestTraceToUpload() override;
void SetTraceToUpload(std::unique_ptr<std::string> trace_data); void SetTraceToUpload(std::unique_ptr<std::string> trace_data);
std::string GetBackgroundTracingUploadUrl() override;
std::unique_ptr<BackgroundTracingConfig> GetBackgroundTracingConfig()
override;
// Add/remove EnabledStateObserver. // Add/remove EnabledStateObserver.
CONTENT_EXPORT void AddEnabledStateObserver(EnabledStateObserver* observer); CONTENT_EXPORT void AddEnabledStateObserver(EnabledStateObserver* observer);
...@@ -142,6 +145,8 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager { ...@@ -142,6 +145,8 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager {
CONTENT_EXPORT void AbortScenarioForTesting() override; CONTENT_EXPORT void AbortScenarioForTesting() override;
CONTENT_EXPORT void SetTraceToUploadForTesting( CONTENT_EXPORT void SetTraceToUploadForTesting(
std::unique_ptr<std::string> trace_data) override; std::unique_ptr<std::string> trace_data) override;
void SetConfigTextFilterForTesting(
ConfigTextFilterForTesting predicate) override;
private: private:
friend class base::NoDestructor<BackgroundTracingManagerImpl>; friend class base::NoDestructor<BackgroundTracingManagerImpl>;
...@@ -185,6 +190,9 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager { ...@@ -185,6 +190,9 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager {
// This field contains serialized trace log proto. // This field contains serialized trace log proto.
std::string trace_to_upload_; std::string trace_to_upload_;
// Callback to override the background tracing config for testing.
ConfigTextFilterForTesting config_text_filter_for_testing_;
DISALLOW_COPY_AND_ASSIGN(BackgroundTracingManagerImpl); DISALLOW_COPY_AND_ASSIGN(BackgroundTracingManagerImpl);
}; };
......
...@@ -100,11 +100,25 @@ class BackgroundTracingManager { ...@@ -100,11 +100,25 @@ class BackgroundTracingManager {
// the serialized trace proto. // the serialized trace proto.
virtual std::string GetLatestTraceToUpload() = 0; virtual std::string GetLatestTraceToUpload() = 0;
// Returns the URL to upload the trace files to.
virtual std::string GetBackgroundTracingUploadUrl() = 0;
// Returns the configuration for background tracing.
virtual std::unique_ptr<BackgroundTracingConfig>
GetBackgroundTracingConfig() = 0;
// For tests // For tests
virtual void AbortScenarioForTesting() = 0; virtual void AbortScenarioForTesting() = 0;
virtual void SetTraceToUploadForTesting( virtual void SetTraceToUploadForTesting(
std::unique_ptr<std::string> trace_data) = 0; std::unique_ptr<std::string> trace_data) = 0;
using ConfigTextFilterForTesting =
base::RepeatingCallback<std::string(const std::string&)>;
// Sets a callback to override the background tracing config for testing.
virtual void SetConfigTextFilterForTesting(
ConfigTextFilterForTesting predicate) = 0;
protected: protected:
virtual ~BackgroundTracingManager() {} virtual ~BackgroundTracingManager() {}
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment