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_status_code.h" | 9 #include "net/http/http_status_code.h" |
10 #include "net/traffic_annotation/network_traffic_annotation.h" | 10 #include "net/traffic_annotation/network_traffic_annotation.h" |
11 #include "net/url_request/url_fetcher.h" | 11 #include "net/url_request/url_fetcher.h" |
12 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
13 #include "net/url_request/url_request_status.h" | 13 #include "net/url_request/url_request_status.h" |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 | 15 |
16 namespace offline_prefetch { | 16 namespace offline_pages { |
17 | 17 |
18 namespace { | 18 namespace { |
19 const char kRequestContentType[] = "application/x-protobuf"; | 19 const char kRequestContentType[] = "application/x-protobuf"; |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 PrefetchRequestFetcher::PrefetchRequestFetcher( | 22 PrefetchRequestFetcher::PrefetchRequestFetcher( |
23 const GURL& url, | 23 const GURL& url, |
24 const std::string& message, | 24 const std::string& message, |
25 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 25 net::URLRequestContextGetter* request_context_getter, |
26 const FinishedCallback& callback) | 26 const FinishedCallback& callback) |
27 : request_context_getter_(request_context_getter), callback_(callback) { | 27 : request_context_getter_(request_context_getter), callback_(callback) { |
28 net::NetworkTrafficAnnotationTag traffic_annotation = | 28 net::NetworkTrafficAnnotationTag traffic_annotation = |
29 net::DefineNetworkTrafficAnnotation("offline_prefetch", R"( | 29 net::DefineNetworkTrafficAnnotation("offline_prefetch", R"( |
30 semantics { | 30 semantics { |
31 sender: "Offline Prefetch" | 31 sender: "Offline Prefetch" |
32 description: | 32 description: |
33 "Chromium interacts with Offline Page Service to prefetch " | 33 "Chromium interacts with Offline Page Service to prefetch " |
34 "suggested website resources." | 34 "suggested website resources." |
35 trigger: | 35 trigger: |
(...skipping 18 matching lines...) Expand all Loading... |
54 url_fetcher_->SetUploadData(kRequestContentType, message); | 54 url_fetcher_->SetUploadData(kRequestContentType, message); |
55 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 55 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
56 net::LOAD_DO_NOT_SAVE_COOKIES); | 56 net::LOAD_DO_NOT_SAVE_COOKIES); |
57 url_fetcher_->Start(); | 57 url_fetcher_->Start(); |
58 } | 58 } |
59 | 59 |
60 PrefetchRequestFetcher::~PrefetchRequestFetcher() {} | 60 PrefetchRequestFetcher::~PrefetchRequestFetcher() {} |
61 | 61 |
62 void PrefetchRequestFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 62 void PrefetchRequestFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
63 std::string data; | 63 std::string data; |
64 Status status = ParseResponse(source, &data); | 64 PrefetchRequestStatus status = ParseResponse(source, &data); |
65 | 65 |
66 // TODO(jianli): Report UMA. | 66 // TODO(jianli): Report UMA. |
67 | 67 |
68 callback_.Run(status, data); | 68 callback_.Run(status, data); |
69 } | 69 } |
70 | 70 |
71 PrefetchRequestFetcher::Status PrefetchRequestFetcher::ParseResponse( | 71 PrefetchRequestStatus PrefetchRequestFetcher::ParseResponse( |
72 const net::URLFetcher* source, | 72 const net::URLFetcher* source, |
73 std::string* data) { | 73 std::string* data) { |
74 if (!source->GetStatus().is_success()) { | 74 if (!source->GetStatus().is_success()) { |
75 net::Error net_error = source->GetStatus().ToNetError(); | 75 net::Error net_error = source->GetStatus().ToNetError(); |
76 DVLOG(1) << "Net error: " << net_error; | 76 DVLOG(1) << "Net error: " << net_error; |
77 return (net_error == net::ERR_BLOCKED_BY_ADMINISTRATOR) | 77 return (net_error == net::ERR_BLOCKED_BY_ADMINISTRATOR) |
78 ? Status::SHOULD_SUSPEND | 78 ? PrefetchRequestStatus::SHOULD_SUSPEND |
79 : Status::SHOULD_RETRY_WITHOUT_BACKOFF; | 79 : PrefetchRequestStatus::SHOULD_RETRY_WITHOUT_BACKOFF; |
80 } | 80 } |
81 | 81 |
82 net::HttpStatusCode response_status = | 82 net::HttpStatusCode response_status = |
83 static_cast<net::HttpStatusCode>(source->GetResponseCode()); | 83 static_cast<net::HttpStatusCode>(source->GetResponseCode()); |
84 if (response_status != net::HTTP_OK) { | 84 if (response_status != net::HTTP_OK) { |
85 DVLOG(1) << "HTTP status: " << response_status; | 85 DVLOG(1) << "HTTP status: " << response_status; |
86 return (response_status == net::HTTP_NOT_IMPLEMENTED) | 86 return (response_status == net::HTTP_NOT_IMPLEMENTED) |
87 ? Status::SHOULD_SUSPEND | 87 ? PrefetchRequestStatus::SHOULD_SUSPEND |
88 : Status::SHOULD_RETRY_WITH_BACKOFF; | 88 : PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF; |
89 } | 89 } |
90 | 90 |
91 if (!source->GetResponseAsString(data) || data->empty()) { | 91 if (!source->GetResponseAsString(data) || data->empty()) { |
92 DVLOG(1) << "Failed to get response or empty response"; | 92 DVLOG(1) << "Failed to get response or empty response"; |
93 return Status::SHOULD_RETRY_WITH_BACKOFF; | 93 return PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF; |
94 } | 94 } |
95 | 95 |
96 return Status::SUCCESS; | 96 return PrefetchRequestStatus::SUCCESS; |
97 } | 97 } |
98 | 98 |
99 } // offline_prefetch | 99 } // offline_pages |
OLD | NEW |