File: stream_attempt.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (115 lines) | stat: -rw-r--r-- 3,701 bytes parent folder | download | duplicates (5)
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
// 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.

#include "net/socket/stream_attempt.h"

#include <memory>

#include "net/base/completion_once_callback.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/http/http_network_session.h"
#include "net/log/net_log.h"
#include "net/log/net_log_with_source.h"
#include "net/socket/stream_socket.h"
#include "net/socket/stream_socket_close_reason.h"
#include "net/ssl/ssl_cert_request_info.h"

namespace net {

// static
StreamAttemptParams StreamAttemptParams::FromHttpNetworkSession(
    HttpNetworkSession* session) {
  return StreamAttemptParams(
      session->context().client_socket_factory, session->ssl_client_context(),
      session->context().socket_performance_watcher_factory,
      session->context().network_quality_estimator, session->net_log());
}

StreamAttemptParams::StreamAttemptParams(
    ClientSocketFactory* client_socket_factory,
    SSLClientContext* ssl_client_context,
    SocketPerformanceWatcherFactory* socket_performance_watcher_factory,
    NetworkQualityEstimator* network_quality_estimator,
    NetLog* net_log)
    : client_socket_factory(client_socket_factory),
      ssl_client_context(ssl_client_context),
      socket_performance_watcher_factory(socket_performance_watcher_factory),
      network_quality_estimator(network_quality_estimator),
      net_log(net_log) {}

StreamAttempt::StreamAttempt(const StreamAttemptParams* params,
                             IPEndPoint ip_endpoint,
                             perfetto::Track track,
                             NetLogSourceType net_log_source_type,
                             NetLogEventType net_log_attempt_event_type,
                             const NetLogWithSource* net_log)
    : params_(params),
      ip_endpoint_(ip_endpoint),
      track_(track),
      net_log_(net_log ? *net_log
                       : NetLogWithSource::Make(params->net_log,
                                                net_log_source_type)),
      net_log_attempt_event_type_(net_log_attempt_event_type) {}

StreamAttempt::~StreamAttempt() {
  // Log this attempt as aborted if the attempt was still in-progress when
  // destroyed.
  if (callback_) {
    LogCompletion(ERR_ABORTED);
  }
}

int StreamAttempt::Start(CompletionOnceCallback callback) {
  net_log().BeginEvent(net_log_attempt_event_type_,
                       [&] { return GetNetLogStartParams(); });

  int rv = StartInternal();
  if (rv != ERR_IO_PENDING) {
    LogCompletion(rv);
  } else {
    callback_ = std::move(callback);
  }
  return rv;
}

std::unique_ptr<StreamSocket> StreamAttempt::ReleaseStreamSocket() {
  return std::move(stream_socket_);
}

scoped_refptr<SSLCertRequestInfo> StreamAttempt::GetCertRequestInfo() {
  return nullptr;
}

void StreamAttempt::SetCancelReason(StreamSocketCloseReason cancel_reason) {
  CHECK(!cancel_reason_.has_value());
  cancel_reason_ = cancel_reason;
}

void StreamAttempt::SetStreamSocket(std::unique_ptr<StreamSocket> socket) {
  stream_socket_ = std::move(socket);
}

void StreamAttempt::NotifyOfCompletion(int rv) {
  CHECK(callback_);

  LogCompletion(rv);
  std::move(callback_).Run(rv);
  // `this` may be deleted.
}

void StreamAttempt::LogCompletion(int rv) {
  connect_timing_.connect_end = base::TimeTicks::Now();
  net_log().EndEvent(net_log_attempt_event_type_, [&] {
    base::Value::Dict dict;
    dict.Set("net_error", rv);
    if (cancel_reason_.has_value()) {
      dict.Set("cancel_reason",
               StreamSocketCloseReasonToString(*cancel_reason_));
    }
    return dict;
  });
}

}  // namespace net