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