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 kPrefetchSever[] = | |
| 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 |
| 22 PrefetchRequestFetcher::PrefetchRequestFetcher( | 30 PrefetchRequestFetcher::PrefetchRequestFetcher( |
| 23 const GURL& url, | 31 const std::string& url_path, |
| 24 const std::string& message, | 32 const std::string& message, |
| 25 net::URLRequestContextGetter* request_context_getter, | 33 net::URLRequestContextGetter* request_context_getter, |
| 26 const FinishedCallback& callback) | 34 const FinishedCallback& callback) |
| 27 : request_context_getter_(request_context_getter), callback_(callback) { | 35 : request_context_getter_(request_context_getter), callback_(callback) { |
| 28 net::NetworkTrafficAnnotationTag traffic_annotation = | 36 net::NetworkTrafficAnnotationTag traffic_annotation = |
| 29 net::DefineNetworkTrafficAnnotation("offline_prefetch", R"( | 37 net::DefineNetworkTrafficAnnotation("offline_prefetch", R"( |
| 30 semantics { | 38 semantics { |
| 31 sender: "Offline Prefetch" | 39 sender: "Offline Prefetch" |
| 32 description: | 40 description: |
| 33 "Chromium interacts with Offline Page Service to prefetch " | 41 "Chromium interacts with Offline Page Service to prefetch " |
| 34 "suggested website resources." | 42 "suggested website resources." |
| 35 trigger: | 43 trigger: |
| 36 "When there are suggested website resources to fetch." | 44 "When there are suggested website resources to fetch." |
| 37 data: | 45 data: |
| 38 "URLs of the suggested website resources to fetch." | 46 "URLs of the suggested website resources to fetch." |
| 39 destination: GOOGLE_OWNED_SERVICE | 47 destination: GOOGLE_OWNED_SERVICE |
| 40 } | 48 } |
| 41 policy { | 49 policy { |
| 42 cookies_allowed: false | 50 cookies_allowed: false |
| 43 setting: | 51 setting: |
| 44 "Users can enable or disable the offline prefetch by toggling" | 52 "Users can enable or disable the offline prefetch by toggling" |
| 45 "chrome://flags#offline-prefetch in Chromium on Android." | 53 "chrome://flags#offline-prefetch in Chromium on Android." |
| 46 policy_exception_justification: | 54 policy_exception_justification: |
| 47 "Not implemented, considered not useful." | 55 "Not implemented, considered not useful." |
| 48 })"); | 56 })"); |
| 49 url_fetcher_ = net::URLFetcher::Create(url, net::URLFetcher::POST, this, | 57 url_fetcher_ = net::URLFetcher::Create( |
| 50 traffic_annotation); | 58 GURL(kPrefetchSever + url_path), |
|
dewittj
2017/05/16 18:30:20
This feels like it should be more like
GURL::Repl
jianli
2017/05/16 22:44:40
Since both server and path are hard coded constant
dewittj
2017/05/16 23:06:22
URL path is not hard coded in the GET case, correc
jianli
2017/05/16 23:52:33
Make sense. Done.
| |
| 59 message.empty() ? net::URLFetcher::GET : net::URLFetcher::POST, this, | |
| 60 traffic_annotation); | |
| 51 url_fetcher_->SetRequestContext(request_context_getter_.get()); | 61 url_fetcher_->SetRequestContext(request_context_getter_.get()); |
| 52 url_fetcher_->SetAutomaticallyRetryOn5xx(false); | 62 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 53 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0); | 63 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0); |
| 54 url_fetcher_->SetUploadData(kRequestContentType, message); | 64 if (message.empty()) { |
| 65 std::string extra_header(net::HttpRequestHeaders::kContentType); | |
| 66 extra_header += ": "; | |
| 67 extra_header += kRequestContentType; | |
| 68 url_fetcher_->AddExtraRequestHeader(extra_header); | |
| 69 } else { | |
| 70 url_fetcher_->SetUploadData(kRequestContentType, message); | |
| 71 } | |
| 55 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 72 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 56 net::LOAD_DO_NOT_SAVE_COOKIES); | 73 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 57 url_fetcher_->Start(); | 74 url_fetcher_->Start(); |
| 58 } | 75 } |
| 59 | 76 |
| 60 PrefetchRequestFetcher::~PrefetchRequestFetcher() {} | 77 PrefetchRequestFetcher::~PrefetchRequestFetcher() {} |
| 61 | 78 |
| 62 void PrefetchRequestFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 79 void PrefetchRequestFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 63 std::string data; | 80 std::string data; |
| 64 PrefetchRequestStatus status = ParseResponse(source, &data); | 81 PrefetchRequestStatus status = ParseResponse(source, &data); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 90 | 107 |
| 91 if (!source->GetResponseAsString(data) || data->empty()) { | 108 if (!source->GetResponseAsString(data) || data->empty()) { |
| 92 DVLOG(1) << "Failed to get response or empty response"; | 109 DVLOG(1) << "Failed to get response or empty response"; |
| 93 return PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF; | 110 return PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF; |
| 94 } | 111 } |
| 95 | 112 |
| 96 return PrefetchRequestStatus::SUCCESS; | 113 return PrefetchRequestStatus::SUCCESS; |
| 97 } | 114 } |
| 98 | 115 |
| 99 } // offline_pages | 116 } // offline_pages |
| OLD | NEW |