Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: chrome/browser/safe_browsing/permission_reporter.h

Issue 2089383005: Add throttling to permission reporter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@add-hooks-to-permission-layer
Patch Set: Resolve comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_ 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_ 6 #define CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_
7 7
8 #include <queue>
8 #include <string> 9 #include <string>
10 #include <unordered_map>
9 11
12 #include "base/time/time.h"
10 #include "chrome/browser/permissions/permission_uma_util.h" 13 #include "chrome/browser/permissions/permission_uma_util.h"
11 #include "url/gurl.h" 14 #include "url/gurl.h"
12 15
16 namespace base {
17 class Clock;
18 } // namespace base
19
13 namespace net { 20 namespace net {
14 class ReportSender; 21 class ReportSender;
15 class URLRequestContext; 22 class URLRequestContext;
16 } // namespace net 23 } // namespace net
17 24
18 namespace safe_browsing { 25 namespace safe_browsing {
19 26
27 struct PermissionAndOrigin {
28 bool operator==(const PermissionAndOrigin& other) const;
29
30 content::PermissionType permission;
31 GURL origin;
32 };
33
34 struct PermissionAndOriginHash {
35 std::size_t operator()(
36 const PermissionAndOrigin& permission_and_origin) const;
Nathan Parker 2016/07/14 22:00:24 Rather than defining this in the .h file, can you
stefanocs 2016/07/15 02:04:29 This doesn't seem to work. I added this: namespa
Nathan Parker 2016/07/15 23:39:40 I was thinking std::size_t std::hash<safe_browsin
stefanocs 2016/07/18 06:06:17 Hmmm, that doesn't seem to work. It complains abou
37 };
38
20 // Provides functionality for building and serializing reports about permissions 39 // Provides functionality for building and serializing reports about permissions
21 // to a report collection server. 40 // to a report collection server.
22 class PermissionReporter { 41 class PermissionReporter {
23 public: 42 public:
24 // Creates a permission reporter that will send permission reports to 43 // Creates a permission reporter that will send permission reports to
25 // the SafeBrowsing permission action server, using |request_context| as the 44 // the SafeBrowsing permission action server, using |request_context| as the
26 // context for the reports. 45 // context for the reports.
27 explicit PermissionReporter(net::URLRequestContext* request_context); 46 explicit PermissionReporter(net::URLRequestContext* request_context);
28 47
29 ~PermissionReporter(); 48 ~PermissionReporter();
30 49
31 // Sends a serialized permission report to the report collection server. 50 // Sends a serialized permission report to the report collection server.
32 // The permission report includes |origin| as the origin of 51 // The permission report includes |origin| as the origin of
33 // the site requesting permission, |permission| as the type of permission 52 // the site requesting permission, |permission| as the type of permission
34 // requested, and |action| as the action taken. The report will be serialized 53 // requested, and |action| as the action taken. The report will be serialized
35 // using protobuf defined in 54 // using protobuf defined in
36 // //src/chrome/common/safe_browsing/permission_report.proto 55 // //src/chrome/common/safe_browsing/permission_report.proto
37 void SendReport(const GURL& origin, 56 void SendReport(const GURL& origin,
38 content::PermissionType permission, 57 content::PermissionType permission,
39 PermissionAction action); 58 PermissionAction action);
40 59
41 private: 60 private:
42 friend class PermissionReporterTest; 61 friend class PermissionReporterTest;
43 62
44 // Used by tests. This constructor allows tests to have access to the 63 // Used by tests. This constructor allows tests to have access to the
45 // ReportSender. 64 // ReportSender and use a test Clock.
46 explicit PermissionReporter(std::unique_ptr<net::ReportSender> report_sender); 65 PermissionReporter(std::unique_ptr<net::ReportSender> report_sender,
66 std::unique_ptr<base::Clock> clock);
47 67
48 // Builds and serializes a permission report with |origin| as the origin of 68 // Builds and serializes a permission report with |origin| as the origin of
49 // the site requesting permission, |permission| as the type of permission 69 // the site requesting permission, |permission| as the type of permission
50 // requested, and |action| as the action taken. The serialized report is 70 // requested, and |action| as the action taken. The serialized report is
51 // written into |output|. Returns true if the serialization was successful and 71 // written into |output|. Returns true if the serialization was successful and
52 // false otherwise. 72 // false otherwise.
53 static bool BuildReport(const GURL& origin, 73 static bool BuildReport(const GURL& origin,
54 content::PermissionType permission, 74 content::PermissionType permission,
55 PermissionAction action, 75 PermissionAction action,
56 std::string* output); 76 std::string* output);
57 77
78 // Returns false if the number of reports sent in the last one minute per
79 // origin per permission is under a threshold, otherwise true.
80 bool IsReportThresholdExceeded(content::PermissionType permission,
81 const GURL& origin);
82
58 std::unique_ptr<net::ReportSender> permission_report_sender_; 83 std::unique_ptr<net::ReportSender> permission_report_sender_;
59 84
85 std::unordered_map<PermissionAndOrigin,
86 std::queue<base::Time>,
87 PermissionAndOriginHash>
88 sent_histories;
Nathan Parker 2016/07/14 22:00:24 Should end in underscore. How about report_log_?
stefanocs 2016/07/15 02:04:29 Done.
89
90 std::unique_ptr<base::Clock> clock_;
91
60 DISALLOW_COPY_AND_ASSIGN(PermissionReporter); 92 DISALLOW_COPY_AND_ASSIGN(PermissionReporter);
61 }; 93 };
62 94
63 } // namespace safe_browsing 95 } // namespace safe_browsing
64 96
65 #endif // CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_ 97 #endif // CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698