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" |
| 8 #include "base/files/file_path.h" |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/path_service.h" |
8 #include "base/shared_memory.h" | 11 #include "base/shared_memory.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" |
9 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/time/time.h" |
| 16 #include "chrome/browser/media/webrtc_log_upload_list.h" |
| 17 #include "chrome/common/chrome_paths.h" |
10 #include "chrome/common/chrome_version_info.h" | 18 #include "chrome/common/chrome_version_info.h" |
11 #include "chrome/common/partial_circular_buffer.h" | 19 #include "chrome/common/partial_circular_buffer.h" |
12 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
13 #include "net/base/mime_util.h" | 21 #include "net/base/mime_util.h" |
14 #include "net/base/network_delegate.h" | 22 #include "net/base/network_delegate.h" |
15 #include "net/proxy/proxy_config.h" | 23 #include "net/proxy/proxy_config.h" |
16 #include "net/proxy/proxy_config_service.h" | 24 #include "net/proxy/proxy_config_service.h" |
17 #include "net/url_request/url_fetcher.h" | 25 #include "net/url_request/url_fetcher.h" |
18 #include "net/url_request/url_request_context.h" | 26 #include "net/url_request/url_request_context.h" |
19 #include "net/url_request/url_request_context_builder.h" | 27 #include "net/url_request/url_request_context_builder.h" |
20 #include "net/url_request/url_request_context_getter.h" | 28 #include "net/url_request/url_request_context_getter.h" |
21 #include "third_party/zlib/zlib.h" | 29 #include "third_party/zlib/zlib.h" |
22 | 30 |
23 namespace { | 31 namespace { |
24 | 32 |
25 const int kLogCountLimit = 5; | 33 const int kLogCountLimit = 5; |
26 const uint32 kIntermediateCompressionBufferBytes = 256 * 1024; // 256 KB | 34 const uint32 kIntermediateCompressionBufferBytes = 256 * 1024; // 256 KB |
| 35 const int kLogListLimitLines = 50; |
27 | 36 |
28 const char kUploadURL[] = "https://clients2.google.com/cr/report"; | 37 const char kUploadURL[] = "https://clients2.google.com/cr/report"; |
29 const char kUploadContentType[] = "multipart/form-data"; | 38 const char kUploadContentType[] = "multipart/form-data"; |
30 const char kMultipartBoundary[] = | 39 const char kMultipartBoundary[] = |
31 "----**--yradnuoBgoLtrapitluMklaTelgooG--**----"; | 40 "----**--yradnuoBgoLtrapitluMklaTelgooG--**----"; |
32 | 41 |
33 } // namespace | 42 } // namespace |
34 | 43 |
35 WebRtcLogUploader::WebRtcLogUploader() | 44 WebRtcLogUploader::WebRtcLogUploader() |
36 : log_count_(0), | 45 : log_count_(0), |
37 post_data_(NULL) { | 46 post_data_(NULL) { |
| 47 base::FilePath log_dir_path; |
| 48 PathService::Get(chrome::DIR_USER_DATA, &log_dir_path); |
| 49 upload_list_path_ = |
| 50 log_dir_path.AppendASCII(WebRtcLogUploadList::kWebRtcLogListFilename); |
38 } | 51 } |
39 | 52 |
40 WebRtcLogUploader::~WebRtcLogUploader() { | 53 WebRtcLogUploader::~WebRtcLogUploader() {} |
41 } | |
42 | 54 |
43 void WebRtcLogUploader::OnURLFetchComplete( | 55 void WebRtcLogUploader::OnURLFetchComplete( |
44 const net::URLFetcher* source) { | 56 const net::URLFetcher* source) { |
| 57 int response_code = source->GetResponseCode(); |
| 58 std::string report_id; |
| 59 if (response_code == 200 && source->GetResponseAsString(&report_id)) |
| 60 AddUploadedLogInfoToUploadListFile(report_id); |
45 } | 61 } |
46 | 62 |
47 void WebRtcLogUploader::OnURLFetchUploadProgress( | 63 void WebRtcLogUploader::OnURLFetchUploadProgress( |
48 const net::URLFetcher* source, int64 current, int64 total) { | 64 const net::URLFetcher* source, int64 current, int64 total) { |
49 } | 65 } |
50 | 66 |
51 bool WebRtcLogUploader::ApplyForStartLogging() { | 67 bool WebRtcLogUploader::ApplyForStartLogging() { |
52 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
53 if (log_count_ < kLogCountLimit) { | 69 if (log_count_ < kLogCountLimit) { |
54 ++log_count_; | 70 ++log_count_; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 size_t old_size = post_data->size() - stream->avail_out; | 214 size_t old_size = post_data->size() - stream->avail_out; |
199 post_data->resize(old_size + kIntermediateCompressionBufferBytes); | 215 post_data->resize(old_size + kIntermediateCompressionBufferBytes); |
200 stream->next_out = reinterpret_cast<uint8*>(&(*post_data)[old_size]); | 216 stream->next_out = reinterpret_cast<uint8*>(&(*post_data)[old_size]); |
201 stream->avail_out = kIntermediateCompressionBufferBytes; | 217 stream->avail_out = kIntermediateCompressionBufferBytes; |
202 } | 218 } |
203 | 219 |
204 void WebRtcLogUploader::DecreaseLogCount() { | 220 void WebRtcLogUploader::DecreaseLogCount() { |
205 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 221 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
206 --log_count_; | 222 --log_count_; |
207 } | 223 } |
| 224 |
| 225 void WebRtcLogUploader::AddUploadedLogInfoToUploadListFile( |
| 226 const std::string& report_id) { |
| 227 std::string contents; |
| 228 bool read_ok = file_util::ReadFileToString(upload_list_path_, &contents); |
| 229 DPCHECK(read_ok); |
| 230 |
| 231 // Limit the number of log entries to |kLogListLimitLines| - 1, to make room |
| 232 // for the new entry. Each line including the last ends with a '\n', so hit |
| 233 // n will be before line n-1 (from the back). |
| 234 int lf_count = 0; |
| 235 int i = contents.size() - 1; |
| 236 for (; i >= 0 && lf_count < kLogListLimitLines; --i) { |
| 237 if (contents[i] == '\n') |
| 238 ++lf_count; |
| 239 } |
| 240 if (lf_count >= kLogListLimitLines) { |
| 241 // + 1 to compensate for the for loop decrease before the conditional |
| 242 // check and + 1 to get the length. |
| 243 contents.erase(0, i + 2); |
| 244 } |
| 245 |
| 246 // Write the Unix time and report ID to the log list file. |
| 247 base::Time time_now = base::Time::Now(); |
| 248 contents += base::DoubleToString(time_now.ToDoubleT()) + |
| 249 "," + report_id + '\n'; |
| 250 |
| 251 int written = file_util::WriteFile(upload_list_path_, &contents[0], |
| 252 contents.size()); |
| 253 DPCHECK(written == static_cast<int>(contents.size())); |
| 254 } |
OLD | NEW |