Chromium Code Reviews| 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()); | |
|
dewittj
2017/05/02 21:16:35
I think this section also needs:
url_fetcher_->Set
jianli
2017/05/02 22:23:10
Added 1st and 2nd. For SetStopOnRedirect, I am a b
| |
| 52 url_fetcher_->SetUploadData(kRequestContentType, message); | |
| 53 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 54 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 55 url_fetcher_->Start(); | |
| 56 } | |
| 57 | |
| 58 PrefetchRequestFetcher::~PrefetchRequestFetcher() {} | |
| 59 | |
| 60 void PrefetchRequestFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 61 std::string data; | |
| 62 Status status = ParseResponse(source, &data); | |
| 63 | |
| 64 // TODO(jianli): Report UMA. | |
|
dewittj
2017/05/02 21:16:35
nit: TODO(crbug.com/XXX)
jianli
2017/05/02 22:23:10
I am going to add this probably in next patch afte
| |
| 65 | |
| 66 callback_.Run(status, data); | |
| 67 } | |
| 68 | |
| 69 PrefetchRequestFetcher::Status PrefetchRequestFetcher::ParseResponse( | |
| 70 const net::URLFetcher* source, | |
| 71 std::string* data) { | |
| 72 if (!source->GetStatus().is_success()) { | |
| 73 DVLOG(1) << "Net error: " << source->GetStatus().ToNetError(); | |
| 74 return Status::SHOULD_RETRY_WITHOUT_BACKOFF; | |
|
dewittj
2017/05/02 21:16:35
ERR_BLOCKED_BY_ADMINISTRATOR -> SHOULD_SUSPEND
ER
jianli
2017/05/02 22:23:10
Added ERR_BLOCKED_BY_ADMINISTRATOR. All other net
| |
| 75 } | |
| 76 | |
| 77 net::HttpStatusCode response_status = | |
| 78 static_cast<net::HttpStatusCode>(source->GetResponseCode()); | |
| 79 if (response_status != net::HTTP_OK) { | |
| 80 DVLOG(1) << "HTTP status: " << response_status; | |
|
dewittj
2017/05/02 21:16:35
http 400 is currently DISCARD per the design doc g
jianli
2017/05/02 22:23:10
I also think we may not want to support DISCARD si
| |
| 81 if (response_status == net::HTTP_NOT_IMPLEMENTED) | |
| 82 return Status::SHOULD_SUSPEND; | |
| 83 return Status::SHOULD_RETRY_WITH_BACKOFF; | |
| 84 } | |
| 85 | |
| 86 if (!source->GetResponseAsString(data) || data->empty()) { | |
| 87 DVLOG(1) << "Failed to get response or empty response"; | |
| 88 return Status::SHOULD_RETRY_WITH_BACKOFF; | |
| 89 } | |
| 90 | |
| 91 return Status::SUCCESS; | |
| 92 } | |
| 93 | |
| 94 } // offline_prefetch | |
| OLD | NEW |