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