| 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
|
| index 1f2c173cf44c10e1260e97be0afc19a03ae86b06..e6a3c99e38b6cb6addacf79fb5214e7ec1397655 100644
|
| --- a/components/offline_pages/core/prefetch/generate_page_bundle_request.cc
|
| +++ b/components/offline_pages/core/prefetch/generate_page_bundle_request.cc
|
| @@ -7,20 +7,17 @@
|
| #include "base/bind.h"
|
| #include "base/location.h"
|
| #include "base/logging.h"
|
| +#include "components/offline_pages/core/prefetch/prefetch_proto_utils.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");
|
| -}
|
| +const char kGeneratePageBundleRequestURLPath[] = "v1:GeneratePageBundle";
|
| +} // namespace
|
|
|
| GeneratePageBundleRequest::GeneratePageBundleRequest(
|
| const std::string& user_agent,
|
| @@ -28,7 +25,7 @@ GeneratePageBundleRequest::GeneratePageBundleRequest(
|
| int max_bundle_size_bytes,
|
| const std::vector<std::string>& page_urls,
|
| net::URLRequestContextGetter* request_context_getter,
|
| - const FinishedCallback& callback)
|
| + const PrefetchRequestFinishedCallback& callback)
|
| : callback_(callback) {
|
| proto::GeneratePageBundleRequest request;
|
| request.set_user_agent(user_agent);
|
| @@ -45,71 +42,30 @@ GeneratePageBundleRequest::GeneratePageBundleRequest(
|
| std::string upload_data;
|
| request.SerializeToString(&upload_data);
|
|
|
| - fetcher_.reset(new PrefetchRequestFetcher(
|
| - kOfflinePrefetchServiceUrl, upload_data, request_context_getter,
|
| + fetcher_ = PrefetchRequestFetcher::CreateForPost(
|
| + kGeneratePageBundleRequestURLPath, upload_data, request_context_getter,
|
| base::Bind(&GeneratePageBundleRequest::OnCompleted,
|
| // Fetcher is owned by this instance.
|
| - base::Unretained(this))));
|
| + 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();
|
| + if (status != PrefetchRequestStatus::SUCCESS) {
|
| + callback_.Run(status, std::vector<RenderPageInfo>());
|
| 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();
|
| + if (!ParseOperationResponse(data, &pages)) {
|
| + callback_.Run(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF,
|
| + std::vector<RenderPageInfo>());
|
| return;
|
| }
|
|
|
| callback_.Run(PrefetchRequestStatus::SUCCESS, pages);
|
| }
|
|
|
| -void GeneratePageBundleRequest::NotifyParsingFailure() {
|
| - callback_.Run(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF,
|
| - std::vector<RenderPageInfo>());
|
| -}
|
| -
|
| } // offline_pages
|
|
|