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

Unified Diff: chrome/browser/safe_browsing/permission_reporter.cc

Issue 2089383005: Add throttling to permission reporter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@add-hooks-to-permission-layer
Patch Set: Rename constant Created 4 years, 6 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 side-by-side diff with in-line comments
Download patch
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..7478e0b7a59fb55fd2585eee1ba41a15b655db9d 100644
--- a/chrome/browser/safe_browsing/permission_reporter.cc
+++ b/chrome/browser/safe_browsing/permission_reporter.cc
@@ -2,8 +2,12 @@
// 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 "chrome/common/safe_browsing/permission_report.pb.h"
#include "content/public/browser/permission_type.h"
#include "net/url_request/report_sender.h"
@@ -18,6 +22,8 @@ const char kPermissionActionReportingUploadUrl[] =
"http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/"
"permission-action";
+const int kMaximumReportsPerOriginPerPermission = 5;
raymes 2016/06/27 06:26:56 ...PerMinute?
stefanocs 2016/06/27 07:34:57 Done.
+
PermissionReport::PermissionType PermissionTypeForReport(
PermissionType permission) {
switch (permission) {
@@ -74,6 +80,19 @@ 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,
@@ -88,6 +107,8 @@ 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 +129,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 = base::Time::Now();
+ // Remove entries that are sent more than one minute ago.
+ while (!history.empty() &&
+ current_time - history.front() > base::TimeDelta::FromMinutes(1))
+ history.pop();
+ if (history.size() < kMaximumReportsPerOriginPerPermission) {
+ history.push(current_time);
+ return true;
+ } else {
+ return false;
+ }
+}
+
} // namespace safe_browsing

Powered by Google App Engine
This is Rietveld 408576698