| 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/reading_list_distiller_page.h" | 5 #include "ios/chrome/browser/reading_list/reading_list_distiller_page.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/mac/foundation_util.h" | 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "components/favicon/ios/web_favicon_driver.h" | 12 #include "components/favicon/ios/web_favicon_driver.h" |
| 12 #include "components/google/core/browser/google_util.h" | 13 #include "components/google/core/browser/google_util.h" |
| 13 #include "ios/chrome/browser/reading_list/favicon_web_state_dispatcher_impl.h" | 14 #include "ios/chrome/browser/reading_list/favicon_web_state_dispatcher_impl.h" |
| 14 #import "ios/web/public/navigation_item.h" | 15 #import "ios/web/public/navigation_item.h" |
| 15 #import "ios/web/public/navigation_manager.h" | 16 #import "ios/web/public/navigation_manager.h" |
| 16 #include "ios/web/public/ssl_status.h" | 17 #include "ios/web/public/ssl_status.h" |
| 17 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 18 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 18 #import "ios/web/public/web_state/web_state.h" | 19 #import "ios/web/public/web_state/web_state.h" |
| 19 #import "net/base/mac/url_conversions.h" | 20 #import "net/base/mac/url_conversions.h" |
| 20 #include "net/cert/cert_status_flags.h" | 21 #include "net/cert/cert_status_flags.h" |
| 21 #include "url/url_constants.h" | 22 #include "url/url_constants.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 // The delay given to the web page to render after the PageLoaded callback. | 25 // The delay given to the web page to render after the PageLoaded callback. |
| 25 const int64_t kPageLoadDelayInSeconds = 2; | 26 const int64_t kPageLoadDelayInSeconds = 2; |
| 26 | 27 |
| 27 const char* kGetIframeURLJavaScript = | 28 const char* kGetIframeURLJavaScript = |
| 28 "document.getElementsByTagName('iframe')[0].src;"; | 29 "(() => {" |
| 30 " var link = document.evaluate('//link[@rel=\"amphtml\"]'," |
| 31 " document," |
| 32 " null," |
| 33 " XPathResult.ORDERED_NODE_SNAPSHOT_TYPE," |
| 34 " null ).snapshotItem(0);" |
| 35 " if (link !== null) {" |
| 36 " return link.getAttribute('href');" |
| 37 " }" |
| 38 " return document.getElementsByTagName('iframe')[0].src;" |
| 39 "})()"; |
| 40 |
| 41 const char* kWikipediaWorkaround = |
| 42 "(() => {" |
| 43 " var s = document.createElement('style');" |
| 44 " s.innerHTML='.client-js .collapsible-block { display: block }';" |
| 45 " document.head.appendChild(s);" |
| 46 "})()"; |
| 29 } // namespace | 47 } // namespace |
| 30 | 48 |
| 31 namespace reading_list { | 49 namespace reading_list { |
| 32 | 50 |
| 33 ReadingListDistillerPageDelegate::ReadingListDistillerPageDelegate() {} | 51 ReadingListDistillerPageDelegate::ReadingListDistillerPageDelegate() {} |
| 34 ReadingListDistillerPageDelegate::~ReadingListDistillerPageDelegate() {} | 52 ReadingListDistillerPageDelegate::~ReadingListDistillerPageDelegate() {} |
| 35 | 53 |
| 36 ReadingListDistillerPage::ReadingListDistillerPage( | 54 ReadingListDistillerPage::ReadingListDistillerPage( |
| 37 web::BrowserState* browser_state, | 55 web::BrowserState* browser_state, |
| 38 FaviconWebStateDispatcher* web_state_dispatcher, | 56 FaviconWebStateDispatcher* web_state_dispatcher, |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 | 150 |
| 133 void ReadingListDistillerPage::DelayedOnLoadURLDone(int delayed_task_id) { | 151 void ReadingListDistillerPage::DelayedOnLoadURLDone(int delayed_task_id) { |
| 134 if (!CurrentWebState() || delayed_task_id != delayed_task_id_) { | 152 if (!CurrentWebState() || delayed_task_id != delayed_task_id_) { |
| 135 // Something interrupted the distillation. | 153 // Something interrupted the distillation. |
| 136 // Abort here. | 154 // Abort here. |
| 137 return; | 155 return; |
| 138 } | 156 } |
| 139 if (IsGoogleCachedAMPPage()) { | 157 if (IsGoogleCachedAMPPage()) { |
| 140 // Workaround for Google AMP pages. | 158 // Workaround for Google AMP pages. |
| 141 HandleGoogleCachedAMPPage(); | 159 HandleGoogleCachedAMPPage(); |
| 142 } else { | 160 return; |
| 143 ContinuePageDistillation(); | |
| 144 } | 161 } |
| 162 if (IsWikipediaPage()) { |
| 163 // Workaround for Wikipedia pages. |
| 164 // TODO(crbug.com/647667): remove workaround once DOM distiller handle this |
| 165 // case. |
| 166 HandleWikipediaPage(); |
| 167 return; |
| 168 } |
| 169 ContinuePageDistillation(); |
| 145 } | 170 } |
| 146 | 171 |
| 147 void ReadingListDistillerPage::ContinuePageDistillation() { | 172 void ReadingListDistillerPage::ContinuePageDistillation() { |
| 148 // The page is ready to be distilled. | 173 // The page is ready to be distilled. |
| 149 // If the visible URL is not the original URL, notify the caller that URL | 174 // If the visible URL is not the original URL, notify the caller that URL |
| 150 // changed. | 175 // changed. |
| 151 GURL redirected_url = CurrentWebState()->GetVisibleURL(); | 176 GURL redirected_url = CurrentWebState()->GetVisibleURL(); |
| 152 if (redirected_url != original_url_ && delegate_) { | 177 if (redirected_url != original_url_ && delegate_) { |
| 153 delegate_->DistilledPageRedirectedToURL(original_url_, redirected_url); | 178 delegate_->DistilledPageRedirectedToURL(original_url_, redirected_url); |
| 154 } | 179 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 } | 245 } |
| 221 GURL new_gurl = net::GURLWithNSURL(new_url); | 246 GURL new_gurl = net::GURLWithNSURL(new_url); |
| 222 if (!new_gurl.is_valid()) { | 247 if (!new_gurl.is_valid()) { |
| 223 return false; | 248 return false; |
| 224 } | 249 } |
| 225 web::NavigationManager::WebLoadParams params(new_gurl); | 250 web::NavigationManager::WebLoadParams params(new_gurl); |
| 226 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); | 251 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); |
| 227 return true; | 252 return true; |
| 228 } | 253 } |
| 229 | 254 |
| 255 bool ReadingListDistillerPage::IsWikipediaPage() { |
| 256 // All wikipedia pages are in the form "https://xxx.m.wikipedia.org/..." |
| 257 const GURL& url = CurrentWebState()->GetLastCommittedURL(); |
| 258 if (!url.is_valid() || !url.SchemeIs(url::kHttpsScheme)) { |
| 259 return false; |
| 260 } |
| 261 return (base::EndsWith(url.host(), ".m.wikipedia.org", |
| 262 base::CompareCase::SENSITIVE)); |
| 263 } |
| 264 |
| 265 void ReadingListDistillerPage::HandleWikipediaPage() { |
| 266 base::WeakPtr<ReadingListDistillerPage> weak_this = |
| 267 weak_ptr_factory_.GetWeakPtr(); |
| 268 [CurrentWebState()->GetJSInjectionReceiver() |
| 269 executeJavaScript:@(kWikipediaWorkaround) |
| 270 completionHandler:^(id result, NSError* error) { |
| 271 if (weak_this) { |
| 272 weak_this->ContinuePageDistillation(); |
| 273 } |
| 274 }]; |
| 275 } |
| 276 |
| 230 } // namespace reading_list | 277 } // namespace reading_list |
| OLD | NEW |