| 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" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 9 #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" |
| 10 #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" |
| 11 #import "ios/web/public/navigation_item.h" | 14 #import "ios/web/public/navigation_item.h" |
| 12 #import "ios/web/public/navigation_manager.h" | 15 #import "ios/web/public/navigation_manager.h" |
| 13 #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" |
| 14 #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" |
| 15 #include "net/cert/cert_status_flags.h" | 20 #include "net/cert/cert_status_flags.h" |
| 21 #include "url/url_constants.h" |
| 16 | 22 |
| 17 namespace { | 23 namespace { |
| 18 // The delay between the page load and the distillation in seconds. | 24 // The delay between the page load and the distillation in seconds. |
| 19 const int64_t kDistillationDelayInSeconds = 2; | 25 const int64_t kDistillationDelayInSeconds = 2; |
| 26 const char* kGetIframeURLJavaScript = |
| 27 "document.getElementsByTagName('iframe')[0].src;"; |
| 20 } // namespace | 28 } // namespace |
| 21 | 29 |
| 22 namespace reading_list { | 30 namespace reading_list { |
| 23 | 31 |
| 24 ReadingListDistillerPage::ReadingListDistillerPage( | 32 ReadingListDistillerPage::ReadingListDistillerPage( |
| 25 web::BrowserState* browser_state, | 33 web::BrowserState* browser_state, |
| 26 FaviconWebStateDispatcher* web_state_dispatcher) | 34 FaviconWebStateDispatcher* web_state_dispatcher) |
| 27 : dom_distiller::DistillerPageIOS(browser_state), | 35 : dom_distiller::DistillerPageIOS(browser_state), |
| 28 web_state_dispatcher_(web_state_dispatcher), | 36 web_state_dispatcher_(web_state_dispatcher), |
| 29 weak_ptr_factory_(this) {} | 37 weak_ptr_factory_(this) {} |
| (...skipping 22 matching lines...) Expand all Loading... |
| 52 const base::Value* value) { | 60 const base::Value* value) { |
| 53 std::unique_ptr<web::WebState> old_web_state = DetachWebState(); | 61 std::unique_ptr<web::WebState> old_web_state = DetachWebState(); |
| 54 if (old_web_state) { | 62 if (old_web_state) { |
| 55 web_state_dispatcher_->ReturnWebState(std::move(old_web_state)); | 63 web_state_dispatcher_->ReturnWebState(std::move(old_web_state)); |
| 56 } | 64 } |
| 57 DistillerPageIOS::OnDistillationDone(page_url, value); | 65 DistillerPageIOS::OnDistillationDone(page_url, value); |
| 58 } | 66 } |
| 59 | 67 |
| 60 bool ReadingListDistillerPage::IsLoadingSuccess( | 68 bool ReadingListDistillerPage::IsLoadingSuccess( |
| 61 web::PageLoadCompletionStatus load_completion_status) { | 69 web::PageLoadCompletionStatus load_completion_status) { |
| 62 if (load_completion_status == web::PageLoadCompletionStatus::FAILURE) { | 70 if (load_completion_status != web::PageLoadCompletionStatus::SUCCESS) { |
| 63 return false; | 71 return false; |
| 64 } | 72 } |
| 65 if (!CurrentWebState() || !CurrentWebState()->GetNavigationManager() || | 73 if (!CurrentWebState() || !CurrentWebState()->GetNavigationManager() || |
| 66 !CurrentWebState()->GetNavigationManager()->GetLastCommittedItem()) { | 74 !CurrentWebState()->GetNavigationManager()->GetLastCommittedItem()) { |
| 67 // Only distill fully loaded, committed pages. If the page was not fully | 75 // Only distill fully loaded, committed pages. If the page was not fully |
| 68 // loaded, web::PageLoadCompletionStatus::FAILURE should have been passed to | 76 // loaded, web::PageLoadCompletionStatus::FAILURE should have been passed to |
| 69 // OnLoadURLDone. But check that the item exist before using it anyway. | 77 // OnLoadURLDone. But check that the item exist before using it anyway. |
| 70 return false; | 78 return false; |
| 71 } | 79 } |
| 72 web::NavigationItem* item = | 80 web::NavigationItem* item = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 84 } | 92 } |
| 85 return true; | 93 return true; |
| 86 } | 94 } |
| 87 | 95 |
| 88 void ReadingListDistillerPage::OnLoadURLDone( | 96 void ReadingListDistillerPage::OnLoadURLDone( |
| 89 web::PageLoadCompletionStatus load_completion_status) { | 97 web::PageLoadCompletionStatus load_completion_status) { |
| 90 if (!IsLoadingSuccess(load_completion_status)) { | 98 if (!IsLoadingSuccess(load_completion_status)) { |
| 91 DistillerPageIOS::OnLoadURLDone(load_completion_status); | 99 DistillerPageIOS::OnLoadURLDone(load_completion_status); |
| 92 return; | 100 return; |
| 93 } | 101 } |
| 102 if (IsGoogleCachedAMPPage()) { |
| 103 // Workaround for Google AMP pages. |
| 104 HandleGoogleCachedAMPPage(); |
| 105 } else { |
| 106 WaitForPageLoadCompletion(); |
| 107 } |
| 108 } |
| 109 |
| 110 void ReadingListDistillerPage::WaitForPageLoadCompletion() { |
| 94 base::WeakPtr<ReadingListDistillerPage> weak_this = | 111 base::WeakPtr<ReadingListDistillerPage> weak_this = |
| 95 weak_ptr_factory_.GetWeakPtr(); | 112 weak_ptr_factory_.GetWeakPtr(); |
| 96 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 113 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 97 FROM_HERE, base::Bind(&ReadingListDistillerPage::DelayedOnLoadURLDone, | 114 FROM_HERE, |
| 98 weak_this, load_completion_status), | 115 base::Bind(&ReadingListDistillerPage::DelayedOnLoadURLDone, weak_this), |
| 99 base::TimeDelta::FromSeconds(kDistillationDelayInSeconds)); | 116 base::TimeDelta::FromSeconds(kDistillationDelayInSeconds)); |
| 100 } | 117 } |
| 101 | 118 |
| 102 void ReadingListDistillerPage::DelayedOnLoadURLDone( | 119 void ReadingListDistillerPage::DelayedOnLoadURLDone() { |
| 103 web::PageLoadCompletionStatus load_completion_status) { | 120 DistillerPageIOS::OnLoadURLDone(web::PageLoadCompletionStatus::SUCCESS); |
| 104 DistillerPageIOS::OnLoadURLDone(load_completion_status); | |
| 105 } | 121 } |
| 122 |
| 123 bool ReadingListDistillerPage::IsGoogleCachedAMPPage() { |
| 124 // All google AMP pages have URL in the form "https://google_domain/amp/..." |
| 125 // and a valid certificate. |
| 126 // This method checks that this is strictly the case. |
| 127 const GURL& url = CurrentWebState()->GetLastCommittedURL(); |
| 128 if (!url.is_valid() || !url.SchemeIs(url::kHttpsScheme)) { |
| 129 return false; |
| 130 } |
| 131 if (!google_util::IsGoogleDomainUrl( |
| 132 url, google_util::DISALLOW_SUBDOMAIN, |
| 133 google_util::DISALLOW_NON_STANDARD_PORTS) || |
| 134 !url.path().compare(0, 4, "amp/")) { |
| 135 return false; |
| 136 } |
| 137 const web::SSLStatus& ssl_status = CurrentWebState() |
| 138 ->GetNavigationManager() |
| 139 ->GetLastCommittedItem() |
| 140 ->GetSSL(); |
| 141 if (!ssl_status.certificate || |
| 142 (net::IsCertStatusError(ssl_status.cert_status) && |
| 143 !net::IsCertStatusMinorError(ssl_status.cert_status))) { |
| 144 return false; |
| 145 } |
| 146 |
| 147 return true; |
| 148 } |
| 149 |
| 150 void ReadingListDistillerPage::HandleGoogleCachedAMPPage() { |
| 151 base::WeakPtr<ReadingListDistillerPage> weak_this = |
| 152 weak_ptr_factory_.GetWeakPtr(); |
| 153 [CurrentWebState()->GetJSInjectionReceiver() |
| 154 executeJavaScript:@(kGetIframeURLJavaScript) |
| 155 completionHandler:^(id result, NSError* error) { |
| 156 if (weak_this && |
| 157 !weak_this->HandleGoogleCachedAMPPageJavaScriptResult(result, |
| 158 error)) { |
| 159 // If there is an error on navigation, continue normal distillation. |
| 160 weak_this->WaitForPageLoadCompletion(); |
| 161 } |
| 162 // If there is no error, the navigation completion will trigger a new |
| 163 // |OnLoadURLDone| call that will resume the distillation. |
| 164 }]; |
| 165 } |
| 166 |
| 167 bool ReadingListDistillerPage::HandleGoogleCachedAMPPageJavaScriptResult( |
| 168 id result, |
| 169 NSError* error) { |
| 170 if (error) { |
| 171 return false; |
| 172 } |
| 173 NSString* result_string = base::mac::ObjCCast<NSString>(result); |
| 174 NSURL* new_url = [NSURL URLWithString:result_string]; |
| 175 if (!new_url) { |
| 176 return false; |
| 177 } |
| 178 bool is_cdn_ampproject = |
| 179 [[new_url host] isEqualToString:@"cdn.ampproject.org"]; |
| 180 bool is_cdn_ampproject_subdomain = |
| 181 [[new_url host] hasSuffix:@".cdn.ampproject.org"]; |
| 182 |
| 183 if (!is_cdn_ampproject && !is_cdn_ampproject_subdomain) { |
| 184 return false; |
| 185 } |
| 186 GURL new_gurl = net::GURLWithNSURL(new_url); |
| 187 if (!new_gurl.is_valid()) { |
| 188 return false; |
| 189 } |
| 190 web::NavigationManager::WebLoadParams params(new_gurl); |
| 191 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); |
| 192 return true; |
| 193 } |
| 194 |
| 106 } // namespace reading_list | 195 } // namespace reading_list |
| OLD | NEW |