OLD | NEW |
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 #ifndef COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_ | 5 #ifndef COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_ |
6 #define COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_ | 6 #define COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <memory> | 12 #include <memory> |
13 #include <string> | 13 #include <string> |
14 #include <utility> | 14 #include <utility> |
| 15 #include <vector> |
15 | 16 |
16 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
18 #include "base/memory/scoped_vector.h" | |
19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
20 | 20 |
21 namespace userfeedback { | 21 namespace userfeedback { |
22 class ExtensionSubmit; | 22 class ExtensionSubmit; |
23 } | 23 } |
24 | 24 |
25 namespace feedback_util { | |
26 bool ZipString(const base::FilePath& filename, | |
27 const std::string& data, | |
28 std::string* compressed_data); | |
29 } | |
30 | |
31 // This is the base class for FeedbackData. It primarily knows about | 25 // This is the base class for FeedbackData. It primarily knows about |
32 // data common to all feedback reports and how to zip things. | 26 // data common to all feedback reports and how to zip things. |
33 class FeedbackCommon : public base::RefCountedThreadSafe<FeedbackCommon> { | 27 class FeedbackCommon : public base::RefCountedThreadSafe<FeedbackCommon> { |
34 public: | 28 public: |
35 typedef std::map<std::string, std::string> SystemLogsMap; | 29 using SystemLogsMap = std::map<std::string, std::string>; |
36 | 30 |
37 struct AttachedFile { | 31 struct AttachedFile { |
38 explicit AttachedFile(const std::string& filename, | 32 explicit AttachedFile(const std::string& filename, |
39 std::unique_ptr<std::string> data); | 33 std::unique_ptr<std::string> data); |
40 ~AttachedFile(); | 34 ~AttachedFile(); |
41 | 35 |
42 std::string name; | 36 std::string name; |
43 std::unique_ptr<std::string> data; | 37 std::unique_ptr<std::string> data; |
44 }; | 38 }; |
45 | 39 |
46 // Determine if the given feedback value is small enough to not need to | |
47 // be compressed. | |
48 static bool BelowCompressionThreshold(const std::string& content); | |
49 | |
50 FeedbackCommon(); | 40 FeedbackCommon(); |
51 | 41 |
52 void CompressFile(const base::FilePath& filename, | |
53 const std::string& zipname, | |
54 std::unique_ptr<std::string> data); | |
55 void AddFile(const std::string& filename, std::unique_ptr<std::string> data); | 42 void AddFile(const std::string& filename, std::unique_ptr<std::string> data); |
56 | 43 |
57 void AddLog(const std::string& name, const std::string& value); | 44 void AddLog(const std::string& name, const std::string& value); |
58 void AddLogs(std::unique_ptr<SystemLogsMap> logs); | 45 void AddLogs(std::unique_ptr<SystemLogsMap> logs); |
59 void CompressLogs(); | |
60 | |
61 void AddFilesAndLogsToReport( | |
62 userfeedback::ExtensionSubmit* feedback_data) const; | |
63 | 46 |
64 // Fill in |feedback_data| with all the data that we have collected. | 47 // Fill in |feedback_data| with all the data that we have collected. |
65 // CompressLogs() must have already been called. | 48 // CompressLogs() must have already been called. |
66 void PrepareReport(userfeedback::ExtensionSubmit* feedback_data) const; | 49 void PrepareReport(userfeedback::ExtensionSubmit* feedback_data) const; |
67 | 50 |
68 // Getters | 51 // Getters |
69 const std::string& category_tag() const { return category_tag_; } | 52 const std::string& category_tag() const { return category_tag_; } |
70 const std::string& page_url() const { return page_url_; } | 53 const std::string& page_url() const { return page_url_; } |
71 const std::string& description() const { return description_; } | 54 const std::string& description() const { return description_; } |
72 const std::string& user_email() const { return user_email_; } | 55 const std::string& user_email() const { return user_email_; } |
73 const std::string* image() const { return image_.get(); } | 56 const std::string* image() const { return image_.get(); } |
74 const SystemLogsMap* sys_info() const { return logs_.get(); } | 57 const SystemLogsMap* sys_info() const { return logs_.get(); } |
75 int32_t product_id() const { return product_id_; } | 58 int32_t product_id() const { return product_id_; } |
76 std::string user_agent() const { return user_agent_; } | 59 std::string user_agent() const { return user_agent_; } |
77 std::string locale() const { return locale_; } | 60 std::string locale() const { return locale_; } |
78 | 61 |
79 const AttachedFile* attachment(size_t i) const { return attachments_[i]; } | 62 const AttachedFile* attachment(size_t i) const { |
| 63 return attachments_[i].get(); |
| 64 } |
80 size_t attachments() const { return attachments_.size(); } | 65 size_t attachments() const { return attachments_.size(); } |
81 | 66 |
82 // Setters | 67 // Setters |
83 void set_category_tag(const std::string& category_tag) { | 68 void set_category_tag(const std::string& category_tag) { |
84 category_tag_ = category_tag; | 69 category_tag_ = category_tag; |
85 } | 70 } |
86 void set_page_url(const std::string& page_url) { page_url_ = page_url; } | 71 void set_page_url(const std::string& page_url) { page_url_ = page_url; } |
87 void set_description(const std::string& description) { | 72 void set_description(const std::string& description) { |
88 description_ = description; | 73 description_ = description; |
89 } | 74 } |
90 void set_user_email(const std::string& user_email) { | 75 void set_user_email(const std::string& user_email) { |
91 user_email_ = user_email; | 76 user_email_ = user_email; |
92 } | 77 } |
93 void set_image(std::unique_ptr<std::string> image) { | 78 void set_image(std::unique_ptr<std::string> image) { |
94 image_ = std::move(image); | 79 image_ = std::move(image); |
95 } | 80 } |
96 void set_product_id(int32_t product_id) { product_id_ = product_id; } | 81 void set_product_id(int32_t product_id) { product_id_ = product_id; } |
97 void set_user_agent(const std::string& user_agent) { | 82 void set_user_agent(const std::string& user_agent) { |
98 user_agent_ = user_agent; | 83 user_agent_ = user_agent; |
99 } | 84 } |
100 void set_locale(const std::string& locale) { locale_ = locale; } | 85 void set_locale(const std::string& locale) { locale_ = locale; } |
101 | 86 |
102 protected: | 87 protected: |
103 virtual ~FeedbackCommon(); | 88 virtual ~FeedbackCommon(); |
104 | 89 |
| 90 // Compresses the |data_to_be_compressed| to an attachment file to this |
| 91 // feedback data with name |zipname|. If |zipname| is empty, the |filename| |
| 92 // will be used and appended a ".zip" extension. |
| 93 void CompressFile(const base::FilePath& filename, |
| 94 const std::string& zipname, |
| 95 std::unique_ptr<std::string> data_to_be_compressed); |
| 96 |
| 97 void CompressLogs(); |
| 98 |
105 private: | 99 private: |
106 friend class base::RefCountedThreadSafe<FeedbackCommon>; | 100 friend class base::RefCountedThreadSafe<FeedbackCommon>; |
107 friend class FeedbackCommonTest; | 101 friend class FeedbackCommonTest; |
108 | 102 |
| 103 void AddFilesAndLogsToReport( |
| 104 userfeedback::ExtensionSubmit* feedback_data) const; |
| 105 |
109 // Returns true if a product ID was set in the feedback report. | 106 // Returns true if a product ID was set in the feedback report. |
110 bool HasProductId() const { return product_id_ != -1; } | 107 bool HasProductId() const { return product_id_ != -1; } |
111 | 108 |
112 std::string category_tag_; | 109 std::string category_tag_; |
113 std::string page_url_; | 110 std::string page_url_; |
114 std::string description_; | 111 std::string description_; |
115 std::string user_email_; | 112 std::string user_email_; |
116 int32_t product_id_; | 113 int32_t product_id_; |
117 std::string user_agent_; | 114 std::string user_agent_; |
118 std::string locale_; | 115 std::string locale_; |
119 | 116 |
120 std::unique_ptr<std::string> image_; | 117 std::unique_ptr<std::string> image_; |
121 | 118 |
122 // It is possible that multiple attachment add calls are running in | 119 // It is possible that multiple attachment add calls are running in |
123 // parallel, so synchronize access. | 120 // parallel, so synchronize access. |
124 base::Lock attachments_lock_; | 121 base::Lock attachments_lock_; |
125 ScopedVector<AttachedFile> attachments_; | 122 std::vector<std::unique_ptr<AttachedFile>> attachments_; |
126 | 123 |
127 std::unique_ptr<SystemLogsMap> logs_; | 124 std::unique_ptr<SystemLogsMap> logs_; |
128 }; | 125 }; |
129 | 126 |
130 #endif // COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_ | 127 #endif // COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_ |
OLD | NEW |