Chromium Code Reviews| 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_delivery_agent.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/json/json_writer.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/memory/ptr_util.h" | |
| 15 #include "base/time/tick_clock.h" | |
| 16 #include "base/values.h" | |
| 17 #include "net/reporting/reporting_cache.h" | |
| 18 #include "net/reporting/reporting_endpoint_manager.h" | |
| 19 #include "net/reporting/reporting_report.h" | |
| 20 #include "net/reporting/reporting_uploader.h" | |
| 21 #include "url/gurl.h" | |
| 22 #include "url/origin.h" | |
| 23 | |
| 24 namespace net { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 std::string SerializeReports(const std::vector<const ReportingReport*>& reports, | |
| 29 base::TimeTicks now) { | |
| 30 base::ListValue reports_value; | |
| 31 | |
| 32 for (const ReportingReport* report : reports) { | |
| 33 std::unique_ptr<base::DictionaryValue> report_value = | |
| 34 base::MakeUnique<base::DictionaryValue>(); | |
| 35 | |
| 36 report_value->SetInteger("age", (now - report->queued).InMilliseconds()); | |
| 37 report_value->SetString("type", report->type); | |
| 38 report_value->SetString("url", report->url.spec()); | |
| 39 report_value->Set("report", report->body->DeepCopy()); | |
| 40 | |
| 41 reports_value.Append(std::move(report_value)); | |
| 42 } | |
| 43 | |
| 44 std::string reports_json = ""; | |
| 45 bool json_written = base::JSONWriter::Write(reports_value, &reports_json); | |
| 46 DCHECK(json_written); | |
| 47 | |
| 48 return reports_json; | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 ReportingDeliveryAgent::ReportingDeliveryAgent( | |
| 54 base::TickClock* clock, | |
| 55 ReportingCache* cache, | |
| 56 ReportingUploader* uploader, | |
| 57 const BackoffEntry::Policy* endpoint_backoff_policy) | |
| 58 : clock_(clock), | |
| 59 cache_(cache), | |
| 60 uploader_(uploader), | |
| 61 endpoint_manager_(clock, cache, endpoint_backoff_policy), | |
| 62 weak_factory_(this) {} | |
| 63 ReportingDeliveryAgent::~ReportingDeliveryAgent() {} | |
| 64 | |
| 65 class ReportingDeliveryAgent::Delivery { | |
| 66 public: | |
| 67 Delivery(const GURL& endpoint, | |
| 68 const std::vector<const ReportingReport*>& reports) | |
| 69 : endpoint(endpoint), reports(reports) {} | |
| 70 | |
| 71 ~Delivery() {} | |
| 72 | |
| 73 const GURL endpoint; | |
| 74 const std::vector<const ReportingReport*> reports; | |
| 75 }; | |
| 76 | |
| 77 void ReportingDeliveryAgent::SendReports() { | |
| 78 std::vector<const ReportingReport*> reports; | |
| 79 cache_->GetReports(&reports); | |
| 80 | |
| 81 // Sort reports into (origin, group) buckets. | |
|
shivanisha
2017/03/30 20:16:11
Since we want to key the reports with <origin, gro
Julia Tuttle
2017/03/31 14:49:48
Good call -- I wrote this before I added pending_o
| |
| 82 std::map<url::Origin, | |
| 83 std::map<std::string, std::vector<const ReportingReport*>>> | |
| 84 origin_group_reports; | |
| 85 for (const ReportingReport* report : reports) { | |
| 86 url::Origin origin(report->url); | |
| 87 origin_group_reports[origin][report->group].push_back(report); | |
| 88 } | |
| 89 | |
| 90 // Find endpoint for each (origin, group) bucket and sort reports into | |
| 91 // endpoint buckets. Don't allow concurrent uploads to the same (origin, | |
| 92 // group) bucket. | |
| 93 std::map<GURL, std::vector<const ReportingReport*>> endpoint_reports; | |
|
shivanisha
2017/03/30 20:16:11
Since its not allowed for multiple reports for the
Julia Tuttle
2017/03/31 14:49:48
What is disallowed is multiple *uploads*, not mult
| |
| 94 for (auto& it : origin_group_reports) { | |
| 95 for (auto& jt : it.second) { | |
| 96 OriginGroup origin_group(it.first, jt.first); | |
| 97 if (pending_origin_groups_.count(origin_group) > 0u) | |
| 98 continue; | |
| 99 GURL endpoint_url; | |
| 100 if (!endpoint_manager_.FindEndpointForOriginAndGroup(it.first, jt.first, | |
| 101 &endpoint_url)) { | |
| 102 continue; | |
| 103 } | |
| 104 | |
| 105 endpoint_reports[endpoint_url].insert( | |
| 106 endpoint_reports[endpoint_url].end(), jt.second.begin(), | |
| 107 jt.second.end()); | |
| 108 pending_origin_groups_.insert(origin_group); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 // Start an upload to each endpoint. | |
| 113 for (auto& it : endpoint_reports) { | |
| 114 endpoint_manager_.SetEndpointPending(it.first); | |
| 115 cache_->SetReportsPending(it.second); | |
| 116 | |
| 117 uploader_->StartUpload( | |
| 118 it.first, SerializeReports(it.second, clock_->NowTicks()), | |
| 119 base::Bind(&ReportingDeliveryAgent::OnUploadComplete, | |
| 120 weak_factory_.GetWeakPtr(), | |
| 121 base::MakeUnique<Delivery>(it.first, it.second))); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 void ReportingDeliveryAgent::OnUploadComplete( | |
| 126 const std::unique_ptr<Delivery>& delivery, | |
| 127 ReportingUploader::Outcome outcome) { | |
| 128 if (outcome == ReportingUploader::SUCCESS) { | |
| 129 cache_->RemoveReports(delivery->reports); | |
| 130 endpoint_manager_.InformOfEndpointRequest(delivery->endpoint, true); | |
| 131 } else { | |
| 132 cache_->IncrementReportsAttempts(delivery->reports); | |
| 133 endpoint_manager_.InformOfEndpointRequest(delivery->endpoint, false); | |
| 134 } | |
| 135 | |
| 136 if (outcome == ReportingUploader::REMOVE_ENDPOINT) | |
| 137 cache_->RemoveClientsForEndpoint(delivery->endpoint); | |
| 138 | |
| 139 for (const ReportingReport* report : delivery->reports) { | |
| 140 url::Origin origin(report->url); | |
| 141 pending_origin_groups_.erase(std::make_pair(origin, report->group)); | |
| 142 } | |
| 143 | |
| 144 cache_->ClearReportsPending(delivery->reports); | |
| 145 endpoint_manager_.ClearEndpointPending(delivery->endpoint); | |
| 146 } | |
| 147 | |
| 148 } // namespace net | |
| OLD | NEW |