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