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/prefetch_utils.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/time/time.h" |
| 9 #include "components/offline_pages/core/prefetch/proto/any.pb.h" |
| 10 #include "components/offline_pages/core/prefetch/proto/offline_pages.pb.h" |
| 11 |
| 12 namespace offline_pages { |
| 13 |
| 14 const char kPageBundleTypeURL[] = |
| 15 "type.googleapis.com/chrome.offlinepages.v1.PageBundle"; |
| 16 |
| 17 bool ParsePageBundleInAnyData(const proto::Any& any_data, |
| 18 std::vector<RenderPageInfo>* pages) { |
| 19 if (any_data.type_url() != kPageBundleTypeURL) { |
| 20 DVLOG(1) << "Wrong type url in any data"; |
| 21 return false; |
| 22 } |
| 23 |
| 24 proto::PageBundle page_bundle; |
| 25 if (!page_bundle.ParseFromString(any_data.value())) { |
| 26 DVLOG(1) << "Failed to parse PageBundle in any data"; |
| 27 return false; |
| 28 } |
| 29 |
| 30 if (!page_bundle.archives_size()) { |
| 31 DVLOG(1) << "No archive in PageBundle"; |
| 32 return false; |
| 33 } |
| 34 |
| 35 for (int i = 0; i < page_bundle.archives_size(); ++i) { |
| 36 const proto::Archive& archive = page_bundle.archives(i); |
| 37 |
| 38 if (!archive.page_infos_size()) { |
| 39 DVLOG(1) << "No page in archive"; |
| 40 return false; |
| 41 ; |
| 42 } |
| 43 |
| 44 // Only one page is available in PageInfos. |
| 45 const proto::PageInfo& page_info = archive.page_infos(0); |
| 46 |
| 47 if (page_info.url().empty()) { |
| 48 DVLOG(1) << "Empty page url"; |
| 49 return false; |
| 50 ; |
| 51 } |
| 52 |
| 53 RenderPageInfo page; |
| 54 page.url = page_info.url(); |
| 55 page.redirect_url = page_info.redirect_url(); |
| 56 if (page_info.has_status()) { |
| 57 switch (page_info.status().code()) { |
| 58 case proto::OK: |
| 59 page.status = RenderStatus::RENDERED; |
| 60 break; |
| 61 case proto::NOT_FOUND: |
| 62 page.status = RenderStatus::PENDING; |
| 63 break; |
| 64 case proto::FAILED_PRECONDITION: |
| 65 page.status = RenderStatus::EXCEEDED_LIMIT; |
| 66 break; |
| 67 case proto::UNKNOWN: |
| 68 page.status = RenderStatus::FAILED; |
| 69 break; |
| 70 default: |
| 71 NOTREACHED(); |
| 72 break; |
| 73 } |
| 74 } else { |
| 75 page.status = RenderStatus::RENDERED; |
| 76 } |
| 77 |
| 78 if (page.status == RenderStatus::RENDERED) { |
| 79 page.body_name = archive.body_name(); |
| 80 page.body_length = archive.body_length(); |
| 81 page.render_time = |
| 82 base::Time::FromJavaTime(page_info.render_time().seconds() * 1000 + |
| 83 page_info.render_time().nanos() / 1000000); |
| 84 } |
| 85 |
| 86 DVLOG(1) << "Got page " << page.url << " " << static_cast<int>(page.status) |
| 87 << " " << page.body_name << " " << page.body_length; |
| 88 pages->push_back(page); |
| 89 } |
| 90 |
| 91 return true; |
| 92 } |
| 93 |
| 94 } // namespace offline_pages |
OLD | NEW |