| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/reporting/reporting_endpoint_manager.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/rand_util.h" |
| 13 #include "base/stl_util.h" |
| 14 #include "base/time/tick_clock.h" |
| 15 #include "net/base/backoff_entry.h" |
| 16 #include "net/reporting/reporting_cache.h" |
| 17 #include "net/reporting/reporting_client.h" |
| 18 #include "url/gurl.h" |
| 19 #include "url/origin.h" |
| 20 |
| 21 namespace net { |
| 22 |
| 23 ReportingEndpointManager::ReportingEndpointManager( |
| 24 base::TickClock* clock, |
| 25 const ReportingCache* cache, |
| 26 const BackoffEntry::Policy* backoff_policy) |
| 27 : clock_(clock), cache_(cache), backoff_policy_(backoff_policy) {} |
| 28 |
| 29 ReportingEndpointManager::~ReportingEndpointManager() {} |
| 30 |
| 31 bool ReportingEndpointManager::FindEndpointForOriginAndGroup( |
| 32 const url::Origin& origin, |
| 33 const std::string& group, |
| 34 GURL* endpoint_url_out) { |
| 35 std::vector<const ReportingClient*> clients; |
| 36 cache_->GetClientsForOriginAndGroup(origin, group, &clients); |
| 37 |
| 38 // Filter out expired, pending, and backed-off endpoints. |
| 39 std::vector<const ReportingClient*> available_clients; |
| 40 base::TimeTicks now = clock_->NowTicks(); |
| 41 for (const ReportingClient* client : clients) { |
| 42 if (client->expires < now) |
| 43 continue; |
| 44 if (base::ContainsKey(pending_endpoints_, client->endpoint)) |
| 45 continue; |
| 46 if (base::ContainsKey(endpoint_backoff_, client->endpoint) && |
| 47 endpoint_backoff_[client->endpoint]->ShouldRejectRequest()) { |
| 48 continue; |
| 49 } |
| 50 available_clients.push_back(client); |
| 51 } |
| 52 |
| 53 if (available_clients.empty()) { |
| 54 *endpoint_url_out = GURL(); |
| 55 return false; |
| 56 } |
| 57 |
| 58 int random_index = base::RandInt(0, available_clients.size() - 1); |
| 59 *endpoint_url_out = available_clients[random_index]->endpoint; |
| 60 return true; |
| 61 } |
| 62 |
| 63 void ReportingEndpointManager::SetEndpointPending(const GURL& endpoint) { |
| 64 DCHECK(!base::ContainsKey(pending_endpoints_, endpoint)); |
| 65 pending_endpoints_.insert(endpoint); |
| 66 } |
| 67 |
| 68 void ReportingEndpointManager::ClearEndpointPending(const GURL& endpoint) { |
| 69 DCHECK(base::ContainsKey(pending_endpoints_, endpoint)); |
| 70 pending_endpoints_.erase(endpoint); |
| 71 } |
| 72 |
| 73 void ReportingEndpointManager::InformOfEndpointRequest(const GURL& endpoint, |
| 74 bool succeeded) { |
| 75 if (!base::ContainsKey(endpoint_backoff_, endpoint)) { |
| 76 endpoint_backoff_[endpoint] = |
| 77 base::MakeUnique<BackoffEntry>(backoff_policy_, clock_); |
| 78 } |
| 79 endpoint_backoff_[endpoint]->InformOfRequest(succeeded); |
| 80 } |
| 81 |
| 82 } // namespace net |
| OLD | NEW |