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

Unified Diff: net/reporting/reporting_endpoint_manager.cc

Issue 2737483004: Reporting: Implement endpoint manager. (Closed)
Patch Set: rebase Created 3 years, 9 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
« no previous file with comments | « net/reporting/reporting_endpoint_manager.h ('k') | net/reporting/reporting_endpoint_manager_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/reporting/reporting_endpoint_manager.cc
diff --git a/net/reporting/reporting_endpoint_manager.cc b/net/reporting/reporting_endpoint_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6333a9afb7de3d50a9864e0f6e290f80c69e58a9
--- /dev/null
+++ b/net/reporting/reporting_endpoint_manager.cc
@@ -0,0 +1,82 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/reporting/reporting_endpoint_manager.h"
+
+#include <string>
+#include <vector>
+
+#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#include "base/rand_util.h"
+#include "base/stl_util.h"
+#include "base/time/tick_clock.h"
+#include "net/base/backoff_entry.h"
+#include "net/reporting/reporting_cache.h"
+#include "net/reporting/reporting_client.h"
+#include "url/gurl.h"
+#include "url/origin.h"
+
+namespace net {
+
+ReportingEndpointManager::ReportingEndpointManager(
+ base::TickClock* clock,
+ const ReportingCache* cache,
+ const BackoffEntry::Policy* backoff_policy)
+ : clock_(clock), cache_(cache), backoff_policy_(backoff_policy) {}
+
+ReportingEndpointManager::~ReportingEndpointManager() {}
+
+bool ReportingEndpointManager::FindEndpointForOriginAndGroup(
+ const url::Origin& origin,
+ const std::string& group,
+ GURL* endpoint_url_out) {
+ std::vector<const ReportingClient*> clients;
+ cache_->GetClientsForOriginAndGroup(origin, group, &clients);
+
+ // Filter out expired, pending, and backed-off endpoints.
+ std::vector<const ReportingClient*> available_clients;
+ base::TimeTicks now = clock_->NowTicks();
+ for (const ReportingClient* client : clients) {
+ if (client->expires < now)
+ continue;
+ if (base::ContainsKey(pending_endpoints_, client->endpoint))
+ continue;
+ if (base::ContainsKey(endpoint_backoff_, client->endpoint) &&
+ endpoint_backoff_[client->endpoint]->ShouldRejectRequest()) {
+ continue;
+ }
+ available_clients.push_back(client);
+ }
+
+ if (available_clients.empty()) {
+ *endpoint_url_out = GURL();
+ return false;
+ }
+
+ int random_index = base::RandInt(0, available_clients.size() - 1);
+ *endpoint_url_out = available_clients[random_index]->endpoint;
+ return true;
+}
+
+void ReportingEndpointManager::SetEndpointPending(const GURL& endpoint) {
+ DCHECK(!base::ContainsKey(pending_endpoints_, endpoint));
+ pending_endpoints_.insert(endpoint);
+}
+
+void ReportingEndpointManager::ClearEndpointPending(const GURL& endpoint) {
+ DCHECK(base::ContainsKey(pending_endpoints_, endpoint));
+ pending_endpoints_.erase(endpoint);
+}
+
+void ReportingEndpointManager::InformOfEndpointRequest(const GURL& endpoint,
+ bool succeeded) {
+ if (!base::ContainsKey(endpoint_backoff_, endpoint)) {
+ endpoint_backoff_[endpoint] =
+ base::MakeUnique<BackoffEntry>(backoff_policy_, clock_);
+ }
+ endpoint_backoff_[endpoint]->InformOfRequest(succeeded);
+}
+
+} // namespace net
« no previous file with comments | « net/reporting/reporting_endpoint_manager.h ('k') | net/reporting/reporting_endpoint_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698