unauthenticated_http_fetcher.cc 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2021 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 "ash/quick_pair/repository/unauthenticated_http_fetcher.h"
  5. #include "ash/quick_pair/common/fast_pair/fast_pair_http_result.h"
  6. #include "ash/quick_pair/common/logging.h"
  7. #include "ash/quick_pair/common/quick_pair_browser_delegate.h"
  8. #include "services/network/public/cpp/resource_request.h"
  9. #include "services/network/public/cpp/shared_url_loader_factory.h"
  10. #include "services/network/public/cpp/simple_url_loader.h"
  11. #include "services/network/public/mojom/url_response_head.mojom.h"
  12. namespace {
  13. // Max size set to 2MB. This is well over the expected maximum for our
  14. // expected responses, however it can be increased if needed in the future.
  15. constexpr int kMaxDownloadBytes = 2 * 1024 * 1024;
  16. } // namespace
  17. namespace ash {
  18. namespace quick_pair {
  19. UnauthenticatedHttpFetcher::UnauthenticatedHttpFetcher(
  20. const net::NetworkTrafficAnnotationTag& traffic_annotation)
  21. : traffic_annotation_(traffic_annotation) {}
  22. UnauthenticatedHttpFetcher::~UnauthenticatedHttpFetcher() = default;
  23. void UnauthenticatedHttpFetcher::ExecuteGetRequest(
  24. const GURL& url,
  25. FetchCompleteCallback callback) {
  26. QP_LOG(VERBOSE) << __func__ << ": executing request to: " << url;
  27. auto resource_request = std::make_unique<network::ResourceRequest>();
  28. resource_request->url = url;
  29. resource_request->method = "GET";
  30. resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
  31. auto loader = network::SimpleURLLoader::Create(std::move(resource_request),
  32. traffic_annotation_);
  33. // Enable an immediate retry for client-side transient failures:
  34. // DNS resolution errors and network configuration changes.
  35. // Server HTTP 5xx errors are not retried.
  36. int retry_mode = network::SimpleURLLoader::RETRY_ON_NETWORK_CHANGE |
  37. network::SimpleURLLoader::RETRY_ON_NAME_NOT_RESOLVED;
  38. loader->SetRetryOptions(/*max_retries=*/1, retry_mode);
  39. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory =
  40. QuickPairBrowserDelegate::Get()->GetURLLoaderFactory();
  41. if (!url_loader_factory) {
  42. QP_LOG(WARNING) << __func__ << ": No SharedURLLoaderFactory is available.";
  43. std::move(callback).Run(nullptr, nullptr);
  44. return;
  45. }
  46. auto* loader_ptr = loader.get();
  47. loader_ptr->DownloadToString(
  48. url_loader_factory.get(),
  49. base::BindOnce(&UnauthenticatedHttpFetcher::OnComplete,
  50. weak_ptr_factory_.GetWeakPtr(), std::move(loader),
  51. std::move(callback)),
  52. kMaxDownloadBytes);
  53. }
  54. void UnauthenticatedHttpFetcher::OnComplete(
  55. std::unique_ptr<network::SimpleURLLoader> simple_loader,
  56. FetchCompleteCallback callback,
  57. std::unique_ptr<std::string> response_body) {
  58. std::unique_ptr<FastPairHttpResult> http_result =
  59. std::make_unique<FastPairHttpResult>(
  60. /*net_error=*/simple_loader->NetError(),
  61. /*head=*/simple_loader->ResponseInfo());
  62. if (http_result->IsSuccess()) {
  63. QP_LOG(VERBOSE) << "Successfully fetched "
  64. << simple_loader->GetContentSize() << " bytes from "
  65. << simple_loader->GetFinalURL();
  66. std::move(callback).Run(std::move(response_body), std::move(http_result));
  67. return;
  68. }
  69. QP_LOG(WARNING) << "Downloading to string from "
  70. << simple_loader->GetFinalURL()
  71. << " failed: " << http_result->ToString();
  72. // TODO(jonmann): Implement retries with back-off.
  73. std::move(callback).Run(nullptr, std::move(http_result));
  74. }
  75. } // namespace quick_pair
  76. } // namespace ash