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