Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(347)

Unified Diff: components/offline_pages/core/prefetch/prefetch_request_fetcher.cc

Issue 2856793002: [Offline Prefetch] Prefetch request fetcher (Closed)
Patch Set: Address feedback Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/offline_pages/core/prefetch/prefetch_request_fetcher.cc
diff --git a/components/offline_pages/core/prefetch/prefetch_request_fetcher.cc b/components/offline_pages/core/prefetch/prefetch_request_fetcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..10e0de23577a9e89f4948f5cebe33d81addcd8ea
--- /dev/null
+++ b/components/offline_pages/core/prefetch/prefetch_request_fetcher.cc
@@ -0,0 +1,99 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/offline_pages/core/prefetch/prefetch_request_fetcher.h"
+
+#include "base/logging.h"
+#include "net/base/load_flags.h"
+#include "net/http/http_status_code.h"
+#include "net/traffic_annotation/network_traffic_annotation.h"
+#include "net/url_request/url_fetcher.h"
+#include "net/url_request/url_request_context_getter.h"
+#include "net/url_request/url_request_status.h"
+#include "url/gurl.h"
+
+namespace offline_prefetch {
+
+namespace {
+const char kRequestContentType[] = "application/x-protobuf";
+} // namespace
+
+PrefetchRequestFetcher::PrefetchRequestFetcher(
+ const GURL& url,
+ const std::string& message,
+ scoped_refptr<net::URLRequestContextGetter> request_context_getter,
+ const FinishedCallback& callback)
+ : request_context_getter_(request_context_getter), callback_(callback) {
+ net::NetworkTrafficAnnotationTag traffic_annotation =
+ net::DefineNetworkTrafficAnnotation("offline_prefetch", R"(
+ semantics {
+ sender: "Offline Prefetch"
+ description:
+ "Chromium interacts with Offline Page Service to prefetch "
+ "suggested website resources."
+ trigger:
+ "When there are suggested website resources to fetch."
+ data:
+ "URLs of the suggested website resources to fetch."
+ destination: GOOGLE_OWNED_SERVICE
+ }
+ policy {
+ cookies_allowed: false
+ setting:
+ "Users can enable or disable the offline prefetch by toggling"
+ "chrome://flags#offline-prefetch in Chromium on Android."
+ policy_exception_justification:
+ "Not implemented, considered not useful."
+ })");
+ url_fetcher_ = net::URLFetcher::Create(url, net::URLFetcher::POST, this,
+ traffic_annotation);
+ url_fetcher_->SetRequestContext(request_context_getter_.get());
+ url_fetcher_->SetAutomaticallyRetryOn5xx(false);
+ url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(0);
+ url_fetcher_->SetUploadData(kRequestContentType, message);
+ url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
+ net::LOAD_DO_NOT_SAVE_COOKIES);
+ url_fetcher_->Start();
+}
+
+PrefetchRequestFetcher::~PrefetchRequestFetcher() {}
+
+void PrefetchRequestFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
+ std::string data;
+ Status status = ParseResponse(source, &data);
+
+ // TODO(jianli): Report UMA.
+
+ callback_.Run(status, data);
+}
+
+PrefetchRequestFetcher::Status PrefetchRequestFetcher::ParseResponse(
+ const net::URLFetcher* source,
+ std::string* data) {
+ if (!source->GetStatus().is_success()) {
+ net::Error net_error = source->GetStatus().ToNetError();
+ DVLOG(1) << "Net error: " << net_error;
+ return (net_error == net::ERR_BLOCKED_BY_ADMINISTRATOR)
+ ? Status::SHOULD_SUSPEND
+ : Status::SHOULD_RETRY_WITHOUT_BACKOFF;
+ }
+
+ net::HttpStatusCode response_status =
+ static_cast<net::HttpStatusCode>(source->GetResponseCode());
+ if (response_status != net::HTTP_OK) {
+ DVLOG(1) << "HTTP status: " << response_status;
+ return (response_status == net::HTTP_NOT_IMPLEMENTED)
+ ? Status::SHOULD_SUSPEND
+ : Status::SHOULD_RETRY_WITH_BACKOFF;
+ }
+
+ if (!source->GetResponseAsString(data) || data->empty()) {
+ DVLOG(1) << "Failed to get response or empty response";
+ return Status::SHOULD_RETRY_WITH_BACKOFF;
+ }
+
+ return Status::SUCCESS;
+}
+
+} // offline_prefetch

Powered by Google App Engine
This is Rietveld 408576698