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
3 // found in the LICENSE file.
5 #include "chrome/browser/net/net_log_temp_file.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_file.h"
9 #include "base/values.h"
10 #include "chrome/browser/net/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"
15 using content::BrowserThread
;
17 NetLogTempFile::NetLogTempFile(ChromeNetLog
* chrome_net_log
)
18 : state_(STATE_UNINITIALIZED
),
19 log_type_(LOG_TYPE_NONE
),
20 log_filename_(FILE_PATH_LITERAL("chrome-net-export-log.json")),
21 chrome_net_log_(chrome_net_log
) {
24 NetLogTempFile::~NetLogTempFile() {
25 if (write_to_file_observer_
)
26 write_to_file_observer_
->StopObserving(nullptr);
29 void NetLogTempFile::ProcessCommand(Command command
) {
30 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
35 case DO_START_LOG_BYTES
:
36 StartNetLog(LOG_TYPE_LOG_BYTES
);
39 StartNetLog(LOG_TYPE_NORMAL
);
41 case DO_START_STRIP_PRIVATE_DATA
:
42 StartNetLog(LOG_TYPE_STRIP_PRIVATE_DATA
);
53 base::DictionaryValue
* NetLogTempFile::GetState() {
54 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
55 base::DictionaryValue
* dict
= new base::DictionaryValue
;
60 dict
->SetString("file", log_path_
.LossyDisplayName());
64 case STATE_NOT_LOGGING
:
65 dict
->SetString("state", "NOT_LOGGING");
68 dict
->SetString("state", "LOGGING");
70 case STATE_UNINITIALIZED
:
71 dict
->SetString("state", "UNINITIALIZED");
77 dict
->SetString("logType", "NONE");
79 case LOG_TYPE_UNKNOWN
:
80 dict
->SetString("logType", "UNKNOWN");
82 case LOG_TYPE_LOG_BYTES
:
83 dict
->SetString("logType", "LOG_BYTES");
86 dict
->SetString("logType", "NORMAL");
88 case LOG_TYPE_STRIP_PRIVATE_DATA
:
89 dict
->SetString("logType", "STRIP_PRIVATE_DATA");
96 net::NetLogCaptureMode
NetLogTempFile::GetCaptureModeForLogType(
99 case LOG_TYPE_LOG_BYTES
:
100 return net::NetLogCaptureMode::IncludeSocketBytes();
101 case LOG_TYPE_NORMAL
:
102 return net::NetLogCaptureMode::IncludeCookiesAndCredentials();
103 case LOG_TYPE_STRIP_PRIVATE_DATA
:
104 return net::NetLogCaptureMode::Default();
106 case LOG_TYPE_UNKNOWN
:
109 return net::NetLogCaptureMode::Default();
112 bool NetLogTempFile::EnsureInit() {
113 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
114 if (state_
!= STATE_UNINITIALIZED
)
117 if (!GetNetExportLog())
120 state_
= STATE_NOT_LOGGING
;
121 if (NetExportLogExists())
122 log_type_
= LOG_TYPE_UNKNOWN
;
124 log_type_
= LOG_TYPE_NONE
;
129 void NetLogTempFile::StartNetLog(LogType log_type
) {
130 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
131 if (state_
== STATE_LOGGING
)
134 DCHECK_NE(STATE_UNINITIALIZED
, state_
);
135 DCHECK(!log_path_
.empty());
137 // Try to make sure we can create the file.
138 // TODO(rtenneti): Find a better for doing the following. Surface some error
139 // to the user if we couldn't create the file.
140 base::ScopedFILE
file(base::OpenFile(log_path_
, "w"));
144 log_type_
= log_type
;
145 state_
= STATE_LOGGING
;
147 scoped_ptr
<base::Value
> constants(NetInternalsUI::GetConstants());
148 write_to_file_observer_
.reset(new net::WriteToFileNetLogObserver());
149 write_to_file_observer_
->set_capture_mode(GetCaptureModeForLogType(log_type
));
150 write_to_file_observer_
->StartObserving(chrome_net_log_
, file
.Pass(),
151 constants
.get(), nullptr);
154 void NetLogTempFile::StopNetLog() {
155 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
156 if (state_
!= STATE_LOGGING
)
159 write_to_file_observer_
->StopObserving(nullptr);
160 write_to_file_observer_
.reset();
161 state_
= STATE_NOT_LOGGING
;
164 bool NetLogTempFile::GetFilePath(base::FilePath
* path
) {
165 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
166 if (log_type_
== LOG_TYPE_NONE
|| state_
== STATE_LOGGING
)
169 if (!NetExportLogExists())
172 DCHECK(!log_path_
.empty());
173 #if defined(OS_POSIX)
174 // Users, group and others can read, write and traverse.
175 int mode
= base::FILE_PERMISSION_MASK
;
176 base::SetPosixFilePermissions(log_path_
, mode
);
177 #endif // defined(OS_POSIX)
183 bool NetLogTempFile::GetNetExportLog() {
184 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
185 base::FilePath temp_dir
;
186 if (!GetNetExportLogDirectory(&temp_dir
))
189 log_path_
= temp_dir
.Append(log_filename_
);
193 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath
* path
) {
194 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
195 return base::GetTempDir(path
);
198 bool NetLogTempFile::NetExportLogExists() {
199 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING
);
200 DCHECK(!log_path_
.empty());
201 return base::PathExists(log_path_
);