protobuf_http_request_base.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/base/protobuf_http_request_base.h"
  5. #include "net/base/net_errors.h"
  6. #include "remoting/base/protobuf_http_request_config.h"
  7. #include "remoting/base/scoped_protobuf_http_request.h"
  8. #include "services/network/public/cpp/simple_url_loader.h"
  9. #include "services/network/public/mojom/url_response_head.mojom.h"
  10. namespace remoting {
  11. ProtobufHttpRequestBase::ProtobufHttpRequestBase(
  12. std::unique_ptr<ProtobufHttpRequestConfig> config)
  13. : config_(std::move(config)) {
  14. config_->Validate();
  15. }
  16. ProtobufHttpRequestBase::~ProtobufHttpRequestBase() {
  17. #if DCHECK_IS_ON()
  18. DCHECK(request_deadline_.is_null() ||
  19. request_deadline_ >= base::TimeTicks::Now())
  20. << "The request must have been deleted before the deadline.";
  21. #endif // DCHECK_IS_ON()
  22. }
  23. std::unique_ptr<ScopedProtobufHttpRequest>
  24. ProtobufHttpRequestBase::CreateScopedRequest() {
  25. return std::make_unique<ScopedProtobufHttpRequest>(base::BindOnce(
  26. &ProtobufHttpRequestBase::Invalidate, weak_factory_.GetWeakPtr()));
  27. }
  28. ProtobufHttpStatus ProtobufHttpRequestBase::GetUrlLoaderStatus() const {
  29. net::Error net_error = static_cast<net::Error>(url_loader_->NetError());
  30. if (net_error != net::Error::OK &&
  31. net_error != net::Error::ERR_HTTP_RESPONSE_CODE_FAILURE) {
  32. return ProtobufHttpStatus(net_error);
  33. }
  34. // Depending on the configuration, url_loader_->NetError() can be OK even if
  35. // the error code is 4xx or 5xx.
  36. if (!url_loader_->ResponseInfo() || !url_loader_->ResponseInfo()->headers ||
  37. url_loader_->ResponseInfo()->headers->response_code() <= 0) {
  38. return ProtobufHttpStatus(
  39. ProtobufHttpStatus::Code::INTERNAL,
  40. "Failed to get HTTP status from the response header.");
  41. }
  42. return ProtobufHttpStatus(static_cast<net::HttpStatusCode>(
  43. url_loader_->ResponseInfo()->headers->response_code()));
  44. }
  45. void ProtobufHttpRequestBase::StartRequest(
  46. network::mojom::URLLoaderFactory* loader_factory,
  47. std::unique_ptr<network::SimpleURLLoader> url_loader,
  48. base::OnceClosure invalidator) {
  49. DCHECK(!url_loader_);
  50. DCHECK(!invalidator_);
  51. url_loader_ = std::move(url_loader);
  52. invalidator_ = std::move(invalidator);
  53. StartRequestInternal(loader_factory);
  54. #if DCHECK_IS_ON()
  55. base::TimeDelta timeout_duration = GetRequestTimeoutDuration();
  56. if (!timeout_duration.is_zero()) {
  57. // Add a 500ms fuzz to account for task dispatching delay and other stuff.
  58. request_deadline_ =
  59. base::TimeTicks::Now() + timeout_duration + base::Milliseconds(500);
  60. }
  61. #endif // DCHECK_IS_ON()
  62. }
  63. void ProtobufHttpRequestBase::Invalidate() {
  64. // This is not necessarily true if the request has never been added to a
  65. // client.
  66. if (invalidator_) {
  67. std::move(invalidator_).Run();
  68. }
  69. }
  70. } // namespace remoting