| 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/values.h" | 9 #include "base/values.h" |
| 9 #include "chrome/browser/net/chrome_net_log.h" | 10 #include "chrome/browser/net/chrome_net_log.h" |
| 10 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" | 11 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" |
| 11 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 12 #include "net/base/net_log_logger.h" | 13 #include "net/base/net_log_logger.h" |
| 13 | 14 |
| 14 using content::BrowserThread; | 15 using content::BrowserThread; |
| 15 | 16 |
| 16 NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log) | 17 NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log) |
| 17 : state_(STATE_UNINITIALIZED), | 18 : state_(STATE_UNINITIALIZED), |
| 18 log_type_(LOG_TYPE_NONE), | 19 log_type_(LOG_TYPE_NONE), |
| 19 log_filename_(FILE_PATH_LITERAL("chrome-net-export-log.json")), | 20 log_filename_(FILE_PATH_LITERAL("chrome-net-export-log.json")), |
| 20 chrome_net_log_(chrome_net_log) { | 21 chrome_net_log_(chrome_net_log) { |
| 21 } | 22 } |
| 22 | 23 |
| 23 NetLogTempFile::~NetLogTempFile() { | 24 NetLogTempFile::~NetLogTempFile() { |
| 24 if (net_log_logger_) | 25 if (net_log_logger_) |
| 25 net_log_logger_->StopObserving(); | 26 net_log_logger_->StopObserving(nullptr); |
| 26 } | 27 } |
| 27 | 28 |
| 28 void NetLogTempFile::ProcessCommand(Command command) { | 29 void NetLogTempFile::ProcessCommand(Command command) { |
| 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 30 if (!EnsureInit()) | 31 if (!EnsureInit()) |
| 31 return; | 32 return; |
| 32 | 33 |
| 33 switch (command) { | 34 switch (command) { |
| 34 case DO_START_LOG_BYTES: | 35 case DO_START_LOG_BYTES: |
| 35 StartNetLog(LOG_TYPE_LOG_BYTES); | 36 StartNetLog(LOG_TYPE_LOG_BYTES); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 129 if (state_ == STATE_LOGGING) | 130 if (state_ == STATE_LOGGING) |
| 130 return; | 131 return; |
| 131 | 132 |
| 132 DCHECK_NE(STATE_UNINITIALIZED, state_); | 133 DCHECK_NE(STATE_UNINITIALIZED, state_); |
| 133 DCHECK(!log_path_.empty()); | 134 DCHECK(!log_path_.empty()); |
| 134 | 135 |
| 135 // Try to make sure we can create the file. | 136 // Try to make sure we can create the file. |
| 136 // TODO(rtenneti): Find a better for doing the following. Surface some error | 137 // TODO(rtenneti): Find a better for doing the following. Surface some error |
| 137 // to the user if we couldn't create the file. | 138 // to the user if we couldn't create the file. |
| 138 FILE* file = base::OpenFile(log_path_, "w"); | 139 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 139 if (file == NULL) | 140 if (!file) |
| 140 return; | 141 return; |
| 141 | 142 |
| 142 log_type_ = log_type; | 143 log_type_ = log_type; |
| 143 state_ = STATE_LOGGING; | 144 state_ = STATE_LOGGING; |
| 144 | 145 |
| 145 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); | 146 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); |
| 146 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 147 net_log_logger_.reset(new net::NetLogLogger()); |
| 147 net_log_logger_->set_log_level(GetLogLevelForLogType(log_type)); | 148 net_log_logger_->set_log_level(GetLogLevelForLogType(log_type)); |
| 148 net_log_logger_->StartObserving(chrome_net_log_); | 149 net_log_logger_->StartObserving(chrome_net_log_, file.Pass(), constants.get(), |
| 150 nullptr); |
| 149 } | 151 } |
| 150 | 152 |
| 151 void NetLogTempFile::StopNetLog() { | 153 void NetLogTempFile::StopNetLog() { |
| 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 153 if (state_ != STATE_LOGGING) | 155 if (state_ != STATE_LOGGING) |
| 154 return; | 156 return; |
| 155 | 157 |
| 156 net_log_logger_->StopObserving(); | 158 net_log_logger_->StopObserving(nullptr); |
| 157 net_log_logger_.reset(); | 159 net_log_logger_.reset(); |
| 158 state_ = STATE_NOT_LOGGING; | 160 state_ = STATE_NOT_LOGGING; |
| 159 } | 161 } |
| 160 | 162 |
| 161 bool NetLogTempFile::GetFilePath(base::FilePath* path) { | 163 bool NetLogTempFile::GetFilePath(base::FilePath* path) { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 163 if (log_type_ == LOG_TYPE_NONE || state_ == STATE_LOGGING) | 165 if (log_type_ == LOG_TYPE_NONE || state_ == STATE_LOGGING) |
| 164 return false; | 166 return false; |
| 165 | 167 |
| 166 if (!NetExportLogExists()) | 168 if (!NetExportLogExists()) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 190 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath* path) { | 192 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath* path) { |
| 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 192 return base::GetTempDir(path); | 194 return base::GetTempDir(path); |
| 193 } | 195 } |
| 194 | 196 |
| 195 bool NetLogTempFile::NetExportLogExists() { | 197 bool NetLogTempFile::NetExportLogExists() { |
| 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 197 DCHECK(!log_path_.empty()); | 199 DCHECK(!log_path_.empty()); |
| 198 return base::PathExists(log_path_); | 200 return base::PathExists(log_path_); |
| 199 } | 201 } |
| OLD | NEW |