Chromium Code Reviews| Index: components/ranker/ranker_url_fetcher.cc |
| diff --git a/components/ranker/ranker_url_fetcher.cc b/components/ranker/ranker_url_fetcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..673565209e0d93fbe7ceb79cbebc5ac01ac5f2fa |
| --- /dev/null |
| +++ b/components/ranker/ranker_url_fetcher.cc |
| @@ -0,0 +1,109 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/ranker/ranker_url_fetcher.h" |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "components/data_use_measurement/core/data_use_user_data.h" |
| +#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" |
| + |
| +namespace ranker { |
| + |
| +namespace { |
| + |
| +// Retry parameter for fetching. |
| +const int kMaxRetry = 16; |
| + |
| +} // namespace |
| + |
| +RankerURLFetcher::RankerURLFetcher() : state_(IDLE), retry_count_(0) {} |
| + |
| +RankerURLFetcher::~RankerURLFetcher() {} |
| + |
| +bool RankerURLFetcher::Request(const GURL& url, |
| + const RankerURLFetcher::Callback& callback) { |
| + // This function is not supposed to be called if the previous operation is not |
| + // finished. |
| + if (state_ == REQUESTING) { |
| + NOTREACHED(); |
| + return false; |
| + } |
| + |
| + if (retry_count_ >= kMaxRetry) |
| + return false; |
| + retry_count_++; |
| + |
| + state_ = REQUESTING; |
| + url_ = url; |
| + callback_ = callback; |
| + |
| + // FIXME |
| + // scoped_refptr<net::URLRequestContextGetter> request_context_getter; |
| + // if (request_context_getter == nullptr) |
| + // return false; |
| + |
| + net::NetworkTrafficAnnotationTag traffic_annotation = |
| + net::DefineNetworkTrafficAnnotation("ranker_url_fetcher", R"( |
| + semantics { |
| + sender: "Ranker" |
| + description: |
| + "TODO" |
| + trigger: |
| + "TODO" |
| + data: |
| + "TODO" |
| + destination: GOOGLE_OWNED_SERVICE |
| + } |
| + policy { |
| + cookies_allowed: false |
| + setting: |
| + "TODO" |
| + chrome_policy { |
| + TODO |
| + } |
| + policy_exception_justification: |
| + "TODO" |
| + })"); |
| + // Create and initialize the URL fetcher. |
| + fetcher_ = net::URLFetcher::Create(url_, net::URLFetcher::GET, this, |
| + traffic_annotation); |
| + // FIXME Add RANKER to enum. |
| + data_use_measurement::DataUseUserData::AttachToFetcher( |
| + fetcher_.get(), data_use_measurement::DataUseUserData::TRANSLATE); |
| + fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| + net::LOAD_DO_NOT_SAVE_COOKIES); |
| + // FIXME |
| + // fetcher_->SetRequestContext(request_context_getter.get()); |
|
Roger McFarlane (Chromium)
2017/06/06 09:48:31
yup... this is a problem. More specifically, the s
hamelphi
2017/06/09 18:49:15
Fixed.
|
| + |
| + // Set retry parameter for HTTP status code 5xx. This doesn't work against |
| + // 106 (net::ERR_INTERNET_DISCONNECTED) and so on. |
| + // TranslateLanguageList handles network status, and implements retry. |
| + fetcher_->SetMaxRetriesOn5xx(max_retry_on_5xx_); |
| + fetcher_->Start(); |
| + |
| + return true; |
| +} |
| + |
| +void RankerURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| + DCHECK(fetcher_.get() == source); |
| + |
| + std::string data; |
| + if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS && |
| + source->GetResponseCode() == net::HTTP_OK) { |
| + state_ = COMPLETED; |
| + source->GetResponseAsString(&data); |
| + } 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); |
| +} |
| + |
| +} // namespace ranker |