| 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(ChromeNetLog* chrome_net_log, |
| 118 const std::string& channel_string) |
| 119 : state_(STATE_UNINITIALIZED), | 119 : state_(STATE_UNINITIALIZED), |
| 120 log_type_(LOG_TYPE_NONE), | 120 log_type_(LOG_TYPE_NONE), |
| 121 chrome_net_log_(chrome_net_log) { | 121 chrome_net_log_(chrome_net_log), |
| 122 channel_string_(channel_string) { |
| 123 // NetLogTempFile can be created on one thread and used on another. |
| 124 thread_checker_.DetachFromThread(); |
| 122 } | 125 } |
| 123 | 126 |
| 124 bool NetLogTempFile::GetNetExportLogBaseDirectory( | 127 bool NetLogTempFile::GetNetExportLogBaseDirectory(base::FilePath* path) const { |
| 125 base::FilePath* path) const { | 128 DCHECK(thread_checker_.CalledOnValidThread()); |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | |
| 127 return base::GetTempDir(path); | 129 return base::GetTempDir(path); |
| 128 } | 130 } |
| 129 | 131 |
| 130 net::NetLogCaptureMode NetLogTempFile::GetCaptureModeForLogType( | 132 net::NetLogCaptureMode NetLogTempFile::GetCaptureModeForLogType( |
| 131 LogType log_type) { | 133 LogType log_type) { |
| 132 switch (log_type) { | 134 switch (log_type) { |
| 133 case LOG_TYPE_LOG_BYTES: | 135 case LOG_TYPE_LOG_BYTES: |
| 134 return net::NetLogCaptureMode::IncludeSocketBytes(); | 136 return net::NetLogCaptureMode::IncludeSocketBytes(); |
| 135 case LOG_TYPE_NORMAL: | 137 case LOG_TYPE_NORMAL: |
| 136 return net::NetLogCaptureMode::IncludeCookiesAndCredentials(); | 138 return net::NetLogCaptureMode::IncludeCookiesAndCredentials(); |
| 137 case LOG_TYPE_STRIP_PRIVATE_DATA: | 139 case LOG_TYPE_STRIP_PRIVATE_DATA: |
| 138 return net::NetLogCaptureMode::Default(); | 140 return net::NetLogCaptureMode::Default(); |
| 139 case LOG_TYPE_NONE: | 141 case LOG_TYPE_NONE: |
| 140 case LOG_TYPE_UNKNOWN: | 142 case LOG_TYPE_UNKNOWN: |
| 141 NOTREACHED(); | 143 NOTREACHED(); |
| 142 } | 144 } |
| 143 return net::NetLogCaptureMode::Default(); | 145 return net::NetLogCaptureMode::Default(); |
| 144 } | 146 } |
| 145 | 147 |
| 146 bool NetLogTempFile::EnsureInit() { | 148 bool NetLogTempFile::EnsureInit() { |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 149 DCHECK(thread_checker_.CalledOnValidThread()); |
| 148 if (state_ != STATE_UNINITIALIZED) | 150 if (state_ != STATE_UNINITIALIZED) |
| 149 return true; | 151 return true; |
| 150 | 152 |
| 151 if (!SetUpNetExportLogPath()) | 153 if (!SetUpNetExportLogPath()) |
| 152 return false; | 154 return false; |
| 153 | 155 |
| 154 state_ = STATE_NOT_LOGGING; | 156 state_ = STATE_NOT_LOGGING; |
| 155 if (NetExportLogExists()) | 157 if (NetExportLogExists()) |
| 156 log_type_ = LOG_TYPE_UNKNOWN; | 158 log_type_ = LOG_TYPE_UNKNOWN; |
| 157 else | 159 else |
| 158 log_type_ = LOG_TYPE_NONE; | 160 log_type_ = LOG_TYPE_NONE; |
| 159 | 161 |
| 160 return true; | 162 return true; |
| 161 } | 163 } |
| 162 | 164 |
| 163 void NetLogTempFile::StartNetLog(LogType log_type) { | 165 void NetLogTempFile::StartNetLog(LogType log_type) { |
| 164 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 166 DCHECK(thread_checker_.CalledOnValidThread()); |
| 165 if (state_ == STATE_LOGGING) | 167 if (state_ == STATE_LOGGING) |
| 166 return; | 168 return; |
| 167 | 169 |
| 168 DCHECK_NE(STATE_UNINITIALIZED, state_); | 170 DCHECK_NE(STATE_UNINITIALIZED, state_); |
| 169 DCHECK(!log_path_.empty()); | 171 DCHECK(!log_path_.empty()); |
| 170 | 172 |
| 171 // Try to make sure we can create the file. | 173 // Try to make sure we can create the file. |
| 172 // TODO(rtenneti): Find a better for doing the following. Surface some error | 174 // TODO(rtenneti): Find a better for doing the following. Surface some error |
| 173 // to the user if we couldn't create the file. | 175 // to the user if we couldn't create the file. |
| 174 base::ScopedFILE file(base::OpenFile(log_path_, "w")); | 176 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 175 if (!file) | 177 if (!file) |
| 176 return; | 178 return; |
| 177 | 179 |
| 178 log_type_ = log_type; | 180 log_type_ = log_type; |
| 179 state_ = STATE_LOGGING; | 181 state_ = STATE_LOGGING; |
| 180 | 182 |
| 181 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); | 183 scoped_ptr<base::Value> constants( |
| 184 ChromeNetLog::GetConstants(channel_string_)); |
| 182 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); | 185 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); |
| 183 write_to_file_observer_->set_capture_mode(GetCaptureModeForLogType(log_type)); | 186 write_to_file_observer_->set_capture_mode(GetCaptureModeForLogType(log_type)); |
| 184 write_to_file_observer_->StartObserving(chrome_net_log_, file.Pass(), | 187 write_to_file_observer_->StartObserving(chrome_net_log_, file.Pass(), |
| 185 constants.get(), nullptr); | 188 constants.get(), nullptr); |
| 186 } | 189 } |
| 187 | 190 |
| 188 void NetLogTempFile::StopNetLog() { | 191 void NetLogTempFile::StopNetLog() { |
| 189 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 192 DCHECK(thread_checker_.CalledOnValidThread()); |
| 190 if (state_ != STATE_LOGGING) | 193 if (state_ != STATE_LOGGING) |
| 191 return; | 194 return; |
| 192 | 195 |
| 193 write_to_file_observer_->StopObserving(nullptr); | 196 write_to_file_observer_->StopObserving(nullptr); |
| 194 write_to_file_observer_.reset(); | 197 write_to_file_observer_.reset(); |
| 195 state_ = STATE_NOT_LOGGING; | 198 state_ = STATE_NOT_LOGGING; |
| 196 } | 199 } |
| 197 | 200 |
| 198 bool NetLogTempFile::SetUpNetExportLogPath() { | 201 bool NetLogTempFile::SetUpNetExportLogPath() { |
| 199 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 202 DCHECK(thread_checker_.CalledOnValidThread()); |
| 200 base::FilePath temp_dir; | 203 base::FilePath temp_dir; |
| 201 if (!GetNetExportLogBaseDirectory(&temp_dir)) | 204 if (!GetNetExportLogBaseDirectory(&temp_dir)) |
| 202 return false; | 205 return false; |
| 203 | 206 |
| 204 // Delete log file at old location, if present. | 207 // Delete log file at old location, if present. |
| 205 DeleteFile(temp_dir.Append(kOldLogRelativePath), false); | 208 DeleteFile(temp_dir.Append(kOldLogRelativePath), false); |
| 206 | 209 |
| 207 base::FilePath log_path = temp_dir.Append(kLogRelativePath); | 210 base::FilePath log_path = temp_dir.Append(kLogRelativePath); |
| 208 | 211 |
| 209 if (!base::CreateDirectoryAndGetError(log_path.DirName(), | 212 if (!base::CreateDirectoryAndGetError(log_path.DirName(), nullptr)) { |
| 210 nullptr)) { | |
| 211 return false; | 213 return false; |
| 212 } | 214 } |
| 213 | 215 |
| 214 log_path_ = log_path; | 216 log_path_ = log_path; |
| 215 return true; | 217 return true; |
| 216 } | 218 } |
| 217 | 219 |
| 218 bool NetLogTempFile::NetExportLogExists() const { | 220 bool NetLogTempFile::NetExportLogExists() const { |
| 219 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | 221 DCHECK(thread_checker_.CalledOnValidThread()); |
| 220 DCHECK(!log_path_.empty()); | 222 DCHECK(!log_path_.empty()); |
| 221 return base::PathExists(log_path_); | 223 return base::PathExists(log_path_); |
| 222 } | 224 } |
| 225 |
| 226 } // namespace net_log |
| OLD | NEW |