1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/net_log/net_log_file_writer.h"
#include <utility>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/net_log/chrome_net_log.h"
#include "net/log/write_to_file_net_log_observer.h"
namespace net_log {
// Path of logs if relative to default temporary directory of
// base::GetTempDir(). Must be kept in sync with
// chrome/android/java/res/xml/file_paths.xml. Only used if
// not saving log file to a custom path.
base::FilePath::CharType kLogRelativePath[] =
FILE_PATH_LITERAL("net-export/chrome-net-export-log.json");
// Old path used by net-export. Used to delete old files.
// TODO(mmenke): Should remove at some point. Added in M46.
base::FilePath::CharType kOldLogRelativePath[] =
FILE_PATH_LITERAL("chrome-net-export-log.json");
NetLogFileWriter::~NetLogFileWriter() {
if (write_to_file_observer_)
write_to_file_observer_->StopObserving(nullptr);
}
void NetLogFileWriter::ProcessCommand(Command command) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!EnsureInit())
return;
switch (command) {
case DO_START_LOG_BYTES:
StartNetLog(LOG_TYPE_LOG_BYTES);
break;
case DO_START:
StartNetLog(LOG_TYPE_NORMAL);
break;
case DO_START_STRIP_PRIVATE_DATA:
StartNetLog(LOG_TYPE_STRIP_PRIVATE_DATA);
break;
case DO_STOP:
StopNetLog();
break;
default:
NOTREACHED();
break;
}
}
bool NetLogFileWriter::GetFilePath(base::FilePath* path) {
DCHECK(thread_checker_.CalledOnValidThread());
if (log_type_ == LOG_TYPE_NONE || state_ == STATE_LOGGING)
return false;
if (!NetExportLogExists())
return false;
DCHECK(!log_path_.empty());
#if defined(OS_POSIX)
// Users, group and others can read, write and traverse.
int mode = base::FILE_PERMISSION_MASK;
base::SetPosixFilePermissions(log_path_, mode);
#endif // defined(OS_POSIX)
*path = log_path_;
return true;
}
base::DictionaryValue* NetLogFileWriter::GetState() {
DCHECK(thread_checker_.CalledOnValidThread());
base::DictionaryValue* dict = new base::DictionaryValue;
EnsureInit();
#ifndef NDEBUG
dict->SetString("file", log_path_.LossyDisplayName());
#endif // NDEBUG
switch (state_) {
case STATE_NOT_LOGGING:
dict->SetString("state", "NOT_LOGGING");
break;
case STATE_LOGGING:
dict->SetString("state", "LOGGING");
break;
case STATE_UNINITIALIZED:
dict->SetString("state", "UNINITIALIZED");
break;
}
switch (log_type_) {
case LOG_TYPE_NONE:
dict->SetString("logType", "NONE");
break;
case LOG_TYPE_UNKNOWN:
dict->SetString("logType", "UNKNOWN");
break;
case LOG_TYPE_LOG_BYTES:
dict->SetString("logType", "LOG_BYTES");
break;
case LOG_TYPE_NORMAL:
dict->SetString("logType", "NORMAL");
break;
case LOG_TYPE_STRIP_PRIVATE_DATA:
dict->SetString("logType", "STRIP_PRIVATE_DATA");
break;
}
return dict;
}
NetLogFileWriter::NetLogFileWriter(
ChromeNetLog* chrome_net_log,
const base::CommandLine::StringType& command_line_string,
const std::string& channel_string)
: state_(STATE_UNINITIALIZED),
log_type_(LOG_TYPE_NONE),
chrome_net_log_(chrome_net_log),
command_line_string_(command_line_string),
channel_string_(channel_string) {
// NetLogFileWriter can be created on one thread and used on another.
thread_checker_.DetachFromThread();
}
bool NetLogFileWriter::GetNetExportLogBaseDirectory(
base::FilePath* path) const {
DCHECK(thread_checker_.CalledOnValidThread());
return base::GetTempDir(path);
}
net::NetLogCaptureMode NetLogFileWriter::GetCaptureModeForLogType(
LogType log_type) {
switch (log_type) {
case LOG_TYPE_LOG_BYTES:
return net::NetLogCaptureMode::IncludeSocketBytes();
case LOG_TYPE_NORMAL:
return net::NetLogCaptureMode::IncludeCookiesAndCredentials();
case LOG_TYPE_STRIP_PRIVATE_DATA:
return net::NetLogCaptureMode::Default();
case LOG_TYPE_NONE:
case LOG_TYPE_UNKNOWN:
NOTREACHED();
}
return net::NetLogCaptureMode::Default();
}
bool NetLogFileWriter::EnsureInit() {
DCHECK(thread_checker_.CalledOnValidThread());
if (state_ != STATE_UNINITIALIZED)
return true;
if (log_path_.empty() && !SetUpDefaultNetExportLogPath())
return false;
state_ = STATE_NOT_LOGGING;
if (NetExportLogExists())
log_type_ = LOG_TYPE_UNKNOWN;
else
log_type_ = LOG_TYPE_NONE;
return true;
}
void NetLogFileWriter::StartNetLog(LogType log_type) {
DCHECK(thread_checker_.CalledOnValidThread());
if (state_ == STATE_LOGGING)
return;
DCHECK_NE(STATE_UNINITIALIZED, state_);
DCHECK(!log_path_.empty());
// Try to make sure we can create the file.
// TODO(rtenneti): Find a better for doing the following. Surface some error
// to the user if we couldn't create the file.
base::ScopedFILE file(base::OpenFile(log_path_, "w"));
if (!file)
return;
log_type_ = log_type;
state_ = STATE_LOGGING;
std::unique_ptr<base::Value> constants(
ChromeNetLog::GetConstants(command_line_string_, channel_string_));
write_to_file_observer_.reset(new net::WriteToFileNetLogObserver());
write_to_file_observer_->set_capture_mode(GetCaptureModeForLogType(log_type));
write_to_file_observer_->StartObserving(chrome_net_log_, std::move(file),
constants.get(), nullptr);
}
void NetLogFileWriter::StopNetLog() {
DCHECK(thread_checker_.CalledOnValidThread());
if (state_ != STATE_LOGGING)
return;
write_to_file_observer_->StopObserving(nullptr);
write_to_file_observer_.reset();
state_ = STATE_NOT_LOGGING;
}
void NetLogFileWriter::SetUpNetExportLogPath(
const base::FilePath& custom_path) {
DCHECK(thread_checker_.CalledOnValidThread());
// The directory should always exist because the custom path
// is taken from a file selector dialog window.
DCHECK(base::PathExists(custom_path.DirName()));
log_path_ = custom_path;
}
bool NetLogFileWriter::SetUpDefaultNetExportLogPath() {
DCHECK(thread_checker_.CalledOnValidThread());
base::FilePath temp_dir;
if (!GetNetExportLogBaseDirectory(&temp_dir))
return false;
// Delete log file at old location, if present.
DeleteFile(temp_dir.Append(kOldLogRelativePath), false);
base::FilePath log_path = temp_dir.Append(kLogRelativePath);
if (!base::CreateDirectoryAndGetError(log_path.DirName(), nullptr)) {
return false;
}
log_path_ = log_path;
return true;
}
bool NetLogFileWriter::NetExportLogExists() const {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!log_path_.empty());
return base::PathExists(log_path_);
}
} // namespace net_log
|