| 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" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 ReadingListDistillerPage::ReadingListDistillerPage( | 32 ReadingListDistillerPage::ReadingListDistillerPage( |
| 33 web::BrowserState* browser_state, | 33 web::BrowserState* browser_state, |
| 34 FaviconWebStateDispatcher* web_state_dispatcher) | 34 FaviconWebStateDispatcher* web_state_dispatcher) |
| 35 : dom_distiller::DistillerPageIOS(browser_state), | 35 : dom_distiller::DistillerPageIOS(browser_state), |
| 36 web_state_dispatcher_(web_state_dispatcher), | 36 web_state_dispatcher_(web_state_dispatcher), |
| 37 weak_ptr_factory_(this) {} | 37 weak_ptr_factory_(this) {} |
| 38 | 38 |
| 39 ReadingListDistillerPage::~ReadingListDistillerPage() {} | 39 ReadingListDistillerPage::~ReadingListDistillerPage() {} |
| 40 | 40 |
| 41 void ReadingListDistillerPage::SetRedirectionCallback( |
| 42 RedirectionCallback redirection_callback) { |
| 43 redirection_callback_ = redirection_callback; |
| 44 } |
| 45 |
| 41 void ReadingListDistillerPage::DistillPageImpl(const GURL& url, | 46 void ReadingListDistillerPage::DistillPageImpl(const GURL& url, |
| 42 const std::string& script) { | 47 const std::string& script) { |
| 43 std::unique_ptr<web::WebState> old_web_state = DetachWebState(); | 48 std::unique_ptr<web::WebState> old_web_state = DetachWebState(); |
| 44 if (old_web_state) { | 49 if (old_web_state) { |
| 45 web_state_dispatcher_->ReturnWebState(std::move(old_web_state)); | 50 web_state_dispatcher_->ReturnWebState(std::move(old_web_state)); |
| 46 } | 51 } |
| 47 std::unique_ptr<web::WebState> new_web_state = | 52 std::unique_ptr<web::WebState> new_web_state = |
| 48 web_state_dispatcher_->RequestWebState(); | 53 web_state_dispatcher_->RequestWebState(); |
| 49 if (new_web_state) { | 54 if (new_web_state) { |
| 50 favicon::WebFaviconDriver* favicon_driver = | 55 favicon::WebFaviconDriver* favicon_driver = |
| 51 favicon::WebFaviconDriver::FromWebState(new_web_state.get()); | 56 favicon::WebFaviconDriver::FromWebState(new_web_state.get()); |
| 52 favicon_driver->FetchFavicon(url); | 57 favicon_driver->FetchFavicon(url); |
| 53 } | 58 } |
| 54 AttachWebState(std::move(new_web_state)); | 59 AttachWebState(std::move(new_web_state)); |
| 60 original_url_ = url; |
| 55 | 61 |
| 56 DistillerPageIOS::DistillPageImpl(url, script); | 62 DistillerPageIOS::DistillPageImpl(url, script); |
| 57 } | 63 } |
| 58 | 64 |
| 59 void ReadingListDistillerPage::OnDistillationDone(const GURL& page_url, | 65 void ReadingListDistillerPage::OnDistillationDone(const GURL& page_url, |
| 60 const base::Value* value) { | 66 const base::Value* value) { |
| 61 std::unique_ptr<web::WebState> old_web_state = DetachWebState(); | 67 std::unique_ptr<web::WebState> old_web_state = DetachWebState(); |
| 62 if (old_web_state) { | 68 if (old_web_state) { |
| 63 web_state_dispatcher_->ReturnWebState(std::move(old_web_state)); | 69 web_state_dispatcher_->ReturnWebState(std::move(old_web_state)); |
| 64 } | 70 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 void ReadingListDistillerPage::WaitForPageLoadCompletion() { | 116 void ReadingListDistillerPage::WaitForPageLoadCompletion() { |
| 111 base::WeakPtr<ReadingListDistillerPage> weak_this = | 117 base::WeakPtr<ReadingListDistillerPage> weak_this = |
| 112 weak_ptr_factory_.GetWeakPtr(); | 118 weak_ptr_factory_.GetWeakPtr(); |
| 113 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 119 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 114 FROM_HERE, | 120 FROM_HERE, |
| 115 base::Bind(&ReadingListDistillerPage::DelayedOnLoadURLDone, weak_this), | 121 base::Bind(&ReadingListDistillerPage::DelayedOnLoadURLDone, weak_this), |
| 116 base::TimeDelta::FromSeconds(kDistillationDelayInSeconds)); | 122 base::TimeDelta::FromSeconds(kDistillationDelayInSeconds)); |
| 117 } | 123 } |
| 118 | 124 |
| 119 void ReadingListDistillerPage::DelayedOnLoadURLDone() { | 125 void ReadingListDistillerPage::DelayedOnLoadURLDone() { |
| 126 // The page is ready to be distilled. |
| 127 // If the visible URL is not the original URL, notify the caller that URL |
| 128 // changed. |
| 129 GURL redirected_url = CurrentWebState()->GetVisibleURL(); |
| 130 if (redirected_url != original_url_ && !redirection_callback_.is_null()) { |
| 131 redirection_callback_.Run(original_url_, redirected_url); |
| 132 } |
| 120 DistillerPageIOS::OnLoadURLDone(web::PageLoadCompletionStatus::SUCCESS); | 133 DistillerPageIOS::OnLoadURLDone(web::PageLoadCompletionStatus::SUCCESS); |
| 121 } | 134 } |
| 122 | 135 |
| 123 bool ReadingListDistillerPage::IsGoogleCachedAMPPage() { | 136 bool ReadingListDistillerPage::IsGoogleCachedAMPPage() { |
| 124 // All google AMP pages have URL in the form "https://google_domain/amp/..." | 137 // All google AMP pages have URL in the form "https://google_domain/amp/..." |
| 125 // and a valid certificate. | 138 // and a valid certificate. |
| 126 // This method checks that this is strictly the case. | 139 // This method checks that this is strictly the case. |
| 127 const GURL& url = CurrentWebState()->GetLastCommittedURL(); | 140 const GURL& url = CurrentWebState()->GetLastCommittedURL(); |
| 128 if (!url.is_valid() || !url.SchemeIs(url::kHttpsScheme)) { | 141 if (!url.is_valid() || !url.SchemeIs(url::kHttpsScheme)) { |
| 129 return false; | 142 return false; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 159 // If there is an error on navigation, continue normal distillation. | 172 // If there is an error on navigation, continue normal distillation. |
| 160 weak_this->WaitForPageLoadCompletion(); | 173 weak_this->WaitForPageLoadCompletion(); |
| 161 } | 174 } |
| 162 // If there is no error, the navigation completion will trigger a new | 175 // If there is no error, the navigation completion will trigger a new |
| 163 // |OnLoadURLDone| call that will resume the distillation. | 176 // |OnLoadURLDone| call that will resume the distillation. |
| 164 }]; | 177 }]; |
| 165 } | 178 } |
| 166 | 179 |
| 167 bool ReadingListDistillerPage::HandleGoogleCachedAMPPageJavaScriptResult( | 180 bool ReadingListDistillerPage::HandleGoogleCachedAMPPageJavaScriptResult( |
| 168 id result, | 181 id result, |
| 169 NSError* error) { | 182 id error) { |
| 170 if (error) { | 183 if (error) { |
| 171 return false; | 184 return false; |
| 172 } | 185 } |
| 173 NSString* result_string = base::mac::ObjCCast<NSString>(result); | 186 NSString* result_string = base::mac::ObjCCast<NSString>(result); |
| 174 NSURL* new_url = [NSURL URLWithString:result_string]; | 187 NSURL* new_url = [NSURL URLWithString:result_string]; |
| 175 if (!new_url) { | 188 if (!new_url) { |
| 176 return false; | 189 return false; |
| 177 } | 190 } |
| 178 bool is_cdn_ampproject = | 191 bool is_cdn_ampproject = |
| 179 [[new_url host] isEqualToString:@"cdn.ampproject.org"]; | 192 [[new_url host] isEqualToString:@"cdn.ampproject.org"]; |
| 180 bool is_cdn_ampproject_subdomain = | 193 bool is_cdn_ampproject_subdomain = |
| 181 [[new_url host] hasSuffix:@".cdn.ampproject.org"]; | 194 [[new_url host] hasSuffix:@".cdn.ampproject.org"]; |
| 182 | 195 |
| 183 if (!is_cdn_ampproject && !is_cdn_ampproject_subdomain) { | 196 if (!is_cdn_ampproject && !is_cdn_ampproject_subdomain) { |
| 184 return false; | 197 return false; |
| 185 } | 198 } |
| 186 GURL new_gurl = net::GURLWithNSURL(new_url); | 199 GURL new_gurl = net::GURLWithNSURL(new_url); |
| 187 if (!new_gurl.is_valid()) { | 200 if (!new_gurl.is_valid()) { |
| 188 return false; | 201 return false; |
| 189 } | 202 } |
| 190 web::NavigationManager::WebLoadParams params(new_gurl); | 203 web::NavigationManager::WebLoadParams params(new_gurl); |
| 191 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); | 204 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); |
| 192 return true; | 205 return true; |
| 193 } | 206 } |
| 194 | 207 |
| 195 } // namespace reading_list | 208 } // namespace reading_list |
| OLD | NEW |