• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_URL_REQUEST_REPORT_SENDER_H_
6 #define NET_URL_REQUEST_REPORT_SENDER_H_
7 
8 #include <map>
9 #include <memory>
10 
11 #include "base/functional/callback.h"
12 #include "base/memory/raw_ptr.h"
13 #include "net/base/net_export.h"
14 #include "net/http/transport_security_state.h"
15 #include "net/traffic_annotation/network_traffic_annotation.h"
16 #include "net/url_request/url_request.h"
17 
18 class GURL;
19 
20 namespace net {
21 
22 class NetworkAnonymizationKey;
23 class URLRequestContext;
24 
25 // ReportSender asynchronously sends serialized reports to a URI.
26 // It takes serialized reports as a sequence of bytes so as to be agnostic to
27 // the format of the report being sent (JSON, protobuf, etc.) and the particular
28 // data that it contains. Multiple reports can be in-flight at once. This class
29 // owns inflight requests and cleans them up when necessary.
30 //
31 // Despite this class's name, it has nothing to do with the Reporting API,
32 // which is implemented in net/reporting.
33 class NET_EXPORT ReportSender
34     : public URLRequest::Delegate,
35       public TransportSecurityState::ReportSenderInterface {
36  public:
37   static const int kLoadFlags;
38 
39   using SuccessCallback = base::OnceCallback<void()>;
40   using ErrorCallback = base::OnceCallback<
41       void(const GURL&, int /* net_error */, int /* http_response_code */)>;
42 
43   // Constructs a ReportSender that sends reports with the
44   // given |request_context|, always excluding cookies. |request_context| must
45   // outlive the ReportSender.
46   explicit ReportSender(URLRequestContext* request_context,
47                         net::NetworkTrafficAnnotationTag traffic_annotation);
48 
49   ReportSender(const ReportSender&) = delete;
50   ReportSender& operator=(const ReportSender&) = delete;
51 
52   ~ReportSender() override;
53 
54   // TransportSecurityState::ReportSenderInterface implementation.
55   void Send(const GURL& report_uri,
56             base::StringPiece content_type,
57             base::StringPiece report,
58             const NetworkAnonymizationKey& network_anonymization_key,
59             SuccessCallback success_callback,
60             ErrorCallback error_callback) override;
61 
62   // net::URLRequest::Delegate implementation.
63   void OnResponseStarted(URLRequest* request, int net_error) override;
64   void OnReadCompleted(URLRequest* request, int bytes_read) override;
65 
66  private:
67   const raw_ptr<net::URLRequestContext> request_context_;
68   std::map<URLRequest*, std::unique_ptr<URLRequest>> inflight_requests_;
69   const net::NetworkTrafficAnnotationTag traffic_annotation_;
70 };
71 
72 }  // namespace net
73 
74 #endif  // NET_URL_REQUEST_REPORT_SENDER_H_
75