Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 | |
| 5 #include "components/ranker/ranker_url_fetcher.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "components/data_use_measurement/core/data_use_user_data.h" | |
| 9 #include "net/base/load_flags.h" | |
| 10 #include "net/http/http_status_code.h" | |
| 11 #include "net/traffic_annotation/network_traffic_annotation.h" | |
| 12 #include "net/url_request/url_fetcher.h" | |
| 13 #include "net/url_request/url_request_status.h" | |
| 14 | |
| 15 namespace ranker { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Retry parameter for fetching. | |
| 20 const int kMaxRetry = 16; | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 RankerURLFetcher::RankerURLFetcher() : state_(IDLE), retry_count_(0) {} | |
| 25 | |
| 26 RankerURLFetcher::~RankerURLFetcher() {} | |
| 27 | |
| 28 bool RankerURLFetcher::Request(const GURL& url, | |
| 29 const RankerURLFetcher::Callback& callback) { | |
| 30 // This function is not supposed to be called if the previous operation is not | |
| 31 // finished. | |
| 32 if (state_ == REQUESTING) { | |
| 33 NOTREACHED(); | |
| 34 return false; | |
| 35 } | |
| 36 | |
| 37 if (retry_count_ >= kMaxRetry) | |
| 38 return false; | |
| 39 retry_count_++; | |
| 40 | |
| 41 state_ = REQUESTING; | |
| 42 url_ = url; | |
| 43 callback_ = callback; | |
| 44 | |
| 45 // FIXME | |
| 46 // scoped_refptr<net::URLRequestContextGetter> request_context_getter; | |
| 47 // if (request_context_getter == nullptr) | |
| 48 // return false; | |
| 49 | |
| 50 net::NetworkTrafficAnnotationTag traffic_annotation = | |
| 51 net::DefineNetworkTrafficAnnotation("ranker_url_fetcher", R"( | |
| 52 semantics { | |
| 53 sender: "Ranker" | |
| 54 description: | |
| 55 "TODO" | |
| 56 trigger: | |
| 57 "TODO" | |
| 58 data: | |
| 59 "TODO" | |
| 60 destination: GOOGLE_OWNED_SERVICE | |
| 61 } | |
| 62 policy { | |
| 63 cookies_allowed: false | |
| 64 setting: | |
| 65 "TODO" | |
| 66 chrome_policy { | |
| 67 TODO | |
| 68 } | |
| 69 policy_exception_justification: | |
| 70 "TODO" | |
| 71 })"); | |
| 72 // Create and initialize the URL fetcher. | |
| 73 fetcher_ = net::URLFetcher::Create(url_, net::URLFetcher::GET, this, | |
| 74 traffic_annotation); | |
| 75 // FIXME Add RANKER to enum. | |
| 76 data_use_measurement::DataUseUserData::AttachToFetcher( | |
| 77 fetcher_.get(), data_use_measurement::DataUseUserData::TRANSLATE); | |
| 78 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 79 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 80 // FIXME | |
| 81 // 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.
| |
| 82 | |
| 83 // Set retry parameter for HTTP status code 5xx. This doesn't work against | |
| 84 // 106 (net::ERR_INTERNET_DISCONNECTED) and so on. | |
| 85 // TranslateLanguageList handles network status, and implements retry. | |
| 86 fetcher_->SetMaxRetriesOn5xx(max_retry_on_5xx_); | |
| 87 fetcher_->Start(); | |
| 88 | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 void RankerURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 93 DCHECK(fetcher_.get() == source); | |
| 94 | |
| 95 std::string data; | |
| 96 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS && | |
| 97 source->GetResponseCode() == net::HTTP_OK) { | |
| 98 state_ = COMPLETED; | |
| 99 source->GetResponseAsString(&data); | |
| 100 } else { | |
| 101 state_ = FAILED; | |
| 102 } | |
| 103 | |
| 104 // Transfer URLFetcher's ownership before invoking a callback. | |
| 105 std::unique_ptr<const net::URLFetcher> delete_ptr(fetcher_.release()); | |
| 106 callback_.Run(state_ == COMPLETED, data); | |
| 107 } | |
| 108 | |
| 109 } // namespace ranker | |
| OLD | NEW |