OLD | NEW |
---|---|
(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/bind.h" | |
10 #include "base/file_util.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/string_number_conversions.h" | |
mmenke
2013/01/11 18:31:12
This isn't used here, right?
ramant (doing other things)
2013/01/12 04:34:04
Done.
| |
13 #include "base/string_util.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "base/values.h" | |
16 #include "chrome/browser/browser_process.h" | |
17 #include "chrome/browser/net/chrome_net_log.h" | |
18 #include "chrome/browser/net/net_log_logger.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 | |
21 #if defined(OS_ANDROID) | |
22 #include "chrome/browser/android/intent_helper.h" | |
23 #endif | |
24 | |
25 using content::BrowserThread; | |
26 | |
27 NetLogTempFile::NetLogTempFile() : state_(UNINITIALIZED) { | |
28 } | |
29 | |
30 NetLogTempFile::~NetLogTempFile() { | |
31 } | |
32 | |
33 void NetLogTempFile::ProcessCommand(Command command) { | |
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
mmenke
2013/01/11 18:31:12
Out of paranoia, suggest an Init() call here if un
ramant (doing other things)
2013/01/12 04:34:04
Done.
| |
35 switch (command) { | |
36 case DO_START: | |
37 StartNetLog(); | |
38 break; | |
39 case DO_STOP: | |
40 StopNetLog(); | |
41 break; | |
42 case DO_SEND: | |
43 SendNetLog(); | |
44 break; | |
45 default: | |
46 break; | |
47 } | |
48 } | |
49 | |
50 Value* NetLogTempFile::NetLogTempFileToValue() { | |
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
52 | |
53 // Determine the state_ if it is not set. | |
54 if (state_ == UNINITIALIZED) | |
55 Init(); | |
56 | |
57 base::DictionaryValue* dict = new base::DictionaryValue; | |
58 | |
59 #ifndef NDEBUG | |
60 dict->SetString("file", log_path_.MaybeAsASCII()); | |
61 #endif // NDEBUG | |
62 | |
63 switch (state_) { | |
64 case ALLOW_STOP: | |
65 dict->SetString("state", "ALLOW_STOP"); | |
66 break; | |
67 case ALLOW_START_SEND: | |
68 dict->SetString("state", "ALLOW_START_SEND"); | |
69 break; | |
70 default: | |
71 dict->SetString("state", "ALLOW_START"); | |
72 break; | |
73 } | |
74 return dict; | |
75 } | |
76 | |
77 bool GetTempNetLogDir(FilePath* path) { | |
mmenke
2013/01/11 18:31:12
This should be in an anonymous namespace. Suggest
ramant (doing other things)
2013/01/12 04:34:04
Done.
| |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
79 | |
80 FilePath temp_dir; | |
81 if (!file_util::GetTempDir(&temp_dir)) | |
82 return false; | |
83 | |
84 FilePath net_log_dir = temp_dir.Append(FILE_PATH_LITERAL("NetLog")); | |
85 if (file_util::PathExists(net_log_dir)) { | |
86 *path = net_log_dir; | |
87 return true; | |
88 } | |
89 | |
90 if (!file_util::CreateDirectory(net_log_dir)) | |
91 return false; | |
92 | |
93 *path = net_log_dir; | |
94 return true; | |
95 } | |
96 | |
97 void NetLogTempFile::Init() { | |
mmenke
2013/01/11 18:31:12
Suggest either a DCHECK on the state_ or early abo
ramant (doing other things)
2013/01/12 04:34:04
Done.
| |
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
99 | |
100 FilePath net_log_dir; | |
101 | |
102 if (!GetTempNetLogDir(&net_log_dir)) { | |
103 state_ = ALLOW_START; | |
104 return; | |
105 } | |
106 if (file_util::IsDirectoryEmpty(net_log_dir)) | |
107 state_ = ALLOW_START; | |
108 else | |
109 state_ = ALLOW_START_SEND; | |
110 } | |
111 | |
112 void NetLogTempFile::StartNetLog() { | |
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
114 | |
115 if (!g_browser_process->net_log()) | |
116 return; | |
117 | |
118 if (state_ == ALLOW_STOP) | |
119 return; | |
120 | |
121 FilePath net_log_dir; | |
122 if (!GetTempNetLogDir(&net_log_dir)) | |
123 return; | |
mmenke
2013/01/11 18:31:12
If the temp directory is in some cache/temp dir wh
ramant (doing other things)
2013/01/12 04:34:04
As we had talked, tried to create "chrome-net-expo
| |
124 | |
125 file_util::FileEnumerator file_iter( | |
126 net_log_dir, false, file_util::FileEnumerator::FILES); | |
127 for (FilePath current = file_iter.Next(); !current.empty(); | |
128 current = file_iter.Next()) { | |
129 file_util::Delete(current, false); | |
130 } | |
131 | |
132 if (!file_util::CreateTemporaryFileInDir(net_log_dir, &log_path_)) | |
mmenke
2013/01/11 18:31:12
This seems a little weird.
Normally, one uses thi
ramant (doing other things)
2013/01/12 04:34:04
As we had talked, will create "chrome-net-export/l
| |
133 return; | |
134 | |
135 net_log_logger_.reset(new NetLogLogger(log_path_)); | |
136 net_log_logger_->StartObserving(g_browser_process->net_log()); | |
mmenke
2013/01/11 18:31:12
Rather than messing with globals, suggest having t
ramant (doing other things)
2013/01/12 04:34:04
Done.
| |
137 state_ = ALLOW_STOP; | |
138 } | |
139 | |
140 void NetLogTempFile::StopNetLog() { | |
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
142 | |
143 if (!g_browser_process->net_log()) | |
144 return; | |
145 | |
146 if (state_ != ALLOW_STOP) | |
147 return; | |
148 | |
149 net_log_logger_->StopObserving(); | |
150 net_log_logger_.reset(); | |
151 state_ = ALLOW_START_SEND; | |
152 } | |
153 | |
154 void NetLogTempFile::SendNetLog() { | |
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); | |
156 | |
157 if (state_ != ALLOW_START_SEND) | |
158 return; | |
159 | |
160 FilePath net_log_dir; | |
161 if (!GetTempNetLogDir(&net_log_dir)) | |
162 return; | |
163 | |
164 FilePath file_to_send; | |
165 file_util::FileEnumerator file_iter( | |
166 net_log_dir, false, file_util::FileEnumerator::FILES); | |
167 for (FilePath current = file_iter.Next(); !current.empty(); | |
168 current = file_iter.Next()) { | |
169 file_to_send = current; | |
170 break; | |
171 } | |
172 if (file_to_send.empty()) | |
173 return; | |
174 | |
175 #if defined(OS_POSIX) | |
176 // Users, group and others can read, write and traverse. | |
177 int mode = file_util::FILE_PERMISSION_MASK; | |
178 file_util::SetPosixFilePermissions(file_to_send, mode); | |
179 #endif // defined(OS_POSIX) | |
180 | |
181 BrowserThread::PostTask( | |
182 BrowserThread::UI, | |
183 FROM_HERE, | |
184 base::Bind(&NetLogTempFile::SendEmail, file_to_send)); | |
185 } | |
186 | |
187 // static | |
188 void NetLogTempFile::SendEmail(const FilePath& file_to_send) { | |
mmenke
2013/01/11 18:31:12
Wonder if it would make more sense to put the in n
ramant (doing other things)
2013/01/12 04:34:04
Would like to talk to you more about the above.
"S
ramant (doing other things)
2013/01/15 02:19:28
Moved the SendEmail (UI) code to net_export_ui.cc
| |
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
190 | |
191 #if defined(OS_ANDROID) | |
192 std::string email; | |
193 std::string subject = "net_internals_log"; | |
194 std::string title = "test_title"; | |
195 std::string body = "Net Internals log data"; | |
196 FilePath::StringType file_to_attach(file_to_send.value()); | |
197 chrome::android::SendEmail( | |
198 UTF8ToUTF16(email), UTF8ToUTF16(subject), | |
199 UTF8ToUTF16(body), UTF8ToUTF16(title), UTF8ToUTF16(file_to_attach)); | |
200 #endif | |
201 } | |
OLD | NEW |