File: protobuf_http_request_base.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (81 lines) | stat: -rw-r--r-- 2,875 bytes parent folder | download | duplicates (3)
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
// 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.

#include "remoting/base/protobuf_http_request_base.h"

#include "net/base/net_errors.h"
#include "remoting/base/protobuf_http_request_config.h"
#include "remoting/base/scoped_protobuf_http_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_response_head.mojom.h"

namespace remoting {

ProtobufHttpRequestBase::ProtobufHttpRequestBase(
    std::unique_ptr<ProtobufHttpRequestConfig> config)
    : config_(std::move(config)) {
  config_->Validate();
}

ProtobufHttpRequestBase::~ProtobufHttpRequestBase() {
#if DCHECK_IS_ON()
  DCHECK(request_deadline_.is_null() ||
         request_deadline_ >= base::TimeTicks::Now())
      << "The request must have been deleted before the deadline.";
#endif  // DCHECK_IS_ON()
}

std::unique_ptr<ScopedProtobufHttpRequest>
ProtobufHttpRequestBase::CreateScopedRequest() {
  return std::make_unique<ScopedProtobufHttpRequest>(base::BindOnce(
      &ProtobufHttpRequestBase::Invalidate, weak_factory_.GetWeakPtr()));
}

HttpStatus ProtobufHttpRequestBase::GetUrlLoaderStatus() const {
  net::Error net_error = static_cast<net::Error>(url_loader_->NetError());
  if (net_error != net::Error::OK &&
      net_error != net::Error::ERR_HTTP_RESPONSE_CODE_FAILURE) {
    return HttpStatus(net_error);
  }
  // Depending on the configuration, url_loader_->NetError() can be OK even if
  // the error code is 4xx or 5xx.
  if (!url_loader_->ResponseInfo() || !url_loader_->ResponseInfo()->headers ||
      url_loader_->ResponseInfo()->headers->response_code() <= 0) {
    return HttpStatus(HttpStatus::Code::INTERNAL,
                      "Failed to get HTTP status from the response header.");
  }
  return HttpStatus(static_cast<net::HttpStatusCode>(
      url_loader_->ResponseInfo()->headers->response_code()));
}

void ProtobufHttpRequestBase::StartRequest(
    network::mojom::URLLoaderFactory* loader_factory,
    std::unique_ptr<network::SimpleURLLoader> url_loader,
    base::OnceClosure invalidator) {
  DCHECK(!url_loader_);
  DCHECK(!invalidator_);

  url_loader_ = std::move(url_loader);
  invalidator_ = std::move(invalidator);
  StartRequestInternal(loader_factory);

#if DCHECK_IS_ON()
  base::TimeDelta timeout_duration = GetRequestTimeoutDuration();
  if (!timeout_duration.is_zero()) {
    // Add a 500ms fuzz to account for task dispatching delay and other stuff.
    request_deadline_ =
        base::TimeTicks::Now() + timeout_duration + base::Milliseconds(500);
  }
#endif  // DCHECK_IS_ON()
}

void ProtobufHttpRequestBase::Invalidate() {
  // This is not necessarily true if the request has never been added to a
  // client.
  if (invalidator_) {
    std::move(invalidator_).Run();
  }
}

}  // namespace remoting