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/core/offline_url_utils.h" | 12 #include "components/reading_list/core/offline_url_utils.h" |
| 13 #include "components/reading_list/core/reading_list_entry.h" |
| 14 #include "components/reading_list/core/reading_list_model.h" |
13 #include "ios/chrome/browser/chrome_url_constants.h" | 15 #include "ios/chrome/browser/chrome_url_constants.h" |
14 #include "net/base/url_util.h" | 16 #include "net/base/url_util.h" |
15 | 17 |
16 namespace { | 18 namespace { |
17 const char kEntryURLQueryParam[] = "entryURL"; | 19 const char kEntryURLQueryParam[] = "entryURL"; |
18 const char kVirtualURLQueryParam[] = "virtualURL"; | 20 const char kVirtualURLQueryParam[] = "virtualURL"; |
19 } | 21 } |
20 | 22 |
21 namespace reading_list { | 23 namespace reading_list { |
22 | 24 |
23 GURL OfflineURLForPath(const base::FilePath& distilled_path, | 25 GURL OfflineURLForPath(const base::FilePath& distilled_path, |
24 const GURL& entry_url, | 26 const GURL& entry_url, |
25 const GURL& virtual_url) { | 27 const GURL& virtual_url) { |
26 if (distilled_path.empty()) { | 28 DCHECK(!distilled_path.empty()); |
27 return GURL(); | 29 DCHECK(entry_url.is_valid()); |
28 } | 30 DCHECK(virtual_url.is_valid()); |
29 GURL page_url(kChromeUIOfflineURL); | 31 GURL page_url(kChromeUIOfflineURL); |
30 GURL::Replacements replacements; | 32 GURL::Replacements replacements; |
31 replacements.SetPathStr(distilled_path.value()); | 33 replacements.SetPathStr(distilled_path.value()); |
32 page_url = page_url.ReplaceComponents(replacements); | 34 page_url = page_url.ReplaceComponents(replacements); |
33 if (entry_url.is_valid()) { | 35 page_url = net::AppendQueryParameter(page_url, kEntryURLQueryParam, |
34 page_url = net::AppendQueryParameter(page_url, kEntryURLQueryParam, | 36 entry_url.spec()); |
35 entry_url.spec()); | 37 |
36 } | 38 page_url = net::AppendQueryParameter(page_url, kVirtualURLQueryParam, |
37 if (virtual_url.is_valid()) { | 39 virtual_url.spec()); |
38 page_url = net::AppendQueryParameter(page_url, kVirtualURLQueryParam, | 40 |
39 virtual_url.spec()); | |
40 } | |
41 return page_url; | 41 return page_url; |
42 } | 42 } |
43 | 43 |
44 GURL EntryURLForOfflineURL(const GURL& offline_url) { | 44 GURL EntryURLForOfflineURL(const GURL& offline_url) { |
45 std::string entry_url_string; | 45 std::string entry_url_string; |
46 if (net::GetValueForKeyInQuery(offline_url, kEntryURLQueryParam, | 46 if (net::GetValueForKeyInQuery(offline_url, kEntryURLQueryParam, |
47 &entry_url_string)) { | 47 &entry_url_string)) { |
48 GURL entry_url = GURL(entry_url_string); | 48 GURL entry_url = GURL(entry_url_string); |
49 if (entry_url.is_valid()) { | 49 if (entry_url.is_valid()) { |
50 return entry_url; | 50 return entry_url; |
51 } | 51 } |
52 } | 52 } |
53 return offline_url; | 53 return GURL::EmptyGURL(); |
54 } | 54 } |
55 | 55 |
56 GURL VirtualURLForOfflineURL(const GURL& offline_url) { | 56 GURL VirtualURLForOfflineURL(const GURL& offline_url) { |
57 std::string virtual_url_string; | 57 std::string virtual_url_string; |
58 if (net::GetValueForKeyInQuery(offline_url, kVirtualURLQueryParam, | 58 if (net::GetValueForKeyInQuery(offline_url, kVirtualURLQueryParam, |
59 &virtual_url_string)) { | 59 &virtual_url_string)) { |
60 GURL virtual_url = GURL(virtual_url_string); | 60 GURL virtual_url = GURL(virtual_url_string); |
61 if (virtual_url.is_valid()) { | 61 if (virtual_url.is_valid()) { |
62 return virtual_url; | 62 return virtual_url; |
63 } | 63 } |
64 } | 64 } |
65 return EntryURLForOfflineURL(offline_url); | 65 return GURL::EmptyGURL(); |
66 } | 66 } |
67 | 67 |
68 GURL FileURLForDistilledURL(const GURL& distilled_url, | 68 GURL FileURLForDistilledURL(const GURL& distilled_url, |
69 const base::FilePath& offline_path, | 69 const base::FilePath& offline_path, |
70 GURL* resources_root_url) { | 70 GURL* resources_root_url) { |
71 if (!distilled_url.is_valid()) { | 71 if (!distilled_url.is_valid()) { |
72 return GURL(); | 72 return GURL(); |
73 } | 73 } |
74 DCHECK(distilled_url.SchemeIs(kChromeUIScheme)); | 74 DCHECK(distilled_url.SchemeIs(kChromeUIScheme)); |
75 GURL file_url(base::StringPrintf("%s%s", url::kFileScheme, | 75 GURL file_url(base::StringPrintf("%s%s", url::kFileScheme, |
76 url::kStandardSchemeSeparator) + | 76 url::kStandardSchemeSeparator) + |
77 offline_path.value() + distilled_url.path()); | 77 offline_path.value() + distilled_url.path()); |
78 if (resources_root_url) { | 78 if (resources_root_url) { |
79 *resources_root_url = file_url.Resolve("."); | 79 *resources_root_url = file_url.Resolve("."); |
80 } | 80 } |
81 return file_url; | 81 return file_url; |
82 } | 82 } |
83 | 83 |
84 bool IsOfflineURL(const GURL& url) { | 84 bool IsOfflineURL(const GURL& url) { |
85 return url.SchemeIs(kChromeUIScheme) && url.host() == kChromeUIOfflineHost; | 85 return url.SchemeIs(kChromeUIScheme) && url.host() == kChromeUIOfflineHost; |
86 } | 86 } |
| 87 |
| 88 bool IsOfflineURLValid(const GURL& url, ReadingListModel* model) { |
| 89 if (!IsOfflineURL(url)) { |
| 90 return false; |
| 91 } |
| 92 GURL entry_url = EntryURLForOfflineURL(url); |
| 93 if (!entry_url.is_valid() || !model || !model->loaded()) { |
| 94 return false; |
| 95 } |
| 96 const ReadingListEntry* entry = model->GetEntryByURL(entry_url); |
| 97 if (!entry || entry->DistilledState() != ReadingListEntry::PROCESSED) { |
| 98 return false; |
| 99 } |
| 100 // It is possible (unlikely) for a user to type directly a URL that passes all |
| 101 // the tests above but still is not exactly the one returned by |
| 102 // |OfflineURLForPath|. Make a final test to check it. |
| 103 return url == reading_list::OfflineURLForPath(entry->DistilledPath(), |
| 104 entry->URL(), |
| 105 entry->DistilledURL()); |
87 } | 106 } |
| 107 } |
OLD | NEW |