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.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() |
36 : log_count_(0) { | 44 : log_count_(0) { |
37 } | 45 } |
38 | 46 |
39 WebRtcLogUploader::~WebRtcLogUploader() { | 47 WebRtcLogUploader::~WebRtcLogUploader() { |
40 } | 48 } |
41 | 49 |
42 void WebRtcLogUploader::OnURLFetchComplete( | 50 void WebRtcLogUploader::OnURLFetchComplete( |
43 const net::URLFetcher* source) { | 51 const net::URLFetcher* source) { |
52 // Append this uploaded log to a log list file, limited to | |
53 // |kLogListLimitLines| entries. This list is used for viewing the uploaded | |
54 // logs under chrome://webrtc-logs, see WebRtcLogUploadList. | |
55 | |
56 int response_code = source->GetResponseCode(); | |
57 std::string response_string; | |
58 if (response_code != 200 || | |
59 !source->GetResponseAsString(&response_string)) { | |
60 return; | |
61 } | |
62 | |
63 base::FilePath log_dir_path; | |
64 PathService::Get(chrome::DIR_USER_DATA, &log_dir_path); | |
65 base::FilePath upload_log_path = | |
66 log_dir_path.AppendASCII(WebRtcLogUploadList::kWebRtcLogListFilename); | |
67 | |
68 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | | |
69 base::PLATFORM_FILE_READ | | |
70 base::PLATFORM_FILE_WRITE; | |
71 base::PlatformFileError error = base::PLATFORM_FILE_OK; | |
72 base::PlatformFile logs_uploaded_file = | |
73 base::CreatePlatformFile(upload_log_path, flags, NULL, &error); | |
74 if (error != base::PLATFORM_FILE_OK) { | |
75 LOG(WARNING) << "Could not open WebRTC log list file."; | |
76 return; | |
77 } | |
78 | |
79 // Read the log list file. Each line is ~35 chars (bytes). Use a buffer size | |
80 // of 3 times the estimated max size to have a margin. | |
81 std::string contents(3 * kLogListLimitLines * 35, '\0'); | |
82 size_t read = base::ReadPlatformFileAtCurrentPos(logs_uploaded_file, | |
83 &contents[0], | |
84 contents.size()); | |
85 DCHECK_LT(read, contents.size()); | |
86 contents.resize(read); | |
87 | |
88 // Limit the number of log entries to |kLogListLimitLines|. | |
89 std::vector<std::string> log_entries; | |
90 base::SplitStringAlongWhitespace(contents, &log_entries); | |
91 if (log_entries.size() >= kLogListLimitLines) { | |
92 std::string& new_first_entry = | |
93 log_entries[log_entries.size() - kLogListLimitLines + 1]; | |
94 size_t new_first_entry_pos = contents.find(new_first_entry); | |
tommi (sloooow) - chröme
2013/06/27 08:45:40
Is this guaranteed to be correct?
What if kLogLis
Henrik Grunell
2013/06/27 11:56:22
The lines will not contain other whitespaces than
| |
95 contents.erase(0, new_first_entry_pos); | |
96 } | |
97 | |
98 // Write the Unix time and report ID to the log list file. The response in | |
99 // |response_string| contains the report ID. | |
100 base::Time time_now = base::Time::Now(); | |
101 contents += base::DoubleToString(time_now.ToDoubleT()) + | |
102 "," + response_string + '\n'; | |
103 | |
104 base::SeekPlatformFile(logs_uploaded_file, base::PLATFORM_FILE_FROM_BEGIN, 0); | |
105 size_t written = base::WritePlatformFileAtCurrentPos(logs_uploaded_file, | |
106 contents.c_str(), | |
107 contents.size()); | |
108 DCHECK_EQ(written, contents.size()); | |
109 bool ret = base::TruncatePlatformFile(logs_uploaded_file, written); | |
110 DCHECK(ret); | |
tommi (sloooow) - chröme
2013/06/27 08:45:40
FYI - there is also DPCHECK which will print out a
Henrik Grunell
2013/06/27 11:56:22
OK, good to know. Changed.
| |
111 | |
112 ret = base::ClosePlatformFile(logs_uploaded_file); | |
113 DCHECK(ret); | |
tommi (sloooow) - chröme
2013/06/27 08:45:40
you could use that here too
Henrik Grunell
2013/06/27 11:56:22
Done.
| |
44 } | 114 } |
45 | 115 |
46 void WebRtcLogUploader::OnURLFetchUploadProgress( | 116 void WebRtcLogUploader::OnURLFetchUploadProgress( |
47 const net::URLFetcher* source, int64 current, int64 total) { | 117 const net::URLFetcher* source, int64 current, int64 total) { |
48 } | 118 } |
49 | 119 |
50 bool WebRtcLogUploader::ApplyForStartLogging() { | 120 bool WebRtcLogUploader::ApplyForStartLogging() { |
51 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
52 if (log_count_ < kLogCountLimit) { | 122 if (log_count_ < kLogCountLimit) { |
53 ++log_count_; | 123 ++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; | 259 size_t old_size = post_data->size() - stream->avail_out; |
190 post_data->resize(old_size + kIntermediateCompressionBufferBytes); | 260 post_data->resize(old_size + kIntermediateCompressionBufferBytes); |
191 stream->next_out = reinterpret_cast<uint8*>(&(*post_data)[old_size]); | 261 stream->next_out = reinterpret_cast<uint8*>(&(*post_data)[old_size]); |
192 stream->avail_out = kIntermediateCompressionBufferBytes; | 262 stream->avail_out = kIntermediateCompressionBufferBytes; |
193 } | 263 } |
194 | 264 |
195 void WebRtcLogUploader::DecreaseLogCount() { | 265 void WebRtcLogUploader::DecreaseLogCount() { |
196 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 266 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
197 --log_count_; | 267 --log_count_; |
198 } | 268 } |
OLD | NEW |