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/values.h"
9 #include "chrome/browser/net/chrome_net_log.h"
10 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "net/base/net_log_logger.h"
14 using content::BrowserThread
;
16 NetLogTempFile::NetLogTempFile(ChromeNetLog
* chrome_net_log
)
17 : state_(STATE_UNINITIALIZED
),
18 log_type_(LOG_TYPE_NONE
),
19 log_filename_(FILE_PATH_LITERAL("chrome-net-export-log.json")),
20 chrome_net_log_(chrome_net_log
) {
23 NetLogTempFile::~NetLogTempFile() {
25 net_log_logger_
->StopObserving();
28 void NetLogTempFile::ProcessCommand(Command command
) {
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
37 case DO_START_STRIP_PRIVATE_DATA
:
49 base::DictionaryValue
* NetLogTempFile::GetState() {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
51 base::DictionaryValue
* dict
= new base::DictionaryValue
;
56 dict
->SetString("file", log_path_
.LossyDisplayName());
60 case STATE_NOT_LOGGING
:
61 dict
->SetString("state", "NOT_LOGGING");
64 dict
->SetString("state", "LOGGING");
66 case STATE_UNINITIALIZED
:
67 dict
->SetString("state", "UNINITIALIZED");
73 dict
->SetString("logType", "NONE");
75 case LOG_TYPE_UNKNOWN
:
76 dict
->SetString("logType", "UNKNOWN");
79 dict
->SetString("logType", "NORMAL");
81 case LOG_TYPE_STRIP_PRIVATE_DATA
:
82 dict
->SetString("logType", "STRIP_PRIVATE_DATA");
89 bool NetLogTempFile::EnsureInit() {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
91 if (state_
!= STATE_UNINITIALIZED
)
94 if (!GetNetExportLog())
97 state_
= STATE_NOT_LOGGING
;
98 if (NetExportLogExists())
99 log_type_
= LOG_TYPE_UNKNOWN
;
101 log_type_
= LOG_TYPE_NONE
;
106 void NetLogTempFile::StartNetLog(bool strip_private_data
) {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
108 if (state_
== STATE_LOGGING
)
111 DCHECK_NE(STATE_UNINITIALIZED
, state_
);
112 DCHECK(!log_path_
.empty());
114 // Try to make sure we can create the file.
115 // TODO(rtenneti): Find a better for doing the following. Surface some error
116 // to the user if we couldn't create the file.
117 FILE* file
= base::OpenFile(log_path_
, "w");
121 scoped_ptr
<base::Value
> constants(NetInternalsUI::GetConstants());
122 net_log_logger_
.reset(new net::NetLogLogger(file
, *constants
));
123 if (strip_private_data
) {
124 net_log_logger_
->set_log_level(net::NetLog::LOG_STRIP_PRIVATE_DATA
);
125 log_type_
= LOG_TYPE_STRIP_PRIVATE_DATA
;
127 net_log_logger_
->set_log_level(net::NetLog::LOG_ALL_BUT_BYTES
);
128 log_type_
= LOG_TYPE_NORMAL
;
130 net_log_logger_
->StartObserving(chrome_net_log_
);
131 state_
= STATE_LOGGING
;
134 void NetLogTempFile::StopNetLog() {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
136 if (state_
!= STATE_LOGGING
)
139 net_log_logger_
->StopObserving();
140 net_log_logger_
.reset();
141 state_
= STATE_NOT_LOGGING
;
144 bool NetLogTempFile::GetFilePath(base::FilePath
* path
) {
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
146 if (log_type_
== LOG_TYPE_NONE
|| state_
== STATE_LOGGING
)
149 if (!NetExportLogExists())
152 DCHECK(!log_path_
.empty());
153 #if defined(OS_POSIX)
154 // Users, group and others can read, write and traverse.
155 int mode
= base::FILE_PERMISSION_MASK
;
156 base::SetPosixFilePermissions(log_path_
, mode
);
157 #endif // defined(OS_POSIX)
163 bool NetLogTempFile::GetNetExportLog() {
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
165 base::FilePath temp_dir
;
166 if (!GetNetExportLogDirectory(&temp_dir
))
169 log_path_
= temp_dir
.Append(log_filename_
);
173 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath
* path
) {
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
175 return base::GetTempDir(path
);
178 bool NetLogTempFile::NetExportLogExists() {
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
180 DCHECK(!log_path_
.empty());
181 return base::PathExists(log_path_
);