Add a webstorePrivate API to show a permission prompt for delegated bundle installs
[chromium-blink-merge.git] / chrome / browser / net / net_log_temp_file.cc
blob0d2400f8a8466ba0ff8a6728d476a09d8eae119c
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);
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_CURRENTLY_ON(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::NetLogCaptureMode NetLogTempFile::GetCaptureModeForLogType(
97 LogType log_type) {
98 switch (log_type) {
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();
105 case LOG_TYPE_NONE:
106 case LOG_TYPE_UNKNOWN:
107 NOTREACHED();
109 return net::NetLogCaptureMode::Default();
112 bool NetLogTempFile::EnsureInit() {
113 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING);
114 if (state_ != STATE_UNINITIALIZED)
115 return true;
117 if (!GetNetExportLog())
118 return false;
120 state_ = STATE_NOT_LOGGING;
121 if (NetExportLogExists())
122 log_type_ = LOG_TYPE_UNKNOWN;
123 else
124 log_type_ = LOG_TYPE_NONE;
126 return true;
129 void NetLogTempFile::StartNetLog(LogType log_type) {
130 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING);
131 if (state_ == STATE_LOGGING)
132 return;
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"));
141 if (!file)
142 return;
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)
157 return;
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)
167 return false;
169 if (!NetExportLogExists())
170 return false;
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)
179 *path = log_path_;
180 return true;
183 bool NetLogTempFile::GetNetExportLog() {
184 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING);
185 base::FilePath temp_dir;
186 if (!GetNetExportLogDirectory(&temp_dir))
187 return false;
189 log_path_ = temp_dir.Append(log_filename_);
190 return true;
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_);