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/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/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/sys_info.h" | 10 #include "base/sys_info.h" |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 // Create app listener for the pending request. | 248 // Create app listener for the pending request. |
| 249 app_listener_.reset(new base::android::ApplicationStatusListener( | 249 app_listener_.reset(new base::android::ApplicationStatusListener( |
| 250 base::Bind(&PrerenderingOffliner::OnApplicationStateChange, | 250 base::Bind(&PrerenderingOffliner::OnApplicationStateChange, |
| 251 weak_ptr_factory_.GetWeakPtr()))); | 251 weak_ptr_factory_.GetWeakPtr()))); |
| 252 } | 252 } |
| 253 | 253 |
| 254 return accepted; | 254 return accepted; |
| 255 } | 255 } |
| 256 | 256 |
| 257 void PrerenderingOffliner::Cancel(const CancelCallback& callback) { | 257 void PrerenderingOffliner::Cancel(const CancelCallback& callback) { |
| 258 int64_t request_id = 0LL; | 258 DCHECK(pending_request_); |
| 259 if (pending_request_) { | 259 if (!pending_request_) |
|
Pete Williamson
2017/04/24 17:41:38
Shouldn't we be running the callback even if there
fgorski
2017/04/24 21:40:58
Handled by signature change as explained earlier.
| |
| 260 request_id = pending_request_->request_id(); | 260 return; |
| 261 pending_request_.reset(nullptr); | 261 SavePageRequest canceled_request(*pending_request_.get()); |
| 262 app_listener_.reset(nullptr); | 262 pending_request_.reset(nullptr); |
| 263 GetOrCreateLoader()->StopLoading(); | 263 app_listener_.reset(nullptr); |
| 264 // TODO(dougarnett): Consider ability to cancel SavePage request. | 264 GetOrCreateLoader()->StopLoading(); |
| 265 } | 265 callback.Run(canceled_request); |
| 266 callback.Run(request_id); | |
| 267 } | 266 } |
| 268 | 267 |
| 269 bool PrerenderingOffliner::HandleTimeout(const SavePageRequest& request) { | 268 bool PrerenderingOffliner::HandleTimeout(int64_t request_id) { |
| 270 if (pending_request_) { | 269 if (pending_request_) { |
| 271 DCHECK(request.request_id() == pending_request_->request_id()); | 270 DCHECK(request_id == pending_request_->request_id()); |
| 272 if (GetOrCreateLoader()->IsLowbarMet() && | 271 if (GetOrCreateLoader()->IsLowbarMet() && |
| 273 (request.started_attempt_count() + 1 >= policy_->GetMaxStartedTries() || | 272 (pending_request_->started_attempt_count() + 1 >= |
| 274 request.completed_attempt_count() + 1 >= | 273 policy_->GetMaxStartedTries() || |
| 274 pending_request_->completed_attempt_count() + 1 >= | |
| 275 policy_->GetMaxCompletedTries())) { | 275 policy_->GetMaxCompletedTries())) { |
| 276 saved_on_last_retry_ = true; | 276 saved_on_last_retry_ = true; |
| 277 GetOrCreateLoader()->StartSnapshot(); | 277 GetOrCreateLoader()->StartSnapshot(); |
| 278 return true; | 278 return true; |
| 279 } | 279 } |
| 280 } | 280 } |
| 281 return false; | 281 return false; |
| 282 } | 282 } |
| 283 | 283 |
| 284 void PrerenderingOffliner::SetLoaderForTesting( | 284 void PrerenderingOffliner::SetLoaderForTesting( |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 311 } | 311 } |
| 312 return loader_.get(); | 312 return loader_.get(); |
| 313 } | 313 } |
| 314 | 314 |
| 315 void PrerenderingOffliner::OnApplicationStateChange( | 315 void PrerenderingOffliner::OnApplicationStateChange( |
| 316 base::android::ApplicationState application_state) { | 316 base::android::ApplicationState application_state) { |
| 317 if (pending_request_ && is_low_end_device_ && | 317 if (pending_request_ && is_low_end_device_ && |
| 318 application_state == | 318 application_state == |
| 319 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 319 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 320 DVLOG(1) << "App became active, canceling current offlining request"; | 320 DVLOG(1) << "App became active, canceling current offlining request"; |
| 321 SavePageRequest* request = pending_request_.get(); | |
| 322 // This works because Bind will make a copy of request, and we | |
| 323 // should not have to worry about reset being called before cancel callback. | |
| 324 Cancel(base::Bind(&PrerenderingOffliner::HandleApplicationStateChangeCancel, | 321 Cancel(base::Bind(&PrerenderingOffliner::HandleApplicationStateChangeCancel, |
| 325 weak_ptr_factory_.GetWeakPtr(), *request)); | 322 weak_ptr_factory_.GetWeakPtr())); |
|
Pete Williamson
2017/04/24 17:41:38
If we don't pass the pending request here, how is
chili
2017/04/24 17:44:53
I think it's simply passing in the pending_request
fgorski
2017/04/24 21:40:58
Cathy is correct here, the value comes from Cancel
| |
| 326 } | 323 } |
| 327 } | 324 } |
| 328 | 325 |
| 329 void PrerenderingOffliner::HandleApplicationStateChangeCancel( | 326 void PrerenderingOffliner::HandleApplicationStateChangeCancel( |
| 330 const SavePageRequest& request, | 327 const SavePageRequest& canceled_request) { |
| 331 int64_t offline_id) { | |
| 332 // This shouldn't be immediate, but account for case where request was reset | 328 // This shouldn't be immediate, but account for case where request was reset |
| 333 // while waiting for callback. | 329 // while waiting for callback. |
| 334 if (pending_request_ && pending_request_->request_id() != offline_id) | 330 if (pending_request_ && |
| 331 pending_request_->request_id() != canceled_request.request_id()) { | |
| 335 return; | 332 return; |
| 336 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); | 333 } |
| 334 completion_callback_.Run(canceled_request, | |
| 335 RequestStatus::FOREGROUND_CANCELED); | |
| 337 } | 336 } |
| 338 } // namespace offline_pages | 337 } // namespace offline_pages |
| OLD | NEW |