Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / net / net_log_temp_file.cc
blob1da33e6cd5e95017881a80cc36a67c35ee8a72f9
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() {
25 if (net_log_logger_)
26 net_log_logger_->StopObserving(nullptr);
29 void NetLogTempFile::ProcessCommand(Command command) {
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
31 if (!EnsureInit())
32 return;
34 switch (command) {
35 case DO_START_LOG_BYTES:
36 StartNetLog(LOG_TYPE_LOG_BYTES);
37 break;
38 case DO_START:
39 StartNetLog(LOG_TYPE_NORMAL);
40 break;
41 case DO_START_STRIP_PRIVATE_DATA:
42 StartNetLog(LOG_TYPE_STRIP_PRIVATE_DATA);
43 break;
44 case DO_STOP:
45 StopNetLog();
46 break;
47 default:
48 NOTREACHED();
49 break;
53 base::DictionaryValue* NetLogTempFile::GetState() {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
55 base::DictionaryValue* dict = new base::DictionaryValue;
57 EnsureInit();
59 #ifndef NDEBUG
60 dict->SetString("file", log_path_.LossyDisplayName());
61 #endif // NDEBUG
63 switch (state_) {
64 case STATE_NOT_LOGGING:
65 dict->SetString("state", "NOT_LOGGING");
66 break;
67 case STATE_LOGGING:
68 dict->SetString("state", "LOGGING");
69 break;
70 case STATE_UNINITIALIZED:
71 dict->SetString("state", "UNINITIALIZED");
72 break;
75 switch (log_type_) {
76 case LOG_TYPE_NONE:
77 dict->SetString("logType", "NONE");
78 break;
79 case LOG_TYPE_UNKNOWN:
80 dict->SetString("logType", "UNKNOWN");
81 break;
82 case LOG_TYPE_LOG_BYTES:
83 dict->SetString("logType", "LOG_BYTES");
84 break;
85 case LOG_TYPE_NORMAL:
86 dict->SetString("logType", "NORMAL");
87 break;
88 case LOG_TYPE_STRIP_PRIVATE_DATA:
89 dict->SetString("logType", "STRIP_PRIVATE_DATA");
90 break;
93 return dict;
96 net::NetLog::LogLevel NetLogTempFile::GetLogLevelForLogType(LogType log_type) {
97 switch (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;
104 case LOG_TYPE_NONE:
105 case LOG_TYPE_UNKNOWN:
106 NOTREACHED();
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)
114 return true;
116 if (!GetNetExportLog())
117 return false;
119 state_ = STATE_NOT_LOGGING;
120 if (NetExportLogExists())
121 log_type_ = LOG_TYPE_UNKNOWN;
122 else
123 log_type_ = LOG_TYPE_NONE;
125 return true;
128 void NetLogTempFile::StartNetLog(LogType log_type) {
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
130 if (state_ == STATE_LOGGING)
131 return;
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"));
140 if (!file)
141 return;
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(),
150 nullptr);
153 void NetLogTempFile::StopNetLog() {
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
155 if (state_ != STATE_LOGGING)
156 return;
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)
166 return false;
168 if (!NetExportLogExists())
169 return false;
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)
178 *path = log_path_;
179 return true;
182 bool NetLogTempFile::GetNetExportLog() {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
184 base::FilePath temp_dir;
185 if (!GetNetExportLogDirectory(&temp_dir))
186 return false;
188 log_path_ = temp_dir.Append(log_filename_);
189 return true;
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_);