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