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

Side by Side Diff: net/reporting/reporting_delivery_agent.cc

Issue 2751103002: Reporting: Wrap existing classes in context. (Closed)
Patch Set: rebase Created 3 years, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/reporting/reporting_delivery_agent.h" 5 #include "net/reporting/reporting_delivery_agent.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 30 matching lines...) Expand all
41 41
42 reports_value.Append(std::move(report_value)); 42 reports_value.Append(std::move(report_value));
43 } 43 }
44 44
45 bool json_written = base::JSONWriter::Write(reports_value, json_out); 45 bool json_written = base::JSONWriter::Write(reports_value, json_out);
46 DCHECK(json_written); 46 DCHECK(json_written);
47 } 47 }
48 48
49 } // namespace 49 } // namespace
50 50
51 ReportingDeliveryAgent::ReportingDeliveryAgent( 51 ReportingDeliveryAgent::ReportingDeliveryAgent(ReportingContext* context)
52 base::TickClock* clock, 52 : context_(context), weak_factory_(this) {}
53 ReportingCache* cache,
54 ReportingUploader* uploader,
55 const BackoffEntry::Policy* endpoint_backoff_policy)
56 : clock_(clock),
57 cache_(cache),
58 uploader_(uploader),
59 endpoint_manager_(clock, cache, endpoint_backoff_policy),
60 weak_factory_(this) {}
61 ReportingDeliveryAgent::~ReportingDeliveryAgent() {} 53 ReportingDeliveryAgent::~ReportingDeliveryAgent() {}
62 54
63 class ReportingDeliveryAgent::Delivery { 55 class ReportingDeliveryAgent::Delivery {
64 public: 56 public:
65 Delivery(const GURL& endpoint, 57 Delivery(const GURL& endpoint,
66 const std::vector<const ReportingReport*>& reports) 58 const std::vector<const ReportingReport*>& reports)
67 : endpoint(endpoint), reports(reports) {} 59 : endpoint(endpoint), reports(reports) {}
68 60
69 ~Delivery() {} 61 ~Delivery() {}
70 62
71 const GURL endpoint; 63 const GURL endpoint;
72 const std::vector<const ReportingReport*> reports; 64 const std::vector<const ReportingReport*> reports;
73 }; 65 };
74 66
75 void ReportingDeliveryAgent::SendReports() { 67 void ReportingDeliveryAgent::SendReports() {
76 std::vector<const ReportingReport*> reports; 68 std::vector<const ReportingReport*> reports;
77 cache_->GetReports(&reports); 69 cache()->GetReports(&reports);
78 70
79 // Sort reports into (origin, group) buckets. 71 // Sort reports into (origin, group) buckets.
80 std::map<OriginGroup, std::vector<const ReportingReport*>> 72 std::map<OriginGroup, std::vector<const ReportingReport*>>
81 origin_group_reports; 73 origin_group_reports;
82 for (const ReportingReport* report : reports) { 74 for (const ReportingReport* report : reports) {
83 OriginGroup origin_group(url::Origin(report->url), report->group); 75 OriginGroup origin_group(url::Origin(report->url), report->group);
84 origin_group_reports[origin_group].push_back(report); 76 origin_group_reports[origin_group].push_back(report);
85 } 77 }
86 78
87 // Find endpoint for each (origin, group) bucket and sort reports into 79 // Find endpoint for each (origin, group) bucket and sort reports into
88 // endpoint buckets. Don't allow concurrent deliveries to the same (origin, 80 // endpoint buckets. Don't allow concurrent deliveries to the same (origin,
89 // group) bucket. 81 // group) bucket.
90 std::map<GURL, std::vector<const ReportingReport*>> endpoint_reports; 82 std::map<GURL, std::vector<const ReportingReport*>> endpoint_reports;
91 for (auto& it : origin_group_reports) { 83 for (auto& it : origin_group_reports) {
92 const OriginGroup& origin_group = it.first; 84 const OriginGroup& origin_group = it.first;
93 85
94 if (base::ContainsKey(pending_origin_groups_, origin_group)) 86 if (base::ContainsKey(pending_origin_groups_, origin_group))
95 continue; 87 continue;
96 88
97 GURL endpoint_url; 89 GURL endpoint_url;
98 if (!endpoint_manager_.FindEndpointForOriginAndGroup( 90 if (!endpoint_manager()->FindEndpointForOriginAndGroup(
99 origin_group.first, origin_group.second, &endpoint_url)) { 91 origin_group.first, origin_group.second, &endpoint_url)) {
100 continue; 92 continue;
101 } 93 }
102 94
103 endpoint_reports[endpoint_url].insert(endpoint_reports[endpoint_url].end(), 95 endpoint_reports[endpoint_url].insert(endpoint_reports[endpoint_url].end(),
104 it.second.begin(), it.second.end()); 96 it.second.begin(), it.second.end());
105 pending_origin_groups_.insert(origin_group); 97 pending_origin_groups_.insert(origin_group);
106 } 98 }
107 99
108 // Start a delivery to each endpoint. 100 // Start a delivery to each endpoint.
109 for (auto& it : endpoint_reports) { 101 for (auto& it : endpoint_reports) {
110 const GURL& endpoint = it.first; 102 const GURL& endpoint = it.first;
111 const std::vector<const ReportingReport*>& reports = it.second; 103 const std::vector<const ReportingReport*>& reports = it.second;
112 104
113 endpoint_manager_.SetEndpointPending(endpoint); 105 endpoint_manager()->SetEndpointPending(endpoint);
114 cache_->SetReportsPending(reports); 106 cache()->SetReportsPending(reports);
115 107
116 std::string json; 108 std::string json;
117 SerializeReports(reports, clock_->NowTicks(), &json); 109 SerializeReports(reports, tick_clock()->NowTicks(), &json);
118 110
119 uploader_->StartUpload( 111 uploader()->StartUpload(
120 endpoint, json, 112 endpoint, json,
121 base::Bind(&ReportingDeliveryAgent::OnUploadComplete, 113 base::Bind(&ReportingDeliveryAgent::OnUploadComplete,
122 weak_factory_.GetWeakPtr(), 114 weak_factory_.GetWeakPtr(),
123 base::MakeUnique<Delivery>(endpoint, reports))); 115 base::MakeUnique<Delivery>(endpoint, reports)));
124 } 116 }
125 } 117 }
126 118
127 void ReportingDeliveryAgent::OnUploadComplete( 119 void ReportingDeliveryAgent::OnUploadComplete(
128 const std::unique_ptr<Delivery>& delivery, 120 const std::unique_ptr<Delivery>& delivery,
129 ReportingUploader::Outcome outcome) { 121 ReportingUploader::Outcome outcome) {
130 if (outcome == ReportingUploader::Outcome::SUCCESS) { 122 if (outcome == ReportingUploader::Outcome::SUCCESS) {
131 cache_->RemoveReports(delivery->reports); 123 cache()->RemoveReports(delivery->reports);
132 endpoint_manager_.InformOfEndpointRequest(delivery->endpoint, true); 124 endpoint_manager()->InformOfEndpointRequest(delivery->endpoint, true);
133 } else { 125 } else {
134 cache_->IncrementReportsAttempts(delivery->reports); 126 cache()->IncrementReportsAttempts(delivery->reports);
135 endpoint_manager_.InformOfEndpointRequest(delivery->endpoint, false); 127 endpoint_manager()->InformOfEndpointRequest(delivery->endpoint, false);
136 } 128 }
137 129
138 if (outcome == ReportingUploader::Outcome::REMOVE_ENDPOINT) 130 if (outcome == ReportingUploader::Outcome::REMOVE_ENDPOINT)
139 cache_->RemoveClientsForEndpoint(delivery->endpoint); 131 cache()->RemoveClientsForEndpoint(delivery->endpoint);
140 132
141 for (const ReportingReport* report : delivery->reports) { 133 for (const ReportingReport* report : delivery->reports) {
142 pending_origin_groups_.erase( 134 pending_origin_groups_.erase(
143 OriginGroup(url::Origin(report->url), report->group)); 135 OriginGroup(url::Origin(report->url), report->group));
144 } 136 }
145 137
146 cache_->ClearReportsPending(delivery->reports); 138 endpoint_manager()->ClearEndpointPending(delivery->endpoint);
147 endpoint_manager_.ClearEndpointPending(delivery->endpoint); 139 cache()->ClearReportsPending(delivery->reports);
148 } 140 }
149 141
150 } // namespace net 142 } // namespace net
OLDNEW
« no previous file with comments | « net/reporting/reporting_delivery_agent.h ('k') | net/reporting/reporting_delivery_agent_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698