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