File: feedback_common.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (177 lines) | stat: -rw-r--r-- 6,056 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_
#define COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_

#include <stddef.h>
#include <stdint.h>

#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "components/feedback/redaction_tool/redaction_tool.h"

namespace base {
class FilePath;
}

namespace userfeedback {
class ExtensionSubmit;
}

// This is the base class for FeedbackData. It primarily knows about
// data common to all feedback reports and how to zip things.
class FeedbackCommon : public base::RefCountedThreadSafe<FeedbackCommon> {
 public:
  using SystemLogsMap = std::map<std::string, std::string>;

  struct AttachedFile {
    explicit AttachedFile(const std::string& filename, std::string data);
    ~AttachedFile();

    std::string name;
    std::string data;
  };

  FeedbackCommon();

  void AddFile(const std::string& filename, std::string data);

  void AddLog(std::string name, std::string value);
  void AddLogs(SystemLogsMap logs);
  // Remove a log with the specified name.
  // Returns true iff there was a log with the specified name.
  bool RemoveLog(std::string name);

  // Fill in |feedback_data| with all the data that we have collected.
  // CompressLogs() must have already been called.
  void PrepareReport(userfeedback::ExtensionSubmit* feedback_data) const;

  void RedactDescription(redaction::RedactionTool& redactor);

  // Return true if we want to include the feedback item with a key of |key| in
  // the feedback report's system logs.
  static bool IncludeInSystemLogs(const std::string& key, bool is_google_email);

  static int GetChromeBrowserProductId();

  // Mahi feature has the dedicated product id.
  static int GetMahiProductId();

#if BUILDFLAG(IS_CHROMEOS)
  static int GetChromeOSProductId();
#endif  // BUILDFLAG(IS_CHROMEOS)

  // Getters
  const std::optional<std::string>& mac_address() const { return mac_address_; }
  const std::string& category_tag() const { return category_tag_; }
  const std::string& page_url() const { return page_url_; }
  const std::string& description() const { return description_; }
  const std::string& user_email() const { return user_email_; }
  const std::string& image() const { return image_; }
  const std::string& image_mime_type() const { return image_mime_type_; }
  const SystemLogsMap* sys_info() const { return &logs_; }
  int32_t product_id() const { return product_id_; }
  std::string user_agent() const { return user_agent_; }
  std::string locale() const { return locale_; }
  std::string& autofill_metadata() { return autofill_metadata_; }
  bool include_chrome_platform() const { return include_chrome_platform_; }
  const std::optional<bool>& is_offensive_or_unsafe() {
    return is_offensive_or_unsafe_;
  }
  std::string& ai_metadata() { return ai_metadata_; }

  const AttachedFile* attachment(size_t i) const { return &attachments_[i]; }
  size_t attachments() const { return attachments_.size(); }

  // Setters
  void set_mac_address(const std::optional<std::string>& mac_address) {
    mac_address_ = mac_address;
  }
  void set_category_tag(const std::string& category_tag) {
    category_tag_ = category_tag;
  }
  void set_page_url(const std::string& page_url) { page_url_ = page_url; }
  void set_description(const std::string& description) {
    description_ = description;
  }
  void set_user_email(const std::string& user_email) {
    user_email_ = user_email;
  }
  void set_image(std::string image) { image_ = std::move(image); }
  void set_image_mime_type(std::string image_mime_type) {
    image_mime_type_ = std::move(image_mime_type);
  }
  void set_product_id(int32_t product_id) { product_id_ = product_id; }
  void set_user_agent(const std::string& user_agent) {
    user_agent_ = user_agent;
  }
  void set_locale(const std::string& locale) { locale_ = locale; }
  void set_autofill_metadata(const std::string& autofill_metadata) {
    autofill_metadata_ = autofill_metadata;
  }
  // If true, includes whether the report is from ChromeOS or Chrome on another
  // platform.
  void set_include_chrome_platform(bool include_chrome_platform) {
    include_chrome_platform_ = include_chrome_platform;
  }
  void set_is_offensive_or_unsafe(const std::optional<bool>& value) {
    is_offensive_or_unsafe_ = value;
  }
  void set_ai_metadata(const std::string& value) { ai_metadata_ = value; }

 protected:
  virtual ~FeedbackCommon();

  // Compresses the |data_to_be_compressed| to an attachment file to this
  // feedback data with name |zipname|. If |zipname| is empty, the |filename|
  // will be used and appended a ".zip" extension.
  void CompressFile(const base::FilePath& filename,
                    const std::string& zipname,
                    std::string data_to_be_compressed);

  void CompressLogs();

 private:
  friend class base::RefCountedThreadSafe<FeedbackCommon>;
  friend class FeedbackCommonTest;

  void AddFilesAndLogsToReport(
      userfeedback::ExtensionSubmit* feedback_data) const;

  // Returns true if a product ID was set in the feedback report.
  bool HasProductId() const { return product_id_ != -1; }

  std::optional<std::string> mac_address_;
  std::string category_tag_;
  std::string page_url_;
  std::string description_;
  std::string user_email_;
  int32_t product_id_;
  std::string user_agent_;
  std::string locale_;
  std::string autofill_metadata_;
  bool include_chrome_platform_ = true;
  std::optional<bool> is_offensive_or_unsafe_;
  std::string ai_metadata_;

  std::string image_;
  // If empty, assumed to be PNG.
  std::string image_mime_type_;

  // It is possible that multiple attachment add calls are running in
  // parallel, so synchronize access.
  base::Lock attachments_lock_;
  std::vector<AttachedFile> attachments_;

  SystemLogsMap logs_;
};

#endif  // COMPONENTS_FEEDBACK_FEEDBACK_COMMON_H_