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