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

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: Keeping it as a clean-up only CL. 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
« no previous file with comments | « components/feedback/feedback_common.h ('k') | components/feedback/feedback_common_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
29 // The below thresholds were chosen arbitrarily to conveniently show small data
30 // as part of the report itself without having to look into the system_logs.zip
31 // file.
28 const size_t kFeedbackMaxLength = 4 * 1024; 32 const size_t kFeedbackMaxLength = 4 * 1024;
29 const size_t kFeedbackMaxLineCount = 40; 33 const size_t kFeedbackMaxLineCount = 40;
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. This method only converts those logs that we want in
42 // the compressed zip file sent with the report, hence it ignores any logs 57 // the compressed zip file sent with the report, hence it ignores any logs
43 // below the size threshold of what we want compressed. 58 // below the size threshold of what we want compressed.
44 // TODO(dcheng): This should probably just take advantage of string's move 59 // TODO(dcheng): This should probably just take advantage of string's move
45 // constructor. 60 // constructor.
46 std::unique_ptr<std::string> LogsToString( 61 std::unique_ptr<std::string> LogsToString(
47 const FeedbackCommon::SystemLogsMap& sys_info) { 62 const FeedbackCommon::SystemLogsMap& sys_info) {
48 std::unique_ptr<std::string> syslogs_string(new std::string); 63 std::unique_ptr<std::string> syslogs_string(new std::string);
49 for (FeedbackCommon::SystemLogsMap::const_iterator it = sys_info.begin(); 64 for (const auto& iter : sys_info) {
50 it != sys_info.end(); 65 std::string key = iter.first;
51 ++it) { 66 std::string value = iter.second;
52 std::string key = it->first;
53 std::string value = it->second;
54 67
55 if (FeedbackCommon::BelowCompressionThreshold(value)) 68 if (BelowCompressionThreshold(value))
56 continue; 69 continue;
57 70
58 base::TrimString(key, "\n ", &key); 71 base::TrimString(key, "\n ", &key);
59 base::TrimString(value, "\n ", &value); 72 base::TrimString(value, "\n ", &value);
60 73
61 if (value.find("\n") != std::string::npos) { 74 if (value.find("\n") != std::string::npos) {
62 syslogs_string->append(key + "=" + kMultilineIndicatorString + 75 syslogs_string->append(key + "=" + kMultilineIndicatorString +
63 kMultilineStartString + value + "\n" + 76 kMultilineStartString + value + "\n" +
64 kMultilineEndString); 77 kMultilineEndString);
65 } else { 78 } else {
(...skipping 26 matching lines...) Expand all
92 105
93 userfeedback::ProductSpecificBinaryData* attachment = 106 userfeedback::ProductSpecificBinaryData* attachment =
94 feedback_data->add_product_specific_binary_data(); 107 feedback_data->add_product_specific_binary_data();
95 attachment->set_mime_type(kArbitraryMimeType); 108 attachment->set_mime_type(kArbitraryMimeType);
96 attachment->set_name(name); 109 attachment->set_name(name);
97 attachment->set_data(data); 110 attachment->set_data(data);
98 } 111 }
99 112
100 } // namespace 113 } // namespace
101 114
115 ////////////////////////////////////////////////////////////////////////////////
116 // FeedbackCommon::AttachedFile::
117 ////////////////////////////////////////////////////////////////////////////////
118
102 FeedbackCommon::AttachedFile::AttachedFile(const std::string& filename, 119 FeedbackCommon::AttachedFile::AttachedFile(const std::string& filename,
103 std::unique_ptr<std::string> data) 120 std::unique_ptr<std::string> data)
104 : name(filename), data(std::move(data)) {} 121 : name(filename), data(std::move(data)) {}
105 122
106 FeedbackCommon::AttachedFile::~AttachedFile() {} 123 FeedbackCommon::AttachedFile::~AttachedFile() {}
107 124
125 ////////////////////////////////////////////////////////////////////////////////
126 // FeedbackCommon::
127 ////////////////////////////////////////////////////////////////////////////////
128
108 FeedbackCommon::FeedbackCommon() : product_id_(-1) {} 129 FeedbackCommon::FeedbackCommon() : product_id_(-1) {}
109 130
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, 131 void FeedbackCommon::AddFile(const std::string& filename,
140 std::unique_ptr<std::string> data) { 132 std::unique_ptr<std::string> data) {
141 base::AutoLock lock(attachments_lock_); 133 base::AutoLock lock(attachments_lock_);
142 attachments_.push_back(new AttachedFile(filename, std::move(data))); 134 attachments_.emplace_back(new AttachedFile(filename, std::move(data)));
143 } 135 }
144 136
145 void FeedbackCommon::AddLog(const std::string& name, const std::string& value) { 137 void FeedbackCommon::AddLog(const std::string& name, const std::string& value) {
146 if (!logs_.get()) 138 if (!logs_)
147 logs_ = base::WrapUnique(new SystemLogsMap); 139 logs_ = base::WrapUnique(new SystemLogsMap);
148 (*logs_)[name] = value; 140 (*logs_)[name] = value;
149 } 141 }
150 142
151 void FeedbackCommon::AddLogs(std::unique_ptr<SystemLogsMap> logs) { 143 void FeedbackCommon::AddLogs(std::unique_ptr<SystemLogsMap> logs) {
152 if (logs_) { 144 if (logs_)
153 logs_->insert(logs->begin(), logs->end()); 145 logs_->insert(logs->begin(), logs->end());
154 } else { 146 else
155 logs_ = std::move(logs); 147 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 } 148 }
185 149
186 void FeedbackCommon::PrepareReport( 150 void FeedbackCommon::PrepareReport(
187 userfeedback::ExtensionSubmit* feedback_data) const { 151 userfeedback::ExtensionSubmit* feedback_data) const {
188 // Unused field, needs to be 0 though. 152 // Unused field, needs to be 0 though.
189 feedback_data->set_type_id(0); 153 feedback_data->set_type_id(0);
190 154
191 // Set whether we're reporting from ChromeOS or Chrome on another platform. 155 // Set whether we're reporting from ChromeOS or Chrome on another platform.
192 userfeedback::ChromeData chrome_data; 156 userfeedback::ChromeData chrome_data;
193 #if defined(OS_CHROMEOS) 157 #if defined(OS_CHROMEOS)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 202
239 *(screenshot.mutable_dimensions()) = dimensions; 203 *(screenshot.mutable_dimensions()) = dimensions;
240 screenshot.set_binary_content(*image()); 204 screenshot.set_binary_content(*image());
241 205
242 *(feedback_data->mutable_screenshot()) = screenshot; 206 *(feedback_data->mutable_screenshot()) = screenshot;
243 } 207 }
244 208
245 if (category_tag().size()) 209 if (category_tag().size())
246 feedback_data->set_bucket(category_tag()); 210 feedback_data->set_bucket(category_tag());
247 } 211 }
212
213 FeedbackCommon::~FeedbackCommon() {}
214
215 void FeedbackCommon::CompressFile(
216 const base::FilePath& filename,
217 const std::string& zipname,
218 std::unique_ptr<std::string> data_to_be_compressed) {
219 std::unique_ptr<std::string> compressed_data(new std::string());
220 if (feedback_util::ZipString(filename, *data_to_be_compressed,
221 compressed_data.get())) {
222 std::string attachment_file_name = zipname;
223 if (attachment_file_name.empty()) {
224 // We need to use the UTF8Unsafe methods here to accommodate Windows,
225 // which uses wide strings to store file paths.
226 attachment_file_name = filename.BaseName().AsUTF8Unsafe().append(kZipExt);
227 }
228
229 AddFile(attachment_file_name, std::move(compressed_data));
230 }
231 }
232
233 void FeedbackCommon::CompressLogs() {
234 if (!logs_)
235 return;
236 std::unique_ptr<std::string> logs = LogsToString(*logs_);
237 if (!logs->empty()) {
238 CompressFile(base::FilePath(kLogsFilename), kLogsAttachmentName,
239 std::move(logs));
240 }
241 }
242 void FeedbackCommon::AddFilesAndLogsToReport(
243 userfeedback::ExtensionSubmit* feedback_data) const {
244 for (size_t i = 0; i < attachments(); ++i) {
245 const AttachedFile* file = attachment(i);
246 AddAttachment(feedback_data, file->name.c_str(), *file->data);
247 }
248
249 if (!logs_)
250 return;
251
252 for (const auto& iter : *logs_) {
253 if (BelowCompressionThreshold(iter.second)) {
254 // Small enough logs should end up in the report data itself. However,
255 // they're still added as part of the system_logs.zip file.
256 AddFeedbackData(feedback_data, iter.first, iter.second);
257 }
258 }
259 }
OLDNEW
« no previous file with comments | « components/feedback/feedback_common.h ('k') | components/feedback/feedback_common_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698