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
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104

content / browser / attribution_reporting / attribution_report_network_sender.h [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_ATTRIBUTION_REPORTING_ATTRIBUTION_REPORT_NETWORK_SENDER_H_
#define CONTENT_BROWSER_ATTRIBUTION_REPORTING_ATTRIBUTION_REPORT_NETWORK_SENDER_H_

#include <list>
#include <memory>
#include <string>

#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "content/browser/attribution_reporting/attribution_report_sender.h"
#include "content/common/content_export.h"

class GURL;

namespace net {
class HttpResponseHeaders;
}  // namespace net

namespace network {
class SharedURLLoaderFactory;
class SimpleURLLoader;
}  // namespace network

namespace url {
class Origin;
}  // namespace url

namespace content {

class AttributionReport;

// Issues POST requests containing attribution reports. Maintains a set of all
// ongoing UrlLoaders used for posting reports. Created and owned by
// `AttributionManagerImpl`.
class CONTENT_EXPORT AttributionReportNetworkSender
    : public AttributionReportSender {
 public:
  explicit AttributionReportNetworkSender(
      scoped_refptr<network::SharedURLLoaderFactory>);
  AttributionReportNetworkSender(const AttributionReportNetworkSender&) =
      delete;
  AttributionReportNetworkSender& operator=(
      const AttributionReportNetworkSender&) = delete;
  AttributionReportNetworkSender(AttributionReportNetworkSender&&) = delete;
  AttributionReportNetworkSender& operator=(AttributionReportNetworkSender&&) =
      delete;
  ~AttributionReportNetworkSender() override;

  // AttributionReportSender:
  void SendReport(AttributionReport report,
                  bool is_debug_report,
                  ReportSentCallback sent_callback) override;
  void SendReport(AttributionDebugReport report,
                  DebugReportSentCallback) override;

  void SendReport(AggregatableDebugReport,
                  base::Value::Dict report_body,
                  AggregatableDebugReportSentCallback) override;

 private:
  // This is a std::list so that iterators remain valid during modifications.
  using UrlLoaderList = std::list<std::unique_ptr<network::SimpleURLLoader>>;

  using UrlLoaderCallback =
      base::OnceCallback<void(UrlLoaderList::iterator it,
                              scoped_refptr<net::HttpResponseHeaders>)>;

  void SendReport(GURL url,
                  url::Origin origin,
                  std::string body,
                  UrlLoaderCallback callback);

  // Called when headers are available for a sent report.
  void OnReportSent(const AttributionReport&,
                    bool is_debug_report,
                    ReportSentCallback sent_callback,
                    UrlLoaderList::iterator it,
                    scoped_refptr<net::HttpResponseHeaders> headers);

  // Called when headers are available for a sent verbose debug report.
  void OnVerboseDebugReportSent(
      base::OnceCallback<void(int status)> callback,
      UrlLoaderList::iterator it,
      scoped_refptr<net::HttpResponseHeaders> headers);

  void OnAggregatableDebugReportSent(
      base::OnceCallback<void(int status)> callback,
      UrlLoaderList::iterator,
      scoped_refptr<net::HttpResponseHeaders>);

  // Reports that are actively being sent.
  UrlLoaderList loaders_in_progress_;

  // Used for network requests.
  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_ATTRIBUTION_REPORTING_ATTRIBUTION_REPORT_NETWORK_SENDER_H_