Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: chrome/browser/media/webrtc_log_uploader.cc

Issue 17589014: Write a log list for uploaded WebRTC logs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Nicer line limitation code. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 information (time and report ID) about this uploaded log to a log
53 // list file, limited to |kLogListLimitLines| entries. This list is used for
54 // viewing the uploaded logs under chrome://webrtc-logs, see
55 // WebRtcLogUploadList. The list has the format
56 // time,id
57 // time,id
58 // etc.
59 // where each line represents an uploaded log and "time" is Unix time.
60
61 int response_code = source->GetResponseCode();
62 std::string report_id;
63 if (response_code != 200 ||
64 !source->GetResponseAsString(&report_id)) {
65 return;
66 }
67
68 base::FilePath log_dir_path;
69 PathService::Get(chrome::DIR_USER_DATA, &log_dir_path);
70 base::FilePath upload_log_path =
71 log_dir_path.AppendASCII(WebRtcLogUploadList::kWebRtcLogListFilename);
72
73 int flags = base::PLATFORM_FILE_OPEN_ALWAYS |
74 base::PLATFORM_FILE_READ |
75 base::PLATFORM_FILE_WRITE;
76 base::PlatformFileError error = base::PLATFORM_FILE_OK;
77 base::PlatformFile logs_uploaded_file =
78 base::CreatePlatformFile(upload_log_path, flags, NULL, &error);
79 if (error != base::PLATFORM_FILE_OK) {
80 LOG(WARNING) << "Could not open WebRTC log list file.";
81 return;
82 }
83
84 // Read the log list file. Each line is ~35 chars (bytes). Use a buffer size
85 // of 3 times the estimated max size to have a margin.
86 std::string contents(3 * kLogListLimitLines * 35, '\0');
87 size_t read = base::ReadPlatformFileAtCurrentPos(logs_uploaded_file,
88 &contents[0],
89 contents.size());
90 DCHECK_LT(read, contents.size());
91 contents.resize(read);
92
93 // Limit the number of log entries to |kLogListLimitLines| - 1, to make room
94 // for the new entry. Each line including the last ends with a '\n', so hit
95 // n will be before line n-1 (from the back).
96 if (!contents.empty()) {
97 int lf_count = 0;
98 int i = contents.size() - 1;
99 for (; i >= 0 && lf_count < kLogListLimitLines; --i) {
100 if (contents[i] == '\n')
101 ++lf_count;
102 }
103 // + 1 to compensate for the for loop decrease before the conditional check
104 // and + 1 to get the length.
105 contents.erase(0, i + 2);
tommi (sloooow) - chröme 2013/06/28 11:00:05 I think we'll then also need to add a check if i >
Henrik Grunell 2013/06/28 14:12:23 Good point. Added check.
106 }
107
108 // Write the Unix time and report ID to the log list file.
109 base::Time time_now = base::Time::Now();
110 contents += base::DoubleToString(time_now.ToDoubleT()) +
111 "," + report_id + '\n';
112
113 base::SeekPlatformFile(logs_uploaded_file, base::PLATFORM_FILE_FROM_BEGIN, 0);
114 size_t written = base::WritePlatformFileAtCurrentPos(logs_uploaded_file,
115 contents.c_str(),
116 contents.size());
117 DCHECK_EQ(written, contents.size());
118 bool ret = base::TruncatePlatformFile(logs_uploaded_file, written);
119 DPCHECK(ret);
120
121 ret = base::ClosePlatformFile(logs_uploaded_file);
122 DPCHECK(ret);
44 } 123 }
45 124
46 void WebRtcLogUploader::OnURLFetchUploadProgress( 125 void WebRtcLogUploader::OnURLFetchUploadProgress(
47 const net::URLFetcher* source, int64 current, int64 total) { 126 const net::URLFetcher* source, int64 current, int64 total) {
48 } 127 }
49 128
50 bool WebRtcLogUploader::ApplyForStartLogging() { 129 bool WebRtcLogUploader::ApplyForStartLogging() {
51 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 130 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
52 if (log_count_ < kLogCountLimit) { 131 if (log_count_ < kLogCountLimit) {
53 ++log_count_; 132 ++log_count_;
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 size_t old_size = post_data->size() - stream->avail_out; 268 size_t old_size = post_data->size() - stream->avail_out;
190 post_data->resize(old_size + kIntermediateCompressionBufferBytes); 269 post_data->resize(old_size + kIntermediateCompressionBufferBytes);
191 stream->next_out = reinterpret_cast<uint8*>(&(*post_data)[old_size]); 270 stream->next_out = reinterpret_cast<uint8*>(&(*post_data)[old_size]);
192 stream->avail_out = kIntermediateCompressionBufferBytes; 271 stream->avail_out = kIntermediateCompressionBufferBytes;
193 } 272 }
194 273
195 void WebRtcLogUploader::DecreaseLogCount() { 274 void WebRtcLogUploader::DecreaseLogCount() {
196 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 275 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
197 --log_count_; 276 --log_count_;
198 } 277 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698