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 "chrome/browser/android/offline_pages/background_loader_offliner.h" | 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h" |
6 | 6 |
7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "base/sys_info.h" | 8 #include "base/sys_info.h" |
9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
10 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 10 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "components/offline_pages/core/background/save_page_request.h" | 12 #include "components/offline_pages/core/background/save_page_request.h" |
13 #include "components/offline_pages/core/client_namespace_constants.h" | 13 #include "components/offline_pages/core/client_namespace_constants.h" |
14 #include "components/offline_pages/core/offline_page_model.h" | 14 #include "components/offline_pages/core/offline_page_model.h" |
15 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
16 #include "content/public/browser/navigation_handle.h" | 16 #include "content/public/browser/navigation_handle.h" |
17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
18 | 18 |
19 namespace offline_pages { | 19 namespace offline_pages { |
20 | 20 |
21 namespace { | |
22 long kOfflinePageDelayMs = 2000; | |
Pete Williamson
2017/02/24 17:31:45
As a follow up change, let's put this into Offline
chili
2017/02/24 21:43:47
Acknowledged.
| |
23 } // namespace | |
24 | |
21 BackgroundLoaderOffliner::BackgroundLoaderOffliner( | 25 BackgroundLoaderOffliner::BackgroundLoaderOffliner( |
22 content::BrowserContext* browser_context, | 26 content::BrowserContext* browser_context, |
23 const OfflinerPolicy* policy, | 27 const OfflinerPolicy* policy, |
24 OfflinePageModel* offline_page_model) | 28 OfflinePageModel* offline_page_model) |
25 : browser_context_(browser_context), | 29 : browser_context_(browser_context), |
26 offline_page_model_(offline_page_model), | 30 offline_page_model_(offline_page_model), |
27 is_low_end_device_(base::SysInfo::IsLowEndDevice()), | 31 is_low_end_device_(base::SysInfo::IsLowEndDevice()), |
28 save_state_(NONE), | 32 save_state_(NONE), |
29 page_load_state_(SUCCESS), | 33 page_load_state_(SUCCESS), |
30 weak_ptr_factory_(this) { | 34 weak_ptr_factory_(this) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 } | 91 } |
88 | 92 |
89 if (!OfflinePageModel::CanSaveURL(request.url())) { | 93 if (!OfflinePageModel::CanSaveURL(request.url())) { |
90 DVLOG(1) << "Not able to save page for requested url: " << request.url(); | 94 DVLOG(1) << "Not able to save page for requested url: " << request.url(); |
91 return false; | 95 return false; |
92 } | 96 } |
93 | 97 |
94 if (!loader_) | 98 if (!loader_) |
95 ResetState(); | 99 ResetState(); |
96 | 100 |
101 // Invalidate ptrs for all delayed/saving tasks. | |
102 weak_ptr_factory_.InvalidateWeakPtrs(); | |
103 | |
97 // Track copy of pending request. | 104 // Track copy of pending request. |
98 pending_request_.reset(new SavePageRequest(request)); | 105 pending_request_.reset(new SavePageRequest(request)); |
99 completion_callback_ = callback; | 106 completion_callback_ = callback; |
100 | 107 |
101 // Listen for app foreground/background change. | 108 // Listen for app foreground/background change. |
102 app_listener_.reset(new base::android::ApplicationStatusListener( | 109 app_listener_.reset(new base::android::ApplicationStatusListener( |
103 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, | 110 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, |
104 weak_ptr_factory_.GetWeakPtr()))); | 111 weak_ptr_factory_.GetWeakPtr()))); |
105 | 112 |
106 // Load page attempt. | 113 // Load page attempt. |
(...skipping 15 matching lines...) Expand all Loading... | |
122 | 129 |
123 ResetState(); | 130 ResetState(); |
124 } | 131 } |
125 | 132 |
126 void BackgroundLoaderOffliner::DidStopLoading() { | 133 void BackgroundLoaderOffliner::DidStopLoading() { |
127 if (!pending_request_.get()) { | 134 if (!pending_request_.get()) { |
128 DVLOG(1) << "DidStopLoading called even though no pending request."; | 135 DVLOG(1) << "DidStopLoading called even though no pending request."; |
129 return; | 136 return; |
130 } | 137 } |
131 | 138 |
132 SavePageRequest request(*pending_request_.get()); | 139 // Invalidate ptrs for any ongoing save operation. |
Pete Williamson
2017/02/24 17:31:45
How about leaving this code where it is instead of
chili
2017/02/24 21:43:47
I think we still need this check in SavePage becau
| |
133 // If there was an error navigating to page, return loading failed. | 140 weak_ptr_factory_.InvalidateWeakPtrs(); |
134 if (page_load_state_ != SUCCESS) { | |
135 Offliner::RequestStatus status = | |
136 (page_load_state_ == RETRIABLE) | |
137 ? Offliner::RequestStatus::LOADING_FAILED | |
138 : Offliner::RequestStatus::LOADING_FAILED_NO_RETRY; | |
139 completion_callback_.Run(request, status); | |
140 ResetState(); | |
141 return; | |
142 } | |
143 | 141 |
144 save_state_ = SAVING; | 142 // Post SavePage task with 2 second delay. |
145 content::WebContents* web_contents( | 143 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
146 content::WebContentsObserver::web_contents()); | 144 FROM_HERE, |
147 | 145 base::Bind(&BackgroundLoaderOffliner::SavePage, |
148 std::unique_ptr<OfflinePageArchiver> archiver( | 146 weak_ptr_factory_.GetWeakPtr()), |
149 new OfflinePageMHTMLArchiver(web_contents)); | 147 base::TimeDelta::FromMilliseconds(kOfflinePageDelayMs)); |
150 | |
151 OfflinePageModel::SavePageParams params; | |
152 params.url = web_contents->GetLastCommittedURL(); | |
153 params.client_id = request.client_id(); | |
154 params.proposed_offline_id = request.request_id(); | |
155 params.is_background = true; | |
156 | |
157 // Pass in the original URL if it's different from last committed | |
158 // when redirects occur. | |
159 if (params.url != request.url()) | |
160 params.original_url = request.url(); | |
161 | |
162 offline_page_model_->SavePage( | |
163 params, std::move(archiver), | |
164 base::Bind(&BackgroundLoaderOffliner::OnPageSaved, | |
165 weak_ptr_factory_.GetWeakPtr())); | |
166 } | 148 } |
167 | 149 |
168 void BackgroundLoaderOffliner::RenderProcessGone( | 150 void BackgroundLoaderOffliner::RenderProcessGone( |
169 base::TerminationStatus status) { | 151 base::TerminationStatus status) { |
170 if (pending_request_) { | 152 if (pending_request_) { |
171 SavePageRequest request(*pending_request_.get()); | 153 SavePageRequest request(*pending_request_.get()); |
172 switch (status) { | 154 switch (status) { |
173 case base::TERMINATION_STATUS_OOM: | 155 case base::TERMINATION_STATUS_OOM: |
174 case base::TERMINATION_STATUS_PROCESS_CRASHED: | 156 case base::TERMINATION_STATUS_PROCESS_CRASHED: |
175 case base::TERMINATION_STATUS_STILL_RUNNING: | 157 case base::TERMINATION_STATUS_STILL_RUNNING: |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 case net::ERR_SSL_SERVER_CERT_BAD_FORMAT: | 207 case net::ERR_SSL_SERVER_CERT_BAD_FORMAT: |
226 case net::ERR_UNKNOWN_URL_SCHEME: | 208 case net::ERR_UNKNOWN_URL_SCHEME: |
227 page_load_state_ = NONRETRIABLE; | 209 page_load_state_ = NONRETRIABLE; |
228 break; | 210 break; |
229 default: | 211 default: |
230 page_load_state_ = RETRIABLE; | 212 page_load_state_ = RETRIABLE; |
231 } | 213 } |
232 } | 214 } |
233 } | 215 } |
234 | 216 |
217 void BackgroundLoaderOffliner::SavePage() { | |
218 if (!pending_request_.get()) { | |
219 DVLOG(1) << "Pending request was cleared during delay."; | |
220 return; | |
221 } | |
222 | |
223 SavePageRequest request(*pending_request_.get()); | |
224 // If there was an error navigating to page, return loading failed. | |
225 if (page_load_state_ != SUCCESS) { | |
226 Offliner::RequestStatus status = | |
227 (page_load_state_ == RETRIABLE) | |
228 ? Offliner::RequestStatus::LOADING_FAILED | |
229 : Offliner::RequestStatus::LOADING_FAILED_NO_RETRY; | |
230 completion_callback_.Run(request, status); | |
231 ResetState(); | |
232 return; | |
233 } | |
234 | |
235 save_state_ = SAVING; | |
236 content::WebContents* web_contents( | |
237 content::WebContentsObserver::web_contents()); | |
238 | |
239 std::unique_ptr<OfflinePageArchiver> archiver( | |
240 new OfflinePageMHTMLArchiver(web_contents)); | |
241 | |
242 OfflinePageModel::SavePageParams params; | |
243 params.url = web_contents->GetLastCommittedURL(); | |
244 params.client_id = request.client_id(); | |
245 params.proposed_offline_id = request.request_id(); | |
246 params.is_background = true; | |
247 | |
248 // Pass in the original URL if it's different from last committed | |
249 // when redirects occur. | |
250 if (params.url != request.url()) | |
251 params.original_url = request.url(); | |
252 | |
253 offline_page_model_->SavePage( | |
254 params, std::move(archiver), | |
255 base::Bind(&BackgroundLoaderOffliner::OnPageSaved, | |
256 weak_ptr_factory_.GetWeakPtr())); | |
257 } | |
258 | |
235 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, | 259 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, |
236 int64_t offline_id) { | 260 int64_t offline_id) { |
237 if (!pending_request_) | 261 if (!pending_request_) |
238 return; | 262 return; |
239 | 263 |
240 SavePageRequest request(*pending_request_.get()); | 264 SavePageRequest request(*pending_request_.get()); |
241 ResetState(); | 265 ResetState(); |
242 | 266 |
243 if (save_state_ == DELETE_AFTER_SAVE) { | 267 if (save_state_ == DELETE_AFTER_SAVE) { |
244 save_state_ = NONE; | 268 save_state_ = NONE; |
(...skipping 30 matching lines...) Expand all Loading... | |
275 application_state == | 299 application_state == |
276 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 300 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
277 DVLOG(1) << "App became active, canceling current offlining request"; | 301 DVLOG(1) << "App became active, canceling current offlining request"; |
278 SavePageRequest* request = pending_request_.get(); | 302 SavePageRequest* request = pending_request_.get(); |
279 Cancel(); | 303 Cancel(); |
280 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED); | 304 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED); |
281 } | 305 } |
282 } | 306 } |
283 | 307 |
284 } // namespace offline_pages | 308 } // namespace offline_pages |
OLD | NEW |