Chromium Code Reviews| Index: chrome/browser/safe_browsing/permission_reporter.cc |
| diff --git a/chrome/browser/safe_browsing/permission_reporter.cc b/chrome/browser/safe_browsing/permission_reporter.cc |
| index b64f60ceff6ea32b265873919d5740b8253b4819..9618fd95ecdaf008ebedf77198bd1e295e6eda21 100644 |
| --- a/chrome/browser/safe_browsing/permission_reporter.cc |
| +++ b/chrome/browser/safe_browsing/permission_reporter.cc |
| @@ -2,8 +2,13 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "base/memory/ptr_util.h" |
| #include "chrome/browser/safe_browsing/permission_reporter.h" |
| + |
| +#include <functional> |
| + |
| +#include "base/hash.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/time/default_clock.h" |
| #include "chrome/common/safe_browsing/permission_report.pb.h" |
| #include "content/public/browser/permission_type.h" |
| #include "net/url_request/report_sender.h" |
| @@ -18,6 +23,8 @@ const char kPermissionActionReportingUploadUrl[] = |
| "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" |
| "permission-action"; |
| +const int kMaximumReportsPerOriginPerPermissionPerMinute = 5; |
| + |
| PermissionReport::PermissionType PermissionTypeForReport( |
| PermissionType permission) { |
| switch (permission) { |
| @@ -74,20 +81,39 @@ PermissionReport::Action PermissionActionForReport(PermissionAction action) { |
| } // namespace |
| +bool PermissionAndOrigin::operator==(const PermissionAndOrigin& other) const { |
| + return (permission == other.permission && origin == other.origin); |
| +} |
| + |
| +std::size_t PermissionAndOriginHash::operator()( |
| + const PermissionAndOrigin& permission_and_origin) const { |
| + std::size_t permission_hash = |
| + static_cast<std::size_t>(permission_and_origin.permission); |
| + std::size_t origin_hash = |
| + std::hash<std::string>()(permission_and_origin.origin.spec()); |
| + return base::HashInts(permission_hash, origin_hash); |
| +} |
| + |
| PermissionReporter::PermissionReporter(net::URLRequestContext* request_context) |
| - : PermissionReporter(base::WrapUnique(new net::ReportSender( |
| - request_context, |
| - net::ReportSender::CookiesPreference::DO_NOT_SEND_COOKIES))) {} |
| + : PermissionReporter( |
| + base::WrapUnique(new net::ReportSender( |
| + request_context, |
| + net::ReportSender::CookiesPreference::DO_NOT_SEND_COOKIES)), |
| + base::WrapUnique(new base::DefaultClock)) {} |
| PermissionReporter::PermissionReporter( |
| - std::unique_ptr<net::ReportSender> report_sender) |
| - : permission_report_sender_(std::move(report_sender)) {} |
| + std::unique_ptr<net::ReportSender> report_sender, |
| + std::unique_ptr<base::Clock> clock) |
| + : permission_report_sender_(std::move(report_sender)), |
| + clock_(std::move(clock)) {} |
| PermissionReporter::~PermissionReporter() {} |
| void PermissionReporter::SendReport(const GURL& origin, |
| content::PermissionType permission, |
| PermissionAction action) { |
| + if (!IsAllowedToSend(permission, origin)) |
| + return; |
| std::string serialized_report; |
| BuildReport(origin, permission, action, &serialized_report); |
| permission_report_sender_->Send(GURL(kPermissionActionReportingUploadUrl), |
| @@ -108,4 +134,20 @@ bool PermissionReporter::BuildReport(const GURL& origin, |
| return report.SerializeToString(output); |
| } |
| +bool PermissionReporter::IsAllowedToSend(content::PermissionType permission, |
| + const GURL& origin) { |
| + std::queue<base::Time>& history = sent_histories[{permission, origin}]; |
| + base::Time current_time = clock_->Now(); |
| + // Remove entries that are sent more than one minute ago. |
| + while (!history.empty() && |
| + current_time - history.front() > base::TimeDelta::FromMinutes(1)) |
|
raymes
2016/06/29 07:09:17
nit: add {}
stefanocs
2016/06/30 00:24:51
Done.
|
| + history.pop(); |
| + if (history.size() < kMaximumReportsPerOriginPerPermissionPerMinute) { |
| + history.push(current_time); |
| + return true; |
| + } else { |
| + return false; |
| + } |
| +} |
| + |
| } // namespace safe_browsing |