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/net_log_logger.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() {
26 net_log_logger_
->StopObserving(nullptr);
29 void NetLogTempFile::ProcessCommand(Command command
) {
30 DCHECK(BrowserThread::CurrentlyOn(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(BrowserThread::CurrentlyOn(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::NetLog::LogLevel
NetLogTempFile::GetLogLevelForLogType(LogType log_type
) {
98 case LOG_TYPE_LOG_BYTES
:
99 return net::NetLog::LOG_ALL
;
100 case LOG_TYPE_NORMAL
:
101 return net::NetLog::LOG_ALL_BUT_BYTES
;
102 case LOG_TYPE_STRIP_PRIVATE_DATA
:
103 return net::NetLog::LOG_STRIP_PRIVATE_DATA
;
105 case LOG_TYPE_UNKNOWN
:
108 return net::NetLog::LOG_STRIP_PRIVATE_DATA
;
111 bool NetLogTempFile::EnsureInit() {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
113 if (state_
!= STATE_UNINITIALIZED
)
116 if (!GetNetExportLog())
119 state_
= STATE_NOT_LOGGING
;
120 if (NetExportLogExists())
121 log_type_
= LOG_TYPE_UNKNOWN
;
123 log_type_
= LOG_TYPE_NONE
;
128 void NetLogTempFile::StartNetLog(LogType log_type
) {
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
130 if (state_
== STATE_LOGGING
)
133 DCHECK_NE(STATE_UNINITIALIZED
, state_
);
134 DCHECK(!log_path_
.empty());
136 // Try to make sure we can create the file.
137 // TODO(rtenneti): Find a better for doing the following. Surface some error
138 // to the user if we couldn't create the file.
139 base::ScopedFILE
file(base::OpenFile(log_path_
, "w"));
143 log_type_
= log_type
;
144 state_
= STATE_LOGGING
;
146 scoped_ptr
<base::Value
> constants(NetInternalsUI::GetConstants());
147 net_log_logger_
.reset(new net::NetLogLogger());
148 net_log_logger_
->set_log_level(GetLogLevelForLogType(log_type
));
149 net_log_logger_
->StartObserving(chrome_net_log_
, file
.Pass(), constants
.get(),
153 void NetLogTempFile::StopNetLog() {
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
155 if (state_
!= STATE_LOGGING
)
158 net_log_logger_
->StopObserving(nullptr);
159 net_log_logger_
.reset();
160 state_
= STATE_NOT_LOGGING
;
163 bool NetLogTempFile::GetFilePath(base::FilePath
* path
) {
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
165 if (log_type_
== LOG_TYPE_NONE
|| state_
== STATE_LOGGING
)
168 if (!NetExportLogExists())
171 DCHECK(!log_path_
.empty());
172 #if defined(OS_POSIX)
173 // Users, group and others can read, write and traverse.
174 int mode
= base::FILE_PERMISSION_MASK
;
175 base::SetPosixFilePermissions(log_path_
, mode
);
176 #endif // defined(OS_POSIX)
182 bool NetLogTempFile::GetNetExportLog() {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
184 base::FilePath temp_dir
;
185 if (!GetNetExportLogDirectory(&temp_dir
))
188 log_path_
= temp_dir
.Append(log_filename_
);
192 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath
* path
) {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
194 return base::GetTempDir(path
);
197 bool NetLogTempFile::NetExportLogExists() {
198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
199 DCHECK(!log_path_
.empty());
200 return base::PathExists(log_path_
);