OLD | NEW |
(Empty) | |
| 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 |
| 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/json/json_writer.h" |
| 12 #include "base/logging.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" |
| 17 #include "base/threading/thread_restrictions.h" |
| 18 #include "base/utf_string_conversions.h" |
| 19 #include "base/values.h" |
| 20 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/net/chrome_net_log.h" |
| 22 #include "chrome/browser/net/net_log_logger.h" |
| 23 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" |
| 24 #include "content/public/browser/browser_thread.h" |
| 25 |
| 26 #if defined(OS_ANDROID) |
| 27 #include "chrome/browser/android/intent_helper.h" |
| 28 #endif |
| 29 |
| 30 using content::BrowserThread; |
| 31 |
| 32 NetLogTempFile* NetLogTempFile::GetInstance() { |
| 33 return Singleton<NetLogTempFile>::get(); |
| 34 } |
| 35 |
| 36 NetLogTempFile::NetLogTempFile() : state_(UNINITIALIZED) { |
| 37 DetermineInitialState(); |
| 38 } |
| 39 |
| 40 NetLogTempFile::~NetLogTempFile() { |
| 41 } |
| 42 |
| 43 void NetLogTempFile::ProcessCommand(Command command) { |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 45 switch (command) { |
| 46 case DO_START: |
| 47 StartNetLog(); |
| 48 break; |
| 49 case DO_STOP: |
| 50 StopNetLog(); |
| 51 break; |
| 52 case DO_SEND: |
| 53 SendNetLog(); |
| 54 break; |
| 55 default: |
| 56 break; |
| 57 } |
| 58 } |
| 59 |
| 60 Value* NetLogTempFile::NetLogTempFileToValue() const { |
| 61 base::DictionaryValue* dict = new base::DictionaryValue; |
| 62 switch (state_) { |
| 63 case STOP: |
| 64 dict->SetString("file", log_path_.MaybeAsASCII()); |
| 65 dict->SetString("state", "STOP"); |
| 66 break; |
| 67 case START_SEND: |
| 68 dict->SetString("file", log_path_.MaybeAsASCII()); |
| 69 dict->SetString("state", "START_SEND"); |
| 70 break; |
| 71 default: |
| 72 dict->SetString("file", ""); |
| 73 dict->SetString("state", "START"); |
| 74 break; |
| 75 } |
| 76 return dict; |
| 77 } |
| 78 |
| 79 bool GetTempNetLogDir(FilePath* path) { |
| 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 81 |
| 82 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 83 FilePath temp_dir; |
| 84 if (!file_util::GetTempDir(&temp_dir)) |
| 85 return false; |
| 86 |
| 87 FilePath net_log_dir = temp_dir.Append(FILE_PATH_LITERAL("NetLog")); |
| 88 if (file_util::PathExists(net_log_dir)) { |
| 89 *path = net_log_dir; |
| 90 return true; |
| 91 } |
| 92 |
| 93 if (!file_util::CreateDirectory(net_log_dir)) |
| 94 return false; |
| 95 |
| 96 *path = net_log_dir; |
| 97 return true; |
| 98 } |
| 99 |
| 100 void NetLogTempFile::DetermineInitialState() { |
| 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 102 |
| 103 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 104 FilePath net_log_dir; |
| 105 if (!GetTempNetLogDir(&net_log_dir)) { |
| 106 state_ = START; |
| 107 return; |
| 108 } |
| 109 |
| 110 if (file_util::IsDirectoryEmpty(net_log_dir)) |
| 111 state_ = START; |
| 112 else |
| 113 state_ = START_SEND; |
| 114 } |
| 115 |
| 116 void NetLogTempFile::StartNetLog() { |
| 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 118 |
| 119 if (!g_browser_process->net_log()) |
| 120 return; |
| 121 |
| 122 if (state_ == STOP) |
| 123 return; |
| 124 |
| 125 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 126 FilePath net_log_dir; |
| 127 if (!GetTempNetLogDir(&net_log_dir)) |
| 128 return; |
| 129 |
| 130 file_util::FileEnumerator file_iter( |
| 131 net_log_dir, false, file_util::FileEnumerator::FILES); |
| 132 for (FilePath current = file_iter.Next(); !current.empty(); |
| 133 current = file_iter.Next()) { |
| 134 file_util::Delete(current, false); |
| 135 } |
| 136 |
| 137 if (!file_util::CreateTemporaryFileInDir(net_log_dir, &log_path_)) |
| 138 return; |
| 139 |
| 140 net_log_logger_.reset(new NetLogLogger(log_path_)); |
| 141 net_log_logger_->StartObserving(g_browser_process->net_log()); |
| 142 state_ = STOP; |
| 143 } |
| 144 |
| 145 void NetLogTempFile::StopNetLog() { |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 147 |
| 148 if (!g_browser_process->net_log()) |
| 149 return; |
| 150 |
| 151 if (state_ != STOP) |
| 152 return; |
| 153 |
| 154 net_log_logger_->StopObserving(g_browser_process->net_log()); |
| 155 net_log_logger_.reset(); |
| 156 state_ = START_SEND; |
| 157 } |
| 158 |
| 159 void NetLogTempFile::SendNetLog() { |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 161 |
| 162 if (state_ != START_SEND) |
| 163 return; |
| 164 |
| 165 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 166 FilePath net_log_dir; |
| 167 if (!GetTempNetLogDir(&net_log_dir)) |
| 168 return; |
| 169 |
| 170 FilePath file_to_send; |
| 171 file_util::FileEnumerator file_iter( |
| 172 net_log_dir, false, file_util::FileEnumerator::FILES); |
| 173 for (FilePath current = file_iter.Next(); !current.empty(); |
| 174 current = file_iter.Next()) { |
| 175 file_to_send = current; |
| 176 break; |
| 177 } |
| 178 if (file_to_send.empty()) |
| 179 return; |
| 180 |
| 181 #if defined(OS_POSIX) |
| 182 // Users, group and others can read, write and traverse. |
| 183 int mode = file_util::FILE_PERMISSION_MASK; |
| 184 file_util::SetPosixFilePermissions(file_to_send, mode); |
| 185 #endif // defined(OS_POSIX) |
| 186 |
| 187 BrowserThread::PostTask( |
| 188 BrowserThread::UI, |
| 189 FROM_HERE, |
| 190 base::Bind(&NetLogTempFile::SendEmail, file_to_send)); |
| 191 } |
| 192 |
| 193 // static |
| 194 void NetLogTempFile::SendEmail(const FilePath& file_to_send) { |
| 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 196 |
| 197 #if defined(OS_ANDROID) |
| 198 std::string email; |
| 199 std::string subject = "net_internals_log"; |
| 200 std::string title = "test_title"; |
| 201 std::string body = "Net Internals log data"; |
| 202 FilePath::StringType file_to_attach(file_to_send.value()); |
| 203 chrome::android::SendEmail( |
| 204 UTF8ToUTF16(email), UTF8ToUTF16(subject), |
| 205 UTF8ToUTF16(body), UTF8ToUTF16(title), UTF8ToUTF16(file_to_attach)); |
| 206 #endif |
| 207 } |
OLD | NEW |