[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 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 | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #ifndef COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_ | ||||
6 | #define COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_ | ||||
7 | |||||
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 8 | #include <stddef.h> |
9 | #include <stdint.h> | ||||
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 10 | |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 11 | #include <map> |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 12 | #include <memory> |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 13 | #include <string> |
dcheng | 5160635 | 2015-12-26 21:16:23 | [diff] [blame] | 14 | #include <utility> |
afakhry | 7945509 | 2016-08-06 00:16:38 | [diff] [blame] | 15 | #include <vector> |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 16 | |
thestig | 819adcc8 | 2014-09-10 22:24:53 | [diff] [blame] | 17 | #include "base/files/file_util.h" |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 18 | #include "base/memory/ref_counted.h" |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
20 | |||||
21 | namespace userfeedback { | ||||
22 | class ExtensionSubmit; | ||||
23 | } | ||||
24 | |||||
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 25 | // This is the base class for FeedbackData. It primarily knows about |
26 | // data common to all feedback reports and how to zip things. | ||||
27 | class FeedbackCommon : public base::RefCountedThreadSafe<FeedbackCommon> { | ||||
28 | public: | ||||
afakhry | 7945509 | 2016-08-06 00:16:38 | [diff] [blame] | 29 | using SystemLogsMap = std::map<std::string, std::string>; |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 30 | |
31 | struct AttachedFile { | ||||
Chris Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 32 | explicit AttachedFile(const std::string& filename, std::string data); |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 33 | ~AttachedFile(); |
34 | |||||
35 | std::string name; | ||||
Chris Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 36 | std::string data; |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 37 | }; |
38 | |||||
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 39 | FeedbackCommon(); |
40 | |||||
Chris Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 41 | void AddFile(const std::string& filename, std::string data); |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 42 | |
Chris Morin | 48c80c1 | 2019-04-12 19:43:41 | [diff] [blame] | 43 | void AddLog(std::string name, std::string value); |
Chris Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 44 | void AddLogs(SystemLogsMap logs); |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 45 | |
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 Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 55 | const std::string& image() const { return image_; } |
56 | const SystemLogsMap* sys_info() const { return &logs_; } | ||||
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 57 | 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 Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 61 | const AttachedFile* attachment(size_t i) const { return &attachments_[i]; } |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 62 | 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 Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 75 | void set_image(std::string image) { image_ = std::move(image); } |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 76 | 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] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 83 | virtual ~FeedbackCommon(); |
84 | |||||
afakhry | 7945509 | 2016-08-06 00:16:38 | [diff] [blame] | 85 | // 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 Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 90 | std::string data_to_be_compressed); |
afakhry | 7945509 | 2016-08-06 00:16:38 | [diff] [blame] | 91 | |
92 | void CompressLogs(); | ||||
93 | |||||
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 94 | private: |
afakhry | 402fadf2 | 2016-08-03 22:27:42 | [diff] [blame] | 95 | friend class base::RefCountedThreadSafe<FeedbackCommon>; |
96 | friend class FeedbackCommonTest; | ||||
97 | |||||
afakhry | 7945509 | 2016-08-06 00:16:38 | [diff] [blame] | 98 | void AddFilesAndLogsToReport( |
99 | userfeedback::ExtensionSubmit* feedback_data) const; | ||||
100 | |||||
afakhry | 402fadf2 | 2016-08-03 22:27:42 | [diff] [blame] | 101 | // Returns true if a product ID was set in the feedback report. |
102 | bool HasProductId() const { return product_id_ != -1; } | ||||
103 | |||||
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 104 | 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 Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 112 | std::string image_; |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 113 | |
114 | // It is possible that multiple attachment add calls are running in | ||||
115 | // parallel, so synchronize access. | ||||
116 | base::Lock attachments_lock_; | ||||
Chris Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 117 | std::vector<AttachedFile> attachments_; |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 118 | |
Chris Morin | c7a0344 | 2019-04-03 19:51:15 | [diff] [blame] | 119 | SystemLogsMap logs_; |
[email protected] | 42066d5 | 2014-06-06 17:51:12 | [diff] [blame] | 120 | }; |
121 | |||||
122 | #endif // COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_ |