| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/offline_pages/core/prefetch/prefetch_request_fetcher.h" | 5 #include "components/offline_pages/core/prefetch/prefetch_request_fetcher.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" |
| 8 #include "net/base/load_flags.h" | 9 #include "net/base/load_flags.h" |
| 10 #include "net/http/http_request_headers.h" |
| 9 #include "net/http/http_status_code.h" | 11 #include "net/http/http_status_code.h" |
| 10 #include "net/traffic_annotation/network_traffic_annotation.h" | 12 #include "net/traffic_annotation/network_traffic_annotation.h" |
| 11 #include "net/url_request/url_fetcher.h" | 13 #include "net/url_request/url_fetcher.h" |
| 12 #include "net/url_request/url_request_context_getter.h" | 14 #include "net/url_request/url_request_context_getter.h" |
| 13 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
| 14 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 15 | 17 |
| 16 namespace offline_pages { | 18 namespace offline_pages { |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 21 |
| 22 const char kPrefetchServer[] = |
| 23 "http://staging-offlinepages-pa.sandbox.googleapis.com/"; |
| 24 |
| 25 // Content type needed in order to communicate with the server in binary |
| 26 // proto format. |
| 19 const char kRequestContentType[] = "application/x-protobuf"; | 27 const char kRequestContentType[] = "application/x-protobuf"; |
| 28 |
| 29 GURL CompleteURL(const std::string& url_path) { |
| 30 GURL::Replacements replacements; |
| 31 replacements.SetPathStr(url_path); |
| 32 return GURL(kPrefetchServer).ReplaceComponents(replacements); |
| 33 } |
| 34 |
| 20 } // namespace | 35 } // namespace |
| 21 | 36 |
| 37 // static |
| 38 std::unique_ptr<PrefetchRequestFetcher> PrefetchRequestFetcher::CreateForGet( |
| 39 const std::string& url_path, |
| 40 net::URLRequestContextGetter* request_context_getter, |
| 41 const FinishedCallback& callback) { |
| 42 return base::WrapUnique(new PrefetchRequestFetcher( |
| 43 url_path, std::string(), request_context_getter, callback)); |
| 44 } |
| 45 |
| 46 // static |
| 47 std::unique_ptr<PrefetchRequestFetcher> PrefetchRequestFetcher::CreateForPost( |
| 48 const std::string& url_path, |
| 49 const std::string& message, |
| 50 net::URLRequestContextGetter* request_context_getter, |
| 51 const FinishedCallback& callback) { |
| 52 return base::WrapUnique(new PrefetchRequestFetcher( |
| 53 url_path, message, request_context_getter, callback)); |
| 54 } |
| 55 |
| 22 PrefetchRequestFetcher::PrefetchRequestFetcher( | 56 PrefetchRequestFetcher::PrefetchRequestFetcher( |
| 23 const GURL& url, | 57 const std::string& url_path, |
| 24 const std::string& message, | 58 const std::string& message, |
| 25 net::URLRequestContextGetter* request_context_getter, | 59 net::URLRequestContextGetter* request_context_getter, |
| 26 const FinishedCallback& callback) | 60 const FinishedCallback& callback) |
| 27 : request_context_getter_(request_context_getter), callback_(callback) { | 61 : request_context_getter_(request_context_getter), callback_(callback) { |
| 28 net::NetworkTrafficAnnotationTag traffic_annotation = | 62 net::NetworkTrafficAnnotationTag traffic_annotation = |
| 29 net::DefineNetworkTrafficAnnotation("offline_prefetch", R"( | 63 net::DefineNetworkTrafficAnnotation("offline_prefetch", R"( |
| 30 semantics { | 64 semantics { |
| 31 sender: "Offline Prefetch" | 65 sender: "Offline Prefetch" |
| 32 description: | 66 description: |
| 33 "Chromium interacts with Offline Page Service to prefetch " | 67 "Chromium interacts with Offline Page Service to prefetch " |
| 34 "suggested website resources." | 68 "suggested website resources." |
| 35 trigger: | 69 trigger: |
| 36 "When there are suggested website resources to fetch." | 70 "When there are suggested website resources to fetch." |
| 37 data: | 71 data: |
| 38 "URLs of the suggested website resources to fetch." | 72 "URLs of the suggested website resources to fetch." |
| 39 destination: GOOGLE_OWNED_SERVICE | 73 destination: GOOGLE_OWNED_SERVICE |
| 40 } | 74 } |
| 41 policy { | 75 policy { |
| 42 cookies_allowed: false | 76 cookies_allowed: false |
| 43 setting: | 77 setting: |
| 44 "Users can enable or disable the offline prefetch by toggling" | 78 "Users can enable or disable the offline prefetch by toggling" |
| 45 "chrome://flags#offline-prefetch in Chromium on Android." | 79 "chrome://flags#offline-prefetch in Chromium on Android." |
| 46 policy_exception_justification: | 80 policy_exception_justification: |
| 47 "Not implemented, considered not useful." | 81 "Not implemented, considered not useful." |
| 48 })"); | 82 })"); |
| 49 url_fetcher_ = net::URLFetcher::Create(url, net::URLFetcher::POST, this, | 83 url_fetcher_ = net::URLFetcher::Create( |
| 50 traffic_annotation); | 84 CompleteURL(url_path), |
| 85 message.empty() ? net::URLFetcher::GET : net::URLFetcher::POST, this, |
| 86 traffic_annotation); |
| 51 url_fetcher_->SetRequestContext(request_context_getter_.get()); | 87 url_fetcher_->SetRequestContext(request_context_getter_.get()); |
| 52 url_fetcher_->SetAutomaticallyRetryOn5xx(false); | 88 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 53 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0); | 89 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0); |
| 54 url_fetcher_->SetUploadData(kRequestContentType, message); | 90 if (message.empty()) { |
| 91 std::string extra_header(net::HttpRequestHeaders::kContentType); |
| 92 extra_header += ": "; |
| 93 extra_header += kRequestContentType; |
| 94 url_fetcher_->AddExtraRequestHeader(extra_header); |
| 95 } else { |
| 96 url_fetcher_->SetUploadData(kRequestContentType, message); |
| 97 } |
| 55 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 98 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 56 net::LOAD_DO_NOT_SAVE_COOKIES); | 99 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 57 url_fetcher_->Start(); | 100 url_fetcher_->Start(); |
| 58 } | 101 } |
| 59 | 102 |
| 60 PrefetchRequestFetcher::~PrefetchRequestFetcher() {} | 103 PrefetchRequestFetcher::~PrefetchRequestFetcher() {} |
| 61 | 104 |
| 62 void PrefetchRequestFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 105 void PrefetchRequestFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 63 std::string data; | 106 std::string data; |
| 64 PrefetchRequestStatus status = ParseResponse(source, &data); | 107 PrefetchRequestStatus status = ParseResponse(source, &data); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 90 | 133 |
| 91 if (!source->GetResponseAsString(data) || data->empty()) { | 134 if (!source->GetResponseAsString(data) || data->empty()) { |
| 92 DVLOG(1) << "Failed to get response or empty response"; | 135 DVLOG(1) << "Failed to get response or empty response"; |
| 93 return PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF; | 136 return PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF; |
| 94 } | 137 } |
| 95 | 138 |
| 96 return PrefetchRequestStatus::SUCCESS; | 139 return PrefetchRequestStatus::SUCCESS; |
| 97 } | 140 } |
| 98 | 141 |
| 99 } // offline_pages | 142 } // offline_pages |
| OLD | NEW |