Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / net / net_log_temp_file.cc
blobbfc7cd18f00e07fb167b70ba1d8a3044f3d91be9
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/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_filename_(FILE_PATH_LITERAL("chrome-net-export-log.json")),
19 chrome_net_log_(chrome_net_log) {
22 NetLogTempFile::~NetLogTempFile() {
23 if (net_log_logger_)
24 net_log_logger_->StopObserving();
27 void NetLogTempFile::ProcessCommand(Command command) {
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
29 if (!EnsureInit())
30 return;
32 switch (command) {
33 case DO_START:
34 StartNetLog();
35 break;
36 case DO_STOP:
37 StopNetLog();
38 break;
39 default:
40 NOTREACHED();
41 break;
45 base::DictionaryValue* NetLogTempFile::GetState() {
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
47 base::DictionaryValue* dict = new base::DictionaryValue;
49 EnsureInit();
51 #ifndef NDEBUG
52 dict->SetString("file", log_path_.LossyDisplayName());
53 #endif // NDEBUG
55 switch (state_) {
56 case STATE_ALLOW_START:
57 dict->SetString("state", "ALLOW_START");
58 break;
59 case STATE_ALLOW_STOP:
60 dict->SetString("state", "ALLOW_STOP");
61 break;
62 case STATE_ALLOW_START_SEND:
63 dict->SetString("state", "ALLOW_START_SEND");
64 break;
65 default:
66 dict->SetString("state", "UNINITIALIZED");
67 break;
69 return dict;
72 bool NetLogTempFile::EnsureInit() {
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
74 if (state_ != STATE_UNINITIALIZED)
75 return true;
77 if (!GetNetExportLog())
78 return false;
80 if (NetExportLogExists())
81 state_ = STATE_ALLOW_START_SEND;
82 else
83 state_ = STATE_ALLOW_START;
85 return true;
88 void NetLogTempFile::StartNetLog() {
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
90 if (state_ == STATE_ALLOW_STOP)
91 return;
93 DCHECK_NE(STATE_UNINITIALIZED, state_);
94 DCHECK(!log_path_.empty());
96 // Try to make sure we can create the file.
97 // TODO(rtenneti): Find a better for doing the following. Surface some error
98 // to the user if we couldn't create the file.
99 FILE* file = base::OpenFile(log_path_, "w");
100 if (file == NULL)
101 return;
103 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants());
104 net_log_logger_.reset(new net::NetLogLogger(file, *constants));
105 net_log_logger_->StartObserving(chrome_net_log_);
106 state_ = STATE_ALLOW_STOP;
109 void NetLogTempFile::StopNetLog() {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
111 if (state_ != STATE_ALLOW_STOP)
112 return;
114 net_log_logger_->StopObserving();
115 net_log_logger_.reset();
116 state_ = STATE_ALLOW_START_SEND;
119 bool NetLogTempFile::GetFilePath(base::FilePath* path) {
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
121 if (state_ != STATE_ALLOW_START_SEND)
122 return false;
124 if (!NetExportLogExists())
125 return false;
127 DCHECK(!log_path_.empty());
128 #if defined(OS_POSIX)
129 // Users, group and others can read, write and traverse.
130 int mode = base::FILE_PERMISSION_MASK;
131 base::SetPosixFilePermissions(log_path_, mode);
132 #endif // defined(OS_POSIX)
134 *path = log_path_;
135 return true;
138 bool NetLogTempFile::GetNetExportLog() {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
140 base::FilePath temp_dir;
141 if (!GetNetExportLogDirectory(&temp_dir))
142 return false;
144 log_path_ = temp_dir.Append(log_filename_);
145 return true;
148 bool NetLogTempFile::GetNetExportLogDirectory(base::FilePath* path) {
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
150 return base::GetTempDir(path);
153 bool NetLogTempFile::NetExportLogExists() {
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
155 DCHECK(!log_path_.empty());
156 return base::PathExists(log_path_);