blob: 1163f6b23f3ebbe3af1a24f4b924a06aa8cedfd2 [file] [log] [blame]
[email protected]42066d52014-06-06 17:51:121// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_
6#define COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_
7
avibc5337b2015-12-25 23:16:338#include <stddef.h>
9#include <stdint.h>
dcheng84c358e2016-04-26 07:05:5310
[email protected]42066d52014-06-06 17:51:1211#include <map>
dcheng84c358e2016-04-26 07:05:5312#include <memory>
[email protected]42066d52014-06-06 17:51:1213#include <string>
dcheng51606352015-12-26 21:16:2314#include <utility>
afakhry79455092016-08-06 00:16:3815#include <vector>
[email protected]42066d52014-06-06 17:51:1216
thestig819adcc82014-09-10 22:24:5317#include "base/files/file_util.h"
[email protected]42066d52014-06-06 17:51:1218#include "base/memory/ref_counted.h"
[email protected]42066d52014-06-06 17:51:1219#include "base/synchronization/lock.h"
20
21namespace userfeedback {
22class ExtensionSubmit;
23}
24
[email protected]42066d52014-06-06 17:51:1225// This is the base class for FeedbackData. It primarily knows about
26// data common to all feedback reports and how to zip things.
27class FeedbackCommon : public base::RefCountedThreadSafe<FeedbackCommon> {
28 public:
afakhry79455092016-08-06 00:16:3829 using SystemLogsMap = std::map<std::string, std::string>;
[email protected]42066d52014-06-06 17:51:1230
31 struct AttachedFile {
Chris Morinc7a03442019-04-03 19:51:1532 explicit AttachedFile(const std::string& filename, std::string data);
[email protected]42066d52014-06-06 17:51:1233 ~AttachedFile();
34
35 std::string name;
Chris Morinc7a03442019-04-03 19:51:1536 std::string data;
[email protected]42066d52014-06-06 17:51:1237 };
38
[email protected]42066d52014-06-06 17:51:1239 FeedbackCommon();
40
Chris Morinc7a03442019-04-03 19:51:1541 void AddFile(const std::string& filename, std::string data);
[email protected]42066d52014-06-06 17:51:1242
Chris Morin48c80c12019-04-12 19:43:4143 void AddLog(std::string name, std::string value);
Chris Morinc7a03442019-04-03 19:51:1544 void AddLogs(SystemLogsMap logs);
[email protected]42066d52014-06-06 17:51:1245
46 // Fill in |feedback_data| with all the data that we have collected.
47 // CompressLogs() must have already been called.
48 void PrepareReport(userfeedback::ExtensionSubmit* feedback_data) const;
49
50 // Getters
51 const std::string& category_tag() const { return category_tag_; }
52 const std::string& page_url() const { return page_url_; }
53 const std::string& description() const { return description_; }
54 const std::string& user_email() const { return user_email_; }
Chris Morinc7a03442019-04-03 19:51:1555 const std::string& image() const { return image_; }
56 const SystemLogsMap* sys_info() const { return &logs_; }
[email protected]42066d52014-06-06 17:51:1257 int32_t product_id() const { return product_id_; }
58 std::string user_agent() const { return user_agent_; }
59 std::string locale() const { return locale_; }
60
Chris Morinc7a03442019-04-03 19:51:1561 const AttachedFile* attachment(size_t i) const { return &attachments_[i]; }
[email protected]42066d52014-06-06 17:51:1262 size_t attachments() const { return attachments_.size(); }
63
64 // Setters
65 void set_category_tag(const std::string& category_tag) {
66 category_tag_ = category_tag;
67 }
68 void set_page_url(const std::string& page_url) { page_url_ = page_url; }
69 void set_description(const std::string& description) {
70 description_ = description;
71 }
72 void set_user_email(const std::string& user_email) {
73 user_email_ = user_email;
74 }
Chris Morinc7a03442019-04-03 19:51:1575 void set_image(std::string image) { image_ = std::move(image); }
[email protected]42066d52014-06-06 17:51:1276 void set_product_id(int32_t product_id) { product_id_ = product_id; }
77 void set_user_agent(const std::string& user_agent) {
78 user_agent_ = user_agent;
79 }
80 void set_locale(const std::string& locale) { locale_ = locale; }
81
82 protected:
[email protected]42066d52014-06-06 17:51:1283 virtual ~FeedbackCommon();
84
afakhry79455092016-08-06 00:16:3885 // Compresses the |data_to_be_compressed| to an attachment file to this
86 // feedback data with name |zipname|. If |zipname| is empty, the |filename|
87 // will be used and appended a ".zip" extension.
88 void CompressFile(const base::FilePath& filename,
89 const std::string& zipname,
Chris Morinc7a03442019-04-03 19:51:1590 std::string data_to_be_compressed);
afakhry79455092016-08-06 00:16:3891
92 void CompressLogs();
93
[email protected]42066d52014-06-06 17:51:1294 private:
afakhry402fadf22016-08-03 22:27:4295 friend class base::RefCountedThreadSafe<FeedbackCommon>;
96 friend class FeedbackCommonTest;
97
afakhry79455092016-08-06 00:16:3898 void AddFilesAndLogsToReport(
99 userfeedback::ExtensionSubmit* feedback_data) const;
100
afakhry402fadf22016-08-03 22:27:42101 // Returns true if a product ID was set in the feedback report.
102 bool HasProductId() const { return product_id_ != -1; }
103
[email protected]42066d52014-06-06 17:51:12104 std::string category_tag_;
105 std::string page_url_;
106 std::string description_;
107 std::string user_email_;
108 int32_t product_id_;
109 std::string user_agent_;
110 std::string locale_;
111
Chris Morinc7a03442019-04-03 19:51:15112 std::string image_;
[email protected]42066d52014-06-06 17:51:12113
114 // It is possible that multiple attachment add calls are running in
115 // parallel, so synchronize access.
116 base::Lock attachments_lock_;
Chris Morinc7a03442019-04-03 19:51:15117 std::vector<AttachedFile> attachments_;
[email protected]42066d52014-06-06 17:51:12118
Chris Morinc7a03442019-04-03 19:51:15119 SystemLogsMap logs_;
[email protected]42066d52014-06-06 17:51:12120};
121
122#endif // COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_