File: keep_alive_attribution_request_helper.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 3,983 bytes parent folder | download | duplicates (6)
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
105
106
107
// 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/scoped_refptr.h"
#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 "content/public/browser/weak_document_ptr.h"
#include "services/network/public/mojom/attribution.mojom-forward.h"
#include "url/gurl.h"

namespace attribution_reporting {
enum class AttributionSrcRequestStatus;
}  // namespace attribution_reporting

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 std::optional<AttributionSuitableContext>&,
      WeakDocumentPtr weak_document_ptr = WeakDocumentPtr());

  ~KeepAliveAttributionRequestHelper();

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

  void OnReceiveRedirect(scoped_refptr<net::HttpResponseHeaders> headers,
                         const GURL& redirect_url);
  void OnReceiveResponse(scoped_refptr<net::HttpResponseHeaders> headers);
  void OnError();

 private:
  friend class KeepAliveAttributionRequestHelperTestPeer;

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

  void RecordAttributionSrcRequestStatus(
      attribution_reporting::AttributionSrcRequestStatus);

  void OnComplete();

  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_;

  // Whether the keep alive request is tied with a navigation.
  bool is_navigation_tied_ = false;

  // Whether the keep alive request has been ever redirected, updated at the
  // first redirect if there's a chain of multiple redirects.
  bool redirected_ = false;

  // Points to the document that initiates this request.
  // It may become null at any moment whenever the RenderFrameHost it points to
  // is deleted or navigates to a different document. See its classdoc for more
  // details.
  WeakDocumentPtr weak_document_ptr_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_LOADER_KEEP_ALIVE_ATTRIBUTION_REQUEST_HELPER_H_