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