| 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: | 35 case DO_START: |
| 35 StartNetLog(false); | 36 StartNetLog(false); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 108 if (state_ == STATE_LOGGING) | 109 if (state_ == STATE_LOGGING) |
| 109 return; | 110 return; |
| 110 | 111 |
| 111 DCHECK_NE(STATE_UNINITIALIZED, state_); | 112 DCHECK_NE(STATE_UNINITIALIZED, state_); |
| 112 DCHECK(!log_path_.empty()); | 113 DCHECK(!log_path_.empty()); |
| 113 | 114 |
| 114 // Try to make sure we can create the file. | 115 // Try to make sure we can create the file. |
| 115 // TODO(rtenneti): Find a better for doing the following. Surface some error | 116 // TODO(rtenneti): Find a better for doing the following. Surface some error |
| 116 // to the user if we couldn't create the file. | 117 // to the user if we couldn't create the file. |
| 117 FILE* file = base::OpenFile(log_path_, "w"); | 118 base::ScopedFILE file(base::OpenFile(log_path_, "w")); |
| 118 if (file == NULL) | 119 if (!file) |
| 119 return; | 120 return; |
| 120 | 121 |
| 121 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); | 122 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); |
| 122 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 123 net_log_logger_.reset(new net::NetLogLogger()); |
| 123 if (strip_private_data) { | 124 if (strip_private_data) { |
| 124 net_log_logger_->set_log_level(net::NetLog::LOG_STRIP_PRIVATE_DATA); | 125 net_log_logger_->set_log_level(net::NetLog::LOG_STRIP_PRIVATE_DATA); |
| 125 log_type_ = LOG_TYPE_STRIP_PRIVATE_DATA; | 126 log_type_ = LOG_TYPE_STRIP_PRIVATE_DATA; |
| 126 } else { | 127 } else { |
| 127 net_log_logger_->set_log_level(net::NetLog::LOG_ALL_BUT_BYTES); | 128 net_log_logger_->set_log_level(net::NetLog::LOG_ALL_BUT_BYTES); |
| 128 log_type_ = LOG_TYPE_NORMAL; | 129 log_type_ = LOG_TYPE_NORMAL; |
| 129 } | 130 } |
| 130 net_log_logger_->StartObserving(chrome_net_log_); | 131 net_log_logger_->StartObserving(chrome_net_log_, file.Pass(), constants.get(), |
| 132 nullptr); |
| 131 state_ = STATE_LOGGING; | 133 state_ = STATE_LOGGING; |
| 132 } | 134 } |
| 133 | 135 |
| 134 void NetLogTempFile::StopNetLog() { | 136 void NetLogTempFile::StopNetLog() { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 136 if (state_ != STATE_LOGGING) | 138 if (state_ != STATE_LOGGING) |
| 137 return; | 139 return; |
| 138 | 140 |
| 139 net_log_logger_->StopObserving(); | 141 net_log_logger_->StopObserving(nullptr); |
| 140 net_log_logger_.reset(); | 142 net_log_logger_.reset(); |
| 141 state_ = STATE_NOT_LOGGING; | 143 state_ = STATE_NOT_LOGGING; |
| 142 } | 144 } |
| 143 | 145 |
| 144 bool NetLogTempFile::GetFilePath(base::FilePath* path) { | 146 bool NetLogTempFile::GetFilePath(base::FilePath* path) { |
| 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 146 if (log_type_ == LOG_TYPE_NONE || state_ == STATE_LOGGING) | 148 if (log_type_ == LOG_TYPE_NONE || state_ == STATE_LOGGING) |
| 147 return false; | 149 return false; |
| 148 | 150 |
| 149 if (!NetExportLogExists()) | 151 if (!NetExportLogExists()) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 173 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath* path) { | 175 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath* path) { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 175 return base::GetTempDir(path); | 177 return base::GetTempDir(path); |
| 176 } | 178 } |
| 177 | 179 |
| 178 bool NetLogTempFile::NetExportLogExists() { | 180 bool NetLogTempFile::NetExportLogExists() { |
| 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| 180 DCHECK(!log_path_.empty()); | 182 DCHECK(!log_path_.empty()); |
| 181 return base::PathExists(log_path_); | 183 return base::PathExists(log_path_); |
| 182 } | 184 } |
| OLD | NEW |