| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "ios/chrome/browser/reading_list/offline_url_utils.h" | 5 #include "ios/chrome/browser/reading_list/offline_url_utils.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/md5.h" | 8 #include "base/md5.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "components/reading_list/ios/offline_url_utils.h" | 12 #include "components/reading_list/ios/offline_url_utils.h" |
| 13 #include "ios/chrome/browser/chrome_url_constants.h" | 13 #include "ios/chrome/browser/chrome_url_constants.h" |
| 14 #include "net/base/url_util.h" | 14 #include "net/base/url_util.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 const char kEntryURLQueryParam[] = "entryURL"; |
| 17 const char kVirtualURLQueryParam[] = "virtualURL"; | 18 const char kVirtualURLQueryParam[] = "virtualURL"; |
| 18 } | 19 } |
| 19 | 20 |
| 20 namespace reading_list { | 21 namespace reading_list { |
| 21 | 22 |
| 22 GURL DistilledURLForPath(const base::FilePath& distilled_path, | 23 GURL OfflineURLForPath(const base::FilePath& distilled_path, |
| 23 const GURL& virtual_url) { | 24 const GURL& entry_url, |
| 25 const GURL& virtual_url) { |
| 24 if (distilled_path.empty()) { | 26 if (distilled_path.empty()) { |
| 25 return GURL(); | 27 return GURL(); |
| 26 } | 28 } |
| 27 GURL page_url(kChromeUIOfflineURL); | 29 GURL page_url(kChromeUIOfflineURL); |
| 28 GURL::Replacements replacements; | 30 GURL::Replacements replacements; |
| 29 replacements.SetPathStr(distilled_path.value()); | 31 replacements.SetPathStr(distilled_path.value()); |
| 30 page_url = page_url.ReplaceComponents(replacements); | 32 page_url = page_url.ReplaceComponents(replacements); |
| 33 if (entry_url.is_valid()) { |
| 34 page_url = net::AppendQueryParameter(page_url, kEntryURLQueryParam, |
| 35 entry_url.spec()); |
| 36 } |
| 31 if (virtual_url.is_valid()) { | 37 if (virtual_url.is_valid()) { |
| 32 page_url = net::AppendQueryParameter(page_url, kVirtualURLQueryParam, | 38 page_url = net::AppendQueryParameter(page_url, kVirtualURLQueryParam, |
| 33 virtual_url.spec()); | 39 virtual_url.spec()); |
| 34 } | 40 } |
| 35 return page_url; | 41 return page_url; |
| 36 } | 42 } |
| 37 | 43 |
| 38 GURL VirtualURLForDistilledURL(const GURL& distilled_url) { | 44 GURL EntryURLForOfflineURL(const GURL& offline_url) { |
| 45 std::string entry_url_string; |
| 46 if (net::GetValueForKeyInQuery(offline_url, kEntryURLQueryParam, |
| 47 &entry_url_string)) { |
| 48 GURL entry_url = GURL(entry_url_string); |
| 49 if (entry_url.is_valid()) { |
| 50 return entry_url; |
| 51 } |
| 52 } |
| 53 return offline_url; |
| 54 } |
| 55 |
| 56 GURL VirtualURLForOfflineURL(const GURL& offline_url) { |
| 39 std::string virtual_url_string; | 57 std::string virtual_url_string; |
| 40 if (net::GetValueForKeyInQuery(distilled_url, kVirtualURLQueryParam, | 58 if (net::GetValueForKeyInQuery(offline_url, kVirtualURLQueryParam, |
| 41 &virtual_url_string)) { | 59 &virtual_url_string)) { |
| 42 GURL virtual_url = GURL(virtual_url_string); | 60 GURL virtual_url = GURL(virtual_url_string); |
| 43 if (virtual_url.is_valid()) { | 61 if (virtual_url.is_valid()) { |
| 44 return virtual_url; | 62 return virtual_url; |
| 45 } | 63 } |
| 46 } | 64 } |
| 47 return distilled_url; | 65 return EntryURLForOfflineURL(offline_url); |
| 48 } | 66 } |
| 49 | 67 |
| 50 GURL FileURLForDistilledURL(const GURL& distilled_url, | 68 GURL FileURLForDistilledURL(const GURL& distilled_url, |
| 51 const base::FilePath& offline_path, | 69 const base::FilePath& offline_path, |
| 52 GURL* resources_root_url) { | 70 GURL* resources_root_url) { |
| 53 if (!distilled_url.is_valid()) { | 71 if (!distilled_url.is_valid()) { |
| 54 return GURL(); | 72 return GURL(); |
| 55 } | 73 } |
| 56 DCHECK(distilled_url.SchemeIs(kChromeUIScheme)); | 74 DCHECK(distilled_url.SchemeIs(kChromeUIScheme)); |
| 57 GURL file_url(base::StringPrintf("%s%s", url::kFileScheme, | 75 GURL file_url(base::StringPrintf("%s%s", url::kFileScheme, |
| 58 url::kStandardSchemeSeparator) + | 76 url::kStandardSchemeSeparator) + |
| 59 offline_path.value() + distilled_url.path()); | 77 offline_path.value() + distilled_url.path()); |
| 60 if (resources_root_url) { | 78 if (resources_root_url) { |
| 61 *resources_root_url = file_url.Resolve("."); | 79 *resources_root_url = file_url.Resolve("."); |
| 62 } | 80 } |
| 63 return file_url; | 81 return file_url; |
| 64 } | 82 } |
| 65 | 83 |
| 66 bool IsOfflineURL(const GURL& url) { | 84 bool IsOfflineURL(const GURL& url) { |
| 67 return url.SchemeIs(kChromeUIScheme) && url.host() == kChromeUIOfflineHost; | 85 return url.SchemeIs(kChromeUIScheme) && url.host() == kChromeUIOfflineHost; |
| 68 } | 86 } |
| 69 } | 87 } |
| OLD | NEW |