OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/media/webrtc_log_uploader.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/shared_memory.h" |
| 9 #include "base/stringprintf.h" |
| 10 #include "chrome/common/chrome_version_info.h" |
| 11 #include "chrome/common/partial_circular_buffer.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "net/base/mime_util.h" |
| 14 #include "net/base/network_delegate.h" |
| 15 #include "net/proxy/proxy_config.h" |
| 16 #include "net/proxy/proxy_config_service.h" |
| 17 #include "net/url_request/url_fetcher.h" |
| 18 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_context_builder.h" |
| 20 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "third_party/zlib/zlib.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 const int kLogCountLimit = 5; |
| 26 const uint32 kIntermediateCompressionBufferBytes = 256 * 1024; // 256 KB |
| 27 |
| 28 const char kUploadURL[] = "https://clients2.google.com/cr/report"; |
| 29 const char kUploadContentType[] = "multipart/form-data"; |
| 30 const char kMultipartBoundary[] = |
| 31 "----**--yradnuoBgoLtrapitluMklaTelgooG--**----"; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 WebRtcLogUploader::WebRtcLogUploader() |
| 36 : log_count_(0) { |
| 37 } |
| 38 |
| 39 WebRtcLogUploader::~WebRtcLogUploader() { |
| 40 } |
| 41 |
| 42 void WebRtcLogUploader::OnURLFetchComplete( |
| 43 const net::URLFetcher* source) { |
| 44 } |
| 45 |
| 46 void WebRtcLogUploader::OnURLFetchUploadProgress( |
| 47 const net::URLFetcher* source, int64 current, int64 total) { |
| 48 } |
| 49 |
| 50 bool WebRtcLogUploader::ApplyForStartLogging() { |
| 51 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 52 if (log_count_ < kLogCountLimit) { |
| 53 ++log_count_; |
| 54 return true; |
| 55 } |
| 56 return false; |
| 57 } |
| 58 |
| 59 void WebRtcLogUploader::UploadLog(net::URLRequestContextGetter* request_context, |
| 60 scoped_ptr<base::SharedMemory> shared_memory, |
| 61 uint32 length, |
| 62 const std::string& app_session_id, |
| 63 const std::string& app_url) { |
| 64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 65 DCHECK(shared_memory); |
| 66 DCHECK(shared_memory->memory()); |
| 67 |
| 68 std::string post_data; |
| 69 SetupMultipart(&post_data, reinterpret_cast<uint8*>(shared_memory->memory()), |
| 70 length, app_session_id, app_url); |
| 71 |
| 72 std::string content_type = kUploadContentType; |
| 73 content_type.append("; boundary="); |
| 74 content_type.append(kMultipartBoundary); |
| 75 |
| 76 net::URLFetcher* url_fetcher = |
| 77 net::URLFetcher::Create(GURL(kUploadURL), net::URLFetcher::POST, this); |
| 78 url_fetcher->SetRequestContext(request_context); |
| 79 url_fetcher->SetUploadData(content_type, post_data); |
| 80 url_fetcher->Start(); |
| 81 |
| 82 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 83 base::Bind(&WebRtcLogUploader::DecreaseLogCount, base::Unretained(this))); |
| 84 } |
| 85 |
| 86 void WebRtcLogUploader::SetupMultipart(std::string* post_data, |
| 87 uint8* log_buffer, |
| 88 uint32 log_buffer_length, |
| 89 const std::string& app_session_id, |
| 90 const std::string& app_url) { |
| 91 #if defined(OS_WIN) |
| 92 const char product[] = "Chrome"; |
| 93 #elif defined(OS_MACOSX) |
| 94 const char product[] = "Chrome_Mac"; |
| 95 #elif defined(OS_LINUX) |
| 96 #if !defined(ADDRESS_SANITIZER) |
| 97 const char product[] = "Chrome_Linux"; |
| 98 #else |
| 99 const char product[] = "Chrome_Linux_ASan"; |
| 100 #endif |
| 101 #elif defined(OS_ANDROID) |
| 102 const char product[] = "Chrome_Android"; |
| 103 #elif defined(OS_CHROMEOS) |
| 104 const char product[] = "Chrome_ChromeOS"; |
| 105 #else |
| 106 // This file should not be compiled for other platforms. |
| 107 COMPILE_ASSERT(false); |
| 108 #endif |
| 109 net::AddMultipartValueForUpload("prod", product, kMultipartBoundary, |
| 110 "", post_data); |
| 111 chrome::VersionInfo version_info; |
| 112 net::AddMultipartValueForUpload("ver", version_info.Version(), |
| 113 kMultipartBoundary, "", post_data); |
| 114 net::AddMultipartValueForUpload("guid", "0", kMultipartBoundary, |
| 115 "", post_data); |
| 116 net::AddMultipartValueForUpload("type", "webrtc_log", kMultipartBoundary, |
| 117 "", post_data); |
| 118 net::AddMultipartValueForUpload("app_session_id", app_session_id, |
| 119 kMultipartBoundary, "", post_data); |
| 120 net::AddMultipartValueForUpload("url", app_url, kMultipartBoundary, |
| 121 "", post_data); |
| 122 AddLogData(post_data, log_buffer, log_buffer_length); |
| 123 net::AddMultipartFinalDelimiterForUpload(kMultipartBoundary, post_data); |
| 124 } |
| 125 |
| 126 void WebRtcLogUploader::AddLogData(std::string* post_data, |
| 127 uint8* log_buffer, |
| 128 uint32 log_buffer_length) { |
| 129 post_data->append("--"); |
| 130 post_data->append(kMultipartBoundary); |
| 131 post_data->append("\r\n"); |
| 132 post_data->append("Content-Disposition: form-data; name=\"log\""); |
| 133 post_data->append("; filename=\"log.gz\"\r\n"); |
| 134 post_data->append("Content-Type: application/gzip\r\n\r\n"); |
| 135 |
| 136 CompressLog(post_data, log_buffer, log_buffer_length); |
| 137 |
| 138 post_data->append("\r\n"); |
| 139 } |
| 140 |
| 141 void WebRtcLogUploader::CompressLog(std::string* post_data, |
| 142 uint8* input, |
| 143 uint32 input_size) { |
| 144 PartialCircularBuffer read_pcb(input, input_size); |
| 145 |
| 146 z_stream stream = {0}; |
| 147 int result = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, |
| 148 // windowBits = 15 is default, 16 is added to |
| 149 // produce a gzip header + trailer. |
| 150 15 + 16, |
| 151 8, // memLevel = 8 is default. |
| 152 Z_DEFAULT_STRATEGY); |
| 153 DCHECK_EQ(Z_OK, result); |
| 154 |
| 155 uint8 intermediate_buffer[kIntermediateCompressionBufferBytes] = {0}; |
| 156 ResizeForNextOutput(post_data, &stream); |
| 157 uint32 read = 0; |
| 158 |
| 159 do { |
| 160 if (stream.avail_in == 0) { |
| 161 read = read_pcb.Read(&intermediate_buffer[0], |
| 162 kIntermediateCompressionBufferBytes); |
| 163 stream.next_in = &intermediate_buffer[0]; |
| 164 stream.avail_in = read; |
| 165 if (read != kIntermediateCompressionBufferBytes) |
| 166 break; |
| 167 } |
| 168 result = deflate(&stream, Z_SYNC_FLUSH); |
| 169 DCHECK_EQ(Z_OK, result); |
| 170 if (stream.avail_out == 0) |
| 171 ResizeForNextOutput(post_data, &stream); |
| 172 } while (true); |
| 173 |
| 174 // Ensure we have enough room in the output buffer. Easier to always just do a |
| 175 // resize than looping around and resize if needed. |
| 176 if (stream.avail_out < kIntermediateCompressionBufferBytes) |
| 177 ResizeForNextOutput(post_data, &stream); |
| 178 |
| 179 result = deflate(&stream, Z_FINISH); |
| 180 DCHECK_EQ(Z_STREAM_END, result); |
| 181 result = deflateEnd(&stream); |
| 182 DCHECK_EQ(Z_OK, result); |
| 183 |
| 184 post_data->resize(post_data->size() - stream.avail_out); |
| 185 } |
| 186 |
| 187 void WebRtcLogUploader::ResizeForNextOutput(std::string* post_data, |
| 188 z_stream* stream) { |
| 189 size_t old_size = post_data->size() - stream->avail_out; |
| 190 post_data->resize(old_size + kIntermediateCompressionBufferBytes); |
| 191 stream->next_out = reinterpret_cast<uint8*>(&(*post_data)[old_size]); |
| 192 stream->avail_out = kIntermediateCompressionBufferBytes; |
| 193 } |
| 194 |
| 195 void WebRtcLogUploader::DecreaseLogCount() { |
| 196 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 197 --log_count_; |
| 198 } |
OLD | NEW |