| 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/offline_pages/core/prefetch/prefetch_request_fetcher.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "net/base/load_flags.h" |
| 9 #include "net/http/http_status_code.h" |
| 10 #include "net/traffic_annotation/network_traffic_annotation.h" |
| 11 #include "net/url_request/url_fetcher.h" |
| 12 #include "net/url_request/url_request_context_getter.h" |
| 13 #include "net/url_request/url_request_status.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace offline_prefetch { |
| 17 |
| 18 namespace { |
| 19 const char kRequestContentType[] = "application/x-protobuf"; |
| 20 } // namespace |
| 21 |
| 22 PrefetchRequestFetcher::PrefetchRequestFetcher( |
| 23 const GURL& url, |
| 24 const std::string& message, |
| 25 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 26 const FinishedCallback& callback) |
| 27 : request_context_getter_(request_context_getter), callback_(callback) { |
| 28 net::NetworkTrafficAnnotationTag traffic_annotation = |
| 29 net::DefineNetworkTrafficAnnotation("offline_prefetch", R"( |
| 30 semantics { |
| 31 sender: "Offline Prefetch" |
| 32 description: |
| 33 "Chromium interacts with Offline Page Service to prefetch " |
| 34 "suggested website resources." |
| 35 trigger: |
| 36 "When there are suggested website resources to fetch." |
| 37 data: |
| 38 "URLs of the suggested website resources to fetch." |
| 39 destination: GOOGLE_OWNED_SERVICE |
| 40 } |
| 41 policy { |
| 42 cookies_allowed: false |
| 43 setting: |
| 44 "Users can enable or disable the offline prefetch by toggling" |
| 45 "chrome://flags#offline-prefetch in Chromium on Android." |
| 46 policy_exception_justification: |
| 47 "Not implemented, considered not useful." |
| 48 })"); |
| 49 url_fetcher_ = net::URLFetcher::Create(url, net::URLFetcher::POST, this, |
| 50 traffic_annotation); |
| 51 url_fetcher_->SetRequestContext(request_context_getter_.get()); |
| 52 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 53 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0); |
| 54 url_fetcher_->SetUploadData(kRequestContentType, message); |
| 55 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 56 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 57 url_fetcher_->Start(); |
| 58 } |
| 59 |
| 60 PrefetchRequestFetcher::~PrefetchRequestFetcher() {} |
| 61 |
| 62 void PrefetchRequestFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 63 std::string data; |
| 64 Status status = ParseResponse(source, &data); |
| 65 |
| 66 // TODO(jianli): Report UMA. |
| 67 |
| 68 callback_.Run(status, data); |
| 69 } |
| 70 |
| 71 PrefetchRequestFetcher::Status PrefetchRequestFetcher::ParseResponse( |
| 72 const net::URLFetcher* source, |
| 73 std::string* data) { |
| 74 if (!source->GetStatus().is_success()) { |
| 75 net::Error net_error = source->GetStatus().ToNetError(); |
| 76 DVLOG(1) << "Net error: " << net_error; |
| 77 return (net_error == net::ERR_BLOCKED_BY_ADMINISTRATOR) |
| 78 ? Status::SHOULD_SUSPEND |
| 79 : Status::SHOULD_RETRY_WITHOUT_BACKOFF; |
| 80 } |
| 81 |
| 82 net::HttpStatusCode response_status = |
| 83 static_cast<net::HttpStatusCode>(source->GetResponseCode()); |
| 84 if (response_status != net::HTTP_OK) { |
| 85 DVLOG(1) << "HTTP status: " << response_status; |
| 86 return (response_status == net::HTTP_NOT_IMPLEMENTED) |
| 87 ? Status::SHOULD_SUSPEND |
| 88 : Status::SHOULD_RETRY_WITH_BACKOFF; |
| 89 } |
| 90 |
| 91 if (!source->GetResponseAsString(data) || data->empty()) { |
| 92 DVLOG(1) << "Failed to get response or empty response"; |
| 93 return Status::SHOULD_RETRY_WITH_BACKOFF; |
| 94 } |
| 95 |
| 96 return Status::SUCCESS; |
| 97 } |
| 98 |
| 99 } // offline_prefetch |
| OLD | NEW |