OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/machine_intelligence/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 machine_intelligence { |
| 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( |
| 29 const GURL& url, |
| 30 const RankerURLFetcher::Callback& callback, |
| 31 net::URLRequestContextGetter* request_context_getter) { |
| 32 // This function is not supposed to be called if the previous operation is not |
| 33 // finished. |
| 34 if (state_ == REQUESTING) { |
| 35 NOTREACHED(); |
| 36 return false; |
| 37 } |
| 38 |
| 39 if (retry_count_ >= kMaxRetry) |
| 40 return false; |
| 41 retry_count_++; |
| 42 |
| 43 state_ = REQUESTING; |
| 44 url_ = url; |
| 45 callback_ = callback; |
| 46 |
| 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: "AssistRanker" |
| 54 description: |
| 55 "Chrome can provide a better UI experience by using machine " |
| 56 "learning models to determine if we should show you or not an " |
| 57 "assist prompt. For instance, Chrome may use features such as " |
| 58 "the detected language of the current page and the past " |
| 59 "interaction with the TransalteUI to decide whether or not we " |
| 60 "should offer you to translate this page. Google returns " |
| 61 "trained machine learning models that will be used to take " |
| 62 "such decision." |
| 63 trigger: |
| 64 "At startup." |
| 65 data: |
| 66 "Path to a model. No user data is included." |
| 67 destination: GOOGLE_OWNED_SERVICE |
| 68 } |
| 69 policy { |
| 70 cookies_allowed: false |
| 71 setting: |
| 72 "NA" |
| 73 })"); |
| 74 // Create and initialize the URL fetcher. |
| 75 fetcher_ = net::URLFetcher::Create(url_, net::URLFetcher::GET, this, |
| 76 traffic_annotation); |
| 77 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 78 fetcher_.get(), |
| 79 data_use_measurement::DataUseUserData::MACHINE_INTELLIGENCE); |
| 80 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 81 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 82 fetcher_->SetRequestContext(request_context_getter); |
| 83 |
| 84 // Set retry parameter for HTTP status code 5xx. This doesn't work against |
| 85 // 106 (net::ERR_INTERNET_DISCONNECTED) and so on. |
| 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 machine_intelligence |
OLD | NEW |