| 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/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "components/favicon/ios/web_favicon_driver.h" | 11 #include "components/favicon/ios/web_favicon_driver.h" |
| 12 #include "components/google/core/browser/google_util.h" | 12 #include "components/google/core/browser/google_util.h" |
| 13 #include "ios/chrome/browser/reading_list/favicon_web_state_dispatcher_impl.h" | 13 #include "ios/chrome/browser/reading_list/favicon_web_state_dispatcher_impl.h" |
| 14 #import "ios/web/public/navigation_item.h" | 14 #import "ios/web/public/navigation_item.h" |
| 15 #import "ios/web/public/navigation_manager.h" | 15 #import "ios/web/public/navigation_manager.h" |
| 16 #include "ios/web/public/ssl_status.h" | 16 #include "ios/web/public/ssl_status.h" |
| 17 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 17 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 18 #import "ios/web/public/web_state/web_state.h" | 18 #import "ios/web/public/web_state/web_state.h" |
| 19 #import "net/base/mac/url_conversions.h" | 19 #import "net/base/mac/url_conversions.h" |
| 20 #include "net/cert/cert_status_flags.h" | 20 #include "net/cert/cert_status_flags.h" |
| 21 #include "url/url_constants.h" | 21 #include "url/url_constants.h" |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 // The delay between the page load and the distillation in seconds. | 24 // The delay given to the web page to render after the PageLoaded callback. |
| 25 const int64_t kDistillationDelayInSeconds = 2; | 25 const int64_t kPageLoadDelayInSeconds = 2; |
| 26 |
| 26 const char* kGetIframeURLJavaScript = | 27 const char* kGetIframeURLJavaScript = |
| 27 "document.getElementsByTagName('iframe')[0].src;"; | 28 "document.getElementsByTagName('iframe')[0].src;"; |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 namespace reading_list { | 31 namespace reading_list { |
| 31 | 32 |
| 32 ReadingListDistillerPage::ReadingListDistillerPage( | 33 ReadingListDistillerPage::ReadingListDistillerPage( |
| 33 web::BrowserState* browser_state, | 34 web::BrowserState* browser_state, |
| 34 FaviconWebStateDispatcher* web_state_dispatcher) | 35 FaviconWebStateDispatcher* web_state_dispatcher) |
| 35 : dom_distiller::DistillerPageIOS(browser_state), | 36 : dom_distiller::DistillerPageIOS(browser_state), |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 } | 99 } |
| 99 return true; | 100 return true; |
| 100 } | 101 } |
| 101 | 102 |
| 102 void ReadingListDistillerPage::OnLoadURLDone( | 103 void ReadingListDistillerPage::OnLoadURLDone( |
| 103 web::PageLoadCompletionStatus load_completion_status) { | 104 web::PageLoadCompletionStatus load_completion_status) { |
| 104 if (!IsLoadingSuccess(load_completion_status)) { | 105 if (!IsLoadingSuccess(load_completion_status)) { |
| 105 DistillerPageIOS::OnLoadURLDone(load_completion_status); | 106 DistillerPageIOS::OnLoadURLDone(load_completion_status); |
| 106 return; | 107 return; |
| 107 } | 108 } |
| 108 if (IsGoogleCachedAMPPage()) { | 109 // Page is loaded but rendering may not be done yet. Give a delay to the page. |
| 109 // Workaround for Google AMP pages. | |
| 110 HandleGoogleCachedAMPPage(); | |
| 111 } else { | |
| 112 WaitForPageLoadCompletion(); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void ReadingListDistillerPage::WaitForPageLoadCompletion() { | |
| 117 base::WeakPtr<ReadingListDistillerPage> weak_this = | 110 base::WeakPtr<ReadingListDistillerPage> weak_this = |
| 118 weak_ptr_factory_.GetWeakPtr(); | 111 weak_ptr_factory_.GetWeakPtr(); |
| 119 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 112 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 120 FROM_HERE, | 113 FROM_HERE, |
| 121 base::Bind(&ReadingListDistillerPage::DelayedOnLoadURLDone, weak_this), | 114 base::Bind(&ReadingListDistillerPage::DelayedOnLoadURLDone, weak_this), |
| 122 base::TimeDelta::FromSeconds(kDistillationDelayInSeconds)); | 115 base::TimeDelta::FromSeconds(kPageLoadDelayInSeconds)); |
| 123 } | 116 } |
| 124 | 117 |
| 125 void ReadingListDistillerPage::DelayedOnLoadURLDone() { | 118 void ReadingListDistillerPage::DelayedOnLoadURLDone() { |
| 119 if (IsGoogleCachedAMPPage()) { |
| 120 // Workaround for Google AMP pages. |
| 121 HandleGoogleCachedAMPPage(); |
| 122 } else { |
| 123 ContinuePageDistillation(); |
| 124 } |
| 125 } |
| 126 |
| 127 void ReadingListDistillerPage::ContinuePageDistillation() { |
| 126 // The page is ready to be distilled. | 128 // The page is ready to be distilled. |
| 127 // If the visible URL is not the original URL, notify the caller that URL | 129 // If the visible URL is not the original URL, notify the caller that URL |
| 128 // changed. | 130 // changed. |
| 129 GURL redirected_url = CurrentWebState()->GetVisibleURL(); | 131 GURL redirected_url = CurrentWebState()->GetVisibleURL(); |
| 130 if (redirected_url != original_url_ && !redirection_callback_.is_null()) { | 132 if (redirected_url != original_url_ && !redirection_callback_.is_null()) { |
| 131 redirection_callback_.Run(original_url_, redirected_url); | 133 redirection_callback_.Run(original_url_, redirected_url); |
| 132 } | 134 } |
| 133 DistillerPageIOS::OnLoadURLDone(web::PageLoadCompletionStatus::SUCCESS); | 135 DistillerPageIOS::OnLoadURLDone(web::PageLoadCompletionStatus::SUCCESS); |
| 134 } | 136 } |
| 135 | 137 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 163 void ReadingListDistillerPage::HandleGoogleCachedAMPPage() { | 165 void ReadingListDistillerPage::HandleGoogleCachedAMPPage() { |
| 164 base::WeakPtr<ReadingListDistillerPage> weak_this = | 166 base::WeakPtr<ReadingListDistillerPage> weak_this = |
| 165 weak_ptr_factory_.GetWeakPtr(); | 167 weak_ptr_factory_.GetWeakPtr(); |
| 166 [CurrentWebState()->GetJSInjectionReceiver() | 168 [CurrentWebState()->GetJSInjectionReceiver() |
| 167 executeJavaScript:@(kGetIframeURLJavaScript) | 169 executeJavaScript:@(kGetIframeURLJavaScript) |
| 168 completionHandler:^(id result, NSError* error) { | 170 completionHandler:^(id result, NSError* error) { |
| 169 if (weak_this && | 171 if (weak_this && |
| 170 !weak_this->HandleGoogleCachedAMPPageJavaScriptResult(result, | 172 !weak_this->HandleGoogleCachedAMPPageJavaScriptResult(result, |
| 171 error)) { | 173 error)) { |
| 172 // If there is an error on navigation, continue normal distillation. | 174 // If there is an error on navigation, continue normal distillation. |
| 173 weak_this->WaitForPageLoadCompletion(); | 175 weak_this->ContinuePageDistillation(); |
| 174 } | 176 } |
| 175 // If there is no error, the navigation completion will trigger a new | 177 // If there is no error, the navigation completion will trigger a new |
| 176 // |OnLoadURLDone| call that will resume the distillation. | 178 // |OnLoadURLDone| call that will resume the distillation. |
| 177 }]; | 179 }]; |
| 178 } | 180 } |
| 179 | 181 |
| 180 bool ReadingListDistillerPage::HandleGoogleCachedAMPPageJavaScriptResult( | 182 bool ReadingListDistillerPage::HandleGoogleCachedAMPPageJavaScriptResult( |
| 181 id result, | 183 id result, |
| 182 id error) { | 184 id error) { |
| 183 if (error) { | 185 if (error) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 199 GURL new_gurl = net::GURLWithNSURL(new_url); | 201 GURL new_gurl = net::GURLWithNSURL(new_url); |
| 200 if (!new_gurl.is_valid()) { | 202 if (!new_gurl.is_valid()) { |
| 201 return false; | 203 return false; |
| 202 } | 204 } |
| 203 web::NavigationManager::WebLoadParams params(new_gurl); | 205 web::NavigationManager::WebLoadParams params(new_gurl); |
| 204 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); | 206 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); |
| 205 return true; | 207 return true; |
| 206 } | 208 } |
| 207 | 209 |
| 208 } // namespace reading_list | 210 } // namespace reading_list |
| OLD | NEW |