| 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 "base/memory/ptr_util.h" |
| 9 #include "google_apis/google_api_keys.h" |
| 9 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
| 11 #include "net/base/url_util.h" |
| 10 #include "net/http/http_request_headers.h" | 12 #include "net/http/http_request_headers.h" |
| 11 #include "net/http/http_status_code.h" | 13 #include "net/http/http_status_code.h" |
| 12 #include "net/traffic_annotation/network_traffic_annotation.h" | 14 #include "net/traffic_annotation/network_traffic_annotation.h" |
| 13 #include "net/url_request/url_fetcher.h" | 15 #include "net/url_request/url_fetcher.h" |
| 14 #include "net/url_request/url_request_context_getter.h" | 16 #include "net/url_request/url_request_context_getter.h" |
| 15 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
| 16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 17 | 19 |
| 18 namespace offline_pages { | 20 namespace offline_pages { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 const char kPrefetchServer[] = | 24 const char kPrefetchServer[] = "https://offlinepages-pa.googleapis.com/"; |
| 23 "http://staging-offlinepages-pa.sandbox.googleapis.com/"; | 25 const char kPrefetchStagingServer[] = |
| 26 "https://staging-offlinepages-pa.sandbox.googleapis.com/"; |
| 27 |
| 28 // Used in all offline prefetch request URLs to specify API Key. |
| 29 const char kApiKeyName[] = "key"; |
| 24 | 30 |
| 25 // Content type needed in order to communicate with the server in binary | 31 // Content type needed in order to communicate with the server in binary |
| 26 // proto format. | 32 // proto format. |
| 27 const char kRequestContentType[] = "application/x-protobuf"; | 33 const char kRequestContentType[] = "application/x-protobuf"; |
| 28 | 34 |
| 29 GURL CompleteURL(const std::string& url_path) { | 35 GURL CompleteURL(const std::string& url_path, version_info::Channel channel) { |
| 36 bool is_stable_channel = channel == version_info::Channel::STABLE; |
| 37 GURL server_url(is_stable_channel ? kPrefetchServer : kPrefetchStagingServer); |
| 38 |
| 30 GURL::Replacements replacements; | 39 GURL::Replacements replacements; |
| 31 replacements.SetPathStr(url_path); | 40 replacements.SetPathStr(url_path); |
| 32 return GURL(kPrefetchServer).ReplaceComponents(replacements); | 41 GURL url = server_url.ReplaceComponents(replacements); |
| 42 |
| 43 std::string api_key = is_stable_channel ? google_apis::GetAPIKey() |
| 44 : google_apis::GetNonStableAPIKey(); |
| 45 return net::AppendQueryParameter(url, kApiKeyName, api_key); |
| 33 } | 46 } |
| 34 | 47 |
| 35 } // namespace | 48 } // namespace |
| 36 | 49 |
| 37 // static | 50 // static |
| 38 std::unique_ptr<PrefetchRequestFetcher> PrefetchRequestFetcher::CreateForGet( | 51 std::unique_ptr<PrefetchRequestFetcher> PrefetchRequestFetcher::CreateForGet( |
| 39 const std::string& url_path, | 52 const std::string& url_path, |
| 53 version_info::Channel channel, |
| 40 net::URLRequestContextGetter* request_context_getter, | 54 net::URLRequestContextGetter* request_context_getter, |
| 41 const FinishedCallback& callback) { | 55 const FinishedCallback& callback) { |
| 42 return base::WrapUnique(new PrefetchRequestFetcher( | 56 return base::WrapUnique(new PrefetchRequestFetcher( |
| 43 url_path, std::string(), request_context_getter, callback)); | 57 url_path, std::string(), channel, request_context_getter, callback)); |
| 44 } | 58 } |
| 45 | 59 |
| 46 // static | 60 // static |
| 47 std::unique_ptr<PrefetchRequestFetcher> PrefetchRequestFetcher::CreateForPost( | 61 std::unique_ptr<PrefetchRequestFetcher> PrefetchRequestFetcher::CreateForPost( |
| 48 const std::string& url_path, | 62 const std::string& url_path, |
| 49 const std::string& message, | 63 const std::string& message, |
| 64 version_info::Channel channel, |
| 50 net::URLRequestContextGetter* request_context_getter, | 65 net::URLRequestContextGetter* request_context_getter, |
| 51 const FinishedCallback& callback) { | 66 const FinishedCallback& callback) { |
| 52 return base::WrapUnique(new PrefetchRequestFetcher( | 67 return base::WrapUnique(new PrefetchRequestFetcher( |
| 53 url_path, message, request_context_getter, callback)); | 68 url_path, message, channel, request_context_getter, callback)); |
| 54 } | 69 } |
| 55 | 70 |
| 56 PrefetchRequestFetcher::PrefetchRequestFetcher( | 71 PrefetchRequestFetcher::PrefetchRequestFetcher( |
| 57 const std::string& url_path, | 72 const std::string& url_path, |
| 58 const std::string& message, | 73 const std::string& message, |
| 74 version_info::Channel channel, |
| 59 net::URLRequestContextGetter* request_context_getter, | 75 net::URLRequestContextGetter* request_context_getter, |
| 60 const FinishedCallback& callback) | 76 const FinishedCallback& callback) |
| 61 : request_context_getter_(request_context_getter), callback_(callback) { | 77 : request_context_getter_(request_context_getter), callback_(callback) { |
| 62 net::NetworkTrafficAnnotationTag traffic_annotation = | 78 net::NetworkTrafficAnnotationTag traffic_annotation = |
| 63 net::DefineNetworkTrafficAnnotation("offline_prefetch", R"( | 79 net::DefineNetworkTrafficAnnotation("offline_prefetch", R"( |
| 64 semantics { | 80 semantics { |
| 65 sender: "Offline Prefetch" | 81 sender: "Offline Prefetch" |
| 66 description: | 82 description: |
| 67 "Chromium interacts with Offline Page Service to prefetch " | 83 "Chromium interacts with Offline Page Service to prefetch " |
| 68 "suggested website resources." | 84 "suggested website resources." |
| 69 trigger: | 85 trigger: |
| 70 "When there are suggested website resources to fetch." | 86 "When there are suggested website resources to fetch." |
| 71 data: | 87 data: |
| 72 "URLs of the suggested website resources to fetch." | 88 "URLs of the suggested website resources to fetch." |
| 73 destination: GOOGLE_OWNED_SERVICE | 89 destination: GOOGLE_OWNED_SERVICE |
| 74 } | 90 } |
| 75 policy { | 91 policy { |
| 76 cookies_allowed: false | 92 cookies_allowed: false |
| 77 setting: | 93 setting: |
| 78 "Users can enable or disable the offline prefetch by toggling" | 94 "Users can enable or disable the offline prefetch by toggling" |
| 79 "chrome://flags#offline-prefetch in Chromium on Android." | 95 "chrome://flags#offline-prefetch in Chromium on Android." |
| 80 policy_exception_justification: | 96 policy_exception_justification: |
| 81 "Not implemented, considered not useful." | 97 "Not implemented, considered not useful." |
| 82 })"); | 98 })"); |
| 83 url_fetcher_ = net::URLFetcher::Create( | 99 url_fetcher_ = net::URLFetcher::Create( |
| 84 CompleteURL(url_path), | 100 CompleteURL(url_path, channel), |
| 85 message.empty() ? net::URLFetcher::GET : net::URLFetcher::POST, this, | 101 message.empty() ? net::URLFetcher::GET : net::URLFetcher::POST, this, |
| 86 traffic_annotation); | 102 traffic_annotation); |
| 87 url_fetcher_->SetRequestContext(request_context_getter_.get()); | 103 url_fetcher_->SetRequestContext(request_context_getter_.get()); |
| 88 url_fetcher_->SetAutomaticallyRetryOn5xx(false); | 104 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 89 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0); | 105 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0); |
| 90 if (message.empty()) { | 106 if (message.empty()) { |
| 91 std::string extra_header(net::HttpRequestHeaders::kContentType); | 107 std::string extra_header(net::HttpRequestHeaders::kContentType); |
| 92 extra_header += ": "; | 108 extra_header += ": "; |
| 93 extra_header += kRequestContentType; | 109 extra_header += kRequestContentType; |
| 94 url_fetcher_->AddExtraRequestHeader(extra_header); | 110 url_fetcher_->AddExtraRequestHeader(extra_header); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 | 149 |
| 134 if (!source->GetResponseAsString(data) || data->empty()) { | 150 if (!source->GetResponseAsString(data) || data->empty()) { |
| 135 DVLOG(1) << "Failed to get response or empty response"; | 151 DVLOG(1) << "Failed to get response or empty response"; |
| 136 return PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF; | 152 return PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF; |
| 137 } | 153 } |
| 138 | 154 |
| 139 return PrefetchRequestStatus::SUCCESS; | 155 return PrefetchRequestStatus::SUCCESS; |
| 140 } | 156 } |
| 141 | 157 |
| 142 } // offline_pages | 158 } // offline_pages |
| OLD | NEW |