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

Side by Side Diff: components/feedback/feedback_common.cc

Issue 2217163003: Clean up and modernize the feedback code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Actual Fix Created 4 years, 4 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/feedback/feedback_common.h" 5 #include "components/feedback/feedback_common.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "components/feedback/feedback_util.h"
11 #include "components/feedback/proto/common.pb.h" 12 #include "components/feedback/proto/common.pb.h"
12 #include "components/feedback/proto/dom.pb.h" 13 #include "components/feedback/proto/dom.pb.h"
13 #include "components/feedback/proto/extension.pb.h" 14 #include "components/feedback/proto/extension.pb.h"
14 #include "components/feedback/proto/math.pb.h" 15 #include "components/feedback/proto/math.pb.h"
15 16
16 namespace { 17 namespace {
17 18
18 #if defined(OS_CHROMEOS) 19 #if defined(OS_CHROMEOS)
19 constexpr int kChromeOSProductId = 208; 20 constexpr int kChromeOSProductId = 208;
20 #else 21 #else
21 constexpr int kChromeBrowserProductId = 237; 22 constexpr int kChromeBrowserProductId = 237;
22 #endif 23 #endif
23 24
24 const char kMultilineIndicatorString[] = "<multiline>\n"; 25 const char kMultilineIndicatorString[] = "<multiline>\n";
25 const char kMultilineStartString[] = "---------- START ----------\n"; 26 const char kMultilineStartString[] = "---------- START ----------\n";
26 const char kMultilineEndString[] = "---------- END ----------\n\n"; 27 const char kMultilineEndString[] = "---------- END ----------\n\n";
27 28
28 const size_t kFeedbackMaxLength = 4 * 1024; 29 // The below thresholds were chosen arbitrarily to conveniently show small data
29 const size_t kFeedbackMaxLineCount = 40; 30 // as part of the report itself without having to look into the system_logs.zip
31 // file.
32 const size_t kFeedbackMaxLength = 1024;
33 const size_t kFeedbackMaxLineCount = 10;
30 34
31 const base::FilePath::CharType kLogsFilename[] = 35 const base::FilePath::CharType kLogsFilename[] =
32 FILE_PATH_LITERAL("system_logs.txt"); 36 FILE_PATH_LITERAL("system_logs.txt");
33 const char kLogsAttachmentName[] = "system_logs.zip"; 37 const char kLogsAttachmentName[] = "system_logs.zip";
34 38
35 const char kZipExt[] = ".zip"; 39 const char kZipExt[] = ".zip";
36 40
37 const char kPngMimeType[] = "image/png"; 41 const char kPngMimeType[] = "image/png";
38 const char kArbitraryMimeType[] = "application/octet-stream"; 42 const char kArbitraryMimeType[] = "application/octet-stream";
39 43
44 // Determine if the given feedback value is small enough to not need to
45 // be compressed.
46 bool BelowCompressionThreshold(const std::string& content) {
47 if (content.length() > kFeedbackMaxLength)
48 return false;
49 const size_t line_count = std::count(content.begin(), content.end(), '\n');
50 if (line_count > kFeedbackMaxLineCount)
51 return false;
52 return true;
53 }
54
40 // Converts the system logs into a string that we can compress and send 55 // Converts the system logs into a string that we can compress and send
41 // with the report. This method only converts those logs that we want in 56 // with the report.
42 // the compressed zip file sent with the report, hence it ignores any logs
43 // below the size threshold of what we want compressed.
44 // TODO(dcheng): This should probably just take advantage of string's move 57 // TODO(dcheng): This should probably just take advantage of string's move
45 // constructor. 58 // constructor.
46 std::unique_ptr<std::string> LogsToString( 59 std::unique_ptr<std::string> LogsToString(
47 const FeedbackCommon::SystemLogsMap& sys_info) { 60 const FeedbackCommon::SystemLogsMap& sys_info) {
48 std::unique_ptr<std::string> syslogs_string(new std::string); 61 std::unique_ptr<std::string> syslogs_string(new std::string);
49 for (FeedbackCommon::SystemLogsMap::const_iterator it = sys_info.begin(); 62 for (const auto& iter : sys_info) {
50 it != sys_info.end(); 63 std::string key = iter.first;
51 ++it) { 64 std::string value = iter.second;
52 std::string key = it->first;
53 std::string value = it->second;
54
55 if (FeedbackCommon::BelowCompressionThreshold(value))
56 continue;
57 65
58 base::TrimString(key, "\n ", &key); 66 base::TrimString(key, "\n ", &key);
59 base::TrimString(value, "\n ", &value); 67 base::TrimString(value, "\n ", &value);
60 68
61 if (value.find("\n") != std::string::npos) { 69 if (value.find("\n") != std::string::npos) {
62 syslogs_string->append(key + "=" + kMultilineIndicatorString + 70 syslogs_string->append(key + "=" + kMultilineIndicatorString +
63 kMultilineStartString + value + "\n" + 71 kMultilineStartString + value + "\n" +
64 kMultilineEndString); 72 kMultilineEndString);
65 } else { 73 } else {
66 syslogs_string->append(key + "=" + value + "\n"); 74 syslogs_string->append(key + "=" + value + "\n");
(...skipping 25 matching lines...) Expand all
92 100
93 userfeedback::ProductSpecificBinaryData* attachment = 101 userfeedback::ProductSpecificBinaryData* attachment =
94 feedback_data->add_product_specific_binary_data(); 102 feedback_data->add_product_specific_binary_data();
95 attachment->set_mime_type(kArbitraryMimeType); 103 attachment->set_mime_type(kArbitraryMimeType);
96 attachment->set_name(name); 104 attachment->set_name(name);
97 attachment->set_data(data); 105 attachment->set_data(data);
98 } 106 }
99 107
100 } // namespace 108 } // namespace
101 109
110 ////////////////////////////////////////////////////////////////////////////////
111 // FeedbackCommon::AttachedFile::
112 ////////////////////////////////////////////////////////////////////////////////
113
102 FeedbackCommon::AttachedFile::AttachedFile(const std::string& filename, 114 FeedbackCommon::AttachedFile::AttachedFile(const std::string& filename,
103 std::unique_ptr<std::string> data) 115 std::unique_ptr<std::string> data)
104 : name(filename), data(std::move(data)) {} 116 : name(filename), data(std::move(data)) {}
105 117
106 FeedbackCommon::AttachedFile::~AttachedFile() {} 118 FeedbackCommon::AttachedFile::~AttachedFile() {}
107 119
120 ////////////////////////////////////////////////////////////////////////////////
121 // FeedbackCommon::
122 ////////////////////////////////////////////////////////////////////////////////
123
108 FeedbackCommon::FeedbackCommon() : product_id_(-1) {} 124 FeedbackCommon::FeedbackCommon() : product_id_(-1) {}
109 125
110 FeedbackCommon::~FeedbackCommon() {}
111
112 // static
113 bool FeedbackCommon::BelowCompressionThreshold(const std::string& content) {
114 if (content.length() > kFeedbackMaxLength)
115 return false;
116 const size_t line_count = std::count(content.begin(), content.end(), '\n');
117 if (line_count > kFeedbackMaxLineCount)
118 return false;
119 return true;
120 }
121
122 void FeedbackCommon::CompressFile(const base::FilePath& filename,
123 const std::string& zipname,
124 std::unique_ptr<std::string> data) {
125 std::unique_ptr<AttachedFile> file(
126 new AttachedFile(zipname, base::WrapUnique(new std::string())));
127 if (file->name.empty()) {
128 // We need to use the UTF8Unsafe methods here to accomodate Windows, which
129 // uses wide strings to store filepaths.
130 file->name = filename.BaseName().AsUTF8Unsafe();
131 file->name.append(kZipExt);
132 }
133 if (feedback_util::ZipString(filename, *data, file->data.get())) {
134 base::AutoLock lock(attachments_lock_);
135 attachments_.push_back(file.release());
136 }
137 }
138
139 void FeedbackCommon::AddFile(const std::string& filename, 126 void FeedbackCommon::AddFile(const std::string& filename,
140 std::unique_ptr<std::string> data) { 127 std::unique_ptr<std::string> data) {
141 base::AutoLock lock(attachments_lock_); 128 base::AutoLock lock(attachments_lock_);
142 attachments_.push_back(new AttachedFile(filename, std::move(data))); 129 attachments_.emplace_back(new AttachedFile(filename, std::move(data)));
143 } 130 }
144 131
145 void FeedbackCommon::AddLog(const std::string& name, const std::string& value) { 132 void FeedbackCommon::AddLog(const std::string& name, const std::string& value) {
146 if (!logs_.get()) 133 if (!logs_)
147 logs_ = base::WrapUnique(new SystemLogsMap); 134 logs_ = base::WrapUnique(new SystemLogsMap);
148 (*logs_)[name] = value; 135 (*logs_)[name] = value;
149 } 136 }
150 137
151 void FeedbackCommon::AddLogs(std::unique_ptr<SystemLogsMap> logs) { 138 void FeedbackCommon::AddLogs(std::unique_ptr<SystemLogsMap> logs) {
152 if (logs_) { 139 if (logs_)
153 logs_->insert(logs->begin(), logs->end()); 140 logs_->insert(logs->begin(), logs->end());
154 } else { 141 else
155 logs_ = std::move(logs); 142 logs_ = std::move(logs);
156 }
157 }
158
159 void FeedbackCommon::CompressLogs() {
160 if (!logs_)
161 return;
162 std::unique_ptr<std::string> logs = LogsToString(*logs_);
163 if (!logs->empty()) {
164 CompressFile(base::FilePath(kLogsFilename), kLogsAttachmentName,
165 std::move(logs));
166 }
167 }
168
169 void FeedbackCommon::AddFilesAndLogsToReport(
170 userfeedback::ExtensionSubmit* feedback_data) const {
171 if (sys_info()) {
172 for (FeedbackCommon::SystemLogsMap::const_iterator i = sys_info()->begin();
173 i != sys_info()->end();
174 ++i) {
175 if (BelowCompressionThreshold(i->second))
176 AddFeedbackData(feedback_data, i->first, i->second);
177 }
178 }
179
180 for (size_t i = 0; i < attachments(); i++) {
181 const AttachedFile* file = attachment(i);
182 AddAttachment(feedback_data, file->name.c_str(), *file->data.get());
183 }
184 } 143 }
185 144
186 void FeedbackCommon::PrepareReport( 145 void FeedbackCommon::PrepareReport(
187 userfeedback::ExtensionSubmit* feedback_data) const { 146 userfeedback::ExtensionSubmit* feedback_data) const {
188 // Unused field, needs to be 0 though. 147 // Unused field, needs to be 0 though.
189 feedback_data->set_type_id(0); 148 feedback_data->set_type_id(0);
190 149
191 // Set whether we're reporting from ChromeOS or Chrome on another platform. 150 // Set whether we're reporting from ChromeOS or Chrome on another platform.
192 userfeedback::ChromeData chrome_data; 151 userfeedback::ChromeData chrome_data;
193 #if defined(OS_CHROMEOS) 152 #if defined(OS_CHROMEOS)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 197
239 *(screenshot.mutable_dimensions()) = dimensions; 198 *(screenshot.mutable_dimensions()) = dimensions;
240 screenshot.set_binary_content(*image()); 199 screenshot.set_binary_content(*image());
241 200
242 *(feedback_data->mutable_screenshot()) = screenshot; 201 *(feedback_data->mutable_screenshot()) = screenshot;
243 } 202 }
244 203
245 if (category_tag().size()) 204 if (category_tag().size())
246 feedback_data->set_bucket(category_tag()); 205 feedback_data->set_bucket(category_tag());
247 } 206 }
207
208 FeedbackCommon::~FeedbackCommon() {}
209
210 void FeedbackCommon::CompressFile(
211 const base::FilePath& filename,
212 const std::string& zipname,
213 std::unique_ptr<std::string> data_to_be_compressed) {
214 std::unique_ptr<std::string> compressed_data(new std::string());
215 if (feedback_util::ZipString(filename, *data_to_be_compressed,
216 compressed_data.get())) {
217 std::string attachment_file_name = zipname;
218 if (attachment_file_name.empty()) {
219 // We need to use the UTF8Unsafe methods here to accommodate Windows,
220 // which uses wide strings to store file paths.
221 attachment_file_name = filename.BaseName().AsUTF8Unsafe().append(kZipExt);
222 }
223
224 AddFile(attachment_file_name, std::move(compressed_data));
225 }
226 }
227
228 void FeedbackCommon::CompressLogs() {
229 if (!logs_)
230 return;
231 std::unique_ptr<std::string> logs = LogsToString(*logs_);
232 if (!logs->empty()) {
233 CompressFile(base::FilePath(kLogsFilename), kLogsAttachmentName,
234 std::move(logs));
235 }
236 }
237 void FeedbackCommon::AddFilesAndLogsToReport(
238 userfeedback::ExtensionSubmit* feedback_data) const {
239 for (size_t i = 0; i < attachments(); ++i) {
240 const AttachedFile* file = attachment(i);
241 AddAttachment(feedback_data, file->name.c_str(), *file->data);
242 }
243
244 if (!logs_)
245 return;
246
247 for (const auto& iter : *logs_) {
248 if (BelowCompressionThreshold(iter.second)) {
249 // Small enough logs should end up in the report data itself. However,
250 // they're still added as part of the system_logs.zip file.
251 AddFeedbackData(feedback_data, iter.first, iter.second);
252 }
253 }
254 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698