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/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 Init(); |
| 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 |
| 63 #ifndef NDEBUG |
| 64 dict->SetString("file", log_path_.MaybeAsASCII()); |
| 65 #endif // NDEBUG |
| 66 |
| 67 switch (state_) { |
| 68 case ALLOW_STOP: |
| 69 dict->SetString("state", "ALLOW_STOP"); |
| 70 break; |
| 71 case ALLOW_START_SEND: |
| 72 dict->SetString("state", "ALLOW_START_SEND"); |
| 73 break; |
| 74 default: |
| 75 dict->SetString("state", "ALLOW_START"); |
| 76 break; |
| 77 } |
| 78 return dict; |
| 79 } |
| 80 |
| 81 bool GetTempNetLogDir(FilePath* path) { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 83 |
| 84 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 85 FilePath temp_dir; |
| 86 if (!file_util::GetTempDir(&temp_dir)) |
| 87 return false; |
| 88 |
| 89 FilePath net_log_dir = temp_dir.Append(FILE_PATH_LITERAL("NetLog")); |
| 90 if (file_util::PathExists(net_log_dir)) { |
| 91 *path = net_log_dir; |
| 92 return true; |
| 93 } |
| 94 |
| 95 if (!file_util::CreateDirectory(net_log_dir)) |
| 96 return false; |
| 97 |
| 98 *path = net_log_dir; |
| 99 return true; |
| 100 } |
| 101 |
| 102 void NetLogTempFile::Init() { |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 104 |
| 105 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 106 FilePath net_log_dir; |
| 107 if (!GetTempNetLogDir(&net_log_dir)) { |
| 108 state_ = ALLOW_START; |
| 109 return; |
| 110 } |
| 111 |
| 112 if (file_util::IsDirectoryEmpty(net_log_dir)) |
| 113 state_ = ALLOW_START; |
| 114 else |
| 115 state_ = ALLOW_START_SEND; |
| 116 } |
| 117 |
| 118 void NetLogTempFile::StartNetLog() { |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 120 |
| 121 if (!g_browser_process->net_log()) |
| 122 return; |
| 123 |
| 124 if (state_ == ALLOW_STOP) |
| 125 return; |
| 126 |
| 127 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 128 FilePath net_log_dir; |
| 129 if (!GetTempNetLogDir(&net_log_dir)) |
| 130 return; |
| 131 |
| 132 file_util::FileEnumerator file_iter( |
| 133 net_log_dir, false, file_util::FileEnumerator::FILES); |
| 134 for (FilePath current = file_iter.Next(); !current.empty(); |
| 135 current = file_iter.Next()) { |
| 136 file_util::Delete(current, false); |
| 137 } |
| 138 |
| 139 if (!file_util::CreateTemporaryFileInDir(net_log_dir, &log_path_)) |
| 140 return; |
| 141 |
| 142 net_log_logger_.reset(new NetLogLogger(log_path_)); |
| 143 net_log_logger_->StartObserving(g_browser_process->net_log()); |
| 144 state_ = ALLOW_STOP; |
| 145 } |
| 146 |
| 147 void NetLogTempFile::StopNetLog() { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 149 |
| 150 if (!g_browser_process->net_log()) |
| 151 return; |
| 152 |
| 153 if (state_ != ALLOW_STOP) |
| 154 return; |
| 155 |
| 156 net_log_logger_->StopObserving(); |
| 157 net_log_logger_.reset(); |
| 158 state_ = ALLOW_START_SEND; |
| 159 } |
| 160 |
| 161 void NetLogTempFile::SendNetLog() { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 163 |
| 164 if (state_ != ALLOW_START_SEND) |
| 165 return; |
| 166 |
| 167 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 168 FilePath net_log_dir; |
| 169 if (!GetTempNetLogDir(&net_log_dir)) |
| 170 return; |
| 171 |
| 172 FilePath file_to_send; |
| 173 file_util::FileEnumerator file_iter( |
| 174 net_log_dir, false, file_util::FileEnumerator::FILES); |
| 175 for (FilePath current = file_iter.Next(); !current.empty(); |
| 176 current = file_iter.Next()) { |
| 177 file_to_send = current; |
| 178 break; |
| 179 } |
| 180 if (file_to_send.empty()) |
| 181 return; |
| 182 |
| 183 #if defined(OS_POSIX) |
| 184 // Users, group and others can read, write and traverse. |
| 185 int mode = file_util::FILE_PERMISSION_MASK; |
| 186 file_util::SetPosixFilePermissions(file_to_send, mode); |
| 187 #endif // defined(OS_POSIX) |
| 188 |
| 189 BrowserThread::PostTask( |
| 190 BrowserThread::UI, |
| 191 FROM_HERE, |
| 192 base::Bind(&NetLogTempFile::SendEmail, file_to_send)); |
| 193 } |
| 194 |
| 195 // static |
| 196 void NetLogTempFile::SendEmail(const FilePath& file_to_send) { |
| 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 198 |
| 199 #if defined(OS_ANDROID) |
| 200 std::string email; |
| 201 std::string subject = "net_internals_log"; |
| 202 std::string title = "test_title"; |
| 203 std::string body = "Net Internals log data"; |
| 204 FilePath::StringType file_to_attach(file_to_send.value()); |
| 205 chrome::android::SendEmail( |
| 206 UTF8ToUTF16(email), UTF8ToUTF16(subject), |
| 207 UTF8ToUTF16(body), UTF8ToUTF16(title), UTF8ToUTF16(file_to_attach)); |
| 208 #endif |
| 209 } |
OLD | NEW |