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 namespace tracing { |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Maximum trace config file size that will be loaded, in bytes. |
| 23 const size_t kTraceConfigFileSizeLimit = 64 * 1024; |
| 24 |
| 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 |
| 32 |
| 33 const base::FilePath::CharType kDefaultResultFile[] = |
| 34 FILE_PATH_LITERAL("chrometrace.log"); |
| 35 |
| 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"; |
| 40 |
| 41 } // namespace |
| 42 |
| 43 TraceConfigFile* TraceConfigFile::GetInstance() { |
| 44 return Singleton<TraceConfigFile, |
| 45 DefaultSingletonTraits<TraceConfigFile>>::get(); |
| 46 } |
| 47 |
| 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; |
| 62 } |
| 63 base::FilePath trace_config_file = |
| 64 command_line.GetSwitchValuePath(switches::kTraceConfigFile); |
| 65 #endif |
| 66 |
| 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; |
| 73 } |
| 74 |
| 75 if (!base::PathExists(trace_config_file)) |
| 76 return; |
| 77 |
| 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 TraceConfigFile::~TraceConfigFile() { |
| 88 } |
| 89 |
| 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; |
| 94 |
| 95 scoped_ptr<base::DictionaryValue> dict( |
| 96 static_cast<base::DictionaryValue*>(value.release())); |
| 97 |
| 98 base::DictionaryValue* trace_config_dict = NULL; |
| 99 if (!dict->GetDictionary(kTraceConfigParam, &trace_config_dict)) |
| 100 return false; |
| 101 |
| 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); |
| 105 |
| 106 if (!dict->GetInteger(kStartupDurationParam, &startup_duration_)) |
| 107 startup_duration_ = 0; |
| 108 |
| 109 if (startup_duration_ < 0) |
| 110 startup_duration_ = 0; |
| 111 |
| 112 std::string result_file_str; |
| 113 if (dict->GetString(kResultFileParam, &result_file_str)) |
| 114 result_file_ = base::FilePath().AppendASCII(result_file_str); |
| 115 |
| 116 return true; |
| 117 } |
| 118 |
| 119 bool TraceConfigFile::IsEnabled() const { |
| 120 return is_enabled_; |
| 121 } |
| 122 |
| 123 base::trace_event::TraceConfig TraceConfigFile::GetTraceConfig() const { |
| 124 DCHECK(IsEnabled()); |
| 125 return trace_config_; |
| 126 } |
| 127 |
| 128 int TraceConfigFile::GetStartupDuration() const { |
| 129 DCHECK(IsEnabled()); |
| 130 return startup_duration_; |
| 131 } |
| 132 |
| 133 #if !defined(OS_ANDROID) |
| 134 base::FilePath TraceConfigFile::GetResultFile() const { |
| 135 DCHECK(IsEnabled()); |
| 136 return result_file_; |
| 137 } |
| 138 #endif |
| 139 |
| 140 } // namespace tracing |
OLD | NEW |