OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "chrome/browser/android/offline_pages/offline_page_info_handler.h" |
| 6 |
| 7 #include "base/strings/string_util.h" |
| 8 #include "components/offline_pages/request_header/offline_page_header.h" |
| 9 #include "components/sessions/core/serialized_navigation_driver.h" |
| 10 #include "content/public/browser/navigation_entry.h" |
| 11 |
| 12 namespace offline_pages { |
| 13 |
| 14 namespace { |
| 15 const char kOfflinePageInfoKey[] = "offline"; |
| 16 } |
| 17 |
| 18 // static |
| 19 void OfflinePageInfoHandler::Register() { |
| 20 std::unique_ptr<OfflinePageInfoHandler> instance(new OfflinePageInfoHandler); |
| 21 sessions::SerializedNavigationDriver::Get()->RegisterExtendedInfoHandler( |
| 22 kOfflinePageInfoKey, std::move(instance)); |
| 23 } |
| 24 |
| 25 OfflinePageInfoHandler::OfflinePageInfoHandler() {} |
| 26 |
| 27 OfflinePageInfoHandler::~OfflinePageInfoHandler() {} |
| 28 |
| 29 std::string OfflinePageInfoHandler::GetExtendedInfo( |
| 30 const content::NavigationEntry& entry) const { |
| 31 std::string extra_headers = entry.GetExtraHeaders(); |
| 32 if (extra_headers.empty()) |
| 33 return std::string(); |
| 34 |
| 35 // The offline header will be the only extra header if it is present. |
| 36 std::string offline_header_key(offline_pages::kOfflinePageHeader); |
| 37 offline_header_key += ": "; |
| 38 if (!base::StartsWith(extra_headers, |
| 39 offline_header_key, |
| 40 base::CompareCase::INSENSITIVE_ASCII)) { |
| 41 return std::string(); |
| 42 } |
| 43 std::string header_value = extra_headers.substr(offline_header_key.length()); |
| 44 if (header_value.find("\n") != std::string::npos) |
| 45 return std::string(); |
| 46 |
| 47 OfflinePageHeader header(header_value); |
| 48 if (!header.need_to_persist) |
| 49 return std::string(); |
| 50 |
| 51 return header_value; |
| 52 } |
| 53 |
| 54 void OfflinePageInfoHandler::RestoreExtendedInfo( |
| 55 const std::string& info, content::NavigationEntry* entry) { |
| 56 OfflinePageHeader header(info); |
| 57 // Some sanity check. |
| 58 if (header.reason == OfflinePageHeader::Reason::NONE || |
| 59 !header.need_to_persist) |
| 60 return; |
| 61 entry->AddExtraHeaders(header.GetCompleteHeaderString()); |
| 62 } |
| 63 |
| 64 } // namespace offline_pages |
OLD | NEW |