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 22 matching lines...) Expand all Loading... |
33 ReadingListDistillerPageDelegate::ReadingListDistillerPageDelegate() {} | 33 ReadingListDistillerPageDelegate::ReadingListDistillerPageDelegate() {} |
34 ReadingListDistillerPageDelegate::~ReadingListDistillerPageDelegate() {} | 34 ReadingListDistillerPageDelegate::~ReadingListDistillerPageDelegate() {} |
35 | 35 |
36 ReadingListDistillerPage::ReadingListDistillerPage( | 36 ReadingListDistillerPage::ReadingListDistillerPage( |
37 web::BrowserState* browser_state, | 37 web::BrowserState* browser_state, |
38 FaviconWebStateDispatcher* web_state_dispatcher, | 38 FaviconWebStateDispatcher* web_state_dispatcher, |
39 ReadingListDistillerPageDelegate* delegate) | 39 ReadingListDistillerPageDelegate* delegate) |
40 : dom_distiller::DistillerPageIOS(browser_state), | 40 : dom_distiller::DistillerPageIOS(browser_state), |
41 web_state_dispatcher_(web_state_dispatcher), | 41 web_state_dispatcher_(web_state_dispatcher), |
42 delegate_(delegate), | 42 delegate_(delegate), |
| 43 delayed_task_id_(0), |
43 weak_ptr_factory_(this) { | 44 weak_ptr_factory_(this) { |
44 DCHECK(delegate); | 45 DCHECK(delegate); |
45 } | 46 } |
46 | 47 |
47 ReadingListDistillerPage::~ReadingListDistillerPage() {} | 48 ReadingListDistillerPage::~ReadingListDistillerPage() {} |
48 | 49 |
49 void ReadingListDistillerPage::DistillPageImpl(const GURL& url, | 50 void ReadingListDistillerPage::DistillPageImpl(const GURL& url, |
50 const std::string& script) { | 51 const std::string& script) { |
51 std::unique_ptr<web::WebState> old_web_state = DetachWebState(); | 52 std::unique_ptr<web::WebState> old_web_state = DetachWebState(); |
52 if (old_web_state) { | 53 if (old_web_state) { |
53 web_state_dispatcher_->ReturnWebState(std::move(old_web_state)); | 54 web_state_dispatcher_->ReturnWebState(std::move(old_web_state)); |
54 } | 55 } |
55 std::unique_ptr<web::WebState> new_web_state = | 56 std::unique_ptr<web::WebState> new_web_state = |
56 web_state_dispatcher_->RequestWebState(); | 57 web_state_dispatcher_->RequestWebState(); |
57 if (new_web_state) { | 58 if (new_web_state) { |
58 favicon::WebFaviconDriver* favicon_driver = | 59 favicon::WebFaviconDriver* favicon_driver = |
59 favicon::WebFaviconDriver::FromWebState(new_web_state.get()); | 60 favicon::WebFaviconDriver::FromWebState(new_web_state.get()); |
60 favicon_driver->FetchFavicon(url); | 61 favicon_driver->FetchFavicon(url); |
61 } | 62 } |
62 AttachWebState(std::move(new_web_state)); | 63 AttachWebState(std::move(new_web_state)); |
63 original_url_ = url; | 64 original_url_ = url; |
| 65 delayed_task_id_++; |
64 | 66 |
65 DistillerPageIOS::DistillPageImpl(url, script); | 67 DistillerPageIOS::DistillPageImpl(url, script); |
66 } | 68 } |
67 | 69 |
68 void ReadingListDistillerPage::OnDistillationDone(const GURL& page_url, | 70 void ReadingListDistillerPage::OnDistillationDone(const GURL& page_url, |
69 const base::Value* value) { | 71 const base::Value* value) { |
70 std::unique_ptr<web::WebState> old_web_state = DetachWebState(); | 72 std::unique_ptr<web::WebState> old_web_state = DetachWebState(); |
71 if (old_web_state) { | 73 if (old_web_state) { |
72 web_state_dispatcher_->ReturnWebState(std::move(old_web_state)); | 74 web_state_dispatcher_->ReturnWebState(std::move(old_web_state)); |
73 } | 75 } |
| 76 delayed_task_id_++; |
74 DistillerPageIOS::OnDistillationDone(page_url, value); | 77 DistillerPageIOS::OnDistillationDone(page_url, value); |
75 } | 78 } |
76 | 79 |
77 bool ReadingListDistillerPage::IsLoadingSuccess( | 80 bool ReadingListDistillerPage::IsLoadingSuccess( |
78 web::PageLoadCompletionStatus load_completion_status) { | 81 web::PageLoadCompletionStatus load_completion_status) { |
79 if (load_completion_status != web::PageLoadCompletionStatus::SUCCESS) { | 82 if (load_completion_status != web::PageLoadCompletionStatus::SUCCESS) { |
80 return false; | 83 return false; |
81 } | 84 } |
82 if (!CurrentWebState() || !CurrentWebState()->GetNavigationManager() || | 85 if (!CurrentWebState() || !CurrentWebState()->GetNavigationManager() || |
83 !CurrentWebState()->GetNavigationManager()->GetLastCommittedItem()) { | 86 !CurrentWebState()->GetNavigationManager()->GetLastCommittedItem()) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 // Call the handler to make sure cleaning methods are called correctly. | 118 // Call the handler to make sure cleaning methods are called correctly. |
116 // There is no need to wait for rendering either. | 119 // There is no need to wait for rendering either. |
117 DistillerPageIOS::OnLoadURLDone(load_completion_status); | 120 DistillerPageIOS::OnLoadURLDone(load_completion_status); |
118 return; | 121 return; |
119 } | 122 } |
120 // Page is loaded but rendering may not be done yet. Give a delay to the page. | 123 // Page is loaded but rendering may not be done yet. Give a delay to the page. |
121 base::WeakPtr<ReadingListDistillerPage> weak_this = | 124 base::WeakPtr<ReadingListDistillerPage> weak_this = |
122 weak_ptr_factory_.GetWeakPtr(); | 125 weak_ptr_factory_.GetWeakPtr(); |
123 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 126 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
124 FROM_HERE, | 127 FROM_HERE, |
125 base::Bind(&ReadingListDistillerPage::DelayedOnLoadURLDone, weak_this), | 128 base::Bind(&ReadingListDistillerPage::DelayedOnLoadURLDone, weak_this, |
| 129 delayed_task_id_), |
126 base::TimeDelta::FromSeconds(kPageLoadDelayInSeconds)); | 130 base::TimeDelta::FromSeconds(kPageLoadDelayInSeconds)); |
127 } | 131 } |
128 | 132 |
129 void ReadingListDistillerPage::DelayedOnLoadURLDone() { | 133 void ReadingListDistillerPage::DelayedOnLoadURLDone(int delayed_task_id) { |
| 134 if (!CurrentWebState() || delayed_task_id != delayed_task_id_) { |
| 135 // Something interrupted the distillation. |
| 136 // Abort here. |
| 137 return; |
| 138 } |
130 if (IsGoogleCachedAMPPage()) { | 139 if (IsGoogleCachedAMPPage()) { |
131 // Workaround for Google AMP pages. | 140 // Workaround for Google AMP pages. |
132 HandleGoogleCachedAMPPage(); | 141 HandleGoogleCachedAMPPage(); |
133 } else { | 142 } else { |
134 ContinuePageDistillation(); | 143 ContinuePageDistillation(); |
135 } | 144 } |
136 } | 145 } |
137 | 146 |
138 void ReadingListDistillerPage::ContinuePageDistillation() { | 147 void ReadingListDistillerPage::ContinuePageDistillation() { |
139 // The page is ready to be distilled. | 148 // The page is ready to be distilled. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 GURL new_gurl = net::GURLWithNSURL(new_url); | 221 GURL new_gurl = net::GURLWithNSURL(new_url); |
213 if (!new_gurl.is_valid()) { | 222 if (!new_gurl.is_valid()) { |
214 return false; | 223 return false; |
215 } | 224 } |
216 web::NavigationManager::WebLoadParams params(new_gurl); | 225 web::NavigationManager::WebLoadParams params(new_gurl); |
217 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); | 226 CurrentWebState()->GetNavigationManager()->LoadURLWithParams(params); |
218 return true; | 227 return true; |
219 } | 228 } |
220 | 229 |
221 } // namespace reading_list | 230 } // namespace reading_list |
OLD | NEW |