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

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

Issue 2873383004: [Offline Prefetch] Send GeneratePageBundleRequest to the server (Closed)
Patch Set: Address more feedback Created 3 years, 7 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/generate_page_bundle_request.cc
diff --git a/components/offline_pages/core/prefetch/generate_page_bundle_request.cc b/components/offline_pages/core/prefetch/generate_page_bundle_request.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1f2c173cf44c10e1260e97be0afc19a03ae86b06
--- /dev/null
+++ b/components/offline_pages/core/prefetch/generate_page_bundle_request.cc
@@ -0,0 +1,115 @@
+// 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/generate_page_bundle_request.h"
+
+#include "base/bind.h"
+#include "base/location.h"
+#include "base/logging.h"
+#include "components/offline_pages/core/prefetch/prefetch_request_fetcher.h"
+#include "components/offline_pages/core/prefetch/prefetch_utils.h"
+#include "components/offline_pages/core/prefetch/proto/offline_pages.pb.h"
+#include "components/offline_pages/core/prefetch/proto/operation.pb.h"
+#include "net/url_request/url_request_context_getter.h"
+#include "url/gurl.h"
+
+namespace offline_pages {
+
+namespace {
+// TODO(jianli): Update when server is ready.
+const GURL kOfflinePrefetchServiceUrl(
+ "http://localhost:12345/v1:GeneratePageBundle");
+}
+
+GeneratePageBundleRequest::GeneratePageBundleRequest(
+ const std::string& user_agent,
+ const std::string& gcm_registration_id,
+ int max_bundle_size_bytes,
+ const std::vector<std::string>& page_urls,
+ net::URLRequestContextGetter* request_context_getter,
+ const FinishedCallback& callback)
+ : callback_(callback) {
+ proto::GeneratePageBundleRequest request;
+ request.set_user_agent(user_agent);
+ request.set_max_bundle_size_bytes(max_bundle_size_bytes);
+ request.set_output_format(proto::FORMAT_MHTML);
+ request.set_gcm_registration_id(gcm_registration_id);
+
+ for (const auto& page_url : page_urls) {
+ proto::PageParameters* page = request.add_pages();
+ page->set_url(page_url);
+ page->set_transformation(proto::NO_TRANSFORMATION);
+ }
+
+ std::string upload_data;
+ request.SerializeToString(&upload_data);
+
+ fetcher_.reset(new PrefetchRequestFetcher(
+ kOfflinePrefetchServiceUrl, upload_data, request_context_getter,
+ base::Bind(&GeneratePageBundleRequest::OnCompleted,
+ // Fetcher is owned by this instance.
+ base::Unretained(this))));
+}
+
+GeneratePageBundleRequest::~GeneratePageBundleRequest() {}
+
+void GeneratePageBundleRequest::OnCompleted(PrefetchRequestStatus status,
+ const std::string& data) {
+ proto::Operation operation;
+ if (!operation.ParseFromString(data)) {
+ DVLOG(1) << "Failed to parse operation";
+ NotifyParsingFailure();
+ return;
+ }
+
+ if (operation.done())
+ ParseDoneOperationResponse(operation);
+ else
+ ParsePendingOperationResponse(operation);
+}
+
+void GeneratePageBundleRequest::ParseDoneOperationResponse(
+ const proto::Operation& operation) {
+ switch (operation.result_case()) {
+ case proto::Operation::kError:
+ DVLOG(1) << "Error found in operation";
+ NotifyParsingFailure();
+ break;
+ case proto::Operation::kResponse:
+ ParseAnyData(operation.response());
+ break;
+ default:
+ DVLOG(1) << "Result not set in operation";
+ NotifyParsingFailure();
+ break;
+ }
+}
+
+void GeneratePageBundleRequest::ParsePendingOperationResponse(
+ const proto::Operation& operation) {
+ if (!operation.has_metadata()) {
+ DVLOG(1) << "metadata not found in GeneratePageBundle response";
+ NotifyParsingFailure();
+ return;
+ }
+
+ ParseAnyData(operation.metadata());
+}
+
+void GeneratePageBundleRequest::ParseAnyData(const proto::Any& any_data) {
+ std::vector<RenderPageInfo> pages;
+ if (!ParsePageBundleInAnyData(any_data, &pages)) {
+ NotifyParsingFailure();
+ return;
+ }
+
+ callback_.Run(PrefetchRequestStatus::SUCCESS, pages);
+}
+
+void GeneratePageBundleRequest::NotifyParsingFailure() {
+ callback_.Run(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF,
+ std::vector<RenderPageInfo>());
+}
+
+} // offline_pages

Powered by Google App Engine
This is Rietveld 408576698