Chromium Code Reviews| Index: chrome/browser/net/net_log_logger.cc |
| =================================================================== |
| --- chrome/browser/net/net_log_logger.cc (revision 171385) |
| +++ chrome/browser/net/net_log_logger.cc (working copy) |
| @@ -6,20 +6,38 @@ |
| #include <stdio.h> |
| +#include "base/bind.h" |
| #include "base/file_util.h" |
| #include "base/json/json_writer.h" |
| #include "base/logging.h" |
| -#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/string_util.h" |
| +#include "base/threading/platform_thread.h" |
| #include "base/threading/thread_restrictions.h" |
| +#include "base/utf_string_conversions.h" |
| #include "base/values.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/net/chrome_net_log.h" |
| #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" |
| +#include "content/public/browser/browser_thread.h" |
| -NetLogLogger::NetLogLogger(const FilePath &log_path) { |
| - if (!log_path.empty()) { |
| +#if defined(OS_ANDROID) |
| +#include "chrome/browser/android/intent_helper.h" |
| +#endif |
| + |
| +using content::BrowserThread; |
| + |
| +NetLogLogger::NetLogLogger(const FilePath &log_path) : log_path_(log_path) { |
| + OpenFile(); |
| +} |
| + |
| +void NetLogLogger::OpenFile() { |
| + if (!log_path_.empty()) { |
| base::ThreadRestrictions::ScopedAllowIO allow_io; |
| - FILE* fp = file_util::OpenFile(log_path, "w"); |
| + FILE* fp = file_util::OpenFile(log_path_, "w"); |
| if (!fp) { |
| - LOG(ERROR) << "Could not open file " << log_path.value() |
| + LOG(ERROR) << "Could not open file " << log_path_.value() |
| << " for net logging"; |
| return; |
| } |
| @@ -57,3 +75,117 @@ |
| fprintf(file_.get(), "%s,\n", json.c_str()); |
| } |
| } |
| + |
| +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
|
| + |
| +// static |
| +void NetLogLogger::StartNetLog() { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE_USER_BLOCKING, |
| + FROM_HERE, |
| + base::Bind(&StartNetLog)); |
| + return; |
| + } |
| + |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| + if (g_net_log_logger_ || !g_browser_process->net_log()) |
| + return; |
| + |
| + FilePath log_path; |
| + if (!file_util::CreateTemporaryFile(&log_path)) |
| + return; |
| + |
| + g_net_log_logger_ = new NetLogLogger(log_path); |
| + g_browser_process->net_log()->AddThreadSafeObserver( |
| + g_net_log_logger_, net::NetLog::LOG_ALL_BUT_BYTES); |
| +} |
| + |
| +// static |
| +void NetLogLogger::StopNetLog() { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE_USER_BLOCKING, |
| + FROM_HERE, |
| + base::Bind(&StopNetLog)); |
| + return; |
| + } |
| + |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| + if (!g_net_log_logger_) |
| + return; |
| + |
| + if (g_browser_process->net_log()) |
| + g_browser_process->net_log()->RemoveThreadSafeObserver(g_net_log_logger_); |
| + |
| + if (g_net_log_logger_->file_.get()) |
| + g_net_log_logger_->file_.Close(); |
| + |
| + if (!g_net_log_logger_->log_path_.empty()) |
| + file_util::Delete(g_net_log_logger_->log_path_, false); |
| + |
| + for (unsigned i = 0; i < g_net_log_logger_->files_to_delete_.size(); i++) { |
| + FilePath file_to_attach_path(g_net_log_logger_->files_to_delete_[i]); |
| + file_util::Delete(file_to_attach_path, false); |
| + } |
| + |
| + delete g_net_log_logger_; |
| + g_net_log_logger_ = NULL; |
| +} |
| + |
| +// static |
| +void NetLogLogger::SendNetLog() { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE_USER_BLOCKING, |
| + FROM_HERE, |
| + base::Bind(&SendNetLog)); |
| + return; |
| + } |
| + |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)); |
| + if (!g_net_log_logger_) |
| + return; |
| + |
| + if (g_net_log_logger_->file_.get()) |
| + g_net_log_logger_->file_.Close(); |
| + |
| + FilePath temp_file_path; |
| + if (file_util::CreateTemporaryFile(&temp_file_path)) { |
| + FilePath::StringType file_to_attach(temp_file_path.value()); |
| + FilePath file_to_attach_path(file_to_attach); |
| + |
| + if (file_util::ReplaceFile(g_net_log_logger_->log_path_, |
| + file_to_attach_path)) { |
| +#if defined(OS_POSIX) |
| + // Users, group and others can read, write and traverse. |
| + int mode = file_util::FILE_PERMISSION_MASK; |
| + file_util::SetPosixFilePermissions(file_to_attach_path, mode); |
| +#endif // defined(OS_POSIX) |
| + g_net_log_logger_->files_to_delete_.push_back( |
| + file_to_attach_path.value()); |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&SendEmail, g_net_log_logger_->files_to_delete_.back())); |
| + } |
| + } |
| + |
| + g_net_log_logger_->OpenFile(); |
| +} |
| + |
| +// static |
| +void NetLogLogger::SendEmail(const FilePath::StringType& file_to_attach) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| +#if defined(OS_ANDROID) |
| + std::string email; |
| + std::string subject = "net_internals_log"; |
| + std::string title = "test_title"; |
| + std::string body = "Net Internals log data"; |
| + chrome::android::SendEmail( |
| + UTF8ToUTF16(email), UTF8ToUTF16(subject), |
| + UTF8ToUTF16(body), UTF8ToUTF16(title), UTF8ToUTF16(file_to_attach)); |
| +#endif |
| +} |