| 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 "chrome/browser/net/net_log_temp_file.h" | 5 #include "components/net_log/net_log_temp_file.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" |
| 7 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_file.h" | 9 #include "base/files/scoped_file.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "chrome/browser/net/chrome_net_log.h" | 11 #include "components/net_log/chrome_net_log.h" |
| 11 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "net/log/write_to_file_net_log_observer.h" | 12 #include "net/log/write_to_file_net_log_observer.h" |
| 14 | 13 |
| 15 using content::BrowserThread; | 14 namespace net_log { |
| 16 | 15 |
| 17 // Path of logs relative to base::GetTempDir(). Must be kept in sync | 16 // Path of logs relative to base::GetTempDir(). Must be kept in sync |
| 18 // with chrome/android/java/res/xml/file_paths.xml | 17 // with chrome/android/java/res/xml/file_paths.xml |
| 19 base::FilePath::CharType kLogRelativePath[] = | 18 base::FilePath::CharType kLogRelativePath[] = |
| 20 FILE_PATH_LITERAL("net-export/chrome-net-export-log.json"); | 19 FILE_PATH_LITERAL("net-export/chrome-net-export-log.json"); |
| 21 | 20 |
| 22 // Old path used by net-export. Used to delete old files. | 21 // Old path used by net-export. Used to delete old files. |
| 23 // TODO(mmenke): Should remove at some point. Added in M46. | 22 // TODO(mmenke): Should remove at some point. Added in M46. |
| 24 base::FilePath::CharType kOldLogRelativePath[] = | 23 base::FilePath::CharType kOldLogRelativePath[] = |
| 25 FILE_PATH_LITERAL("chrome-net-export-log.json"); | 24 FILE_PATH_LITERAL("chrome-net-export-log.json"); |
| 26 | 25 |
| 27 NetLogTempFile::~NetLogTempFile() { | 26 NetLogTempFile::~NetLogTempFile() { |
| 28 if (write_to_file_observer_) | 27 if (write_to_file_observer_) |
| 29 write_to_file_observer_->StopObserving(nullptr); | 28 write_to_file_observer_->StopObserving(nullptr); |
| 30 } | 29 } |
| 31 | 30 |
| 32 void NetLogTempFile::ProcessCommand(Command command) { | 31 void NetLogTempFile::ProcessCommand(Command command) { |
| 33 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 32 DCHECK(thread_checker_.CalledOnValidThread()); |
| 34 if (!EnsureInit()) | 33 if (!EnsureInit()) |
| 35 return; | 34 return; |
| 36 | 35 |
| 37 switch (command) { | 36 switch (command) { |
| 38 case DO_START_LOG_BYTES: | 37 case DO_START_LOG_BYTES: |
| 39 StartNetLog(LOG_TYPE_LOG_BYTES); | 38 StartNetLog(LOG_TYPE_LOG_BYTES); |
| 40 break; | 39 break; |
| 41 case DO_START: | 40 case DO_START: |
| 42 StartNetLog(LOG_TYPE_NORMAL); | 41 StartNetLog(LOG_TYPE_NORMAL); |
| 43 break; | 42 break; |
| 44 case DO_START_STRIP_PRIVATE_DATA: | 43 case DO_START_STRIP_PRIVATE_DATA: |
| 45 StartNetLog(LOG_TYPE_STRIP_PRIVATE_DATA); | 44 StartNetLog(LOG_TYPE_STRIP_PRIVATE_DATA); |
| 46 break; | 45 break; |
| 47 case DO_STOP: | 46 case DO_STOP: |
| 48 StopNetLog(); | 47 StopNetLog(); |
| 49 break; | 48 break; |
| 50 default: | 49 default: |
| 51 NOTREACHED(); | 50 NOTREACHED(); |
| 52 break; | 51 break; |
| 53 } | 52 } |
| 54 } | 53 } |
| 55 | 54 |
| 56 bool NetLogTempFile::GetFilePath(base::FilePath* path) { | 55 bool NetLogTempFile::GetFilePath(base::FilePath* path) { |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 58 if (log_type_ == LOG_TYPE_NONE || state_ == STATE_LOGGING) | 57 if (log_type_ == LOG_TYPE_NONE || state_ == STATE_LOGGING) |
| 59 return false; | 58 return false; |
| 60 | 59 |
| 61 if (!NetExportLogExists()) | 60 if (!NetExportLogExists()) |
| 62 return false; | 61 return false; |
| 63 | 62 |
| 64 DCHECK(!log_path_.empty()); | 63 DCHECK(!log_path_.empty()); |
| 65 #if defined(OS_POSIX) | 64 #if defined(OS_POSIX) |
| 66 // Users, group and others can read, write and traverse. | 65 // Users, group and others can read, write and traverse. |
| 67 int mode = base::FILE_PERMISSION_MASK; | 66 int mode = base::FILE_PERMISSION_MASK; |
| 68 base::SetPosixFilePermissions(log_path_, mode); | 67 base::SetPosixFilePermissions(log_path_, mode); |
| 69 #endif // defined(OS_POSIX) | 68 #endif // defined(OS_POSIX) |
| 70 | 69 |
| 71 *path = log_path_; | 70 *path = log_path_; |
| 72 return true; | 71 return true; |
| 73 } | 72 } |
| 74 | 73 |
| 75 base::DictionaryValue* NetLogTempFile::GetState() { | 74 base::DictionaryValue* NetLogTempFile::GetState() { |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 75 DCHECK(thread_checker_.CalledOnValidThread()); |
| 77 base::DictionaryValue* dict = new base::DictionaryValue; | 76 base::DictionaryValue* dict = new base::DictionaryValue; |
| 78 | 77 |
| 79 EnsureInit(); | 78 EnsureInit(); |
| 80 | 79 |
| 81 #ifndef NDEBUG | 80 #ifndef NDEBUG |
| 82 dict->SetString("file", log_path_.LossyDisplayName()); | 81 dict->SetString("file", log_path_.LossyDisplayName()); |
| 83 #endif // NDEBUG | 82 #endif // NDEBUG |
| 84 | 83 |
| 85 switch (state_) { | 84 switch (state_) { |
| 86 case STATE_NOT_LOGGING: | 85 case STATE_NOT_LOGGING: |
| (...skipping 21 matching lines...) Expand all Loading... |
| 108 dict->SetString("logType", "NORMAL"); | 107 dict->SetString("logType", "NORMAL"); |
| 109 break; | 108 break; |
| 110 case LOG_TYPE_STRIP_PRIVATE_DATA: | 109 case LOG_TYPE_STRIP_PRIVATE_DATA: |
| 111 dict->SetString("logType", "STRIP_PRIVATE_DATA"); | 110 dict->SetString("logType", "STRIP_PRIVATE_DATA"); |
| 112 break; | 111 break; |
| 113 } | 112 } |
| 114 | 113 |
| 115 return dict; | 114 return dict; |
| 116 } | 115 } |
| 117 | 116 |
| 118 NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log) | 117 NetLogTempFile::NetLogTempFile( |
| 118 ChromeNetLog* chrome_net_log, |
| 119 const base::CommandLine::StringType& command_line_string, |
| 120 const std::string& channel_string) |
| 119 : state_(STATE_UNINITIALIZED), | 121 : state_(STATE_UNINITIALIZED), |
| 120 log_type_(LOG_TYPE_NONE), | 122 log_type_(LOG_TYPE_NONE), |
| 121 chrome_net_log_(chrome_net_log) { | 123 chrome_net_log_(chrome_net_log), |
| 124 command_line_string_(command_line_string), |
| 125 channel_string_(channel_string) { |
| 126 // NetLogTempFile can be created on one thread and used on another. |
| 127 thread_checker_.DetachFromThread(); |
| 122 } | 128 } |
| 123 | 129 |
| 124 bool NetLogTempFile::GetNetExportLogBaseDirectory( | 130 bool NetLogTempFile::GetNetExportLogBaseDirectory(base::FilePath* path) const { |
| 125 base::FilePath* path) const { | 131 DCHECK(thread_checker_.CalledOnValidThread()); |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | |
| 127 return base::GetTempDir(path); | 132 return base::GetTempDir(path); |
| 128 } | 133 } |
| 129 | 134 |
| 130 net::NetLogCaptureMode NetLogTempFile::GetCaptureModeForLogType( | 135 net::NetLogCaptureMode NetLogTempFile::GetCaptureModeForLogType( |
| 131 LogType log_type) { | 136 LogType log_type) { |
| 132 switch (log_type) { | 137 switch (log_type) { |
| 133 case LOG_TYPE_LOG_BYTES: | 138 case LOG_TYPE_LOG_BYTES: |
| 134 return net::NetLogCaptureMode::IncludeSocketBytes(); | 139 return net::NetLogCaptureMode::IncludeSocketBytes(); |
| 135 case LOG_TYPE_NORMAL: | 140 case LOG_TYPE_NORMAL: |
| 136 return net::NetLogCaptureMode::IncludeCookiesAndCredentials(); | 141 return net::NetLogCaptureMode::IncludeCookiesAndCredentials(); |
| 137 case LOG_TYPE_STRIP_PRIVATE_DATA: | 142 case LOG_TYPE_STRIP_PRIVATE_DATA: |
| 138 return net::NetLogCaptureMode::Default(); | 143 return net::NetLogCaptureMode::Default(); |
| 139 case LOG_TYPE_NONE: | 144 case LOG_TYPE_NONE: |
| 140 case LOG_TYPE_UNKNOWN: | 145 case LOG_TYPE_UNKNOWN: |
| 141 NOTREACHED(); | 146 NOTREACHED(); |
| 142 } | 147 } |
| 143 return net::NetLogCaptureMode::Default(); | 148 return net::NetLogCaptureMode::Default(); |
| 144 } | 149 } |
| 145 | 150 |
| 146 bool NetLogTempFile::EnsureInit() { | 151 bool NetLogTempFile::EnsureInit() { |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 152 DCHECK(thread_checker_.CalledOnValidThread()); |
| 148 if (state_ != STATE_UNINITIALIZED) | 153 if (state_ != STATE_UNINITIALIZED) |
| 149 return true; | 154 return true; |
| 150 | 155 |
| 151 if (!SetUpNetExportLogPath()) | 156 if (!SetUpNetExportLogPath()) |
| 152 return false; | 157 return false; |
| 153 | 158 |
| 154 state_ = STATE_NOT_LOGGING; | 159 state_ = STATE_NOT_LOGGING; |
| 155 if (NetExportLogExists()) | 160 if (NetExportLogExists()) |
| 156 log_type_ = LOG_TYPE_UNKNOWN; | 161 log_type_ = LOG_TYPE_UNKNOWN; |
| 157 else | 162 else |
| 158 log_type_ = LOG_TYPE_NONE; | 163 log_type_ = LOG_TYPE_NONE; |
| 159 | 164 |
| 160 return true; | 165 return true; |
| 161 } | 166 } |
| 162 | 167 |
| 163 void NetLogTempFile::StartNetLog(LogType log_type) { | 168 void NetLogTempFile::StartNetLog(LogType log_type) { |
| 164 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 169 DCHECK(thread_checker_.CalledOnValidThread()); |
| 165 if (state_ == STATE_LOGGING) | 170 if (state_ == STATE_LOGGING) |
| 166 return; | 171 return; |
| 167 | 172 |
| 168 DCHECK_NE(STATE_UNINITIALIZED, state_); | 173 DCHECK_NE(STATE_UNINITIALIZED, state_); |
| 169 DCHECK(!log_path_.empty()); | 174 DCHECK(!log_path_.empty()); |
| 170 | 175 |
| 171 // Try to make sure we can create the file. | 176 // Try to make sure we can create the file. |
| 172 // TODO(rtenneti): Find a better for doing the following. Surface some error | 177 // TODO(rtenneti): Find a better for doing the following. Surface some error |
| 173 // to the user if we couldn't create the file. | 178 // to the user if we couldn't create the file. |
| 174 base::ScopedFILE file(base::OpenFile(log_path_, "w")); | 179 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 175 if (!file) | 180 if (!file) |
| 176 return; | 181 return; |
| 177 | 182 |
| 178 log_type_ = log_type; | 183 log_type_ = log_type; |
| 179 state_ = STATE_LOGGING; | 184 state_ = STATE_LOGGING; |
| 180 | 185 |
| 181 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); | 186 scoped_ptr<base::Value> constants( |
| 187 ChromeNetLog::GetConstants(command_line_string_, channel_string_)); |
| 182 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); | 188 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); |
| 183 write_to_file_observer_->set_capture_mode(GetCaptureModeForLogType(log_type)); | 189 write_to_file_observer_->set_capture_mode(GetCaptureModeForLogType(log_type)); |
| 184 write_to_file_observer_->StartObserving(chrome_net_log_, file.Pass(), | 190 write_to_file_observer_->StartObserving(chrome_net_log_, file.Pass(), |
| 185 constants.get(), nullptr); | 191 constants.get(), nullptr); |
| 186 } | 192 } |
| 187 | 193 |
| 188 void NetLogTempFile::StopNetLog() { | 194 void NetLogTempFile::StopNetLog() { |
| 189 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 195 DCHECK(thread_checker_.CalledOnValidThread()); |
| 190 if (state_ != STATE_LOGGING) | 196 if (state_ != STATE_LOGGING) |
| 191 return; | 197 return; |
| 192 | 198 |
| 193 write_to_file_observer_->StopObserving(nullptr); | 199 write_to_file_observer_->StopObserving(nullptr); |
| 194 write_to_file_observer_.reset(); | 200 write_to_file_observer_.reset(); |
| 195 state_ = STATE_NOT_LOGGING; | 201 state_ = STATE_NOT_LOGGING; |
| 196 } | 202 } |
| 197 | 203 |
| 198 bool NetLogTempFile::SetUpNetExportLogPath() { | 204 bool NetLogTempFile::SetUpNetExportLogPath() { |
| 199 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 205 DCHECK(thread_checker_.CalledOnValidThread()); |
| 200 base::FilePath temp_dir; | 206 base::FilePath temp_dir; |
| 201 if (!GetNetExportLogBaseDirectory(&temp_dir)) | 207 if (!GetNetExportLogBaseDirectory(&temp_dir)) |
| 202 return false; | 208 return false; |
| 203 | 209 |
| 204 // Delete log file at old location, if present. | 210 // Delete log file at old location, if present. |
| 205 DeleteFile(temp_dir.Append(kOldLogRelativePath), false); | 211 DeleteFile(temp_dir.Append(kOldLogRelativePath), false); |
| 206 | 212 |
| 207 base::FilePath log_path = temp_dir.Append(kLogRelativePath); | 213 base::FilePath log_path = temp_dir.Append(kLogRelativePath); |
| 208 | 214 |
| 209 if (!base::CreateDirectoryAndGetError(log_path.DirName(), | 215 if (!base::CreateDirectoryAndGetError(log_path.DirName(), nullptr)) { |
| 210 nullptr)) { | |
| 211 return false; | 216 return false; |
| 212 } | 217 } |
| 213 | 218 |
| 214 log_path_ = log_path; | 219 log_path_ = log_path; |
| 215 return true; | 220 return true; |
| 216 } | 221 } |
| 217 | 222 |
| 218 bool NetLogTempFile::NetExportLogExists() const { | 223 bool NetLogTempFile::NetExportLogExists() const { |
| 219 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 224 DCHECK(thread_checker_.CalledOnValidThread()); |
| 220 DCHECK(!log_path_.empty()); | 225 DCHECK(!log_path_.empty()); |
| 221 return base::PathExists(log_path_); | 226 return base::PathExists(log_path_); |
| 222 } | 227 } |
| 228 |
| 229 } // namespace net_log |
| OLD | NEW |