| 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_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/shared_memory.h" | |
| 12 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 13 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 15 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 16 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/media/webrtc_log_list.h" | 17 #include "chrome/browser/media/webrtc_log_list.h" |
| 18 #include "chrome/browser/media/webrtc_log_util.h" | 18 #include "chrome/browser/media/webrtc_log_util.h" |
| 19 #include "chrome/common/chrome_paths.h" | |
| 20 #include "chrome/common/chrome_version_info.h" | 19 #include "chrome/common/chrome_version_info.h" |
| 21 #include "chrome/common/partial_circular_buffer.h" | 20 #include "chrome/common/partial_circular_buffer.h" |
| 22 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 23 #include "net/base/mime_util.h" | 22 #include "net/base/mime_util.h" |
| 24 #include "net/base/network_delegate.h" | |
| 25 #include "net/proxy/proxy_config.h" | |
| 26 #include "net/proxy/proxy_config_service.h" | |
| 27 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
| 28 #include "net/url_request/url_request_context.h" | |
| 29 #include "net/url_request/url_request_context_builder.h" | |
| 30 #include "net/url_request/url_request_context_getter.h" | |
| 31 #include "third_party/zlib/zlib.h" | 24 #include "third_party/zlib/zlib.h" |
| 32 | 25 |
| 33 namespace { | 26 namespace { |
| 34 | 27 |
| 35 const int kLogCountLimit = 5; | 28 const int kLogCountLimit = 5; |
| 36 const uint32 kIntermediateCompressionBufferBytes = 256 * 1024; // 256 KB | 29 const uint32 kIntermediateCompressionBufferBytes = 256 * 1024; // 256 KB |
| 37 const int kLogListLimitLines = 50; | 30 const int kLogListLimitLines = 50; |
| 38 | 31 |
| 39 const char kUploadURL[] = "https://clients2.google.com/cr/report"; | 32 const char kUploadURL[] = "https://clients2.google.com/cr/report"; |
| 40 const char kUploadContentType[] = "multipart/form-data"; | 33 const char kUploadContentType[] = "multipart/form-data"; |
| 41 const char kMultipartBoundary[] = | 34 const char kMultipartBoundary[] = |
| 42 "----**--yradnuoBgoLtrapitluMklaTelgooG--**----"; | 35 "----**--yradnuoBgoLtrapitluMklaTelgooG--**----"; |
| 43 | 36 |
| 44 const int kHttpResponseOk = 200; | 37 const int kHttpResponseOk = 200; |
| 45 | 38 |
| 46 } // namespace | 39 } // namespace |
| 47 | 40 |
| 48 WebRtcLogUploadDoneData::WebRtcLogUploadDoneData() {} | 41 WebRtcLogUploadDoneData::WebRtcLogUploadDoneData() {} |
| 49 | 42 |
| 50 WebRtcLogUploadDoneData::~WebRtcLogUploadDoneData() {} | 43 WebRtcLogUploadDoneData::~WebRtcLogUploadDoneData() {} |
| 51 | 44 |
| 52 WebRtcLogUploader::WebRtcLogUploader() | 45 WebRtcLogUploader::WebRtcLogUploader() |
| 53 : log_count_(0), | 46 : log_count_(0), |
| 54 post_data_(NULL) { | 47 post_data_(NULL), |
| 48 shutting_down_(false) { |
| 55 file_thread_checker_.DetachFromThread(); | 49 file_thread_checker_.DetachFromThread(); |
| 56 } | 50 } |
| 57 | 51 |
| 58 WebRtcLogUploader::~WebRtcLogUploader() { | 52 WebRtcLogUploader::~WebRtcLogUploader() { |
| 59 DCHECK(create_thread_checker_.CalledOnValidThread()); | 53 DCHECK(create_thread_checker_.CalledOnValidThread()); |
| 60 // Delete all pending URLFetcher and release all references to | 54 DCHECK(upload_done_data_.empty()); |
| 61 // WebRtcLoggingHandlerHost. | 55 DCHECK(shutting_down_); |
| 62 for (UploadDoneDataMap::iterator it = upload_done_data_.begin(); | |
| 63 it != upload_done_data_.end(); ++it) { | |
| 64 delete it->first; | |
| 65 } | |
| 66 upload_done_data_.clear(); | |
| 67 } | 56 } |
| 68 | 57 |
| 69 void WebRtcLogUploader::OnURLFetchComplete( | 58 void WebRtcLogUploader::OnURLFetchComplete( |
| 70 const net::URLFetcher* source) { | 59 const net::URLFetcher* source) { |
| 71 DCHECK(create_thread_checker_.CalledOnValidThread()); | 60 DCHECK(create_thread_checker_.CalledOnValidThread()); |
| 72 DCHECK(upload_done_data_.find(source) != upload_done_data_.end()); | 61 DCHECK(upload_done_data_.find(source) != upload_done_data_.end()); |
| 62 DCHECK(!shutting_down_); |
| 73 int response_code = source->GetResponseCode(); | 63 int response_code = source->GetResponseCode(); |
| 74 std::string report_id; | |
| 75 UploadDoneDataMap::iterator it = upload_done_data_.find(source); | 64 UploadDoneDataMap::iterator it = upload_done_data_.find(source); |
| 76 if (it != upload_done_data_.end()) { | 65 if (it != upload_done_data_.end()) { |
| 77 // The log path can be empty here if we failed getting it before. We still | 66 // The log path can be empty here if we failed getting it before. We still |
| 78 // upload the log if that's the case. | 67 // upload the log if that's the case. |
| 68 std::string report_id; |
| 79 if (response_code == kHttpResponseOk && | 69 if (response_code == kHttpResponseOk && |
| 80 source->GetResponseAsString(&report_id) && | 70 source->GetResponseAsString(&report_id) && |
| 81 !it->second.log_path.empty()) { | 71 !it->second.log_path.empty()) { |
| 82 base::FilePath log_list_path = | 72 base::FilePath log_list_path = |
| 83 WebRtcLogList::GetWebRtcLogListFileForDirectory(it->second.log_path); | 73 WebRtcLogList::GetWebRtcLogListFileForDirectory(it->second.log_path); |
| 84 content::BrowserThread::PostTask( | 74 content::BrowserThread::PostTask( |
| 85 content::BrowserThread::FILE, | 75 content::BrowserThread::FILE, |
| 86 FROM_HERE, | 76 FROM_HERE, |
| 87 base::Bind(&WebRtcLogUploader::AddUploadedLogInfoToUploadListFile, | 77 base::Bind(&WebRtcLogUploader::AddUploadedLogInfoToUploadListFile, |
| 88 base::Unretained(this), | 78 base::Unretained(this), |
| 89 log_list_path, | 79 log_list_path, |
| 90 it->second.local_log_id, | 80 it->second.local_log_id, |
| 91 report_id)); | 81 report_id)); |
| 92 } | 82 } |
| 93 NotifyUploadDone(response_code, report_id, it->second); | 83 NotifyUploadDone(response_code, report_id, it->second); |
| 94 upload_done_data_.erase(it); | 84 upload_done_data_.erase(it); |
| 95 } | 85 } |
| 86 |
| 96 delete source; | 87 delete source; |
| 97 } | 88 } |
| 98 | 89 |
| 99 void WebRtcLogUploader::OnURLFetchUploadProgress( | 90 void WebRtcLogUploader::OnURLFetchUploadProgress( |
| 100 const net::URLFetcher* source, int64 current, int64 total) { | 91 const net::URLFetcher* source, int64 current, int64 total) { |
| 101 } | 92 } |
| 102 | 93 |
| 103 bool WebRtcLogUploader::ApplyForStartLogging() { | 94 bool WebRtcLogUploader::ApplyForStartLogging() { |
| 104 DCHECK(create_thread_checker_.CalledOnValidThread()); | 95 DCHECK(create_thread_checker_.CalledOnValidThread()); |
| 105 if (log_count_ < kLogCountLimit) { | 96 if (log_count_ < kLogCountLimit && !shutting_down_) { |
| 106 ++log_count_; | 97 ++log_count_; |
| 107 return true; | 98 return true; |
| 108 } | 99 } |
| 109 return false; | 100 return false; |
| 110 } | 101 } |
| 111 | 102 |
| 112 void WebRtcLogUploader::LoggingStoppedDontUpload() { | 103 void WebRtcLogUploader::LoggingStoppedDontUpload() { |
| 113 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 104 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 114 base::Bind(&WebRtcLogUploader::DecreaseLogCount, base::Unretained(this))); | 105 base::Bind(&WebRtcLogUploader::DecreaseLogCount, base::Unretained(this))); |
| 115 } | 106 } |
| 116 | 107 |
| 117 void WebRtcLogUploader::LoggingStoppedDoUpload( | 108 void WebRtcLogUploader::LoggingStoppedDoUpload( |
| 118 net::URLRequestContextGetter* request_context, | |
| 119 scoped_ptr<unsigned char[]> log_buffer, | 109 scoped_ptr<unsigned char[]> log_buffer, |
| 120 uint32 length, | 110 uint32 length, |
| 121 const std::map<std::string, std::string>& meta_data, | 111 const std::map<std::string, std::string>& meta_data, |
| 122 const WebRtcLogUploadDoneData& upload_done_data) { | 112 const WebRtcLogUploadDoneData& upload_done_data) { |
| 123 DCHECK(file_thread_checker_.CalledOnValidThread()); | 113 DCHECK(file_thread_checker_.CalledOnValidThread()); |
| 124 DCHECK(log_buffer.get()); | 114 DCHECK(log_buffer.get()); |
| 125 DCHECK(!upload_done_data.log_path.empty()); | 115 DCHECK(!upload_done_data.log_path.empty()); |
| 126 | 116 |
| 127 std::vector<uint8> compressed_log; | 117 std::vector<uint8> compressed_log; |
| 128 CompressLog( | 118 CompressLog( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 *post_data_ = *post_data; | 150 *post_data_ = *post_data; |
| 161 NotifyUploadDone(kHttpResponseOk, "", upload_done_data_with_log_id); | 151 NotifyUploadDone(kHttpResponseOk, "", upload_done_data_with_log_id); |
| 162 return; | 152 return; |
| 163 } | 153 } |
| 164 | 154 |
| 165 content::BrowserThread::PostTask( | 155 content::BrowserThread::PostTask( |
| 166 content::BrowserThread::UI, | 156 content::BrowserThread::UI, |
| 167 FROM_HERE, | 157 FROM_HERE, |
| 168 base::Bind(&WebRtcLogUploader::CreateAndStartURLFetcher, | 158 base::Bind(&WebRtcLogUploader::CreateAndStartURLFetcher, |
| 169 base::Unretained(this), | 159 base::Unretained(this), |
| 170 make_scoped_refptr(request_context), | |
| 171 upload_done_data_with_log_id, | 160 upload_done_data_with_log_id, |
| 172 Passed(&post_data))); | 161 Passed(&post_data))); |
| 173 | 162 |
| 174 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 163 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 175 base::Bind(&WebRtcLogUploader::DecreaseLogCount, base::Unretained(this))); | 164 base::Bind(&WebRtcLogUploader::DecreaseLogCount, base::Unretained(this))); |
| 176 } | 165 } |
| 177 | 166 |
| 167 void WebRtcLogUploader::StartShutdown() { |
| 168 DCHECK(create_thread_checker_.CalledOnValidThread()); |
| 169 DCHECK(!shutting_down_); |
| 170 |
| 171 // Delete all URLFetchers first and clear the upload done map. |
| 172 for (UploadDoneDataMap::iterator it = upload_done_data_.begin(); |
| 173 it != upload_done_data_.end(); |
| 174 ++it) { |
| 175 delete it->first; |
| 176 } |
| 177 upload_done_data_.clear(); |
| 178 shutting_down_ = true; |
| 179 } |
| 180 |
| 178 void WebRtcLogUploader::SetupMultipart( | 181 void WebRtcLogUploader::SetupMultipart( |
| 179 std::string* post_data, | 182 std::string* post_data, |
| 180 const std::vector<uint8>& compressed_log, | 183 const std::vector<uint8>& compressed_log, |
| 181 const std::map<std::string, std::string>& meta_data) { | 184 const std::map<std::string, std::string>& meta_data) { |
| 182 #if defined(OS_WIN) | 185 #if defined(OS_WIN) |
| 183 const char product[] = "Chrome"; | 186 const char product[] = "Chrome"; |
| 184 #elif defined(OS_MACOSX) | 187 #elif defined(OS_MACOSX) |
| 185 const char product[] = "Chrome_Mac"; | 188 const char product[] = "Chrome_Mac"; |
| 186 #elif defined(OS_LINUX) | 189 #elif defined(OS_LINUX) |
| 187 #if !defined(ADDRESS_SANITIZER) | 190 #if !defined(ADDRESS_SANITIZER) |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 | 281 |
| 279 void WebRtcLogUploader::ResizeForNextOutput(std::vector<uint8>* compressed_log, | 282 void WebRtcLogUploader::ResizeForNextOutput(std::vector<uint8>* compressed_log, |
| 280 z_stream* stream) { | 283 z_stream* stream) { |
| 281 size_t old_size = compressed_log->size() - stream->avail_out; | 284 size_t old_size = compressed_log->size() - stream->avail_out; |
| 282 compressed_log->resize(old_size + kIntermediateCompressionBufferBytes); | 285 compressed_log->resize(old_size + kIntermediateCompressionBufferBytes); |
| 283 stream->next_out = &(*compressed_log)[old_size]; | 286 stream->next_out = &(*compressed_log)[old_size]; |
| 284 stream->avail_out = kIntermediateCompressionBufferBytes; | 287 stream->avail_out = kIntermediateCompressionBufferBytes; |
| 285 } | 288 } |
| 286 | 289 |
| 287 void WebRtcLogUploader::CreateAndStartURLFetcher( | 290 void WebRtcLogUploader::CreateAndStartURLFetcher( |
| 288 scoped_refptr<net::URLRequestContextGetter> request_context, | |
| 289 const WebRtcLogUploadDoneData& upload_done_data, | 291 const WebRtcLogUploadDoneData& upload_done_data, |
| 290 scoped_ptr<std::string> post_data) { | 292 scoped_ptr<std::string> post_data) { |
| 291 DCHECK(create_thread_checker_.CalledOnValidThread()); | 293 DCHECK(create_thread_checker_.CalledOnValidThread()); |
| 294 |
| 295 if (shutting_down_) |
| 296 return; |
| 297 |
| 292 std::string content_type = kUploadContentType; | 298 std::string content_type = kUploadContentType; |
| 293 content_type.append("; boundary="); | 299 content_type.append("; boundary="); |
| 294 content_type.append(kMultipartBoundary); | 300 content_type.append(kMultipartBoundary); |
| 295 | 301 |
| 296 net::URLFetcher* url_fetcher = | 302 net::URLFetcher* url_fetcher = |
| 297 net::URLFetcher::Create(GURL(kUploadURL), net::URLFetcher::POST, this); | 303 net::URLFetcher::Create(GURL(kUploadURL), net::URLFetcher::POST, this); |
| 298 url_fetcher->SetRequestContext(request_context); | 304 url_fetcher->SetRequestContext(g_browser_process->system_request_context()); |
| 299 url_fetcher->SetUploadData(content_type, *post_data); | 305 url_fetcher->SetUploadData(content_type, *post_data); |
| 300 url_fetcher->Start(); | 306 url_fetcher->Start(); |
| 301 upload_done_data_[url_fetcher] = upload_done_data; | 307 upload_done_data_[url_fetcher] = upload_done_data; |
| 302 } | 308 } |
| 303 | 309 |
| 304 void WebRtcLogUploader::DecreaseLogCount() { | 310 void WebRtcLogUploader::DecreaseLogCount() { |
| 305 DCHECK(create_thread_checker_.CalledOnValidThread()); | 311 DCHECK(create_thread_checker_.CalledOnValidThread()); |
| 306 --log_count_; | 312 --log_count_; |
| 307 } | 313 } |
| 308 | 314 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 if (!success) { | 417 if (!success) { |
| 412 error_message = "Uploading failed, response code: " + | 418 error_message = "Uploading failed, response code: " + |
| 413 base::IntToString(response_code); | 419 base::IntToString(response_code); |
| 414 } | 420 } |
| 415 content::BrowserThread::PostTask( | 421 content::BrowserThread::PostTask( |
| 416 content::BrowserThread::UI, FROM_HERE, | 422 content::BrowserThread::UI, FROM_HERE, |
| 417 base::Bind(upload_done_data.callback, success, report_id, | 423 base::Bind(upload_done_data.callback, success, report_id, |
| 418 error_message)); | 424 error_message)); |
| 419 } | 425 } |
| 420 } | 426 } |
| OLD | NEW |