File: permission_reporter.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (94 lines) | stat: -rw-r--r-- 3,308 bytes parent folder | download
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_
#define CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_

#include <queue>
#include <string>
#include <unordered_map>

#include "base/time/time.h"
#include "chrome/browser/permissions/permission_uma_util.h"
#include "url/gurl.h"

namespace base {
class Clock;
}  // namespace base

namespace net {
class ReportSender;
class URLRequestContext;
}  // namespace net

namespace safe_browsing {

struct PermissionAndOrigin {
  bool operator==(const PermissionAndOrigin& other) const;

  content::PermissionType permission;
  GURL origin;
};

struct PermissionAndOriginHash {
  std::size_t operator()(
      const PermissionAndOrigin& permission_and_origin) const;
};

// Provides functionality for building and serializing reports about permissions
// to a report collection server.
class PermissionReporter {
 public:
  // Creates a permission reporter that will send permission reports to
  // the SafeBrowsing permission action server, using |request_context| as the
  // context for the reports.
  explicit PermissionReporter(net::URLRequestContext* request_context);

  ~PermissionReporter();

  // Sends a serialized permission report to the report collection server.
  // The permission report includes the origin of the site requesting the
  // permission and other information about the permission action included in
  // |report_info|. The report will be serialized using protobuf defined in
  // //src/chrome/common/safe_browsing/permission_report.proto
  void SendReport(const PermissionReportInfo& report_info);

 private:
  friend class PermissionReporterBrowserTest;
  friend class PermissionReporterTest;

  // Used by tests. This constructor allows tests to have access to the
  // ReportSender and use a test Clock.
  PermissionReporter(std::unique_ptr<net::ReportSender> report_sender,
                     std::unique_ptr<base::Clock> clock);

  // Builds and serializes a permission report with |report_info| included.
  // The serialized report is written into |output|. Returns true if the
  // serialization was successful and false otherwise.
  static bool BuildReport(const PermissionReportInfo& report_info,
                          std::string* output);

  // Returns false if the number of reports sent in the last one minute per
  // origin per permission is under a threshold, otherwise true.
  bool IsReportThresholdExceeded(content::PermissionType permission,
                                 const GURL& origin);

  std::unique_ptr<net::ReportSender> permission_report_sender_;

  // TODO(stefanocs): This might introduce a memory issue since older entries
  // are not removed until a new report with the corresponding key is added. We
  // should address this issue if that becomes a problem in the future.
  std::unordered_map<PermissionAndOrigin,
                     std::queue<base::Time>,
                     PermissionAndOriginHash>
      report_logs_;

  std::unique_ptr<base::Clock> clock_;

  DISALLOW_COPY_AND_ASSIGN(PermissionReporter);
};

}  // namespace safe_browsing

#endif  // CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_