OLD | NEW |
---|---|
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; | |
37 }; | |
raymes
2016/06/29 07:09:17
nit: If we define these as nested classes within P
stefanocs
2016/06/30 00:24:51
Do you mean these structs should be forward declar
| |
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 explicit 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 true if the number of reports sent in the last one minute per | |
79 // origin per permission is under a threshold, otherwise false. | |
80 bool IsAllowedToSend(content::PermissionType permission, const GURL& origin); | |
81 | |
58 std::unique_ptr<net::ReportSender> permission_report_sender_; | 82 std::unique_ptr<net::ReportSender> permission_report_sender_; |
59 | 83 |
84 std::unordered_map<PermissionAndOrigin, | |
85 std::queue<base::Time>, | |
86 PermissionAndOriginHash> | |
87 sent_histories; | |
raymes
2016/06/29 07:09:17
It seems like we might need this complexity then u
stefanocs
2016/06/30 00:24:51
Acknowledged.
| |
88 | |
89 std::unique_ptr<base::Clock> clock_; | |
90 | |
60 DISALLOW_COPY_AND_ASSIGN(PermissionReporter); | 91 DISALLOW_COPY_AND_ASSIGN(PermissionReporter); |
61 }; | 92 }; |
62 | 93 |
63 } // namespace safe_browsing | 94 } // namespace safe_browsing |
64 | 95 |
65 #endif // CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_ | 96 #endif // CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_ |
OLD | NEW |