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 log_type_
= LOG_TYPE_NORMAL
;
129 net_log_logger_
->StartObserving(chrome_net_log_
);
130 state_
= STATE_LOGGING
;
133 void NetLogTempFile::StopNetLog() {
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
135 if (state_
!= STATE_LOGGING
)
138 net_log_logger_
->StopObserving();
139 net_log_logger_
.reset();
140 state_
= STATE_NOT_LOGGING
;
143 bool NetLogTempFile::GetFilePath(base::FilePath
* path
) {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
145 if (log_type_
== LOG_TYPE_NONE
|| state_
== STATE_LOGGING
)
148 if (!NetExportLogExists())
151 DCHECK(!log_path_
.empty());
152 #if defined(OS_POSIX)
153 // Users, group and others can read, write and traverse.
154 int mode
= base::FILE_PERMISSION_MASK
;
155 base::SetPosixFilePermissions(log_path_
, mode
);
156 #endif // defined(OS_POSIX)
162 bool NetLogTempFile::GetNetExportLog() {
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
164 base::FilePath temp_dir
;
165 if (!GetNetExportLogDirectory(&temp_dir
))
168 log_path_
= temp_dir
.Append(log_filename_
);
172 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath
* path
) {
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
174 return base::GetTempDir(path
);
177 bool NetLogTempFile::NetExportLogExists() {
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING
));
179 DCHECK(!log_path_
.empty());
180 return base::PathExists(log_path_
);