| 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 <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <unordered_map> | 10 #include <unordered_map> |
| 11 | 11 |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chrome/browser/permissions/permission_request.h" |
| 13 #include "chrome/browser/permissions/permission_uma_util.h" | 14 #include "chrome/browser/permissions/permission_uma_util.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 class Clock; | 18 class Clock; |
| 18 } // namespace base | 19 } // namespace base |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 class ReportSender; | 22 class ReportSender; |
| 22 class URLRequestContext; | 23 class URLRequestContext; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 43 // Creates a permission reporter that will send permission reports to | 44 // Creates a permission reporter that will send permission reports to |
| 44 // the SafeBrowsing permission action server, using |request_context| as the | 45 // the SafeBrowsing permission action server, using |request_context| as the |
| 45 // context for the reports. | 46 // context for the reports. |
| 46 explicit PermissionReporter(net::URLRequestContext* request_context); | 47 explicit PermissionReporter(net::URLRequestContext* request_context); |
| 47 | 48 |
| 48 ~PermissionReporter(); | 49 ~PermissionReporter(); |
| 49 | 50 |
| 50 // Sends a serialized permission report to the report collection server. | 51 // Sends a serialized permission report to the report collection server. |
| 51 // The permission report includes |origin| as the origin of | 52 // The permission report includes |origin| as the origin of |
| 52 // the site requesting permission, |permission| as the type of permission | 53 // the site requesting permission, |permission| as the type of permission |
| 53 // requested, |action| as the action taken, and |user_gesture| in the list of | 54 // requested, |action| as the action taken, and |gesture_type| as to whether |
| 54 // request triggers if the action occurred after a user gesture. The report | 55 // the action occurred after a user gesture. The report will be serialized |
| 55 // will be serialized using protobuf defined in | 56 // using protobuf defined in |
| 56 // //src/chrome/common/safe_browsing/permission_report.proto | 57 // //src/chrome/common/safe_browsing/permission_report.proto |
| 57 void SendReport(const GURL& origin, | 58 void SendReport(const GURL& origin, |
| 58 content::PermissionType permission, | 59 content::PermissionType permission, |
| 59 PermissionAction action, | 60 PermissionAction action, |
| 60 PermissionSourceUI source_ui, | 61 PermissionSourceUI source_ui, |
| 61 bool user_gesture); | 62 PermissionRequestGestureType gesture_type); |
| 62 | 63 |
| 63 private: | 64 private: |
| 64 friend class PermissionReporterTest; | 65 friend class PermissionReporterTest; |
| 65 | 66 |
| 66 // Used by tests. This constructor allows tests to have access to the | 67 // Used by tests. This constructor allows tests to have access to the |
| 67 // ReportSender and use a test Clock. | 68 // ReportSender and use a test Clock. |
| 68 PermissionReporter(std::unique_ptr<net::ReportSender> report_sender, | 69 PermissionReporter(std::unique_ptr<net::ReportSender> report_sender, |
| 69 std::unique_ptr<base::Clock> clock); | 70 std::unique_ptr<base::Clock> clock); |
| 70 | 71 |
| 71 // Builds and serializes a permission report with |origin| as the origin of | 72 // Builds and serializes a permission report with |origin| as the origin of |
| 72 // the site requesting permission, |permission| as the type of permission | 73 // the site requesting permission, |permission| as the type of permission |
| 73 // requested, and |action| as the action taken. The serialized report is | 74 // requested, and |action| as the action taken. The serialized report is |
| 74 // written into |output|. Returns true if the serialization was successful and | 75 // written into |output|. Returns true if the serialization was successful and |
| 75 // false otherwise. | 76 // false otherwise. |
| 76 static bool BuildReport(const GURL& origin, | 77 static bool BuildReport(const GURL& origin, |
| 77 content::PermissionType permission, | 78 content::PermissionType permission, |
| 78 PermissionAction action, | 79 PermissionAction action, |
| 79 PermissionSourceUI source_ui, | 80 PermissionSourceUI source_ui, |
| 80 bool user_gesture, | 81 PermissionRequestGestureType gesture_type, |
| 81 std::string* output); | 82 std::string* output); |
| 82 | 83 |
| 83 // Returns false if the number of reports sent in the last one minute per | 84 // Returns false if the number of reports sent in the last one minute per |
| 84 // origin per permission is under a threshold, otherwise true. | 85 // origin per permission is under a threshold, otherwise true. |
| 85 bool IsReportThresholdExceeded(content::PermissionType permission, | 86 bool IsReportThresholdExceeded(content::PermissionType permission, |
| 86 const GURL& origin); | 87 const GURL& origin); |
| 87 | 88 |
| 88 std::unique_ptr<net::ReportSender> permission_report_sender_; | 89 std::unique_ptr<net::ReportSender> permission_report_sender_; |
| 89 | 90 |
| 90 // TODO(stefanocs): This might introduce a memory issue since older entries | 91 // TODO(stefanocs): This might introduce a memory issue since older entries |
| 91 // are not removed until a new report with the corresponding key is added. We | 92 // are not removed until a new report with the corresponding key is added. We |
| 92 // should address this issue if that becomes a problem in the future. | 93 // should address this issue if that becomes a problem in the future. |
| 93 std::unordered_map<PermissionAndOrigin, | 94 std::unordered_map<PermissionAndOrigin, |
| 94 std::queue<base::Time>, | 95 std::queue<base::Time>, |
| 95 PermissionAndOriginHash> | 96 PermissionAndOriginHash> |
| 96 report_logs_; | 97 report_logs_; |
| 97 | 98 |
| 98 std::unique_ptr<base::Clock> clock_; | 99 std::unique_ptr<base::Clock> clock_; |
| 99 | 100 |
| 100 DISALLOW_COPY_AND_ASSIGN(PermissionReporter); | 101 DISALLOW_COPY_AND_ASSIGN(PermissionReporter); |
| 101 }; | 102 }; |
| 102 | 103 |
| 103 } // namespace safe_browsing | 104 } // namespace safe_browsing |
| 104 | 105 |
| 105 #endif // CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_ | 106 #endif // CHROME_BROWSER_SAFE_BROWSING_PERMISSION_REPORTER_H_ |
| OLD | NEW |