summaryrefslogtreecommitdiff
path: root/chromium/net/url_request/report_sender.h
blob: 2400db52a6f4adfb8b8f0f5b1c49492ab5b5c918 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_URL_REQUEST_REPORT_SENDER_H_
#define NET_URL_REQUEST_REPORT_SENDER_H_

#include <map>
#include <memory>

#include "base/callback.h"
#include "base/macros.h"
#include "net/base/net_export.h"
#include "net/http/transport_security_state.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_request.h"

class GURL;

namespace net {

class NetworkIsolationKey;
class URLRequestContext;

// ReportSender asynchronously sends serialized reports to a URI.
// It takes serialized reports as a sequence of bytes so as to be agnostic to
// the format of the report being sent (JSON, protobuf, etc.) and the particular
// data that it contains. Multiple reports can be in-flight at once. This class
// owns inflight requests and cleans them up when necessary.
//
// Despite this class's name, it has nothing to do with the Reporting API,
// which is implemented in net/reporting.
class NET_EXPORT ReportSender
    : public URLRequest::Delegate,
      public TransportSecurityState::ReportSenderInterface {
 public:
  static const int kLoadFlags;

  using SuccessCallback = base::OnceCallback<void()>;
  using ErrorCallback = base::OnceCallback<
      void(const GURL&, int /* net_error */, int /* http_response_code */)>;

  // Constructs a ReportSender that sends reports with the
  // given |request_context|, always excluding cookies. |request_context| must
  // outlive the ReportSender.
  explicit ReportSender(URLRequestContext* request_context,
                        net::NetworkTrafficAnnotationTag traffic_annotation);

  ~ReportSender() override;

  // TransportSecurityState::ReportSenderInterface implementation.
  void Send(const GURL& report_uri,
            base::StringPiece content_type,
            base::StringPiece report,
            const NetworkIsolationKey& network_isolation_key,
            SuccessCallback success_callback,
            ErrorCallback error_callback) override;

  // net::URLRequest::Delegate implementation.
  void OnResponseStarted(URLRequest* request, int net_error) override;
  void OnReadCompleted(URLRequest* request, int bytes_read) override;

 private:
  net::URLRequestContext* const request_context_;
  std::map<URLRequest*, std::unique_ptr<URLRequest>> inflight_requests_;
  const net::NetworkTrafficAnnotationTag traffic_annotation_;

  DISALLOW_COPY_AND_ASSIGN(ReportSender);
};

}  // namespace net

#endif  // NET_URL_REQUEST_REPORT_SENDER_H_