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 EnsureInitThenRun( |
| 140 | 88 base::Bind(&NetLogFileWriter::StartNetLogAfterInitialized, |
| 141 net::NetLogCaptureMode NetLogFileWriter::GetCaptureModeForLogType( | 89 weak_ptr_factory_.GetWeakPtr(), log_path, capture_mode), |
| 142 LogType log_type) { | 90 state_callback, true); |
| 143 switch (log_type) { | 91 } |
| 144 case LOG_TYPE_LOG_BYTES: | 92 |
| 145 return net::NetLogCaptureMode::IncludeSocketBytes(); | 93 void NetLogFileWriter::StartNetLogAfterInitialized( |
|
eroman
2017/01/21 02:17:40
style: in chrome code we try to match the definiti
wangyix1
2017/01/23 20:16:42
Done.
| |
| 146 case LOG_TYPE_NORMAL: | 94 const base::FilePath& log_path, |
| 147 return net::NetLogCaptureMode::IncludeCookiesAndCredentials(); | 95 net::NetLogCaptureMode capture_mode) { |
| 148 case LOG_TYPE_STRIP_PRIVATE_DATA: | 96 DCHECK(thread_checker_.CalledOnValidThread()); |
| 149 return net::NetLogCaptureMode::Default(); | 97 DCHECK(state_ != STATE_UNINITIALIZED && state_ != STATE_INITIALIZING); |
| 150 case LOG_TYPE_NONE: | 98 DCHECK(file_task_runner_); |
| 151 case LOG_TYPE_UNKNOWN: | 99 |
| 152 NOTREACHED(); | 100 if (state_ == STATE_NOT_LOGGING) { |
| 153 } | 101 if (!log_path.empty()) |
| 154 return net::NetLogCaptureMode::Default(); | 102 log_path_ = log_path; |
| 155 } | 103 |
| 156 | 104 state_ = STATE_LOGGING; |
| 157 bool NetLogFileWriter::EnsureInit() { | 105 log_exists_ = true; |
| 158 DCHECK(thread_checker_.CalledOnValidThread()); | 106 log_capture_mode_known_ = true; |
| 159 if (state_ != STATE_UNINITIALIZED) | 107 log_capture_mode_ = capture_mode; |
| 160 return true; | 108 |
| 161 | 109 DCHECK(!log_path_.empty()); |
|
eroman
2017/01/21 02:17:40
[optional] I suggest moving this up to line 103.
wangyix1
2017/01/23 20:16:43
Done.
| |
| 162 if (log_path_.empty() && !SetUpDefaultNetExportLogPath()) | 110 |
| 163 return false; | 111 std::unique_ptr<base::Value> constants( |
| 164 | 112 ChromeNetLog::GetConstants(command_line_string_, channel_string_)); |
| 165 state_ = STATE_NOT_LOGGING; | 113 write_to_file_observer_.reset( |
|
eroman
2017/01/21 02:17:40
[optional]: write_to_file_observer_ = base::MakeU
wangyix1
2017/01/23 20:16:42
Done.
| |
| 166 if (NetExportLogExists()) | 114 new net::FileNetLogObserver(file_task_runner_)); |
| 167 log_type_ = LOG_TYPE_UNKNOWN; | 115 write_to_file_observer_->StartObservingUnbounded( |
| 168 else | 116 chrome_net_log_, capture_mode, log_path_, std::move(constants), |
| 169 log_type_ = LOG_TYPE_NONE; | 117 nullptr); |
| 170 | 118 } |
| 171 return true; | 119 } |
| 172 } | 120 |
| 173 | 121 void NetLogFileWriter::StopNetLog( |
| 174 void NetLogFileWriter::StartNetLog(LogType log_type) { | 122 std::unique_ptr<base::DictionaryValue> polled_data, |
| 175 DCHECK(thread_checker_.CalledOnValidThread()); | 123 scoped_refptr<net::URLRequestContextGetter> context_getter, |
| 176 if (state_ == STATE_LOGGING) | 124 const StateCallback& state_callback) { |
| 177 return; | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
| 178 | 126 DCHECK(net_task_runner_); |
| 179 DCHECK_NE(STATE_UNINITIALIZED, state_); | 127 if (state_ == STATE_LOGGING) { |
| 180 DCHECK(!log_path_.empty()); | 128 // Stopping the log requires first grabbing the net info on the net thread. |
| 181 | 129 // Before posting that task to the net thread, change the state to |
| 182 // Try to make sure we can create the file. | 130 // STATE_STOP_LOGGING so that if the NetLogFileWriter receives a command |
| 183 // TODO(rtenneti): Find a better for doing the following. Surface some error | 131 // while the net info is being retrieved on the net thread, the state can be |
| 184 // to the user if we couldn't create the file. | 132 // checked and the command can be ignored. It's the responsibility of the |
| 185 base::ScopedFILE file(base::OpenFile(log_path_, "w")); | 133 // commands (StartNetLog(), StopNetLog(), GetState()) to check the state |
| 186 if (!file) | 134 // before performing their actions. |
| 187 return; | 135 state_ = STATE_STOPPING_LOG; |
| 188 | 136 |
| 189 log_type_ = log_type; | 137 // StopLogging() will always execute its state callback asynchronously, |
| 190 state_ = STATE_LOGGING; | 138 // which means |state_callback| will always be executed asynchronously |
| 191 | 139 // relative to the StopNetLog() call regardless of how StopLogging() is |
| 192 std::unique_ptr<base::Value> constants( | 140 // called here. |
| 193 ChromeNetLog::GetConstants(command_line_string_, channel_string_)); | 141 |
| 194 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); | 142 if (context_getter) { |
| 195 write_to_file_observer_->set_capture_mode(GetCaptureModeForLogType(log_type)); | 143 base::PostTaskAndReplyWithResult( |
| 196 write_to_file_observer_->StartObserving(chrome_net_log_, std::move(file), | 144 net_task_runner_.get(), FROM_HERE, |
| 197 constants.get(), nullptr); | 145 base::Bind(&AddNetInfo, context_getter, base::Passed(&polled_data)), |
| 198 } | 146 base::Bind(&NetLogFileWriter::StopNetLogAfterAddNetInfo, |
| 199 | 147 weak_ptr_factory_.GetWeakPtr(), state_callback)); |
| 200 void NetLogFileWriter::StopNetLog() { | 148 } else { |
| 201 DCHECK(thread_checker_.CalledOnValidThread()); | 149 StopNetLogAfterAddNetInfo(state_callback, std::move(polled_data)); |
| 202 if (state_ != STATE_LOGGING) | 150 } |
| 203 return; | 151 } else { |
| 204 | 152 // No-op; just run |state_callback| asynchronously. |
| 205 write_to_file_observer_->StopObserving(nullptr); | 153 RunStateCallbackAsync(state_callback); |
| 154 } | |
| 155 } | |
| 156 | |
| 157 void NetLogFileWriter::StopNetLogAfterAddNetInfo( | |
| 158 const StateCallback& state_callback, | |
| 159 std::unique_ptr<base::DictionaryValue> polled_data) { | |
| 160 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 161 DCHECK_EQ(state_, STATE_STOPPING_LOG); | |
| 162 | |
| 163 write_to_file_observer_->StopObserving( | |
| 164 std::move(polled_data), | |
| 165 base::Bind(&NetLogFileWriter::ResetObserverThenSetStateNotLogging, | |
| 166 weak_ptr_factory_.GetWeakPtr(), state_callback)); | |
| 167 } | |
| 168 | |
| 169 void NetLogFileWriter::ResetObserverThenSetStateNotLogging( | |
| 170 const StateCallback& state_callback) { | |
| 171 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 206 write_to_file_observer_.reset(); | 172 write_to_file_observer_.reset(); |
| 207 state_ = STATE_NOT_LOGGING; | 173 state_ = STATE_NOT_LOGGING; |
| 208 } | 174 |
| 209 | 175 RunStateCallback(state_callback); |
| 210 void NetLogFileWriter::SetUpNetExportLogPath( | 176 } |
| 211 const base::FilePath& custom_path) { | 177 |
| 212 DCHECK(thread_checker_.CalledOnValidThread()); | 178 void NetLogFileWriter::GetState(const StateCallback& state_callback) { |
| 213 | 179 DCHECK(thread_checker_.CalledOnValidThread()); |
| 214 // The directory should always exist because the custom path | 180 EnsureInitThenRun(base::Bind([] {}), state_callback, false); |
| 215 // is taken from a file selector dialog window. | 181 } |
| 216 DCHECK(base::PathExists(custom_path.DirName())); | 182 |
| 217 | 183 std::unique_ptr<base::DictionaryValue> NetLogFileWriter::GetState() const { |
| 218 log_path_ = custom_path; | 184 DCHECK(thread_checker_.CalledOnValidThread()); |
| 219 } | 185 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
|
eroman
2017/01/21 02:17:40
[optional]: auto dict = base::MakeUnique<base::Dic
wangyix1
2017/01/23 20:16:43
Done.
| |
| 220 | 186 |
| 221 bool NetLogFileWriter::SetUpDefaultNetExportLogPath() { | 187 #ifndef NDEBUG |
|
eroman
2017/01/21 02:17:40
Do you know why this is only enabled in release mo
wangyix1
2017/01/23 20:16:43
I think this is only enabled in debug mode. Having
| |
| 222 DCHECK(thread_checker_.CalledOnValidThread()); | 188 dict->SetString("file", log_path_.LossyDisplayName()); |
| 223 base::FilePath temp_dir; | 189 #endif // NDEBUG |
| 224 if (!GetNetExportLogBaseDirectory(&temp_dir)) | 190 |
| 225 return false; | 191 switch (state_) { |
| 192 case STATE_UNINITIALIZED: | |
| 193 dict->SetString("state", "UNINITIALIZED"); | |
| 194 break; | |
| 195 case STATE_INITIALIZING: | |
| 196 dict->SetString("state", "INITIALIZING"); | |
| 197 break; | |
| 198 case STATE_NOT_LOGGING: | |
| 199 dict->SetString("state", "NOT_LOGGING"); | |
| 200 break; | |
| 201 case STATE_LOGGING: | |
| 202 dict->SetString("state", "LOGGING"); | |
| 203 break; | |
| 204 case STATE_STOPPING_LOG: | |
| 205 dict->SetString("state", "STOPPING_LOG"); | |
| 206 break; | |
| 207 } | |
| 208 | |
| 209 dict->SetBoolean("logExists", log_exists_); | |
| 210 dict->SetBoolean("logCaptureModeKnown", log_capture_mode_known_); | |
| 211 dict->SetString("captureMode", CaptureModeToString(log_capture_mode_)); | |
| 212 | |
| 213 return dict; | |
| 214 } | |
| 215 | |
| 216 void NetLogFileWriter::GetFilePathToCompletedLog( | |
| 217 const FilePathCallback& path_callback) const { | |
| 218 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 219 if (!log_exists_ || state_ == STATE_LOGGING) { | |
| 220 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 221 FROM_HERE, base::Bind(path_callback, base::FilePath())); | |
| 222 return; | |
| 223 } | |
| 224 | |
| 225 DCHECK(file_task_runner_); | |
| 226 DCHECK(!log_path_.empty()); | |
| 227 | |
| 228 base::PostTaskAndReplyWithResult( | |
|
eroman
2017/01/21 02:17:40
[optional] Another way to express this would be:
wangyix1
2017/01/23 20:16:43
Done.
| |
| 229 file_task_runner_.get(), FROM_HERE, | |
| 230 base::Bind(&SetPosixFilePermissionsAll, log_path_), | |
| 231 base::Bind(&NetLogFileWriter::RunFilePathCallback, path_callback, | |
| 232 log_path_)); | |
| 233 } | |
| 234 | |
| 235 void NetLogFileWriter::RunFilePathCallback( | |
| 236 const FilePathCallback& path_callback, | |
| 237 const base::FilePath& path, | |
| 238 bool set_file_permissions_success) { | |
| 239 if (set_file_permissions_success) | |
| 240 path_callback.Run(path); | |
| 241 else | |
| 242 path_callback.Run(base::FilePath()); | |
| 243 } | |
| 244 | |
| 245 void NetLogFileWriter::SetTaskRunners( | |
| 246 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | |
| 247 scoped_refptr<base::SingleThreadTaskRunner> net_task_runner) { | |
| 248 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 249 if (file_task_runner_) | |
| 250 DCHECK_EQ(file_task_runner, file_task_runner_); | |
| 251 file_task_runner_ = file_task_runner; | |
| 252 | |
| 253 if (net_task_runner_) | |
| 254 DCHECK_EQ(net_task_runner, net_task_runner_); | |
| 255 net_task_runner_ = net_task_runner; | |
| 256 } | |
| 257 | |
| 258 std::string NetLogFileWriter::CaptureModeToString( | |
| 259 net::NetLogCaptureMode capture_mode) { | |
| 260 if (capture_mode == net::NetLogCaptureMode::Default()) { | |
| 261 return "STRIP_PRIVATE_DATA"; | |
| 262 } else if (capture_mode == | |
| 263 net::NetLogCaptureMode::IncludeCookiesAndCredentials()) { | |
| 264 return "NORMAL"; | |
| 265 } else if (capture_mode == net::NetLogCaptureMode::IncludeSocketBytes()) { | |
| 266 return "LOG_BYTES"; | |
| 267 } else { | |
| 268 NOTREACHED(); | |
| 269 return "STRIP_PRIVATE_DATA"; | |
| 270 } | |
| 271 } | |
| 272 | |
| 273 net::NetLogCaptureMode NetLogFileWriter::CaptureModeFromString( | |
| 274 const std::string& capture_mode_string) { | |
| 275 if (capture_mode_string == "STRIP_PRIVATE_DATA") { | |
| 276 return net::NetLogCaptureMode::Default(); | |
| 277 } else if (capture_mode_string == "NORMAL") { | |
| 278 return net::NetLogCaptureMode::IncludeCookiesAndCredentials(); | |
| 279 } else if (capture_mode_string == "LOG_BYTES") { | |
| 280 return net::NetLogCaptureMode::IncludeSocketBytes(); | |
| 281 } else { | |
| 282 NOTREACHED(); | |
| 283 return net::NetLogCaptureMode::Default(); | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 void NetLogFileWriter::SetDefaultLogBaseDirectoryGetterForTest( | |
| 288 const DirectoryGetter& getter) { | |
| 289 default_log_base_directory_getter_ = getter; | |
| 290 } | |
| 291 | |
| 292 void NetLogFileWriter::EnsureInitThenRun( | |
| 293 const base::Closure& after_successful_init_callback, | |
| 294 const StateCallback& state_callback, | |
| 295 bool state_callback_wait_if_already_initializing) { | |
| 296 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 297 | |
| 298 if (state_ == STATE_UNINITIALIZED) { | |
| 299 state_ = STATE_INITIALIZING; | |
| 300 // Run initialization tasks on the file thread, then the main thread. Once | |
| 301 // finished, run |after_successful_init_callback| and |state_callback| on | |
| 302 // the main thread. | |
| 303 base::PostTaskAndReplyWithResult( | |
| 304 file_task_runner_.get(), FROM_HERE, | |
| 305 base::Bind(&NetLogFileWriter::SetUpDefaultLogPath, | |
| 306 default_log_base_directory_getter_), | |
| 307 base::Bind(&NetLogFileWriter::SetStateAfterSetUpDefaultLogPathThenRun, | |
| 308 weak_ptr_factory_.GetWeakPtr(), | |
| 309 after_successful_init_callback, state_callback)); | |
| 310 } else if (state_ == STATE_INITIALIZING) { | |
| 311 // If NetLogFileWriter is already in the process of initializing due to a | |
| 312 // previous call to EnsureInitThenRun(), commands received by | |
| 313 // NetLogFileWriter should be ignored, so only |state_callback| will be | |
| 314 // executed. | |
| 315 if (state_callback_wait_if_already_initializing) { | |
|
eroman
2017/01/21 02:17:40
I would say that all cases, including GetState(),
wangyix1
2017/01/23 20:16:42
It makes more sense to me from an API perspective:
wangyix1
2017/01/25 22:48:27
Done. Changed my mind
| |
| 316 // Wait for the in-progress initialization to finish before calling | |
| 317 // |state_callback|. To do this, post a dummy task to the file thread | |
| 318 // (which is guaranteed to run after the tasks belonging to the | |
| 319 // in-progress initialization have finished), and have that dummy task | |
| 320 // post |state_callback| as a reply on the main thread. | |
| 321 file_task_runner_->PostTaskAndReply( | |
| 322 FROM_HERE, base::Bind([] {}), | |
| 323 base::Bind(&NetLogFileWriter::RunStateCallback, | |
| 324 weak_ptr_factory_.GetWeakPtr(), state_callback)); | |
| 325 } else { | |
| 326 RunStateCallbackAsync(state_callback); | |
| 327 } | |
| 328 } else { | |
| 329 // NetLogFileWriter is already fully initialized. Run | |
| 330 // |after_successful_init_callback| synchronously and |state_callback| | |
| 331 // asynchronously. | |
| 332 after_successful_init_callback.Run(); | |
| 333 RunStateCallbackAsync(state_callback); | |
| 334 } | |
| 335 } | |
| 336 | |
| 337 NetLogFileWriter::DefaultLogPathResults NetLogFileWriter::SetUpDefaultLogPath( | |
| 338 const DirectoryGetter& default_log_base_directory_getter) { | |
| 339 DefaultLogPathResults results; | |
| 340 results.default_log_path_success = false; | |
| 341 results.log_exists = false; | |
| 342 | |
| 343 base::FilePath default_base_dir; | |
| 344 if (!default_log_base_directory_getter.Run(&default_base_dir)) | |
| 345 return results; | |
| 226 | 346 |
| 227 // Delete log file at old location, if present. | 347 // Delete log file at old location, if present. |
| 228 DeleteFile(temp_dir.Append(kOldLogRelativePath), false); | 348 base::DeleteFile(default_base_dir.Append(kOldLogRelativePath), false); |
| 229 | 349 |
| 230 base::FilePath log_path = temp_dir.Append(kLogRelativePath); | 350 results.default_log_path = default_base_dir.Append(kLogRelativePath); |
| 231 | 351 if (!base::CreateDirectoryAndGetError(results.default_log_path.DirName(), |
| 232 if (!base::CreateDirectoryAndGetError(log_path.DirName(), nullptr)) { | 352 nullptr)) |
| 233 return false; | 353 return results; |
| 234 } | 354 |
| 235 | 355 results.log_exists = base::PathExists(results.default_log_path); |
| 236 log_path_ = log_path; | 356 results.default_log_path_success = true; |
| 237 return true; | 357 return results; |
| 238 } | 358 } |
| 239 | 359 |
| 240 bool NetLogFileWriter::NetExportLogExists() const { | 360 void NetLogFileWriter::SetStateAfterSetUpDefaultLogPathThenRun( |
| 241 DCHECK(thread_checker_.CalledOnValidThread()); | 361 const base::Closure& after_successful_init_callback, |
| 242 DCHECK(!log_path_.empty()); | 362 const StateCallback& state_callback, |
| 243 return base::PathExists(log_path_); | 363 const DefaultLogPathResults& set_up_default_log_path_results) { |
| 364 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 365 DCHECK_EQ(state_, STATE_INITIALIZING); | |
| 366 | |
| 367 if (set_up_default_log_path_results.default_log_path_success) { | |
| 368 state_ = STATE_NOT_LOGGING; | |
| 369 log_path_ = set_up_default_log_path_results.default_log_path; | |
| 370 log_exists_ = set_up_default_log_path_results.log_exists; | |
| 371 DCHECK(!log_capture_mode_known_); | |
| 372 | |
| 373 after_successful_init_callback.Run(); | |
| 374 } else { | |
| 375 state_ = STATE_UNINITIALIZED; | |
| 376 } | |
| 377 | |
| 378 RunStateCallback(state_callback); | |
| 379 } | |
| 380 | |
| 381 void NetLogFileWriter::RunStateCallback( | |
| 382 const StateCallback& state_callback) const { | |
| 383 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 384 state_callback.Run(GetState()); | |
| 385 } | |
| 386 | |
| 387 void NetLogFileWriter::RunStateCallbackAsync( | |
| 388 const StateCallback& state_callback) { | |
| 389 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 390 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 391 FROM_HERE, base::Bind(&NetLogFileWriter::RunStateCallback, | |
| 392 weak_ptr_factory_.GetWeakPtr(), state_callback)); | |
| 244 } | 393 } |
| 245 | 394 |
| 246 } // namespace net_log | 395 } // namespace net_log |
| OLD | NEW |