| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/media/webrtc_log_uploader.h" | 5 #include "chrome/browser/media/webrtc_log_uploader.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 } | 51 } |
| 52 | 52 |
| 53 WebRtcLogUploader::~WebRtcLogUploader() {} | 53 WebRtcLogUploader::~WebRtcLogUploader() {} |
| 54 | 54 |
| 55 void WebRtcLogUploader::OnURLFetchComplete( | 55 void WebRtcLogUploader::OnURLFetchComplete( |
| 56 const net::URLFetcher* source) { | 56 const net::URLFetcher* source) { |
| 57 int response_code = source->GetResponseCode(); | 57 int response_code = source->GetResponseCode(); |
| 58 std::string report_id; | 58 std::string report_id; |
| 59 if (response_code == 200 && source->GetResponseAsString(&report_id)) | 59 if (response_code == 200 && source->GetResponseAsString(&report_id)) |
| 60 AddUploadedLogInfoToUploadListFile(report_id); | 60 AddUploadedLogInfoToUploadListFile(report_id); |
| 61 DCHECK(upload_done_data_.find(source) != upload_done_data_.end()); |
| 62 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, |
| 63 base::Bind(&WebRtcLoggingHandlerHost::UploadLogDone, |
| 64 upload_done_data_[source].host)); |
| 65 if (!upload_done_data_[source].callback.is_null()) { |
| 66 bool success = response_code == 200; |
| 67 std::string error_message; |
| 68 if (!success) { |
| 69 error_message = "Uploading failed, response code: " + |
| 70 base::IntToString(response_code); |
| 71 } |
| 72 content::BrowserThread::PostTask( |
| 73 content::BrowserThread::UI, FROM_HERE, |
| 74 base::Bind(upload_done_data_[source].callback, success, report_id, |
| 75 error_message)); |
| 76 } |
| 77 upload_done_data_.erase(source); |
| 61 } | 78 } |
| 62 | 79 |
| 63 void WebRtcLogUploader::OnURLFetchUploadProgress( | 80 void WebRtcLogUploader::OnURLFetchUploadProgress( |
| 64 const net::URLFetcher* source, int64 current, int64 total) { | 81 const net::URLFetcher* source, int64 current, int64 total) { |
| 65 } | 82 } |
| 66 | 83 |
| 67 bool WebRtcLogUploader::ApplyForStartLogging() { | 84 bool WebRtcLogUploader::ApplyForStartLogging() { |
| 68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 69 if (log_count_ < kLogCountLimit) { | 86 if (log_count_ < kLogCountLimit) { |
| 70 ++log_count_; | 87 ++log_count_; |
| 71 return true; | 88 return true; |
| 72 } | 89 } |
| 73 return false; | 90 return false; |
| 74 } | 91 } |
| 75 | 92 |
| 76 void WebRtcLogUploader::UploadLog(net::URLRequestContextGetter* request_context, | 93 void WebRtcLogUploader::LoggingStoppedDontUpload() { |
| 77 scoped_ptr<base::SharedMemory> shared_memory, | 94 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 78 uint32 length, | 95 base::Bind(&WebRtcLogUploader::DecreaseLogCount, base::Unretained(this))); |
| 79 const std::string& app_session_id, | 96 } |
| 80 const std::string& app_url) { | 97 |
| 98 void WebRtcLogUploader::LoggingStoppedDoUpload( |
| 99 net::URLRequestContextGetter* request_context, |
| 100 scoped_ptr<base::SharedMemory> shared_memory, |
| 101 uint32 length, |
| 102 const std::map<std::string, std::string>& meta_data, |
| 103 const WebRtcLogUploadDoneData& upload_done_data) { |
| 81 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 104 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 82 DCHECK(shared_memory); | 105 DCHECK(shared_memory); |
| 83 DCHECK(shared_memory->memory()); | 106 DCHECK(shared_memory->memory()); |
| 84 | 107 |
| 85 std::string post_data; | 108 std::string post_data; |
| 86 SetupMultipart(&post_data, reinterpret_cast<uint8*>(shared_memory->memory()), | 109 SetupMultipart(&post_data, reinterpret_cast<uint8*>(shared_memory->memory()), |
| 87 length, app_session_id, app_url); | 110 length, meta_data); |
| 88 | 111 |
| 89 // If a test has set the test string pointer, write to it and skip uploading. | 112 // If a test has set the test string pointer, write to it and skip uploading. |
| 90 // This will be removed when the browser test for this feature is fully done | 113 // This will be removed when the browser test for this feature is fully done |
| 91 // according to the test plan. See http://crbug.com/257329. | 114 // according to the test plan. See http://crbug.com/257329. |
| 92 if (post_data_) { | 115 if (post_data_) { |
| 93 *post_data_ = post_data; | 116 *post_data_ = post_data; |
| 94 return; | 117 return; |
| 95 } | 118 } |
| 96 | 119 |
| 97 std::string content_type = kUploadContentType; | 120 std::string content_type = kUploadContentType; |
| 98 content_type.append("; boundary="); | 121 content_type.append("; boundary="); |
| 99 content_type.append(kMultipartBoundary); | 122 content_type.append(kMultipartBoundary); |
| 100 | 123 |
| 101 net::URLFetcher* url_fetcher = | 124 net::URLFetcher* url_fetcher = |
| 102 net::URLFetcher::Create(GURL(kUploadURL), net::URLFetcher::POST, this); | 125 net::URLFetcher::Create(GURL(kUploadURL), net::URLFetcher::POST, this); |
| 103 url_fetcher->SetRequestContext(request_context); | 126 url_fetcher->SetRequestContext(request_context); |
| 104 url_fetcher->SetUploadData(content_type, post_data); | 127 url_fetcher->SetUploadData(content_type, post_data); |
| 105 url_fetcher->Start(); | 128 url_fetcher->Start(); |
| 129 upload_done_data_[url_fetcher] = upload_done_data; |
| 106 | 130 |
| 107 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 131 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 108 base::Bind(&WebRtcLogUploader::DecreaseLogCount, base::Unretained(this))); | 132 base::Bind(&WebRtcLogUploader::DecreaseLogCount, base::Unretained(this))); |
| 109 } | 133 } |
| 110 | 134 |
| 111 void WebRtcLogUploader::SetupMultipart(std::string* post_data, | 135 void WebRtcLogUploader::SetupMultipart( |
| 112 uint8* log_buffer, | 136 std::string* post_data, uint8* log_buffer, uint32 log_buffer_length, |
| 113 uint32 log_buffer_length, | 137 const std::map<std::string, std::string>& meta_data) { |
| 114 const std::string& app_session_id, | |
| 115 const std::string& app_url) { | |
| 116 #if defined(OS_WIN) | 138 #if defined(OS_WIN) |
| 117 const char product[] = "Chrome"; | 139 const char product[] = "Chrome"; |
| 118 #elif defined(OS_MACOSX) | 140 #elif defined(OS_MACOSX) |
| 119 const char product[] = "Chrome_Mac"; | 141 const char product[] = "Chrome_Mac"; |
| 120 #elif defined(OS_LINUX) | 142 #elif defined(OS_LINUX) |
| 121 #if !defined(ADDRESS_SANITIZER) | 143 #if !defined(ADDRESS_SANITIZER) |
| 122 const char product[] = "Chrome_Linux"; | 144 const char product[] = "Chrome_Linux"; |
| 123 #else | 145 #else |
| 124 const char product[] = "Chrome_Linux_ASan"; | 146 const char product[] = "Chrome_Linux_ASan"; |
| 125 #endif | 147 #endif |
| 126 #elif defined(OS_ANDROID) | 148 #elif defined(OS_ANDROID) |
| 127 const char product[] = "Chrome_Android"; | 149 const char product[] = "Chrome_Android"; |
| 128 #elif defined(OS_CHROMEOS) | 150 #elif defined(OS_CHROMEOS) |
| 129 const char product[] = "Chrome_ChromeOS"; | 151 const char product[] = "Chrome_ChromeOS"; |
| 130 #else | 152 #else |
| 131 // This file should not be compiled for other platforms. | 153 // This file should not be compiled for other platforms. |
| 132 COMPILE_ASSERT(false); | 154 COMPILE_ASSERT(false); |
| 133 #endif | 155 #endif |
| 134 net::AddMultipartValueForUpload("prod", product, kMultipartBoundary, | 156 net::AddMultipartValueForUpload("prod", product, kMultipartBoundary, |
| 135 "", post_data); | 157 "", post_data); |
| 136 chrome::VersionInfo version_info; | 158 chrome::VersionInfo version_info; |
| 137 net::AddMultipartValueForUpload("ver", version_info.Version(), | 159 net::AddMultipartValueForUpload("ver", version_info.Version(), |
| 138 kMultipartBoundary, "", post_data); | 160 kMultipartBoundary, "", post_data); |
| 139 net::AddMultipartValueForUpload("guid", "0", kMultipartBoundary, | 161 net::AddMultipartValueForUpload("guid", "0", kMultipartBoundary, |
| 140 "", post_data); | 162 "", post_data); |
| 141 net::AddMultipartValueForUpload("type", "webrtc_log", kMultipartBoundary, | 163 net::AddMultipartValueForUpload("type", "webrtc_log", kMultipartBoundary, |
| 142 "", post_data); | 164 "", post_data); |
| 143 net::AddMultipartValueForUpload("app_session_id", app_session_id, | 165 |
| 144 kMultipartBoundary, "", post_data); | 166 // Add custom meta data. |
| 145 net::AddMultipartValueForUpload("url", app_url, kMultipartBoundary, | 167 std::map<std::string, std::string>::const_iterator it = meta_data.begin(); |
| 146 "", post_data); | 168 for (; it != meta_data.end(); ++it) { |
| 169 net::AddMultipartValueForUpload(it->first, it->second, kMultipartBoundary, |
| 170 "", post_data); |
| 171 } |
| 172 |
| 147 AddLogData(post_data, log_buffer, log_buffer_length); | 173 AddLogData(post_data, log_buffer, log_buffer_length); |
| 148 net::AddMultipartFinalDelimiterForUpload(kMultipartBoundary, post_data); | 174 net::AddMultipartFinalDelimiterForUpload(kMultipartBoundary, post_data); |
| 149 } | 175 } |
| 150 | 176 |
| 151 void WebRtcLogUploader::AddLogData(std::string* post_data, | 177 void WebRtcLogUploader::AddLogData(std::string* post_data, |
| 152 uint8* log_buffer, | 178 uint8* log_buffer, |
| 153 uint32 log_buffer_length) { | 179 uint32 log_buffer_length) { |
| 154 post_data->append("--"); | 180 post_data->append("--"); |
| 155 post_data->append(kMultipartBoundary); | 181 post_data->append(kMultipartBoundary); |
| 156 post_data->append("\r\n"); | 182 post_data->append("\r\n"); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 274 |
| 249 // Write the Unix time and report ID to the log list file. | 275 // Write the Unix time and report ID to the log list file. |
| 250 base::Time time_now = base::Time::Now(); | 276 base::Time time_now = base::Time::Now(); |
| 251 contents += base::DoubleToString(time_now.ToDoubleT()) + | 277 contents += base::DoubleToString(time_now.ToDoubleT()) + |
| 252 "," + report_id + '\n'; | 278 "," + report_id + '\n'; |
| 253 | 279 |
| 254 int written = file_util::WriteFile(upload_list_path_, &contents[0], | 280 int written = file_util::WriteFile(upload_list_path_, &contents[0], |
| 255 contents.size()); | 281 contents.size()); |
| 256 DPCHECK(written == static_cast<int>(contents.size())); | 282 DPCHECK(written == static_cast<int>(contents.size())); |
| 257 } | 283 } |
| OLD | NEW |