| 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_utils.h" | 5 #include "components/offline_pages/core/prefetch/prefetch_proto_utils.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "components/offline_pages/core/prefetch/proto/any.pb.h" | 9 #include "components/offline_pages/core/prefetch/proto/any.pb.h" |
| 10 #include "components/offline_pages/core/prefetch/proto/offline_pages.pb.h" | 10 #include "components/offline_pages/core/prefetch/proto/offline_pages.pb.h" |
| 11 #include "components/offline_pages/core/prefetch/proto/operation.pb.h" |
| 11 | 12 |
| 12 namespace offline_pages { | 13 namespace offline_pages { |
| 13 | 14 |
| 14 const char kPageBundleTypeURL[] = | 15 const char kPageBundleTypeURL[] = |
| 15 "type.googleapis.com/chrome.offlinepages.v1.PageBundle"; | 16 "type.googleapis.com/google.internal.chrome.offlinepages.v1.PageBundle"; |
| 16 | 17 |
| 18 namespace { |
| 19 |
| 20 // Parse PageBundle data stored as Any proto data. True is returned when the |
| 21 // parsing succeeds and the result pages are stored in |pages|. |
| 17 bool ParsePageBundleInAnyData(const proto::Any& any_data, | 22 bool ParsePageBundleInAnyData(const proto::Any& any_data, |
| 18 std::vector<RenderPageInfo>* pages) { | 23 std::vector<RenderPageInfo>* pages) { |
| 19 if (any_data.type_url() != kPageBundleTypeURL) { | 24 if (any_data.type_url() != kPageBundleTypeURL) { |
| 20 DVLOG(1) << "Wrong type url in any data"; | 25 DVLOG(1) << "Wrong type url in any data"; |
| 21 return false; | 26 return false; |
| 22 } | 27 } |
| 23 | 28 |
| 24 proto::PageBundle page_bundle; | 29 proto::PageBundle page_bundle; |
| 25 if (!page_bundle.ParseFromString(any_data.value())) { | 30 if (!page_bundle.ParseFromString(any_data.value())) { |
| 26 DVLOG(1) << "Failed to parse PageBundle in any data"; | 31 DVLOG(1) << "Failed to parse PageBundle in any data"; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 } | 89 } |
| 85 | 90 |
| 86 DVLOG(1) << "Got page " << page.url << " " << static_cast<int>(page.status) | 91 DVLOG(1) << "Got page " << page.url << " " << static_cast<int>(page.status) |
| 87 << " " << page.body_name << " " << page.body_length; | 92 << " " << page.body_name << " " << page.body_length; |
| 88 pages->push_back(page); | 93 pages->push_back(page); |
| 89 } | 94 } |
| 90 | 95 |
| 91 return true; | 96 return true; |
| 92 } | 97 } |
| 93 | 98 |
| 99 bool ParseDoneOperationResponse(const proto::Operation& operation, |
| 100 std::vector<RenderPageInfo>* pages) { |
| 101 switch (operation.result_case()) { |
| 102 case proto::Operation::kError: |
| 103 DCHECK_NE(proto::OK, operation.error().code()); |
| 104 DVLOG(1) << "Error found in operation: " << operation.error().code() |
| 105 << operation.error().message(); |
| 106 return false; |
| 107 case proto::Operation::kResponse: |
| 108 return ParsePageBundleInAnyData(operation.response(), pages); |
| 109 default: |
| 110 DVLOG(1) << "Result not set in operation"; |
| 111 return false; |
| 112 } |
| 113 } |
| 114 |
| 115 bool ParsePendingOperationResponse(const proto::Operation& operation, |
| 116 std::vector<RenderPageInfo>* pages) { |
| 117 if (!operation.has_metadata()) { |
| 118 DVLOG(1) << "metadata not found in GeneratePageBundle response"; |
| 119 return false; |
| 120 } |
| 121 return ParsePageBundleInAnyData(operation.metadata(), pages); |
| 122 } |
| 123 |
| 124 } // namespace |
| 125 |
| 126 bool ParseOperationResponse(const std::string& data, |
| 127 std::vector<RenderPageInfo>* pages) { |
| 128 proto::Operation operation; |
| 129 if (!operation.ParseFromString(data)) { |
| 130 DVLOG(1) << "Failed to parse operation"; |
| 131 return false; |
| 132 } |
| 133 |
| 134 if (operation.done()) |
| 135 return ParseDoneOperationResponse(operation, pages); |
| 136 else |
| 137 return ParsePendingOperationResponse(operation, pages); |
| 138 } |
| 139 |
| 94 } // namespace offline_pages | 140 } // namespace offline_pages |
| OLD | NEW |