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

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>
mmenke 2013/01/18 15:58:00 Is this needed? We use FILE*'s, but no stdio func
ramant (doing other things) 2013/01/18 23:22:33 Done.
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(const FilePath::StringType& chrome_net_export,
17 FilePath* path) {
18 // We will try to create chrome_net_export (or make sure chrome_net_export
19 // directory exists) directory twice in file_util::GetTempDir.
20 for (int i = 0; i < 2; ++i) {
21 FilePath temp_dir;
22 if (!file_util::GetTempDir(&temp_dir))
23 continue;
24
25 FilePath net_export_dir = temp_dir.Append(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& chrome_net_export,
42 const FilePath::StringType& log_filename,
43 FilePath* path) {
44 FilePath net_export_dir;
45 if (!GetNetExportDir(chrome_net_export, &net_export_dir))
46 return false;
47
48 *path = net_export_dir.Append(log_filename);
49 return true;
50 }
51
52 } // namespace
53
54 NetLogTempFile::NetLogTempFile(ChromeNetLog* chrome_net_log)
55 : state_(STATE_UNINITIALIZED),
56 chrome_net_export_(FILE_PATH_LITERAL("chrome-net-export")),
57 log_filename_(FILE_PATH_LITERAL("log.json")),
mmenke 2013/01/18 15:58:00 Don't think a pathname + a file name really gets u
ramant (doing other things) 2013/01/18 23:22:33 Done.
58 chrome_net_log_(chrome_net_log) {
59 }
60
61 NetLogTempFile::~NetLogTempFile() {
62 if (net_log_logger_.get())
63 net_log_logger_->StopObserving();
64 }
65
66 void NetLogTempFile::ProcessCommand(Command command) {
67 // Determine the state_ if it is not set.
68 if (state_ == STATE_UNINITIALIZED)
69 Init();
70
71 switch (command) {
72 case DO_START:
73 StartNetLog();
74 break;
75 case DO_STOP:
76 StopNetLog();
77 break;
78 default:
79 break;
80 }
81 }
82
83 DictionaryValue* NetLogTempFile::GetState() {
84 // Determine the state_ if it is not set.
85 if (state_ == STATE_UNINITIALIZED)
86 Init();
87
88 base::DictionaryValue* dict = new base::DictionaryValue;
89
90 #ifndef NDEBUG
91 dict->SetString("file", log_path_.MaybeAsASCII());
92 #endif // NDEBUG
93
94 switch (state_) {
95 case STATE_ALLOW_STOP:
96 dict->SetString("state", "ALLOW_STOP");
97 break;
98 case STATE_ALLOW_START_SEND:
99 dict->SetString("state", "ALLOW_START_SEND");
100 break;
101 default:
102 dict->SetString("state", "ALLOW_START");
103 break;
104 }
105 return dict;
106 }
107
108 void NetLogTempFile::Init() {
109 if (state_ != STATE_UNINITIALIZED)
110 return;
111
112 FilePath net_export_log;
113 if (!GetNetExportLog(chrome_net_export_, log_filename_, &net_export_log)) {
114 state_ = STATE_ALLOW_START;
115 return;
116 }
117
118 log_path_ = net_export_log;
119 if (file_util::PathExists(log_path_))
120 state_ = STATE_ALLOW_START_SEND;
121 else
122 state_ = STATE_ALLOW_START;
123 }
124
125 void NetLogTempFile::StartNetLog() {
126 if (state_ == STATE_ALLOW_STOP)
127 return;
128
129 // Call GetNetExportLog to make sure |chrome_net_export_| is created in
130 // file_util::GetTempDir (in case the temporary directory is deleted by the
131 // time user clicks on START button).
132 FilePath net_export_log;
133 if (!GetNetExportLog(chrome_net_export_, log_filename_, &net_export_log))
134 return;
135
136 // Try to make sure we can create the file.
137 FILE* fp = file_util::OpenFile(net_export_log, "w");
mmenke 2013/01/18 15:58:00 Fine for now, but I'd like to look into a better w
ramant (doing other things) 2013/01/18 23:22:33 Added a TODO.
138 if (!fp) {
139 LOG(ERROR) << "Could not open file " << net_export_log.value()
140 << " for net logging";
141 return;
142 } else {
143 file_util::CloseFile(fp);
mmenke 2013/01/18 15:58:00 nit: Fix indent.
ramant (doing other things) 2013/01/18 23:22:33 Done.
144 }
145
146 log_path_ = net_export_log;
147 net_log_logger_.reset(new NetLogLogger(log_path_));
148 net_log_logger_->StartObserving(chrome_net_log_);
149 state_ = STATE_ALLOW_STOP;
150 }
151
152 void NetLogTempFile::StopNetLog() {
153 if (state_ != STATE_ALLOW_STOP)
154 return;
155
156 net_log_logger_->StopObserving();
157 net_log_logger_.reset();
158 state_ = STATE_ALLOW_START_SEND;
159 }
160
161 bool NetLogTempFile::GetFilePath(FilePath* path) {
162 if (state_ != STATE_ALLOW_START_SEND)
163 return false;
164
165 FilePath net_export_log;
166 if (!GetNetExportLog(chrome_net_export_, log_filename_, &net_export_log))
167 return false;
168
169 if (!file_util::PathExists(net_export_log))
170 return false;
171
172 #if defined(OS_POSIX)
173 // Users, group and others can read, write and traverse.
174 int mode = file_util::FILE_PERMISSION_MASK;
175 file_util::SetPosixFilePermissions(net_export_log, mode);
176 #endif // defined(OS_POSIX)
177
178 *path = net_export_log;
179 return true;
180 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698