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 (net_log_logger_) | 25 if (write_to_file_observer_) |
26 net_log_logger_->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(BrowserThread::CurrentlyOn(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); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 net_log_logger_.reset(new net::WriteToFileNetLogObserver()); | 148 write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); |
149 net_log_logger_->set_capture_mode(GetCaptureModeForLogType(log_type)); | 149 write_to_file_observer_->set_capture_mode(GetCaptureModeForLogType(log_type)); |
150 net_log_logger_->StartObserving(chrome_net_log_, file.Pass(), constants.get(), | 150 write_to_file_observer_->StartObserving(chrome_net_log_, file.Pass(), |
151 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(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
156 if (state_ != STATE_LOGGING) | 156 if (state_ != STATE_LOGGING) |
157 return; | 157 return; |
158 | 158 |
159 net_log_logger_->StopObserving(nullptr); | 159 write_to_file_observer_->StopObserving(nullptr); |
160 net_log_logger_.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(BrowserThread::CurrentlyOn(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; |
(...skipping 22 matching lines...) Expand all Loading... |
193 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath* path) { | 193 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath* path) { |
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | 194 DCHECK(BrowserThread::CurrentlyOn(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(BrowserThread::CurrentlyOn(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 |