diff options
Diffstat (limited to 'chromium/components/assist_ranker/ranker_url_fetcher.cc')
-rw-r--r-- | chromium/components/assist_ranker/ranker_url_fetcher.cc | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/chromium/components/assist_ranker/ranker_url_fetcher.cc b/chromium/components/assist_ranker/ranker_url_fetcher.cc index 0f8989e0973..8d0f709499b 100644 --- a/chromium/components/assist_ranker/ranker_url_fetcher.cc +++ b/chromium/components/assist_ranker/ranker_url_fetcher.cc @@ -9,8 +9,9 @@ #include "net/base/load_flags.h" #include "net/http/http_status_code.h" #include "net/traffic_annotation/network_traffic_annotation.h" -#include "net/url_request/url_fetcher.h" -#include "net/url_request/url_request_status.h" +#include "services/network/public/cpp/resource_request.h" +#include "services/network/public/cpp/simple_url_loader.h" +#include "services/network/public/mojom/url_loader_factory.mojom.h" namespace assist_ranker { @@ -21,14 +22,15 @@ const int kMaxRetry = 16; } // namespace -RankerURLFetcher::RankerURLFetcher() : state_(IDLE), retry_count_(0) {} +RankerURLFetcher::RankerURLFetcher() + : state_(IDLE), retry_count_(0), max_retry_on_5xx_(0) {} RankerURLFetcher::~RankerURLFetcher() {} bool RankerURLFetcher::Request( const GURL& url, - const RankerURLFetcher::Callback& callback, - net::URLRequestContextGetter* request_context_getter) { + RankerURLFetcher::Callback callback, + network::mojom::URLLoaderFactory* url_loader_factory) { // This function is not supposed to be called if the previous operation is not // finished. if (state_ == REQUESTING) { @@ -42,9 +44,9 @@ bool RankerURLFetcher::Request( state_ = REQUESTING; url_ = url; - callback_ = callback; + callback_ = std::move(callback); - if (request_context_getter == nullptr) + if (url_loader_factory == nullptr) return false; net::NetworkTrafficAnnotationTag traffic_annotation = @@ -73,39 +75,38 @@ bool RankerURLFetcher::Request( policy_exception_justification: "Not implemented, considered not necessary as no user data is sent." })"); - // Create and initialize the URL fetcher. - fetcher_ = net::URLFetcher::Create(url_, net::URLFetcher::GET, this, - traffic_annotation); - data_use_measurement::DataUseUserData::AttachToFetcher( - fetcher_.get(), - data_use_measurement::DataUseUserData::MACHINE_INTELLIGENCE); - fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | - net::LOAD_DO_NOT_SAVE_COOKIES); - fetcher_->SetRequestContext(request_context_getter); - - // Set retry parameter for HTTP status code 5xx. This doesn't work against - // 106 (net::ERR_INTERNET_DISCONNECTED) and so on. - fetcher_->SetMaxRetriesOn5xx(max_retry_on_5xx_); - fetcher_->Start(); + auto resource_request = std::make_unique<network::ResourceRequest>(); + resource_request->url = url_; + resource_request->load_flags = + net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES; + // TODO(https://crbug.com/808498): Re-add data use measurement once + // SimpleURLLoader supports it. + // ID=data_use_measurement::DataUseUserData::MACHINE_INTELLIGENCE + simple_url_loader_ = network::SimpleURLLoader::Create( + std::move(resource_request), traffic_annotation); + if (max_retry_on_5xx_ > 0) { + simple_url_loader_->SetRetryOptions(max_retry_on_5xx_, + network::SimpleURLLoader::RETRY_ON_5XX); + } + simple_url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie( + url_loader_factory, + base::BindOnce(&RankerURLFetcher::OnSimpleLoaderComplete, + base::Unretained(this))); return true; } -void RankerURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { - DCHECK(fetcher_.get() == source); - +void RankerURLFetcher::OnSimpleLoaderComplete( + std::unique_ptr<std::string> response_body) { std::string data; - if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS && - source->GetResponseCode() == net::HTTP_OK) { + if (response_body) { state_ = COMPLETED; - source->GetResponseAsString(&data); + data = std::move(*response_body); } else { state_ = FAILED; } - - // Transfer URLFetcher's ownership before invoking a callback. - std::unique_ptr<const net::URLFetcher> delete_ptr(fetcher_.release()); - callback_.Run(state_ == COMPLETED, data); + simple_url_loader_.reset(); + std::move(callback_).Run(state_ == COMPLETED, data); } } // namespace assist_ranker |