Chromium Code Reviews| 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" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 32 " var link = document.evaluate('//link[@rel=\"amphtml\"]'," | 33 " var link = document.evaluate('//link[@rel=\"amphtml\"]'," |
| 33 " document," | 34 " document," |
| 34 " null," | 35 " null," |
| 35 " XPathResult.ORDERED_NODE_SNAPSHOT_TYPE," | 36 " XPathResult.ORDERED_NODE_SNAPSHOT_TYPE," |
| 36 " null ).snapshotItem(0);" | 37 " null ).snapshotItem(0);" |
| 37 " if (link !== null) {" | 38 " if (link !== null) {" |
| 38 " return link.getAttribute('href');" | 39 " return link.getAttribute('href');" |
| 39 " }" | 40 " }" |
| 40 " return document.getElementsByTagName('iframe')[0].src;" | 41 " return document.getElementsByTagName('iframe')[0].src;" |
| 41 "})()"; | 42 "})()"; |
| 43 | |
| 44 const char* kWikipediaWorkaround = | |
| 45 "(() => {" | |
| 46 " var s = document.createElement('style');" | |
| 47 " s.innerHTML='.client-js .collapsible-block { display: block }';" | |
| 48 " document.head.appendChild(s);" | |
| 49 "})()"; | |
| 42 } // namespace | 50 } // namespace |
| 43 | 51 |
| 44 namespace reading_list { | 52 namespace reading_list { |
| 45 | 53 |
| 46 ReadingListDistillerPageDelegate::ReadingListDistillerPageDelegate() {} | 54 ReadingListDistillerPageDelegate::ReadingListDistillerPageDelegate() {} |
| 47 ReadingListDistillerPageDelegate::~ReadingListDistillerPageDelegate() {} | 55 ReadingListDistillerPageDelegate::~ReadingListDistillerPageDelegate() {} |
| 48 | 56 |
| 49 ReadingListDistillerPage::ReadingListDistillerPage( | 57 ReadingListDistillerPage::ReadingListDistillerPage( |
| 50 web::BrowserState* browser_state, | 58 web::BrowserState* browser_state, |
| 51 FaviconWebStateDispatcher* web_state_dispatcher, | 59 FaviconWebStateDispatcher* web_state_dispatcher, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 | 176 |
| 169 void ReadingListDistillerPage::DelayedOnLoadURLDone(int delayed_task_id) { | 177 void ReadingListDistillerPage::DelayedOnLoadURLDone(int delayed_task_id) { |
| 170 if (!CurrentWebState() || delayed_task_id != delayed_task_id_) { | 178 if (!CurrentWebState() || delayed_task_id != delayed_task_id_) { |
| 171 // Something interrupted the distillation. | 179 // Something interrupted the distillation. |
| 172 // Abort here. | 180 // Abort here. |
| 173 return; | 181 return; |
| 174 } | 182 } |
| 175 if (IsGoogleCachedAMPPage()) { | 183 if (IsGoogleCachedAMPPage()) { |
| 176 // Workaround for Google AMP pages. | 184 // Workaround for Google AMP pages. |
| 177 HandleGoogleCachedAMPPage(); | 185 HandleGoogleCachedAMPPage(); |
| 178 } else { | 186 return; |
| 179 ContinuePageDistillation(); | |
| 180 } | 187 } |
| 188 if (IsWikipediaPage()) { | |
| 189 // Workaround for Wikipedia pages. | |
| 190 // TODO(crbug.com/647667): remove workaround once DOM distiller handle this | |
| 191 // case. | |
| 192 HandleWikipediaPage(); | |
| 193 return; | |
| 194 } | |
| 195 ContinuePageDistillation(); | |
| 181 } | 196 } |
| 182 | 197 |
| 183 void ReadingListDistillerPage::ContinuePageDistillation() { | 198 void ReadingListDistillerPage::ContinuePageDistillation() { |
| 184 // The page is ready to be distilled. | 199 // The page is ready to be distilled. |
| 185 // If the visible URL is not the original URL, notify the caller that URL | 200 // If the visible URL is not the original URL, notify the caller that URL |
| 186 // changed. | 201 // changed. |
| 187 GURL redirected_url = CurrentWebState()->GetVisibleURL(); | 202 GURL redirected_url = CurrentWebState()->GetVisibleURL(); |
| 188 if (redirected_url != original_url_ && delegate_) { | 203 if (redirected_url != original_url_ && delegate_) { |
| 189 delegate_->DistilledPageRedirectedToURL(original_url_, redirected_url); | 204 delegate_->DistilledPageRedirectedToURL(original_url_, redirected_url); |
| 190 } | 205 } |
| 191 DistillerPageIOS::OnLoadURLDone(web::PageLoadCompletionStatus::SUCCESS); | 206 |
| 207 [CurrentWebState()->GetJSInjectionReceiver() | |
| 208 executeJavaScript:@(kWikipediaWorkaround) | |
| 209 completionHandler:^(id response, id error) { | |
| 210 DistillerPageIOS::OnLoadURLDone(web::PageLoadCompletionStatus::SUCCESS); | |
| 211 }]; | |
|
noyau (Ping after 24h)
2017/03/03 21:52:52
This is leftover from the initial hack you forgot
Olivier
2017/03/04 09:26:23
Done.
| |
| 192 } | 212 } |
| 193 | 213 |
| 194 bool ReadingListDistillerPage::IsGoogleCachedAMPPage() { | 214 bool ReadingListDistillerPage::IsGoogleCachedAMPPage() { |
| 195 // All google AMP pages have URL in the form "https://google_domain/amp/..." | 215 // All google AMP pages have URL in the form "https://google_domain/amp/..." |
| 196 // and a valid certificate. | 216 // and a valid certificate. |
| 197 // This method checks that this is strictly the case. | 217 // This method checks that this is strictly the case. |
| 198 const GURL& url = CurrentWebState()->GetLastCommittedURL(); | 218 const GURL& url = CurrentWebState()->GetLastCommittedURL(); |
| 199 if (!url.is_valid() || !url.SchemeIs(url::kHttpsScheme)) { | 219 if (!url.is_valid() || !url.SchemeIs(url::kHttpsScheme)) { |
| 200 return false; | 220 return false; |
| 201 } | 221 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 GURL new_gurl = net::GURLWithNSURL(new_url); | 270 GURL new_gurl = net::GURLWithNSURL(new_url); |
| 251 if (!new_gurl.is_valid()) { | 271 if (!new_gurl.is_valid()) { |
| 252 return false; | 272 return false; |
| 253 } | 273 } |
| 254 FetchFavicon(new_gurl); | 274 FetchFavicon(new_gurl); |
| 255 web::NavigationManager::WebLoadParams params(new_gurl); | 275 web::NavigationManager::WebLoadParams params(new_gurl); |
| 256 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); | 276 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); |
| 257 return true; | 277 return true; |
| 258 } | 278 } |
| 259 | 279 |
| 280 bool ReadingListDistillerPage::IsWikipediaPage() { | |
| 281 // All wikipedia pages are in the form "https://xxx.wikipedia.org/..." | |
|
noyau (Ping after 24h)
2017/03/03 21:52:52
In the cases that interest us [countrycode].m.wiki
Olivier
2017/03/04 09:26:23
Done.
| |
| 282 const GURL& url = CurrentWebState()->GetLastCommittedURL(); | |
| 283 if (!url.is_valid() || !url.SchemeIs(url::kHttpsScheme)) { | |
| 284 return false; | |
| 285 } | |
| 286 return (base::EndsWith(url.host(), ".wikipedia.org", | |
| 287 base::CompareCase::SENSITIVE)); | |
| 288 } | |
| 289 | |
| 290 void ReadingListDistillerPage::HandleWikipediaPage() { | |
| 291 base::WeakPtr<ReadingListDistillerPage> weak_this = | |
| 292 weak_ptr_factory_.GetWeakPtr(); | |
| 293 [CurrentWebState()->GetJSInjectionReceiver() | |
| 294 executeJavaScript:@(kWikipediaWorkaround) | |
| 295 completionHandler:^(id result, NSError* error) { | |
| 296 if (weak_this) { | |
| 297 weak_this->ContinuePageDistillation(); | |
| 298 } | |
| 299 }]; | |
| 300 } | |
| 301 | |
| 260 } // namespace reading_list | 302 } // namespace reading_list |
| OLD | NEW |