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

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

Issue 11467013: First cut at UI for saving net_internals data into a file for mobile (Android and Ios). (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/net/net_log_logger.h" 5 #include "chrome/browser/net/net_log_logger.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 8
9 #include "base/bind.h"
9 #include "base/file_util.h" 10 #include "base/file_util.h"
10 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/message_loop.h"
14 #include "base/string_number_conversions.h"
15 #include "base/string_util.h"
16 #include "base/threading/platform_thread.h"
13 #include "base/threading/thread_restrictions.h" 17 #include "base/threading/thread_restrictions.h"
18 #include "base/utf_string_conversions.h"
14 #include "base/values.h" 19 #include "base/values.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/net/chrome_net_log.h"
15 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" 22 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h"
23 #include "content/public/browser/browser_thread.h"
16 24
17 NetLogLogger::NetLogLogger(const FilePath &log_path) { 25 #if defined(OS_ANDROID)
18 if (!log_path.empty()) { 26 #include "chrome/browser/android/intent_helper.h"
27 #endif
28
29 using content::BrowserThread;
30
31 NetLogLogger::NetLogLogger(const FilePath &log_path) : log_path_(log_path) {
32 OpenFile();
33 }
34
35 void NetLogLogger::OpenFile() {
36 if (!log_path_.empty()) {
19 base::ThreadRestrictions::ScopedAllowIO allow_io; 37 base::ThreadRestrictions::ScopedAllowIO allow_io;
20 FILE* fp = file_util::OpenFile(log_path, "w"); 38 FILE* fp = file_util::OpenFile(log_path_, "w");
21 if (!fp) { 39 if (!fp) {
22 LOG(ERROR) << "Could not open file " << log_path.value() 40 LOG(ERROR) << "Could not open file " << log_path_.value()
23 << " for net logging"; 41 << " for net logging";
24 return; 42 return;
25 } 43 }
26 file_.Set(fp); 44 file_.Set(fp);
27 45
28 // Write constants to the output file. This allows loading files that have 46 // Write constants to the output file. This allows loading files that have
29 // different source and event types, as they may be added and removed 47 // different source and event types, as they may be added and removed
30 // between Chrome versions. 48 // between Chrome versions.
31 scoped_ptr<Value> value(NetInternalsUI::GetConstants()); 49 scoped_ptr<Value> value(NetInternalsUI::GetConstants());
32 std::string json; 50 std::string json;
(...skipping 17 matching lines...) Expand all
50 // instead of integer identifiers allows logs from older versions to be 68 // instead of integer identifiers allows logs from older versions to be
51 // loaded, though a little extra parsing has to be done when loading a log. 69 // loaded, though a little extra parsing has to be done when loading a log.
52 std::string json; 70 std::string json;
53 base::JSONWriter::Write(value.get(), &json); 71 base::JSONWriter::Write(value.get(), &json);
54 if (!file_.get()) { 72 if (!file_.get()) {
55 VLOG(1) << json; 73 VLOG(1) << json;
56 } else { 74 } else {
57 fprintf(file_.get(), "%s,\n", json.c_str()); 75 fprintf(file_.get(), "%s,\n", json.c_str());
58 } 76 }
59 } 77 }
78
79 static NetLogLogger* g_net_log_logger_ = NULL;
droger 2012/12/10 10:37:07 Can you put |g_net_log_logger_| in an anonymous na
80
81 // static
82 void NetLogLogger::StartNetLog() {
83 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) {
84 BrowserThread::PostTask(
85 BrowserThread::FILE_USER_BLOCKING,
86 FROM_HERE,
87 base::Bind(&StartNetLog));
88 return;
89 }
90
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
92 if (g_net_log_logger_ || !g_browser_process->net_log())
93 return;
94
95 FilePath log_path;
96 if (!file_util::CreateTemporaryFile(&log_path))
97 return;
98
99 g_net_log_logger_ = new NetLogLogger(log_path);
100 g_browser_process->net_log()->AddThreadSafeObserver(
101 g_net_log_logger_, net::NetLog::LOG_ALL_BUT_BYTES);
102 }
103
104 // static
105 void NetLogLogger::StopNetLog() {
106 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) {
107 BrowserThread::PostTask(
108 BrowserThread::FILE_USER_BLOCKING,
109 FROM_HERE,
110 base::Bind(&StopNetLog));
111 return;
112 }
113
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
115 if (!g_net_log_logger_)
116 return;
117
118 if (g_browser_process->net_log())
119 g_browser_process->net_log()->RemoveThreadSafeObserver(g_net_log_logger_);
120
121 if (g_net_log_logger_->file_.get())
122 g_net_log_logger_->file_.Close();
123
124 if (!g_net_log_logger_->log_path_.empty())
125 file_util::Delete(g_net_log_logger_->log_path_, false);
126
127 for (unsigned i = 0; i < g_net_log_logger_->files_to_delete_.size(); i++) {
128 FilePath file_to_attach_path(g_net_log_logger_->files_to_delete_[i]);
129 file_util::Delete(file_to_attach_path, false);
130 }
131
132 delete g_net_log_logger_;
133 g_net_log_logger_ = NULL;
134 }
135
136 // static
137 void NetLogLogger::SendNetLog() {
138 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) {
139 BrowserThread::PostTask(
140 BrowserThread::FILE_USER_BLOCKING,
141 FROM_HERE,
142 base::Bind(&SendNetLog));
143 return;
144 }
145
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING));
147 if (!g_net_log_logger_)
148 return;
149
150 if (g_net_log_logger_->file_.get())
151 g_net_log_logger_->file_.Close();
152
153 FilePath temp_file_path;
154 if (file_util::CreateTemporaryFile(&temp_file_path)) {
155 FilePath::StringType file_to_attach(temp_file_path.value());
156 FilePath file_to_attach_path(file_to_attach);
157
158 if (file_util::ReplaceFile(g_net_log_logger_->log_path_,
159 file_to_attach_path)) {
160 #if defined(OS_POSIX)
161 // Users, group and others can read, write and traverse.
162 int mode = file_util::FILE_PERMISSION_MASK;
163 file_util::SetPosixFilePermissions(file_to_attach_path, mode);
164 #endif // defined(OS_POSIX)
165 g_net_log_logger_->files_to_delete_.push_back(
166 file_to_attach_path.value());
167
168 BrowserThread::PostTask(
169 BrowserThread::UI,
170 FROM_HERE,
171 base::Bind(&SendEmail, g_net_log_logger_->files_to_delete_.back()));
172 }
173 }
174
175 g_net_log_logger_->OpenFile();
176 }
177
178 // static
179 void NetLogLogger::SendEmail(const FilePath::StringType& file_to_attach) {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
181
182 #if defined(OS_ANDROID)
183 std::string email;
184 std::string subject = "net_internals_log";
185 std::string title = "test_title";
186 std::string body = "Net Internals log data";
187 chrome::android::SendEmail(
188 UTF8ToUTF16(email), UTF8ToUTF16(subject),
189 UTF8ToUTF16(body), UTF8ToUTF16(title), UTF8ToUTF16(file_to_attach));
190 #endif
191 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698