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/files/file_path.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/path_service.h" | |
8 #include "base/shared_memory.h" | 10 #include "base/shared_memory.h" |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_split.h" | |
9 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/time/time.h" | |
15 #include "chrome/browser/media/webrtc_log_upload_list.h" | |
16 #include "chrome/common/chrome_paths.h" | |
10 #include "chrome/common/chrome_version_info.h" | 17 #include "chrome/common/chrome_version_info.h" |
11 #include "chrome/common/partial_circular_buffer.h" | 18 #include "chrome/common/partial_circular_buffer.h" |
12 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
13 #include "net/base/mime_util.h" | 20 #include "net/base/mime_util.h" |
14 #include "net/base/network_delegate.h" | 21 #include "net/base/network_delegate.h" |
15 #include "net/proxy/proxy_config.h" | 22 #include "net/proxy/proxy_config.h" |
16 #include "net/proxy/proxy_config_service.h" | 23 #include "net/proxy/proxy_config_service.h" |
17 #include "net/url_request/url_fetcher.h" | 24 #include "net/url_request/url_fetcher.h" |
18 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
19 #include "net/url_request/url_request_context_builder.h" | 26 #include "net/url_request/url_request_context_builder.h" |
20 #include "net/url_request/url_request_context_getter.h" | 27 #include "net/url_request/url_request_context_getter.h" |
21 #include "third_party/zlib/zlib.h" | 28 #include "third_party/zlib/zlib.h" |
22 | 29 |
23 namespace { | 30 namespace { |
24 | 31 |
25 const int kLogCountLimit = 5; | 32 const int kLogCountLimit = 5; |
26 const uint32 kIntermediateCompressionBufferBytes = 256 * 1024; // 256 KB | 33 const uint32 kIntermediateCompressionBufferBytes = 256 * 1024; // 256 KB |
34 const int kLogListLimitLines = 50; | |
27 | 35 |
28 const char kUploadURL[] = "https://clients2.google.com/cr/report"; | 36 const char kUploadURL[] = "https://clients2.google.com/cr/report"; |
29 const char kUploadContentType[] = "multipart/form-data"; | 37 const char kUploadContentType[] = "multipart/form-data"; |
30 const char kMultipartBoundary[] = | 38 const char kMultipartBoundary[] = |
31 "----**--yradnuoBgoLtrapitluMklaTelgooG--**----"; | 39 "----**--yradnuoBgoLtrapitluMklaTelgooG--**----"; |
32 | 40 |
33 } // namespace | 41 } // namespace |
34 | 42 |
35 WebRtcLogUploader::WebRtcLogUploader() | 43 WebRtcLogUploader::WebRtcLogUploader(base::FilePath& upload_list_path) |
36 : log_count_(0) { | 44 : log_count_(0), |
45 upload_list_path_(upload_list_path) { | |
37 } | 46 } |
38 | 47 |
39 WebRtcLogUploader::~WebRtcLogUploader() { | 48 WebRtcLogUploader::~WebRtcLogUploader() { |
40 } | 49 } |
41 | 50 |
42 void WebRtcLogUploader::OnURLFetchComplete( | 51 void WebRtcLogUploader::OnURLFetchComplete( |
43 const net::URLFetcher* source) { | 52 const net::URLFetcher* source) { |
53 int response_code = source->GetResponseCode(); | |
54 std::string report_id; | |
55 if (response_code == 200 && source->GetResponseAsString(&report_id)) | |
56 AddUploadedLogInfoToUploadListFile(report_id); | |
44 } | 57 } |
45 | 58 |
46 void WebRtcLogUploader::OnURLFetchUploadProgress( | 59 void WebRtcLogUploader::OnURLFetchUploadProgress( |
47 const net::URLFetcher* source, int64 current, int64 total) { | 60 const net::URLFetcher* source, int64 current, int64 total) { |
48 } | 61 } |
49 | 62 |
50 bool WebRtcLogUploader::ApplyForStartLogging() { | 63 bool WebRtcLogUploader::ApplyForStartLogging() { |
51 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
52 if (log_count_ < kLogCountLimit) { | 65 if (log_count_ < kLogCountLimit) { |
53 ++log_count_; | 66 ++log_count_; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 size_t old_size = post_data->size() - stream->avail_out; | 202 size_t old_size = post_data->size() - stream->avail_out; |
190 post_data->resize(old_size + kIntermediateCompressionBufferBytes); | 203 post_data->resize(old_size + kIntermediateCompressionBufferBytes); |
191 stream->next_out = reinterpret_cast<uint8*>(&(*post_data)[old_size]); | 204 stream->next_out = reinterpret_cast<uint8*>(&(*post_data)[old_size]); |
192 stream->avail_out = kIntermediateCompressionBufferBytes; | 205 stream->avail_out = kIntermediateCompressionBufferBytes; |
193 } | 206 } |
194 | 207 |
195 void WebRtcLogUploader::DecreaseLogCount() { | 208 void WebRtcLogUploader::DecreaseLogCount() { |
196 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 209 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
197 --log_count_; | 210 --log_count_; |
198 } | 211 } |
212 | |
213 void WebRtcLogUploader::AddUploadedLogInfoToUploadListFile( | |
tommi (sloooow) - chröme
2013/07/01 12:01:01
Since there's no locking in this class can we add
Henrik Grunell
2013/07/01 13:26:22
As discussed offline, not done. Problems for test.
| |
214 const std::string& report_id) { | |
215 if (upload_list_path_.empty()) { | |
216 base::FilePath log_dir_path; | |
217 PathService::Get(chrome::DIR_USER_DATA, &log_dir_path); | |
218 upload_list_path_ = | |
219 log_dir_path.AppendASCII(WebRtcLogUploadList::kWebRtcLogListFilename); | |
220 } | |
221 | |
222 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | | |
223 base::PLATFORM_FILE_READ | | |
224 base::PLATFORM_FILE_WRITE; | |
225 base::PlatformFileError error = base::PLATFORM_FILE_OK; | |
226 base::PlatformFile logs_uploaded_file = | |
227 base::CreatePlatformFile(upload_list_path_, flags, NULL, &error); | |
228 if (error != base::PLATFORM_FILE_OK) { | |
229 LOG(WARNING) << "Could not open WebRTC log list file."; | |
tommi (sloooow) - chröme
2013/07/01 12:01:01
nit: also include the error value
Henrik Grunell
2013/07/01 13:26:22
Done.
| |
230 return; | |
231 } | |
232 | |
233 // Read the log list file. Each line is ~35 chars (bytes). Use a buffer size | |
234 // of 3 times the estimated max size to have a margin. | |
235 std::string contents(3 * kLogListLimitLines * 35, '\0'); | |
236 size_t read = base::ReadPlatformFileAtCurrentPos(logs_uploaded_file, | |
237 &contents[0], | |
238 contents.size()); | |
239 DCHECK_LT(read, contents.size()); | |
tommi (sloooow) - chröme
2013/07/01 12:01:01
isn't it possible that read can be equal to conten
Henrik Grunell
2013/07/01 13:26:22
Is should be ~ a factor 3 smaller, but I can check
| |
240 contents.resize(read); | |
241 | |
242 // Limit the number of log entries to |kLogListLimitLines| - 1, to make room | |
243 // for the new entry. Each line including the last ends with a '\n', so hit | |
244 // n will be before line n-1 (from the back). | |
245 if (!contents.empty()) { | |
tommi (sloooow) - chröme
2013/07/01 12:01:01
nit: this condition isn't really necessary
Henrik Grunell
2013/07/01 13:26:22
That's right. Removed.
| |
246 int lf_count = 0; | |
247 int i = contents.size() - 1; | |
248 for (; i >= 0 && lf_count < kLogListLimitLines; --i) { | |
249 if (contents[i] == '\n') | |
250 ++lf_count; | |
251 } | |
252 if (lf_count <= kLogListLimitLines) { | |
tommi (sloooow) - chröme
2013/07/01 12:01:01
shouldn't this be >= ? Right now it looks like yo
Henrik Grunell
2013/07/01 13:26:22
Yes it should, thanks.
| |
253 // + 1 to compensate for the for loop decrease before the conditional | |
254 // check and + 1 to get the length. | |
255 contents.erase(0, i + 2); | |
256 } | |
257 } | |
258 | |
259 // Write the Unix time and report ID to the log list file. | |
260 base::Time time_now = base::Time::Now(); | |
261 contents += base::DoubleToString(time_now.ToDoubleT()) + | |
262 "," + report_id + '\n'; | |
263 | |
264 base::SeekPlatformFile(logs_uploaded_file, base::PLATFORM_FILE_FROM_BEGIN, 0); | |
265 size_t written = base::WritePlatformFileAtCurrentPos(logs_uploaded_file, | |
266 contents.c_str(), | |
267 contents.size()); | |
268 DCHECK_EQ(written, contents.size()); | |
269 bool ret = base::TruncatePlatformFile(logs_uploaded_file, written); | |
270 DPCHECK(ret); | |
271 | |
272 ret = base::ClosePlatformFile(logs_uploaded_file); | |
273 DPCHECK(ret); | |
274 } | |
OLD | NEW |