Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: chrome/browser/net/net_log_temp_file.cc

Issue 11828036: First cut at UI for saving net_logs data into a temporary file on (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/net/net_log_temp_file.h"
6
7 #include <stdio.h>
8
9 #include "base/file_util.h"
10 #include "base/values.h"
11 #include "chrome/browser/net/chrome_net_log.h"
12 #include "chrome/browser/net/net_log_logger.h"
13
14 namespace {
15
16 bool GetNetExportDir(FilePath* path) {
17 // We will try to create chrome-net-export (or make sure chrome-net-export
18 // exists) twice in file_util::GetTempDir.
19 for (int i = 0; i < 2; ++i) {
20 FilePath temp_dir;
21 if (!file_util::GetTempDir(&temp_dir))
22 continue;
23
24 FilePath net_export_dir = temp_dir.Append(
25 FILE_PATH_LITERAL("chrome-net-export"));
26 if (file_util::PathExists(net_export_dir)) {
27 *path = net_export_dir;
28 return true;
29 }
30
31 if (!file_util::CreateDirectory(net_export_dir))
32 continue;
33
34 *path = net_export_dir;
35 return true;
36 }
37
38 return false;
39 }
40
41 bool GetNetExportLog(const FilePath::StringType& log_filename, FilePath* path) {
42 FilePath net_export_dir;
43 if (!GetNetExportDir(&net_export_dir))
44 return false;
45
46 *path = net_export_dir.Append(log_filename);
47 return true;
48 }
49
50 } // namespace
51
52 NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log)
53 : state_(STATE_UNINITIALIZED),
54 log_filename_(FILE_PATH_LITERAL("log.json")),
55 chrome_net_log_(chrome_net_log) {
56 }
57
58 NetLogTempFile::~NetLogTempFile() {
59 if (net_log_logger_.get())
60 net_log_logger_->StopObserving();
61 }
62
63 void NetLogTempFile::ProcessCommand(Command command) {
64 // Determine the state_ if it is not set.
65 if (state_ == STATE_UNINITIALIZED)
66 Init();
67
68 switch (command) {
69 case DO_START:
70 StartNetLog();
71 break;
72 case DO_STOP:
73 StopNetLog();
74 break;
75 default:
76 break;
77 }
78 }
79
80 DictionaryValue* NetLogTempFile::GetState() {
81 // Determine the state_ if it is not set.
82 if (state_ == STATE_UNINITIALIZED)
83 Init();
84
85 base::DictionaryValue* dict = new base::DictionaryValue;
86
87 #ifndef NDEBUG
88 dict->SetString("file", log_path_.MaybeAsASCII());
89 #endif // NDEBUG
90
91 switch (state_) {
92 case STATE_ALLOW_STOP:
93 dict->SetString("state", "ALLOW_STOP");
94 break;
95 case STATE_ALLOW_START_SEND:
96 dict->SetString("state", "ALLOW_START_SEND");
97 break;
98 default:
99 dict->SetString("state", "ALLOW_START");
100 break;
101 }
102 return dict;
103 }
104
105 void NetLogTempFile::Init() {
106 if (state_ != STATE_UNINITIALIZED)
107 return;
108
109 FilePath net_export_log;
110 if (!GetNetExportLog(log_filename_, &net_export_log)) {
111 state_ = STATE_ALLOW_START;
112 return;
113 }
114
115 log_path_ = net_export_log;
116 if (file_util::PathExists(log_path_))
117 state_ = STATE_ALLOW_START_SEND;
118 else
119 state_ = STATE_ALLOW_START;
120 }
121
122 void NetLogTempFile::StartNetLog() {
123 if (state_ == STATE_ALLOW_STOP)
124 return;
125
126 // Call GetNetExportLog to make sure "chrome-net-export" is created in
127 // file_util::GetTempDir (in case the temporary directory is deleted by the
128 // time user clicks on START button).
129 FilePath net_export_log;
130 if (!GetNetExportLog(log_filename_, &net_export_log))
131 return;
132
133 // Try to make sure we can create the file.
134 FILE* fp = file_util::OpenFile(net_export_log, "w");
135 if (!fp) {
136 LOG(ERROR) << "Could not open file " << net_export_log.value()
137 << " for net logging";
138 return;
139 } else {
140 file_util::CloseFile(fp);
141 }
142
143 log_path_ = net_export_log;
144 net_log_logger_.reset(new NetLogLogger(log_path_));
145 net_log_logger_->StartObserving(chrome_net_log_);
146 state_ = STATE_ALLOW_STOP;
147 }
148
149 void NetLogTempFile::StopNetLog() {
150 if (state_ != STATE_ALLOW_STOP)
151 return;
152
153 net_log_logger_->StopObserving();
154 net_log_logger_.reset();
155 state_ = STATE_ALLOW_START_SEND;
156 }
157
158 bool NetLogTempFile::GetNetLogToSend(FilePath* path) {
159 if (state_ != STATE_ALLOW_START_SEND)
160 return false;
161
162 FilePath net_export_log;
163 if (!GetNetExportLog(log_filename_, &net_export_log))
164 return false;
165
166 if (!file_util::PathExists(net_export_log))
167 return false;
168
169 #if defined(OS_POSIX)
170 // Users, group and others can read, write and traverse.
171 int mode = file_util::FILE_PERMISSION_MASK;
172 file_util::SetPosixFilePermissions(net_export_log, mode);
173 #endif // defined(OS_POSIX)
174
175 *path = net_export_log;
176 return true;
177 }
178
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698