| 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" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 app_listener_.reset(new base::android::ApplicationStatusListener( | 114 app_listener_.reset(new base::android::ApplicationStatusListener( |
| 115 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, | 115 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, |
| 116 weak_ptr_factory_.GetWeakPtr()))); | 116 weak_ptr_factory_.GetWeakPtr()))); |
| 117 | 117 |
| 118 // Load page attempt. | 118 // Load page attempt. |
| 119 loader_.get()->LoadPage(request.url()); | 119 loader_.get()->LoadPage(request.url()); |
| 120 | 120 |
| 121 return true; | 121 return true; |
| 122 } | 122 } |
| 123 | 123 |
| 124 void BackgroundLoaderOffliner::Cancel() { | 124 void BackgroundLoaderOffliner::Cancel(const CancelCallback& callback) { |
| 125 // TODO(chili): We are not able to cancel a pending | 125 // TODO(chili): We are not able to cancel a pending |
| 126 // OfflinePageModel::SavePage() operation. We just ignore the callback. | 126 // OfflinePageModel::SavePage() operation. We will notify caller that |
| 127 if (!pending_request_) | 127 // cancel completed once the SavePage operation returns. |
| 128 if (!pending_request_) { |
| 129 callback.Run(0LL); |
| 128 return; | 130 return; |
| 131 } |
| 129 | 132 |
| 130 if (save_state_ != NONE) { | 133 if (save_state_ != NONE) { |
| 131 save_state_ = DELETE_AFTER_SAVE; | 134 save_state_ = DELETE_AFTER_SAVE; |
| 135 cancel_callback_ = callback; |
| 132 return; | 136 return; |
| 133 } | 137 } |
| 134 | 138 |
| 139 int64_t request_id = pending_request_->request_id(); |
| 135 ResetState(); | 140 ResetState(); |
| 141 callback.Run(request_id); |
| 136 } | 142 } |
| 137 | 143 |
| 138 bool BackgroundLoaderOffliner::HandleTimeout(const SavePageRequest& request) { | 144 bool BackgroundLoaderOffliner::HandleTimeout(const SavePageRequest& request) { |
| 139 // TODO(romax) Decide if we want to also take a snapshot on the last timeout | 145 // TODO(romax) Decide if we want to also take a snapshot on the last timeout |
| 140 // for the background loader offliner. | 146 // for the background loader offliner. |
| 141 return false; | 147 return false; |
| 142 } | 148 } |
| 143 | 149 |
| 144 void BackgroundLoaderOffliner::DidStopLoading() { | 150 void BackgroundLoaderOffliner::DidStopLoading() { |
| 145 if (!pending_request_.get()) { | 151 if (!pending_request_.get()) { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, | 292 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, |
| 287 int64_t offline_id) { | 293 int64_t offline_id) { |
| 288 if (!pending_request_) | 294 if (!pending_request_) |
| 289 return; | 295 return; |
| 290 | 296 |
| 291 SavePageRequest request(*pending_request_.get()); | 297 SavePageRequest request(*pending_request_.get()); |
| 292 ResetState(); | 298 ResetState(); |
| 293 | 299 |
| 294 if (save_state_ == DELETE_AFTER_SAVE) { | 300 if (save_state_ == DELETE_AFTER_SAVE) { |
| 295 save_state_ = NONE; | 301 save_state_ = NONE; |
| 302 cancel_callback_.Run(request.request_id()); |
| 296 return; | 303 return; |
| 297 } | 304 } |
| 298 | 305 |
| 299 save_state_ = NONE; | 306 save_state_ = NONE; |
| 300 | 307 |
| 301 Offliner::RequestStatus save_status; | 308 Offliner::RequestStatus save_status; |
| 302 if (save_result == SavePageResult::SUCCESS) | 309 if (save_result == SavePageResult::SUCCESS) |
| 303 save_status = RequestStatus::SAVED; | 310 save_status = RequestStatus::SAVED; |
| 304 else | 311 else |
| 305 save_status = RequestStatus::SAVE_FAILED; | 312 save_status = RequestStatus::SAVE_FAILED; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 320 content::WebContentsObserver::Observe(loader_.get()->web_contents()); | 327 content::WebContentsObserver::Observe(loader_.get()->web_contents()); |
| 321 } | 328 } |
| 322 | 329 |
| 323 void BackgroundLoaderOffliner::OnApplicationStateChange( | 330 void BackgroundLoaderOffliner::OnApplicationStateChange( |
| 324 base::android::ApplicationState application_state) { | 331 base::android::ApplicationState application_state) { |
| 325 if (pending_request_ && is_low_end_device_ && | 332 if (pending_request_ && is_low_end_device_ && |
| 326 application_state == | 333 application_state == |
| 327 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 334 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 328 DVLOG(1) << "App became active, canceling current offlining request"; | 335 DVLOG(1) << "App became active, canceling current offlining request"; |
| 329 SavePageRequest* request = pending_request_.get(); | 336 SavePageRequest* request = pending_request_.get(); |
| 330 Cancel(); | 337 // This works because Bind will make a copy of request, and we |
| 331 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED); | 338 // should not have to worry about reset being called before cancel callback. |
| 339 Cancel(base::Bind( |
| 340 &BackgroundLoaderOffliner::HandleApplicationStateChangeCancel, |
| 341 weak_ptr_factory_.GetWeakPtr(), *request)); |
| 332 } | 342 } |
| 333 } | 343 } |
| 334 | 344 |
| 345 void BackgroundLoaderOffliner::HandleApplicationStateChangeCancel( |
| 346 const SavePageRequest& request, |
| 347 int64_t offline_id) { |
| 348 // If for some reason the request was reset during while waiting for callback |
| 349 // ignore the completion callback. |
| 350 if (pending_request_ && pending_request_->request_id() != offline_id) |
| 351 return; |
| 352 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); |
| 353 } |
| 335 } // namespace offline_pages | 354 } // namespace offline_pages |
| OLD | NEW |