| 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/prerendering_offliner.h" | 5 #include "chrome/browser/android/offline_pages/prerendering_offliner.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/sys_info.h" | 9 #include "base/sys_info.h" |
| 10 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 10 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 } else { | 203 } else { |
| 204 // Create app listener for the pending request. | 204 // Create app listener for the pending request. |
| 205 app_listener_.reset(new base::android::ApplicationStatusListener( | 205 app_listener_.reset(new base::android::ApplicationStatusListener( |
| 206 base::Bind(&PrerenderingOffliner::OnApplicationStateChange, | 206 base::Bind(&PrerenderingOffliner::OnApplicationStateChange, |
| 207 weak_ptr_factory_.GetWeakPtr()))); | 207 weak_ptr_factory_.GetWeakPtr()))); |
| 208 } | 208 } |
| 209 | 209 |
| 210 return accepted; | 210 return accepted; |
| 211 } | 211 } |
| 212 | 212 |
| 213 void PrerenderingOffliner::Cancel() { | 213 void PrerenderingOffliner::Cancel(const CancelCallback& callback) { |
| 214 int64_t request_id = 0LL; |
| 214 if (pending_request_) { | 215 if (pending_request_) { |
| 216 request_id = pending_request_->request_id(); |
| 215 pending_request_.reset(nullptr); | 217 pending_request_.reset(nullptr); |
| 216 app_listener_.reset(nullptr); | 218 app_listener_.reset(nullptr); |
| 217 GetOrCreateLoader()->StopLoading(); | 219 GetOrCreateLoader()->StopLoading(); |
| 218 // TODO(dougarnett): Consider ability to cancel SavePage request. | 220 // TODO(dougarnett): Consider ability to cancel SavePage request. |
| 219 } | 221 } |
| 222 callback.Run(request_id); |
| 220 } | 223 } |
| 221 | 224 |
| 222 bool PrerenderingOffliner::HandleTimeout(const SavePageRequest& request) { | 225 bool PrerenderingOffliner::HandleTimeout(const SavePageRequest& request) { |
| 223 if (pending_request_) { | 226 if (pending_request_) { |
| 224 DCHECK(request.request_id() == pending_request_->request_id()); | 227 DCHECK(request.request_id() == pending_request_->request_id()); |
| 225 if (GetOrCreateLoader()->IsLowbarMet() && | 228 if (GetOrCreateLoader()->IsLowbarMet() && |
| 226 (request.started_attempt_count() + 1 >= policy_->GetMaxStartedTries() || | 229 (request.started_attempt_count() + 1 >= policy_->GetMaxStartedTries() || |
| 227 request.completed_attempt_count() + 1 >= | 230 request.completed_attempt_count() + 1 >= |
| 228 policy_->GetMaxCompletedTries())) { | 231 policy_->GetMaxCompletedTries())) { |
| 229 GetOrCreateLoader()->StartSnapshot(); | 232 GetOrCreateLoader()->StartSnapshot(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 return loader_.get(); | 267 return loader_.get(); |
| 265 } | 268 } |
| 266 | 269 |
| 267 void PrerenderingOffliner::OnApplicationStateChange( | 270 void PrerenderingOffliner::OnApplicationStateChange( |
| 268 base::android::ApplicationState application_state) { | 271 base::android::ApplicationState application_state) { |
| 269 if (pending_request_ && is_low_end_device_ && | 272 if (pending_request_ && is_low_end_device_ && |
| 270 application_state == | 273 application_state == |
| 271 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 274 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 272 DVLOG(1) << "App became active, canceling current offlining request"; | 275 DVLOG(1) << "App became active, canceling current offlining request"; |
| 273 SavePageRequest* request = pending_request_.get(); | 276 SavePageRequest* request = pending_request_.get(); |
| 274 Cancel(); | 277 // This works because Bind will make a copy of request, and we |
| 275 completion_callback_.Run(*request, | 278 // should not have to worry about reset being called before cancel callback. |
| 276 Offliner::RequestStatus::FOREGROUND_CANCELED); | 279 Cancel(base::Bind(&PrerenderingOffliner::HandleApplicationStateChangeCancel, |
| 280 weak_ptr_factory_.GetWeakPtr(), *request)); |
| 277 } | 281 } |
| 278 } | 282 } |
| 279 | 283 |
| 284 void PrerenderingOffliner::HandleApplicationStateChangeCancel( |
| 285 const SavePageRequest& request, |
| 286 int64_t offline_id) { |
| 287 // This shouldn't be immediate, but account for case where request was reset |
| 288 // while waiting for callback. |
| 289 if (pending_request_ && pending_request_->request_id() != offline_id) |
| 290 return; |
| 291 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); |
| 292 } |
| 280 } // namespace offline_pages | 293 } // namespace offline_pages |
| OLD | NEW |