1 // Copyright (c) 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 "components/tracing/trace_config_file.h"
9 #include "base/command_line.h"
10 #include "base/files/file_util.h"
11 #include "base/json/json_reader.h"
12 #include "base/json/json_writer.h"
13 #include "base/logging.h"
14 #include "base/memory/singleton.h"
15 #include "base/values.h"
16 #include "components/tracing/tracing_switches.h"
22 // Maximum trace config file size that will be loaded, in bytes.
23 const size_t kTraceConfigFileSizeLimit
= 64 * 1024;
25 // Trace config file path:
26 // - Android: /data/local/chrome-trace-config.json
27 // - Others: specified by --trace-config-file flag.
28 #if defined(OS_ANDROID)
29 const base::FilePath::CharType kAndroidTraceConfigFile
[] =
30 FILE_PATH_LITERAL("/data/local/chrome-trace-config.json");
33 const base::FilePath::CharType kDefaultResultFile
[] =
34 FILE_PATH_LITERAL("chrometrace.log");
36 // String parameters that can be used to parse the trace config file content.
37 const char kTraceConfigParam
[] = "trace_config";
38 const char kStartupDurationParam
[] = "startup_duration";
39 const char kResultFileParam
[] = "result_file";
43 TraceConfigFile
* TraceConfigFile::GetInstance() {
44 return base::Singleton
<TraceConfigFile
,
45 base::DefaultSingletonTraits
<TraceConfigFile
>>::get();
48 TraceConfigFile::TraceConfigFile()
50 trace_config_(base::trace_event::TraceConfig()),
52 result_file_(kDefaultResultFile
) {
53 #if defined(OS_ANDROID)
54 base::FilePath
trace_config_file(kAndroidTraceConfigFile
);
56 const base::CommandLine
& command_line
=
57 *base::CommandLine::ForCurrentProcess();
58 if (!command_line
.HasSwitch(switches::kTraceConfigFile
) ||
59 command_line
.HasSwitch(switches::kTraceStartup
) ||
60 command_line
.HasSwitch(switches::kTraceShutdown
)) {
63 base::FilePath trace_config_file
=
64 command_line
.GetSwitchValuePath(switches::kTraceConfigFile
);
67 if (trace_config_file
.empty()) {
68 // If the trace config file path is not specified, trace Chrome with the
69 // default configuration for 5 sec.
70 startup_duration_
= 5;
75 if (!base::PathExists(trace_config_file
))
78 std::string trace_config_file_content
;
79 if (!base::ReadFileToString(trace_config_file
,
80 &trace_config_file_content
,
81 kTraceConfigFileSizeLimit
)) {
84 is_enabled_
= ParseTraceConfigFileContent(trace_config_file_content
);
87 TraceConfigFile::~TraceConfigFile() {
90 bool TraceConfigFile::ParseTraceConfigFileContent(std::string content
) {
91 scoped_ptr
<base::Value
> value(base::JSONReader::Read(content
));
92 if (!value
|| !value
->IsType(base::Value::TYPE_DICTIONARY
))
95 scoped_ptr
<base::DictionaryValue
> dict(
96 static_cast<base::DictionaryValue
*>(value
.release()));
98 base::DictionaryValue
* trace_config_dict
= NULL
;
99 if (!dict
->GetDictionary(kTraceConfigParam
, &trace_config_dict
))
102 std::string trace_config_str
;
103 base::JSONWriter::Write(*trace_config_dict
, &trace_config_str
);
104 trace_config_
= base::trace_event::TraceConfig(trace_config_str
);
106 if (!dict
->GetInteger(kStartupDurationParam
, &startup_duration_
))
107 startup_duration_
= 0;
109 if (startup_duration_
< 0)
110 startup_duration_
= 0;
112 std::string result_file_str
;
113 if (dict
->GetString(kResultFileParam
, &result_file_str
))
114 result_file_
= base::FilePath().AppendASCII(result_file_str
);
119 bool TraceConfigFile::IsEnabled() const {
123 base::trace_event::TraceConfig
TraceConfigFile::GetTraceConfig() const {
125 return trace_config_
;
128 int TraceConfigFile::GetStartupDuration() const {
130 return startup_duration_
;
133 #if !defined(OS_ANDROID)
134 base::FilePath
TraceConfigFile::GetResultFile() const {
140 } // namespace tracing