OLD | NEW |
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 #ifndef NET_REPORTING_REPORTING_DELIVERY_AGENT_H_ | 5 #ifndef NET_REPORTING_REPORTING_DELIVERY_AGENT_H_ |
6 #define NET_REPORTING_REPORTING_DELIVERY_AGENT_H_ | 6 #define NET_REPORTING_REPORTING_DELIVERY_AGENT_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <set> | |
10 #include <string> | |
11 #include <utility> | |
12 | 9 |
13 #include "base/macros.h" | 10 #include "base/macros.h" |
14 #include "base/memory/weak_ptr.h" | |
15 #include "net/base/backoff_entry.h" | |
16 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
17 #include "net/reporting/reporting_context.h" | |
18 #include "net/reporting/reporting_uploader.h" | |
19 #include "url/gurl.h" | |
20 #include "url/origin.h" | |
21 | 12 |
22 namespace base { | 13 namespace base { |
23 class TickClock; | 14 class Timer; |
24 } // namespace base | 15 } // namespace base |
25 | 16 |
26 namespace net { | 17 namespace net { |
27 | 18 |
28 class ReportingCache; | 19 class ReportingContext; |
29 class ReportingEndpointManager; | |
30 | 20 |
31 // Takes reports from the ReportingCache, assembles reports into deliveries to | 21 // Takes reports from the ReportingCache, assembles reports into deliveries to |
32 // endpoints, and sends those deliveries using ReportingUploader. | 22 // endpoints, and sends those deliveries using ReportingUploader. |
33 // | 23 // |
34 // Since the Reporting spec is completely silent on issues of concurrency, the | 24 // Since the Reporting spec is completely silent on issues of concurrency, the |
35 // delivery agent handles it as so: | 25 // delivery agent handles it as so: |
36 // | 26 // |
37 // 1. An individual report can only be included in one delivery at once -- if | 27 // 1. An individual report can only be included in one delivery at once -- if |
38 // SendReports is called again while a report is being delivered, it won't | 28 // SendReports is called again while a report is being delivered, it won't |
39 // be included in another delivery during that call to SendReports. (This is, | 29 // be included in another delivery during that call to SendReports. (This is, |
(...skipping 12 matching lines...) Expand all Loading... |
52 // | 42 // |
53 // 4. Reports for the same origin *can* be included in multiple parallel | 43 // 4. Reports for the same origin *can* be included in multiple parallel |
54 // deliveries if they are in different groups within that origin. | 44 // deliveries if they are in different groups within that origin. |
55 // | 45 // |
56 // (Note that a single delivery can contain an infinite number of reports.) | 46 // (Note that a single delivery can contain an infinite number of reports.) |
57 // | 47 // |
58 // TODO(juliatuttle): Consider capping the maximum number of reports per | 48 // TODO(juliatuttle): Consider capping the maximum number of reports per |
59 // delivery attempt. | 49 // delivery attempt. |
60 class NET_EXPORT ReportingDeliveryAgent { | 50 class NET_EXPORT ReportingDeliveryAgent { |
61 public: | 51 public: |
62 // |context| must outlive the ReportingDeliveryAgent. | 52 // Creates a ReportingDeliveryAgent. |context| must outlive the agent. |
63 ReportingDeliveryAgent(ReportingContext* context); | 53 static std::unique_ptr<ReportingDeliveryAgent> Create( |
64 ~ReportingDeliveryAgent(); | 54 ReportingContext* context); |
65 | 55 |
66 // Tries to deliver all of the reports in the cache. Reports that are already | 56 virtual ~ReportingDeliveryAgent(); |
67 // being delivered will not be attempted a second time, and reports that do | |
68 // not have a viable endpoint will be neither attempted nor removed. | |
69 void SendReports(); | |
70 | 57 |
71 private: | 58 // Initializes the DeliveryAgent, which schedules delivery (after the Policy's |
72 class Delivery; | 59 // delivery_interval) for any previously-persisted reports that can still be |
| 60 // delivered. |
| 61 virtual void Initialize() = 0; |
73 | 62 |
74 using OriginGroup = std::pair<url::Origin, std::string>; | 63 // Replaces the internal Timer used for scheduling report delivery attempts |
75 | 64 // with a caller-specified one so that unittests can provide a MockTimer. |
76 void OnUploadComplete(const std::unique_ptr<Delivery>& delivery, | 65 virtual void SetTimerForTesting(std::unique_ptr<base::Timer> timer) = 0; |
77 ReportingUploader::Outcome outcome); | |
78 | |
79 base::TickClock* tick_clock() { return context_->tick_clock(); } | |
80 ReportingCache* cache() { return context_->cache(); } | |
81 ReportingUploader* uploader() { return context_->uploader(); } | |
82 ReportingEndpointManager* endpoint_manager() { | |
83 return context_->endpoint_manager(); | |
84 } | |
85 | |
86 ReportingContext* context_; | |
87 | |
88 // Tracks OriginGroup tuples for which there is a pending delivery running. | |
89 // (Would be an unordered_set, but there's no hash on pair.) | |
90 std::set<OriginGroup> pending_origin_groups_; | |
91 | |
92 base::WeakPtrFactory<ReportingDeliveryAgent> weak_factory_; | |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(ReportingDeliveryAgent); | |
95 }; | 66 }; |
96 | 67 |
97 } // namespace net | 68 } // namespace net |
98 | 69 |
99 #endif // NET_REPORTING_REPORTING_DELIVERY_AGENT_H_ | 70 #endif // NET_REPORTING_REPORTING_DELIVERY_AGENT_H_ |
OLD | NEW |