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

content / browser / loader / keep_alive_attribution_request_helper.h [blame]

// Copyright 2024 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_LOADER_KEEP_ALIVE_ATTRIBUTION_REQUEST_HELPER_H_
#define CONTENT_BROWSER_LOADER_KEEP_ALIVE_ATTRIBUTION_REQUEST_HELPER_H_

#include <stdint.h>

#include <memory>
#include <optional>
#include <string>

#include "base/memory/weak_ptr.h"
#include "content/browser/attribution_reporting/attribution_background_registrations_id.h"
#include "content/browser/attribution_reporting/attribution_suitable_context.h"
#include "content/common/content_export.h"
#include "services/network/public/mojom/attribution.mojom-forward.h"
#include "url/gurl.h"

namespace net {
class HttpResponseHeaders;
}  // namespace net

namespace content {

class AttributionDataHostManager;

// Handles Attribution Reporting API
// (https://github.com/WICG/attribution-reporting-api) background source or
// trigger registrations. It is meant to be optionally hooked to a
// `KeepAliveURLLoader` instance. This enables registrations to be successfully
// processed even when the renderer which initiated the request is no longer
// active.
//
// A helper instance can handle a single request chain.
class CONTENT_EXPORT KeepAliveAttributionRequestHelper {
 public:
  // Creates a `KeepAliveAttributionRequestHelper` instance when the request is
  // eligible for attribution.
  static std::unique_ptr<KeepAliveAttributionRequestHelper> CreateIfNeeded(
      network::mojom::AttributionReportingEligibility,
      const GURL& request_url,
      const std::optional<base::UnguessableToken>& attribution_src_token,
      const std::optional<std::string>& devtools_request_id,
      const AttributionSuitableContext&);

  ~KeepAliveAttributionRequestHelper();

  // non-copyable and non-movable
  KeepAliveAttributionRequestHelper(const KeepAliveAttributionRequestHelper&) =
      delete;
  KeepAliveAttributionRequestHelper& operator=(
      const KeepAliveAttributionRequestHelper&) = delete;

  void OnReceiveRedirect(const net::HttpResponseHeaders* headers,
                         const GURL& redirect_url);
  void OnReceiveResponse(const net::HttpResponseHeaders* headers);

 private:
  friend class KeepAliveAttributionRequestHelperTestPeer;

  KeepAliveAttributionRequestHelper(BackgroundRegistrationsId,
                                    AttributionDataHostManager*,
                                    const GURL& reporting_url);

  BackgroundRegistrationsId id_;

  base::WeakPtr<AttributionDataHostManager> attribution_data_host_manager_;

  // Reporting url of the ongoing request, it is updated on redirection. The url
  // might be suitable or not, if it is not, when receiving a response, it will
  // be ignored.
  GURL reporting_url_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_LOADER_KEEP_ALIVE_ATTRIBUTION_REQUEST_HELPER_H_