File: pending_beacon_service.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (125 lines) | stat: -rw-r--r-- 5,416 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/pending_beacon_service.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "content/browser/renderer_host/pending_beacon_host.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/data_element.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"

constexpr net::NetworkTrafficAnnotationTag kPendingBeaconNetworkTag =
    net::DefineNetworkTrafficAnnotation("pending_beacon_api",
                                        R"(
        semantics {
          sender: "Pending Beacon API"
          description:
            "This request sends out a pending beacon data as single HTTP POST "
            " or GET request. This is used similarly to javascript "
            "`navigator.sendBeacon`, but can be sent either manually by a "
            "developer using the `sendNow` method, or automatically by the "
            "browser when a document is being discarded."
          trigger:
            "On document destruction or `PendingBeacon.sendNow` is called."
          data:
            "Data sent by the beacon is set by javascript on the page."
          destination: WEBSITE
        }
        policy {
          cookies_allowed: YES
          cookies_store: "user"
          setting: "This feature cannot be fully disabled. Only for the beacon "
            "requests sent on document discarded, they can be disabled via the "
            "`Background sync` section under the `Privacy and security` tab "
            "in chrome://settings. This feature is enabled by default."
          policy_exception_justification: "The policy for Background sync is "
            "not yet implemented."
        }
      )");

namespace content {

class Beacon;

PendingBeaconService* PendingBeaconService::GetInstance() {
  return base::Singleton<PendingBeaconService>::get();
}

PendingBeaconService::PendingBeaconService() = default;
PendingBeaconService::~PendingBeaconService() = default;

void PendingBeaconService::SendBeacons(
    const std::vector<std::unique_ptr<Beacon>>& beacons,
    network::SharedURLLoaderFactory* shared_url_loader_factory) {
  for (const auto& beacon : beacons) {
    auto resource_request = beacon->GenerateResourceRequest();
    // SimpleURLLoader doesn't support bytes and file request body. We need to
    // call AttachStringForUpload and AttachFileForUpload instead in such cases.
    absl::optional<network::DataElement> element;
    if (resource_request->request_body) {
      auto& elements = *resource_request->request_body->elements_mutable();
      DCHECK_EQ(elements.size(), 1u);

      if (elements[0].type() == network::DataElement::Tag::kBytes ||
          elements[0].type() == network::DataElement::Tag::kFile) {
        element = std::move(elements[0]);
        resource_request->request_body = nullptr;
      }
    }

    std::unique_ptr<network::SimpleURLLoader> simple_url_loader =
        network::SimpleURLLoader::Create(std::move(resource_request),
                                         kPendingBeaconNetworkTag);

    if (element.has_value()) {
      const auto& content_type = beacon->content_type();
      if (element->type() == network::DataElement::Tag::kBytes) {
        const auto& bytes = element->As<network::DataElementBytes>();
        if (content_type.empty()) {
          simple_url_loader->AttachStringForUpload(
              std::string(bytes.AsStringPiece()));
        } else {
          simple_url_loader->AttachStringForUpload(
              std::string(bytes.AsStringPiece()), content_type);
        }
      } else if (element->type() == network::DataElement::Tag::kFile) {
        const auto& file = element->As<network::DataElementFile>();
        if (content_type.empty()) {
          simple_url_loader->AttachFileForUpload(file.path(), file.offset(),
                                                 file.length());
        } else {
          simple_url_loader->AttachFileForUpload(file.path(), content_type,
                                                 file.offset(), file.length());
        }
      } else {
        NOTREACHED();
      }
    }

    network::SimpleURLLoader* simple_url_loader_ptr = simple_url_loader.get();

    // Send out the |beacon|.
    // The PendingBeaconService is a singleton with a lifetime the same as the
    // browser process', so it's safe to capture it here.
    UMA_HISTOGRAM_ENUMERATION("PendingBeaconHost.Action",
                              PendingBeaconHost::Action::kNetworkSend);
    simple_url_loader_ptr->DownloadHeadersOnly(
        shared_url_loader_factory,
        base::BindOnce(
            [](std::unique_ptr<network::SimpleURLLoader> loader,
               scoped_refptr<net::HttpResponseHeaders> headers) {
              // Intentionally left empty, this callback captures the |loader|
              // so it stays alive until the beacon request, i.e.
              // |DownloadHeadersOnly|, completes.
              UMA_HISTOGRAM_ENUMERATION(
                  "PendingBeaconHost.Action",
                  PendingBeaconHost::Action::kNetworkComplete);
            },
            std::move(simple_url_loader)));
  }
}

}  // namespace content