Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / tracing / trace_config_file.cc
blob796eb1b7cb5dae5c923ddef43b1589cdd26eb243
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"
7 #include <string>
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"
18 namespace tracing {
20 namespace {
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");
31 #endif
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";
41 } // namespace
43 TraceConfigFile* TraceConfigFile::GetInstance() {
44 return base::Singleton<TraceConfigFile,
45 base::DefaultSingletonTraits<TraceConfigFile>>::get();
48 TraceConfigFile::TraceConfigFile()
49 : is_enabled_(false),
50 trace_config_(base::trace_event::TraceConfig()),
51 startup_duration_(0),
52 result_file_(kDefaultResultFile) {
53 #if defined(OS_ANDROID)
54 base::FilePath trace_config_file(kAndroidTraceConfigFile);
55 #else
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)) {
61 return;
63 base::FilePath trace_config_file =
64 command_line.GetSwitchValuePath(switches::kTraceConfigFile);
65 #endif
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;
71 is_enabled_ = true;
72 return;
75 if (!base::PathExists(trace_config_file))
76 return;
78 std::string trace_config_file_content;
79 if (!base::ReadFileToString(trace_config_file,
80 &trace_config_file_content,
81 kTraceConfigFileSizeLimit)) {
82 return;
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))
93 return false;
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))
100 return false;
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);
116 return true;
119 bool TraceConfigFile::IsEnabled() const {
120 return is_enabled_;
123 base::trace_event::TraceConfig TraceConfigFile::GetTraceConfig() const {
124 DCHECK(IsEnabled());
125 return trace_config_;
128 int TraceConfigFile::GetStartupDuration() const {
129 DCHECK(IsEnabled());
130 return startup_duration_;
133 #if !defined(OS_ANDROID)
134 base::FilePath TraceConfigFile::GetResultFile() const {
135 DCHECK(IsEnabled());
136 return result_file_;
138 #endif
140 } // namespace tracing