Chromium Code Reviews| 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; | |
| 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. |
| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 case net::ERR_SSL_PROTOCOL_ERROR: | 205 case net::ERR_SSL_PROTOCOL_ERROR: |
| 224 case net::ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED: | 206 case net::ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED: |
| 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 } |
| 215 | |
| 216 // If the page is not the same, invalidate any pending save tasks. | |
| 217 // | |
| 218 // Downloads or 204/205 response codes do not commit (no new navigation) | |
| 219 // Same-Page (committed) navigations are: | |
| 220 // - reference fragment navigations | |
| 221 // - pushState/replaceState | |
| 222 // - same page history navigation | |
| 223 if (navigation_handle->HasCommitted() && !navigation_handle->IsSamePage()) { | |
|
fgorski
2017/02/24 22:12:07
int: drop {}
chili
2017/02/27 22:18:29
Done.
| |
| 224 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 void BackgroundLoaderOffliner::SavePage() { | |
| 229 if (!pending_request_.get()) { | |
| 230 DVLOG(1) << "Pending request was cleared during delay."; | |
| 231 return; | |
| 232 } | |
| 233 | |
| 234 SavePageRequest request(*pending_request_.get()); | |
| 235 // If there was an error navigating to page, return loading failed. | |
| 236 if (page_load_state_ != SUCCESS) { | |
| 237 Offliner::RequestStatus status = | |
| 238 (page_load_state_ == RETRIABLE) | |
| 239 ? Offliner::RequestStatus::LOADING_FAILED | |
| 240 : Offliner::RequestStatus::LOADING_FAILED_NO_RETRY; | |
| 241 completion_callback_.Run(request, status); | |
| 242 ResetState(); | |
| 243 return; | |
| 244 } | |
| 245 | |
| 246 save_state_ = SAVING; | |
| 247 content::WebContents* web_contents( | |
| 248 content::WebContentsObserver::web_contents()); | |
| 249 | |
| 250 std::unique_ptr<OfflinePageArchiver> archiver( | |
| 251 new OfflinePageMHTMLArchiver(web_contents)); | |
| 252 | |
| 253 OfflinePageModel::SavePageParams params; | |
| 254 params.url = web_contents->GetLastCommittedURL(); | |
| 255 params.client_id = request.client_id(); | |
| 256 params.proposed_offline_id = request.request_id(); | |
| 257 params.is_background = true; | |
| 258 | |
| 259 // Pass in the original URL if it's different from last committed | |
| 260 // when redirects occur. | |
| 261 if (params.url != request.url()) | |
| 262 params.original_url = request.url(); | |
| 263 | |
| 264 offline_page_model_->SavePage( | |
| 265 params, std::move(archiver), | |
| 266 base::Bind(&BackgroundLoaderOffliner::OnPageSaved, | |
| 267 weak_ptr_factory_.GetWeakPtr())); | |
| 233 } | 268 } |
| 234 | 269 |
| 235 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, | 270 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, |
| 236 int64_t offline_id) { | 271 int64_t offline_id) { |
| 237 if (!pending_request_) | 272 if (!pending_request_) |
| 238 return; | 273 return; |
| 239 | 274 |
| 240 SavePageRequest request(*pending_request_.get()); | 275 SavePageRequest request(*pending_request_.get()); |
| 241 ResetState(); | 276 ResetState(); |
| 242 | 277 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 application_state == | 310 application_state == |
| 276 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 311 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 277 DVLOG(1) << "App became active, canceling current offlining request"; | 312 DVLOG(1) << "App became active, canceling current offlining request"; |
| 278 SavePageRequest* request = pending_request_.get(); | 313 SavePageRequest* request = pending_request_.get(); |
| 279 Cancel(); | 314 Cancel(); |
| 280 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED); | 315 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED); |
| 281 } | 316 } |
| 282 } | 317 } |
| 283 | 318 |
| 284 } // namespace offline_pages | 319 } // namespace offline_pages |
| OLD | NEW |