Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "components/tracing/trace_config_file.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 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" | |
| 17 | |
| 18 | |
|
dsinclair
2015/08/24 15:04:49
nit: remove blank line
Zhen Wang
2015/08/24 16:39:25
Done.
| |
| 19 namespace tracing { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Maximum trace config file size that will be loaded, in bytes. | |
| 24 const size_t kTraceConfigFileSizeLimit = 64 * 1024; | |
| 25 | |
| 26 // Trace config file path: | |
| 27 // - Android: /data/local/chrome-trace-config.json | |
| 28 // - Others: specified by --trace-config-file flag. | |
| 29 #if defined(OS_ANDROID) | |
| 30 const base::FilePath::CharType kAndroidTraceConfigFile[] = | |
| 31 FILE_PATH_LITERAL("/data/local/chrome-trace-config.json"); | |
| 32 #endif | |
| 33 | |
| 34 const base::FilePath::CharType kDefaultResultFile[] = | |
| 35 FILE_PATH_LITERAL("chrometrace.log"); | |
| 36 | |
| 37 // String parameters that can be used to parse the trace config file content. | |
| 38 const char kTraceConfigParam[] = "trace_config"; | |
| 39 const char kStartupDurationParam[] = "startup_duration"; | |
| 40 const char kResultFileParam[] = "result_file"; | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 TraceConfigFile* TraceConfigFile::GetInstance() { | |
| 45 return Singleton<TraceConfigFile, | |
| 46 DefaultSingletonTraits<TraceConfigFile>>::get(); | |
| 47 } | |
| 48 | |
| 49 TraceConfigFile::TraceConfigFile() | |
| 50 : is_enabled_(false), | |
| 51 trace_config_(base::trace_event::TraceConfig()), | |
| 52 startup_duration_(0), | |
| 53 result_file_(kDefaultResultFile) { | |
| 54 #if defined(OS_ANDROID) | |
| 55 base::FilePath trace_config_file(kAndroidTraceConfigFile); | |
| 56 #else | |
| 57 const base::CommandLine& command_line = | |
| 58 *base::CommandLine::ForCurrentProcess(); | |
| 59 if (!command_line.HasSwitch(switches::kTraceConfigFile) || | |
| 60 command_line.HasSwitch(switches::kTraceStartup) || | |
| 61 command_line.HasSwitch(switches::kTraceShutdown)) { | |
| 62 return; | |
| 63 } | |
| 64 base::FilePath trace_config_file = | |
| 65 command_line.GetSwitchValuePath(switches::kTraceConfigFile); | |
| 66 #endif | |
| 67 | |
| 68 if (trace_config_file.empty()) { | |
| 69 // If the trace config file path is not specified, trace Chrome with the | |
| 70 // default configuration for 5 sec. | |
| 71 startup_duration_ = 5; | |
| 72 is_enabled_ = true; | |
| 73 return; | |
| 74 } else { | |
|
dsinclair
2015/08/24 15:04:49
Don't need the else because the if() returns.
Zhen Wang
2015/08/24 16:39:25
Done.
| |
| 75 if (!base::PathExists(trace_config_file)) { | |
| 76 return; | |
| 77 } | |
|
dsinclair
2015/08/24 15:04:50
nit: don't need {}'s here.
Zhen Wang
2015/08/24 16:39:25
Done.
| |
| 78 std::string trace_config_file_content; | |
| 79 if (!base::ReadFileToString(trace_config_file, | |
| 80 &trace_config_file_content, | |
| 81 kTraceConfigFileSizeLimit)) { | |
| 82 return; | |
| 83 } | |
| 84 is_enabled_ = ParseTraceConfigFileContent(trace_config_file_content); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 TraceConfigFile::~TraceConfigFile() { | |
| 89 } | |
| 90 | |
| 91 bool TraceConfigFile::ParseTraceConfigFileContent(std::string content) { | |
| 92 scoped_ptr<base::Value> value(base::JSONReader::Read(content)); | |
| 93 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { | |
| 94 return false; | |
| 95 } | |
|
dsinclair
2015/08/24 15:04:49
nit: {}'s
Zhen Wang
2015/08/24 16:39:25
Done.
| |
| 96 scoped_ptr<base::DictionaryValue> dict( | |
| 97 static_cast<base::DictionaryValue*>(value.release())); | |
| 98 | |
| 99 base::DictionaryValue* trace_config_dict = NULL; | |
| 100 if (dict->GetDictionary(kTraceConfigParam, &trace_config_dict)) { | |
|
dsinclair
2015/08/24 15:04:50
if (!dict->...)
return false;
Zhen Wang
2015/08/24 16:39:25
Done.
| |
| 101 std::string trace_config_str; | |
| 102 base::JSONWriter::Write(*trace_config_dict, &trace_config_str); | |
| 103 trace_config_ = base::trace_event::TraceConfig(trace_config_str); | |
| 104 } else { | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 if (!dict->GetInteger(kStartupDurationParam, &startup_duration_)) | |
| 109 startup_duration_ = 0; | |
| 110 | |
| 111 if (startup_duration_ < 0) | |
| 112 startup_duration_ = 0; | |
| 113 | |
| 114 std::string result_file_str; | |
| 115 if (dict->GetString(kResultFileParam, &result_file_str)) | |
| 116 result_file_ = base::FilePath().AppendASCII(result_file_str); | |
| 117 | |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 bool TraceConfigFile::IsEnabled() const { | |
| 122 return is_enabled_; | |
| 123 } | |
| 124 | |
| 125 base::trace_event::TraceConfig TraceConfigFile::GetTraceConfig() const { | |
| 126 DCHECK(IsEnabled()); | |
| 127 return trace_config_; | |
| 128 } | |
| 129 | |
| 130 int TraceConfigFile::GetStartupDuration() const { | |
| 131 DCHECK(IsEnabled()); | |
| 132 return startup_duration_; | |
| 133 } | |
| 134 | |
| 135 #if !defined(OS_ANDROID) | |
| 136 base::FilePath TraceConfigFile::GetResultFile() const { | |
| 137 DCHECK(IsEnabled()); | |
| 138 return result_file_; | |
| 139 } | |
| 140 #endif | |
| 141 | |
| 142 } // namespace tracing | |
| OLD | NEW |