| 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 "chrome/browser/net/net_log_temp_file.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_file.h" | 8 #include "base/files/scoped_file.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/net/chrome_net_log.h" | 10 #include "chrome/browser/net/chrome_net_log.h" |
| 11 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" | 11 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "net/log/write_to_file_net_log_observer.h" | 13 #include "net/log/write_to_file_net_log_observer.h" |
| 14 | 14 |
| 15 using content::BrowserThread; | 15 using content::BrowserThread; |
| 16 | 16 |
| 17 NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log) | 17 NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log) |
| 18 : state_(STATE_UNINITIALIZED), | 18 : state_(STATE_UNINITIALIZED), |
| 19 log_type_(LOG_TYPE_NONE), | 19 log_type_(LOG_TYPE_NONE), |
| 20 log_filename_(FILE_PATH_LITERAL("chrome-net-export-log.json")), | 20 log_filename_(FILE_PATH_LITERAL("chrome-net-export-log.json")), |
| 21 chrome_net_log_(chrome_net_log) { | 21 chrome_net_log_(chrome_net_log) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 NetLogTempFile::~NetLogTempFile() { | 24 NetLogTempFile::~NetLogTempFile() { |
| 25 if (write_to_file_observer_) | 25 if (write_to_file_observer_) |
| 26 write_to_file_observer_->StopObserving(nullptr); | 26 write_to_file_observer_->StopObserving(nullptr); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void NetLogTempFile::ProcessCommand(Command command) { | 29 void NetLogTempFile::ProcessCommand(Command command) { |
| 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 30 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); |
| 31 if (!EnsureInit()) | 31 if (!EnsureInit()) |
| 32 return; | 32 return; |
| 33 | 33 |
| 34 switch (command) { | 34 switch (command) { |
| 35 case DO_START_LOG_BYTES: | 35 case DO_START_LOG_BYTES: |
| 36 StartNetLog(LOG_TYPE_LOG_BYTES); | 36 StartNetLog(LOG_TYPE_LOG_BYTES); |
| 37 break; | 37 break; |
| 38 case DO_START: | 38 case DO_START: |
| 39 StartNetLog(LOG_TYPE_NORMAL); | 39 StartNetLog(LOG_TYPE_NORMAL); |
| 40 break; | 40 break; |
| 41 case DO_START_STRIP_PRIVATE_DATA: | 41 case DO_START_STRIP_PRIVATE_DATA: |
| 42 StartNetLog(LOG_TYPE_STRIP_PRIVATE_DATA); | 42 StartNetLog(LOG_TYPE_STRIP_PRIVATE_DATA); |
| 43 break; | 43 break; |
| 44 case DO_STOP: | 44 case DO_STOP: |
| 45 StopNetLog(); | 45 StopNetLog(); |
| 46 break; | 46 break; |
| 47 default: | 47 default: |
| 48 NOTREACHED(); | 48 NOTREACHED(); |
| 49 break; | 49 break; |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 base::DictionaryValue* NetLogTempFile::GetState() { | 53 base::DictionaryValue* NetLogTempFile::GetState() { |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 54 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); |
| 55 base::DictionaryValue* dict = new base::DictionaryValue; | 55 base::DictionaryValue* dict = new base::DictionaryValue; |
| 56 | 56 |
| 57 EnsureInit(); | 57 EnsureInit(); |
| 58 | 58 |
| 59 #ifndef NDEBUG | 59 #ifndef NDEBUG |
| 60 dict->SetString("file", log_path_.LossyDisplayName()); | 60 dict->SetString("file", log_path_.LossyDisplayName()); |
| 61 #endif // NDEBUG | 61 #endif // NDEBUG |
| 62 | 62 |
| 63 switch (state_) { | 63 switch (state_) { |
| 64 case STATE_NOT_LOGGING: | 64 case STATE_NOT_LOGGING: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 case LOG_TYPE_STRIP_PRIVATE_DATA: | 103 case LOG_TYPE_STRIP_PRIVATE_DATA: |
| 104 return net::NetLogCaptureMode::Default(); | 104 return net::NetLogCaptureMode::Default(); |
| 105 case LOG_TYPE_NONE: | 105 case LOG_TYPE_NONE: |
| 106 case LOG_TYPE_UNKNOWN: | 106 case LOG_TYPE_UNKNOWN: |
| 107 NOTREACHED(); | 107 NOTREACHED(); |
| 108 } | 108 } |
| 109 return net::NetLogCaptureMode::Default(); | 109 return net::NetLogCaptureMode::Default(); |
| 110 } | 110 } |
| 111 | 111 |
| 112 bool NetLogTempFile::EnsureInit() { | 112 bool NetLogTempFile::EnsureInit() { |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 113 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); |
| 114 if (state_ != STATE_UNINITIALIZED) | 114 if (state_ != STATE_UNINITIALIZED) |
| 115 return true; | 115 return true; |
| 116 | 116 |
| 117 if (!GetNetExportLog()) | 117 if (!GetNetExportLog()) |
| 118 return false; | 118 return false; |
| 119 | 119 |
| 120 state_ = STATE_NOT_LOGGING; | 120 state_ = STATE_NOT_LOGGING; |
| 121 if (NetExportLogExists()) | 121 if (NetExportLogExists()) |
| 122 log_type_ = LOG_TYPE_UNKNOWN; | 122 log_type_ = LOG_TYPE_UNKNOWN; |
| 123 else | 123 else |
| 124 log_type_ = LOG_TYPE_NONE; | 124 log_type_ = LOG_TYPE_NONE; |
| 125 | 125 |
| 126 return true; | 126 return true; |
| 127 } | 127 } |
| 128 | 128 |
| 129 void NetLogTempFile::StartNetLog(LogType log_type) { | 129 void NetLogTempFile::StartNetLog(LogType log_type) { |
| 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 130 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); |
| 131 if (state_ == STATE_LOGGING) | 131 if (state_ == STATE_LOGGING) |
| 132 return; | 132 return; |
| 133 | 133 |
| 134 DCHECK_NE(STATE_UNINITIALIZED, state_); | 134 DCHECK_NE(STATE_UNINITIALIZED, state_); |
| 135 DCHECK(!log_path_.empty()); | 135 DCHECK(!log_path_.empty()); |
| 136 | 136 |
| 137 // Try to make sure we can create the file. | 137 // Try to make sure we can create the file. |
| 138 // TODO(rtenneti): Find a better for doing the following. Surface some error | 138 // TODO(rtenneti): Find a better for doing the following. Surface some error |
| 139 // to the user if we couldn't create the file. | 139 // to the user if we couldn't create the file. |
| 140 base::ScopedFILE file(base::OpenFile(log_path_, "w")); | 140 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 141 if (!file) | 141 if (!file) |
| 142 return; | 142 return; |
| 143 | 143 |
| 144 log_type_ = log_type; | 144 log_type_ = log_type; |
| 145 state_ = STATE_LOGGING; | 145 state_ = STATE_LOGGING; |
| 146 | 146 |
| 147 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); | 147 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); |
| 148 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); | 148 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); |
| 149 write_to_file_observer_->set_capture_mode(GetCaptureModeForLogType(log_type)); | 149 write_to_file_observer_->set_capture_mode(GetCaptureModeForLogType(log_type)); |
| 150 write_to_file_observer_->StartObserving(chrome_net_log_, file.Pass(), | 150 write_to_file_observer_->StartObserving(chrome_net_log_, file.Pass(), |
| 151 constants.get(), nullptr); | 151 constants.get(), nullptr); |
| 152 } | 152 } |
| 153 | 153 |
| 154 void NetLogTempFile::StopNetLog() { | 154 void NetLogTempFile::StopNetLog() { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 155 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); |
| 156 if (state_ != STATE_LOGGING) | 156 if (state_ != STATE_LOGGING) |
| 157 return; | 157 return; |
| 158 | 158 |
| 159 write_to_file_observer_->StopObserving(nullptr); | 159 write_to_file_observer_->StopObserving(nullptr); |
| 160 write_to_file_observer_.reset(); | 160 write_to_file_observer_.reset(); |
| 161 state_ = STATE_NOT_LOGGING; | 161 state_ = STATE_NOT_LOGGING; |
| 162 } | 162 } |
| 163 | 163 |
| 164 bool NetLogTempFile::GetFilePath(base::FilePath* path) { | 164 bool NetLogTempFile::GetFilePath(base::FilePath* path) { |
| 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 165 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); |
| 166 if (log_type_ == LOG_TYPE_NONE || state_ == STATE_LOGGING) | 166 if (log_type_ == LOG_TYPE_NONE || state_ == STATE_LOGGING) |
| 167 return false; | 167 return false; |
| 168 | 168 |
| 169 if (!NetExportLogExists()) | 169 if (!NetExportLogExists()) |
| 170 return false; | 170 return false; |
| 171 | 171 |
| 172 DCHECK(!log_path_.empty()); | 172 DCHECK(!log_path_.empty()); |
| 173 #if defined(OS_POSIX) | 173 #if defined(OS_POSIX) |
| 174 // Users, group and others can read, write and traverse. | 174 // Users, group and others can read, write and traverse. |
| 175 int mode = base::FILE_PERMISSION_MASK; | 175 int mode = base::FILE_PERMISSION_MASK; |
| 176 base::SetPosixFilePermissions(log_path_, mode); | 176 base::SetPosixFilePermissions(log_path_, mode); |
| 177 #endif // defined(OS_POSIX) | 177 #endif // defined(OS_POSIX) |
| 178 | 178 |
| 179 *path = log_path_; | 179 *path = log_path_; |
| 180 return true; | 180 return true; |
| 181 } | 181 } |
| 182 | 182 |
| 183 bool NetLogTempFile::GetNetExportLog() { | 183 bool NetLogTempFile::GetNetExportLog() { |
| 184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 184 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); |
| 185 base::FilePath temp_dir; | 185 base::FilePath temp_dir; |
| 186 if (!GetNetExportLogDirectory(&temp_dir)) | 186 if (!GetNetExportLogDirectory(&temp_dir)) |
| 187 return false; | 187 return false; |
| 188 | 188 |
| 189 log_path_ = temp_dir.Append(log_filename_); | 189 log_path_ = temp_dir.Append(log_filename_); |
| 190 return true; | 190 return true; |
| 191 } | 191 } |
| 192 | 192 |
| 193 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath* path) { | 193 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath* path) { |
| 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 194 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); |
| 195 return base::GetTempDir(path); | 195 return base::GetTempDir(path); |
| 196 } | 196 } |
| 197 | 197 |
| 198 bool NetLogTempFile::NetExportLogExists() { | 198 bool NetLogTempFile::NetExportLogExists() { |
| 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 199 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); |
| 200 DCHECK(!log_path_.empty()); | 200 DCHECK(!log_path_.empty()); |
| 201 return base::PathExists(log_path_); | 201 return base::PathExists(log_path_); |
| 202 } | 202 } |
| OLD | NEW |