Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/net_log/net_log_file_writer.h" | 5 #include "components/net_log/net_log_file_writer.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback.h" | |
| 9 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/task_runner_util.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 12 #include "base/values.h" | 17 #include "base/values.h" |
| 13 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 14 #include "components/net_log/chrome_net_log.h" | 19 #include "components/net_log/chrome_net_log.h" |
| 15 #include "net/log/write_to_file_net_log_observer.h" | 20 #include "net/log/file_net_log_observer.h" |
| 21 #include "net/log/net_log_util.h" | |
| 22 #include "net/url_request/url_request_context_getter.h" | |
| 16 | 23 |
| 17 namespace net_log { | 24 namespace net_log { |
| 18 | 25 |
| 19 // Path of logs if relative to default temporary directory of | 26 // Path of logs relative to default temporary directory given by |
| 20 // base::GetTempDir(). Must be kept in sync with | 27 // base::GetTempDir(). Must be kept in sync with |
| 21 // chrome/android/java/res/xml/file_paths.xml. Only used if | 28 // chrome/android/java/res/xml/file_paths.xml. Only used if not saving log file |
| 22 // not saving log file to a custom path. | 29 // to a custom path. |
| 23 base::FilePath::CharType kLogRelativePath[] = | 30 base::FilePath::CharType kLogRelativePath[] = |
| 24 FILE_PATH_LITERAL("net-export/chrome-net-export-log.json"); | 31 FILE_PATH_LITERAL("net-export/chrome-net-export-log.json"); |
| 25 | 32 |
| 26 // Old path used by net-export. Used to delete old files. | 33 // Old path used by net-export. Used to delete old files. |
| 27 // TODO(mmenke): Should remove at some point. Added in M46. | 34 // TODO(mmenke): Should remove at some point. Added in M46. |
| 28 base::FilePath::CharType kOldLogRelativePath[] = | 35 base::FilePath::CharType kOldLogRelativePath[] = |
| 29 FILE_PATH_LITERAL("chrome-net-export-log.json"); | 36 FILE_PATH_LITERAL("chrome-net-export-log.json"); |
| 30 | 37 |
| 38 // Adds net info from net::GetNetInfo() to |polled_data|. Must run on the | |
| 39 // |net_task_runner_| of NetLogFileWriter. | |
| 40 std::unique_ptr<base::DictionaryValue> AddNetInfo( | |
| 41 scoped_refptr<net::URLRequestContextGetter> context_getter, | |
| 42 std::unique_ptr<base::DictionaryValue> polled_data) { | |
| 43 DCHECK(context_getter); | |
| 44 std::unique_ptr<base::DictionaryValue> net_info = net::GetNetInfo( | |
| 45 context_getter->GetURLRequestContext(), net::NET_INFO_ALL_SOURCES); | |
| 46 if (polled_data) | |
| 47 net_info->MergeDictionary(polled_data.get()); | |
| 48 return net_info; | |
| 49 } | |
| 50 | |
| 51 // If running on a POSIX OS, this sets all the permission flags of the file at | |
| 52 // |path| to 1. Returns false if file does not exist. | |
| 53 bool SetPosixFilePermissionsAll(const base::FilePath& path) { | |
| 54 if (!base::PathExists(path)) | |
| 55 return false; | |
| 56 #if defined(OS_POSIX) | |
| 57 return base::SetPosixFilePermissions(path, base::FILE_PERMISSION_MASK); | |
| 58 #else | |
| 59 return true; | |
| 60 #endif | |
| 61 } | |
| 62 | |
| 31 NetLogFileWriter::~NetLogFileWriter() { | 63 NetLogFileWriter::~NetLogFileWriter() { |
| 32 if (write_to_file_observer_) | 64 if (write_to_file_observer_) |
| 33 write_to_file_observer_->StopObserving(nullptr); | 65 write_to_file_observer_->StopObserving(nullptr, base::Bind([] {})); |
| 34 } | |
| 35 | |
| 36 void NetLogFileWriter::ProcessCommand(Command command) { | |
| 37 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 38 if (!EnsureInit()) | |
| 39 return; | |
| 40 | |
| 41 switch (command) { | |
| 42 case DO_START_LOG_BYTES: | |
| 43 StartNetLog(LOG_TYPE_LOG_BYTES); | |
| 44 break; | |
| 45 case DO_START: | |
| 46 StartNetLog(LOG_TYPE_NORMAL); | |
| 47 break; | |
| 48 case DO_START_STRIP_PRIVATE_DATA: | |
| 49 StartNetLog(LOG_TYPE_STRIP_PRIVATE_DATA); | |
| 50 break; | |
| 51 case DO_STOP: | |
| 52 StopNetLog(); | |
| 53 break; | |
| 54 default: | |
| 55 NOTREACHED(); | |
| 56 break; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 bool NetLogFileWriter::GetFilePath(base::FilePath* path) { | |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 62 if (log_type_ == LOG_TYPE_NONE || state_ == STATE_LOGGING) | |
| 63 return false; | |
| 64 | |
| 65 if (!NetExportLogExists()) | |
| 66 return false; | |
| 67 | |
| 68 DCHECK(!log_path_.empty()); | |
| 69 #if defined(OS_POSIX) | |
| 70 // Users, group and others can read, write and traverse. | |
| 71 int mode = base::FILE_PERMISSION_MASK; | |
| 72 base::SetPosixFilePermissions(log_path_, mode); | |
| 73 #endif // defined(OS_POSIX) | |
| 74 | |
| 75 *path = log_path_; | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 base::DictionaryValue* NetLogFileWriter::GetState() { | |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 81 base::DictionaryValue* dict = new base::DictionaryValue; | |
| 82 | |
| 83 EnsureInit(); | |
| 84 | |
| 85 #ifndef NDEBUG | |
| 86 dict->SetString("file", log_path_.LossyDisplayName()); | |
| 87 #endif // NDEBUG | |
| 88 | |
| 89 switch (state_) { | |
| 90 case STATE_NOT_LOGGING: | |
| 91 dict->SetString("state", "NOT_LOGGING"); | |
| 92 break; | |
| 93 case STATE_LOGGING: | |
| 94 dict->SetString("state", "LOGGING"); | |
| 95 break; | |
| 96 case STATE_UNINITIALIZED: | |
| 97 dict->SetString("state", "UNINITIALIZED"); | |
| 98 break; | |
| 99 } | |
| 100 | |
| 101 switch (log_type_) { | |
| 102 case LOG_TYPE_NONE: | |
| 103 dict->SetString("logType", "NONE"); | |
| 104 break; | |
| 105 case LOG_TYPE_UNKNOWN: | |
| 106 dict->SetString("logType", "UNKNOWN"); | |
| 107 break; | |
| 108 case LOG_TYPE_LOG_BYTES: | |
| 109 dict->SetString("logType", "LOG_BYTES"); | |
| 110 break; | |
| 111 case LOG_TYPE_NORMAL: | |
| 112 dict->SetString("logType", "NORMAL"); | |
| 113 break; | |
| 114 case LOG_TYPE_STRIP_PRIVATE_DATA: | |
| 115 dict->SetString("logType", "STRIP_PRIVATE_DATA"); | |
| 116 break; | |
| 117 } | |
| 118 | |
| 119 return dict; | |
| 120 } | 66 } |
| 121 | 67 |
| 122 NetLogFileWriter::NetLogFileWriter( | 68 NetLogFileWriter::NetLogFileWriter( |
| 123 ChromeNetLog* chrome_net_log, | 69 ChromeNetLog* chrome_net_log, |
| 124 const base::CommandLine::StringType& command_line_string, | 70 const base::CommandLine::StringType& command_line_string, |
| 125 const std::string& channel_string) | 71 const std::string& channel_string) |
| 126 : state_(STATE_UNINITIALIZED), | 72 : state_(STATE_UNINITIALIZED), |
| 127 log_type_(LOG_TYPE_NONE), | 73 log_exists_(false), |
| 74 log_capture_mode_known_(false), | |
| 75 log_capture_mode_(net::NetLogCaptureMode::Default()), | |
| 128 chrome_net_log_(chrome_net_log), | 76 chrome_net_log_(chrome_net_log), |
| 129 command_line_string_(command_line_string), | 77 command_line_string_(command_line_string), |
| 130 channel_string_(channel_string) { | 78 channel_string_(channel_string), |
| 131 // NetLogFileWriter can be created on one thread and used on another. | 79 default_log_base_directory_getter_(base::Bind(&base::GetTempDir)), |
| 132 thread_checker_.DetachFromThread(); | 80 weak_ptr_factory_(this) {} |
| 133 } | 81 |
| 134 | 82 void NetLogFileWriter::StartNetLog(const base::FilePath& log_path, |
| 135 bool NetLogFileWriter::GetNetExportLogBaseDirectory( | 83 net::NetLogCaptureMode capture_mode, |
| 136 base::FilePath* path) const { | 84 const StateCallback& state_callback) { |
| 137 DCHECK(thread_checker_.CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 138 return base::GetTempDir(path); | 86 |
| 139 } | 87 // StartLogging(), if executed, will always be executed asynchronously so that |
| 140 | 88 // |state_callback| is always executed asynchronously relative to the |
| 141 net::NetLogCaptureMode NetLogFileWriter::GetCaptureModeForLogType( | 89 // StartNetLog() call. |
| 142 LogType log_type) { | 90 |
| 143 switch (log_type) { | 91 base::Closure start_logging_callback = base::Bind( |
| 144 case LOG_TYPE_LOG_BYTES: | 92 &NetLogFileWriter::StartLogging, weak_ptr_factory_.GetWeakPtr(), log_path, |
| 145 return net::NetLogCaptureMode::IncludeSocketBytes(); | 93 capture_mode, state_callback); |
| 146 case LOG_TYPE_NORMAL: | 94 |
| 147 return net::NetLogCaptureMode::IncludeCookiesAndCredentials(); | 95 if (state_ == STATE_UNINITIALIZED) { |
| 148 case LOG_TYPE_STRIP_PRIVATE_DATA: | 96 InitThenCallback(start_logging_callback); |
| 149 return net::NetLogCaptureMode::Default(); | 97 } else if (state_ == STATE_NOT_LOGGING) { |
| 150 case LOG_TYPE_NONE: | 98 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 151 case LOG_TYPE_UNKNOWN: | 99 start_logging_callback); |
| 152 NOTREACHED(); | 100 } else { |
| 153 } | 101 RunStateCallbackAsync(state_callback); |
|
eroman
2017/01/18 21:32:43
If StartNetLog() is called multiple times the firs
wangyix1
2017/01/19 23:09:46
Done.
| |
| 154 return net::NetLogCaptureMode::Default(); | 102 } |
| 155 } | 103 } |
| 156 | 104 |
| 157 bool NetLogFileWriter::EnsureInit() { | 105 void NetLogFileWriter::StartLogging(const base::FilePath& log_path, |
| 158 DCHECK(thread_checker_.CalledOnValidThread()); | 106 net::NetLogCaptureMode capture_mode, |
| 159 if (state_ != STATE_UNINITIALIZED) | 107 const StateCallback& state_callback) { |
| 160 return true; | 108 DCHECK(thread_checker_.CalledOnValidThread()); |
| 161 | 109 DCHECK(file_task_runner_); |
| 162 if (log_path_.empty() && !SetUpDefaultNetExportLogPath()) | 110 |
| 163 return false; | 111 // Check if NetLogFileWriter is properly initialized. |
| 164 | 112 if (state_ == STATE_NOT_LOGGING) { |
| 165 state_ = STATE_NOT_LOGGING; | 113 if (!log_path.empty()) |
| 166 if (NetExportLogExists()) | 114 log_path_ = log_path; |
| 167 log_type_ = LOG_TYPE_UNKNOWN; | 115 |
| 168 else | 116 state_ = STATE_LOGGING; |
| 169 log_type_ = LOG_TYPE_NONE; | 117 log_exists_ = true; |
| 170 | 118 log_capture_mode_known_ = true; |
| 171 return true; | 119 log_capture_mode_ = capture_mode; |
| 172 } | 120 |
| 173 | 121 DCHECK(!log_path_.empty()); |
| 174 void NetLogFileWriter::StartNetLog(LogType log_type) { | 122 |
| 175 DCHECK(thread_checker_.CalledOnValidThread()); | 123 std::unique_ptr<base::Value> constants( |
| 176 if (state_ == STATE_LOGGING) | 124 ChromeNetLog::GetConstants(command_line_string_, channel_string_)); |
| 177 return; | 125 write_to_file_observer_.reset( |
| 178 | 126 new net::FileNetLogObserver(file_task_runner_)); |
| 179 DCHECK_NE(STATE_UNINITIALIZED, state_); | 127 write_to_file_observer_->StartObservingUnbounded( |
| 180 DCHECK(!log_path_.empty()); | 128 chrome_net_log_, capture_mode, log_path_, std::move(constants), |
| 181 | 129 nullptr); |
| 182 // Try to make sure we can create the file. | 130 } else { |
| 183 // TODO(rtenneti): Find a better for doing the following. Surface some error | 131 // Only way to get here is if StartLogging() was called as a result of a |
|
eroman
2017/01/18 21:32:43
Because of when StartNetLog() posts this task, thi
wangyix1
2017/01/19 23:09:46
Done.
| |
| 184 // to the user if we couldn't create the file. | 132 // StartNetLog() that triggered initialization and the initialization |
| 185 base::ScopedFILE file(base::OpenFile(log_path_, "w")); | 133 // failed. In that case, simply run |state_callback| and nothing else. |
| 186 if (!file) | 134 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
| 187 return; | 135 } |
| 188 | 136 |
| 189 log_type_ = log_type; | 137 RunStateCallback(state_callback); |
| 190 state_ = STATE_LOGGING; | 138 } |
| 191 | 139 |
| 192 std::unique_ptr<base::Value> constants( | 140 void NetLogFileWriter::StopNetLog( |
| 193 ChromeNetLog::GetConstants(command_line_string_, channel_string_)); | 141 std::unique_ptr<base::DictionaryValue> polled_data, |
| 194 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); | 142 scoped_refptr<net::URLRequestContextGetter> context_getter, |
| 195 write_to_file_observer_->set_capture_mode(GetCaptureModeForLogType(log_type)); | 143 const StateCallback& state_callback) { |
| 196 write_to_file_observer_->StartObserving(chrome_net_log_, std::move(file), | 144 DCHECK(thread_checker_.CalledOnValidThread()); |
| 197 constants.get(), nullptr); | 145 DCHECK(net_task_runner_); |
| 198 } | 146 if (state_ == STATE_LOGGING) { |
| 199 | 147 // Stopping the log requires first grabbing the net info on the net thread. |
| 200 void NetLogFileWriter::StopNetLog() { | 148 // Before posting that task to the net thread, change the state to |
| 201 DCHECK(thread_checker_.CalledOnValidThread()); | 149 // STATE_STOP_LOGGING so that if the NetLogFileWriter receives a command |
| 202 if (state_ != STATE_LOGGING) | 150 // while the net info is being retrieved on the net thread, the state can be |
| 203 return; | 151 // checked and the command can be ignored. It's the responsibility of the |
| 204 | 152 // commands (StartNetLog(), StopNetLog(), GetState()) to check the state |
| 205 write_to_file_observer_->StopObserving(nullptr); | 153 // before performing their actions. |
| 154 state_ = STATE_STOPPING_LOG; | |
| 155 | |
| 156 // StopLogging() will always execute its state callback asynchronously, | |
| 157 // which means |state_callback| will always be executed asynchronously | |
| 158 // relative to the StopNetLog() call regardless of how StopLogging() is | |
| 159 // called here. | |
| 160 | |
| 161 if (context_getter) { | |
| 162 base::PostTaskAndReplyWithResult( | |
| 163 net_task_runner_.get(), FROM_HERE, | |
| 164 base::Bind(&AddNetInfo, context_getter, base::Passed(&polled_data)), | |
| 165 base::Bind(&NetLogFileWriter::StopLogging, | |
| 166 weak_ptr_factory_.GetWeakPtr(), state_callback)); | |
| 167 } else { | |
| 168 StopLogging(state_callback, std::move(polled_data)); | |
| 169 } | |
| 170 } else { | |
| 171 RunStateCallbackAsync(state_callback); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 void NetLogFileWriter::StopLogging( | |
| 176 const StateCallback& state_callback, | |
| 177 std::unique_ptr<base::DictionaryValue> polled_data) { | |
| 178 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 179 DCHECK_EQ(state_, STATE_STOPPING_LOG); | |
| 180 | |
| 181 write_to_file_observer_->StopObserving( | |
| 182 std::move(polled_data), | |
| 183 base::Bind(&NetLogFileWriter::ResetObserverThenSetStateNotLogging, | |
| 184 weak_ptr_factory_.GetWeakPtr(), state_callback)); | |
| 185 } | |
| 186 | |
| 187 void NetLogFileWriter::ResetObserverThenSetStateNotLogging( | |
| 188 const StateCallback& state_callback) { | |
| 189 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 206 write_to_file_observer_.reset(); | 190 write_to_file_observer_.reset(); |
| 207 state_ = STATE_NOT_LOGGING; | 191 state_ = STATE_NOT_LOGGING; |
| 208 } | 192 |
| 209 | 193 RunStateCallback(state_callback); |
| 210 void NetLogFileWriter::SetUpNetExportLogPath( | 194 } |
| 211 const base::FilePath& custom_path) { | 195 |
| 212 DCHECK(thread_checker_.CalledOnValidThread()); | 196 void NetLogFileWriter::GetState(const StateCallback& state_callback) { |
| 213 | 197 DCHECK(thread_checker_.CalledOnValidThread()); |
| 214 // The directory should always exist because the custom path | 198 if (state_ == STATE_UNINITIALIZED) { |
| 215 // is taken from a file selector dialog window. | 199 InitThenCallback(base::Bind(&NetLogFileWriter::RunStateCallback, |
| 216 DCHECK(base::PathExists(custom_path.DirName())); | 200 weak_ptr_factory_.GetWeakPtr(), |
| 217 | 201 state_callback)); |
| 218 log_path_ = custom_path; | 202 } else { |
| 219 } | 203 RunStateCallbackAsync(state_callback); |
| 220 | 204 } |
| 221 bool NetLogFileWriter::SetUpDefaultNetExportLogPath() { | 205 } |
| 222 DCHECK(thread_checker_.CalledOnValidThread()); | 206 |
| 223 base::FilePath temp_dir; | 207 std::unique_ptr<base::DictionaryValue> NetLogFileWriter::GetState() const { |
| 224 if (!GetNetExportLogBaseDirectory(&temp_dir)) | 208 DCHECK(thread_checker_.CalledOnValidThread()); |
| 225 return false; | 209 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 210 | |
| 211 #ifndef NDEBUG | |
| 212 dict->SetString("file", log_path_.LossyDisplayName()); | |
| 213 #endif // NDEBUG | |
| 214 | |
| 215 switch (state_) { | |
| 216 case STATE_UNINITIALIZED: | |
| 217 dict->SetString("state", "UNINITIALIZED"); | |
| 218 break; | |
| 219 case STATE_INITIALIZING: | |
| 220 dict->SetString("state", "INITIALIZING"); | |
| 221 break; | |
| 222 case STATE_NOT_LOGGING: | |
| 223 dict->SetString("state", "NOT_LOGGING"); | |
| 224 break; | |
| 225 case STATE_LOGGING: | |
| 226 dict->SetString("state", "LOGGING"); | |
| 227 break; | |
| 228 case STATE_STOPPING_LOG: | |
| 229 dict->SetString("state", "STOPPING_LOG"); | |
| 230 break; | |
| 231 } | |
| 232 | |
| 233 dict->SetBoolean("logExists", log_exists_); | |
| 234 dict->SetBoolean("logCaptureModeKnown", log_capture_mode_known_); | |
| 235 dict->SetString("captureMode", CaptureModeToString(log_capture_mode_)); | |
| 236 | |
| 237 return dict; | |
| 238 } | |
| 239 | |
| 240 void NetLogFileWriter::RunStateCallback( | |
| 241 const StateCallback& state_callback) const { | |
| 242 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 243 state_callback.Run(GetState()); | |
| 244 } | |
| 245 | |
| 246 void NetLogFileWriter::RunStateCallbackAsync( | |
| 247 const StateCallback& state_callback) const { | |
| 248 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 249 std::unique_ptr<base::DictionaryValue> state = GetState(); | |
|
eroman
2017/01/18 21:32:43
There are disadvantages to running this immediatel
wangyix1
2017/01/19 23:09:46
Done.
| |
| 250 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 251 FROM_HERE, base::Bind(state_callback, base::Passed(&state))); | |
| 252 } | |
| 253 | |
| 254 void NetLogFileWriter::GetFilePathToCompletedLog( | |
| 255 const FilePathCallback& path_callback) const { | |
| 256 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 257 if (!log_exists_ || state_ == STATE_LOGGING) { | |
| 258 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 259 FROM_HERE, base::Bind(path_callback, base::FilePath())); | |
| 260 return; | |
| 261 } | |
| 262 | |
| 263 DCHECK(file_task_runner_); | |
| 264 DCHECK(!log_path_.empty()); | |
| 265 | |
| 266 base::PostTaskAndReplyWithResult( | |
| 267 file_task_runner_.get(), FROM_HERE, | |
| 268 base::Bind(&SetPosixFilePermissionsAll, log_path_), | |
| 269 base::Bind(&NetLogFileWriter::RunFilePathCallback, path_callback, | |
| 270 log_path_)); | |
| 271 } | |
| 272 | |
| 273 void NetLogFileWriter::RunFilePathCallback( | |
| 274 const FilePathCallback& path_callback, | |
| 275 const base::FilePath& path, | |
| 276 bool set_file_permissions_success) { | |
| 277 if (set_file_permissions_success) | |
| 278 path_callback.Run(path); | |
| 279 else | |
| 280 path_callback.Run(base::FilePath()); | |
| 281 } | |
| 282 | |
| 283 void NetLogFileWriter::InitThenCallback(const base::Closure& callback) { | |
|
eroman
2017/01/18 21:32:43
Here is how I think the initialization can be hand
wangyix1
2017/01/19 23:09:46
Done.
| |
| 284 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 285 DCHECK(file_task_runner_); | |
| 286 | |
| 287 // Before posting the file thread tasks, change state to STATE_INITIALIZING so | |
| 288 // that if the NetLogFileWriter receives a command while initialization tasks | |
| 289 // are running on the file thread, the state can be checked and the command | |
| 290 // can be ignored. It's the responsibility of the commands (StartNetLog(), | |
| 291 // StopNetLog(), GetState()) to check the state before performing their | |
| 292 // actions. | |
| 293 DCHECK_EQ(state_, STATE_UNINITIALIZED); | |
| 294 state_ = STATE_INITIALIZING; | |
| 295 | |
| 296 base::PostTaskAndReplyWithResult( | |
| 297 file_task_runner_.get(), FROM_HERE, | |
| 298 base::Bind(&NetLogFileWriter::SetUpDefaultLogPath, | |
| 299 default_log_base_directory_getter_), | |
| 300 base::Bind(&NetLogFileWriter::InitStateThenCallback, | |
| 301 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 302 } | |
| 303 | |
| 304 NetLogFileWriter::DefaultLogPathResults NetLogFileWriter::SetUpDefaultLogPath( | |
| 305 const DirectoryGetter& default_log_base_directory_getter) { | |
| 306 DefaultLogPathResults results; | |
| 307 results.default_log_path_success = false; | |
| 308 results.log_exists = false; | |
| 309 | |
| 310 base::FilePath default_base_dir; | |
| 311 if (!default_log_base_directory_getter.Run(&default_base_dir)) | |
| 312 return results; | |
| 226 | 313 |
| 227 // Delete log file at old location, if present. | 314 // Delete log file at old location, if present. |
| 228 DeleteFile(temp_dir.Append(kOldLogRelativePath), false); | 315 base::DeleteFile(default_base_dir.Append(kOldLogRelativePath), false); |
| 229 | 316 |
| 230 base::FilePath log_path = temp_dir.Append(kLogRelativePath); | 317 results.default_log_path = default_base_dir.Append(kLogRelativePath); |
| 231 | 318 if (!base::CreateDirectoryAndGetError(results.default_log_path.DirName(), |
| 232 if (!base::CreateDirectoryAndGetError(log_path.DirName(), nullptr)) { | 319 nullptr)) |
| 233 return false; | 320 return results; |
| 234 } | 321 |
| 235 | 322 results.log_exists = base::PathExists(results.default_log_path); |
| 236 log_path_ = log_path; | 323 results.default_log_path_success = true; |
| 237 return true; | 324 return results; |
| 238 } | 325 } |
| 239 | 326 |
| 240 bool NetLogFileWriter::NetExportLogExists() const { | 327 void NetLogFileWriter::InitStateThenCallback( |
|
eroman
2017/01/18 21:32:43
These names are very difficult for me to follow be
wangyix1
2017/01/19 23:09:46
Done.
| |
| 241 DCHECK(thread_checker_.CalledOnValidThread()); | 328 const base::Closure& callback, |
| 242 DCHECK(!log_path_.empty()); | 329 const DefaultLogPathResults& set_up_default_log_path_results) { |
| 243 return base::PathExists(log_path_); | 330 DCHECK(thread_checker_.CalledOnValidThread()); |
| 331 DCHECK_EQ(state_, STATE_INITIALIZING); | |
| 332 | |
| 333 if (set_up_default_log_path_results.default_log_path_success) { | |
| 334 state_ = STATE_NOT_LOGGING; | |
| 335 log_path_ = set_up_default_log_path_results.default_log_path; | |
| 336 log_exists_ = set_up_default_log_path_results.log_exists; | |
| 337 DCHECK(!log_capture_mode_known_); | |
| 338 } else { | |
| 339 state_ = STATE_UNINITIALIZED; | |
| 340 } | |
| 341 | |
| 342 callback.Run(); | |
| 343 } | |
| 344 | |
| 345 void NetLogFileWriter::SetTaskRunners( | |
| 346 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | |
| 347 scoped_refptr<base::SingleThreadTaskRunner> net_task_runner) { | |
| 348 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 349 if (file_task_runner_) | |
| 350 DCHECK_EQ(file_task_runner, file_task_runner_); | |
| 351 file_task_runner_ = file_task_runner; | |
| 352 | |
| 353 if (net_task_runner_) | |
| 354 DCHECK_EQ(net_task_runner, net_task_runner_); | |
| 355 net_task_runner_ = net_task_runner; | |
| 356 } | |
| 357 | |
| 358 std::string NetLogFileWriter::CaptureModeToString( | |
| 359 net::NetLogCaptureMode capture_mode) { | |
| 360 if (capture_mode == net::NetLogCaptureMode::Default()) { | |
| 361 return "STRIP_PRIVATE_DATA"; | |
| 362 } else if (capture_mode == | |
| 363 net::NetLogCaptureMode::IncludeCookiesAndCredentials()) { | |
| 364 return "NORMAL"; | |
| 365 } else if (capture_mode == net::NetLogCaptureMode::IncludeSocketBytes()) { | |
| 366 return "LOG_BYTES"; | |
| 367 } else { | |
| 368 NOTREACHED(); | |
| 369 return "NORMAL"; | |
| 370 } | |
| 371 } | |
| 372 | |
| 373 net::NetLogCaptureMode NetLogFileWriter::CaptureModeFromString( | |
| 374 const std::string& capture_mode_string) { | |
| 375 if (capture_mode_string == "STRIP_PRIVATE_DATA") { | |
| 376 return net::NetLogCaptureMode::Default(); | |
| 377 } else if (capture_mode_string == "NORMAL") { | |
| 378 return net::NetLogCaptureMode::IncludeCookiesAndCredentials(); | |
| 379 } else if (capture_mode_string == "LOG_BYTES") { | |
| 380 return net::NetLogCaptureMode::IncludeSocketBytes(); | |
| 381 } else { | |
| 382 NOTREACHED(); | |
| 383 return net::NetLogCaptureMode::IncludeCookiesAndCredentials(); | |
| 384 } | |
| 385 } | |
| 386 | |
| 387 void NetLogFileWriter::SetDefaultLogBaseDirectoryGetter( | |
| 388 const DirectoryGetter& getter) { | |
| 389 default_log_base_directory_getter_ = getter; | |
| 244 } | 390 } |
| 245 | 391 |
| 246 } // namespace net_log | 392 } // namespace net_log |
| OLD | NEW |