OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/offline_pages/core/prefetch/generate_page_bundle_request.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/logging.h" |
| 10 #include "components/offline_pages/core/prefetch/prefetch_request_fetcher.h" |
| 11 #include "components/offline_pages/core/prefetch/prefetch_utils.h" |
| 12 #include "components/offline_pages/core/prefetch/proto/offline_pages.pb.h" |
| 13 #include "components/offline_pages/core/prefetch/proto/operation.pb.h" |
| 14 #include "net/url_request/url_request_context_getter.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 namespace offline_pages { |
| 18 |
| 19 namespace { |
| 20 // TODO(jianli): Update when server is ready. |
| 21 const GURL kOfflinePrefetchServiceUrl( |
| 22 "http://localhost:12345/v1:GeneratePageBundle"); |
| 23 } |
| 24 |
| 25 GeneratePageBundleRequest::GeneratePageBundleRequest( |
| 26 const std::string& user_agent, |
| 27 const std::string& gcm_registration_id, |
| 28 int max_bundle_size_bytes, |
| 29 const std::vector<std::string>& page_urls, |
| 30 net::URLRequestContextGetter* request_context_getter, |
| 31 const FinishedCallback& callback) |
| 32 : callback_(callback) { |
| 33 proto::GeneratePageBundleRequest request; |
| 34 request.set_user_agent(user_agent); |
| 35 request.set_max_bundle_size_bytes(max_bundle_size_bytes); |
| 36 request.set_output_format(proto::FORMAT_MHTML); |
| 37 request.set_gcm_registration_id(gcm_registration_id); |
| 38 |
| 39 for (const auto& page_url : page_urls) { |
| 40 proto::PageParameters* page = request.add_pages(); |
| 41 page->set_url(page_url); |
| 42 page->set_transformation(proto::NO_TRANSFORMATION); |
| 43 } |
| 44 |
| 45 std::string upload_data; |
| 46 request.SerializeToString(&upload_data); |
| 47 |
| 48 fetcher_.reset(new PrefetchRequestFetcher( |
| 49 kOfflinePrefetchServiceUrl, upload_data, request_context_getter, |
| 50 base::Bind(&GeneratePageBundleRequest::OnCompleted, |
| 51 // Fetcher is owned by this instance. |
| 52 base::Unretained(this)))); |
| 53 } |
| 54 |
| 55 GeneratePageBundleRequest::~GeneratePageBundleRequest() {} |
| 56 |
| 57 void GeneratePageBundleRequest::OnCompleted(PrefetchRequestStatus status, |
| 58 const std::string& data) { |
| 59 proto::Operation operation; |
| 60 if (!operation.ParseFromString(data)) { |
| 61 DVLOG(1) << "Failed to parse operation"; |
| 62 NotifyParsingFailure(); |
| 63 return; |
| 64 } |
| 65 |
| 66 if (operation.done()) |
| 67 ParseDoneOperationResponse(operation); |
| 68 else |
| 69 ParsePendingOperationResponse(operation); |
| 70 } |
| 71 |
| 72 void GeneratePageBundleRequest::ParseDoneOperationResponse( |
| 73 const proto::Operation& operation) { |
| 74 switch (operation.result_case()) { |
| 75 case proto::Operation::kError: |
| 76 DVLOG(1) << "Error found in operation"; |
| 77 NotifyParsingFailure(); |
| 78 break; |
| 79 case proto::Operation::kResponse: |
| 80 ParseAnyData(operation.response()); |
| 81 break; |
| 82 default: |
| 83 DVLOG(1) << "Result not set in operation"; |
| 84 NotifyParsingFailure(); |
| 85 break; |
| 86 } |
| 87 } |
| 88 |
| 89 void GeneratePageBundleRequest::ParsePendingOperationResponse( |
| 90 const proto::Operation& operation) { |
| 91 if (!operation.has_metadata()) { |
| 92 DVLOG(1) << "metadata not found in GeneratePageBundle response"; |
| 93 NotifyParsingFailure(); |
| 94 return; |
| 95 } |
| 96 |
| 97 ParseAnyData(operation.metadata()); |
| 98 } |
| 99 |
| 100 void GeneratePageBundleRequest::ParseAnyData(const proto::Any& any_data) { |
| 101 std::vector<RenderPageInfo> pages; |
| 102 if (!ParsePageBundleInAnyData(any_data, &pages)) { |
| 103 NotifyParsingFailure(); |
| 104 return; |
| 105 } |
| 106 |
| 107 callback_.Run(PrefetchRequestStatus::SUCCESS, pages); |
| 108 } |
| 109 |
| 110 void GeneratePageBundleRequest::NotifyParsingFailure() { |
| 111 callback_.Run(PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF, |
| 112 std::vector<RenderPageInfo>()); |
| 113 } |
| 114 |
| 115 } // offline_pages |
OLD | NEW |