Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: chrome/browser/android/offline_pages/background_loader_offliner.cc

Issue 2836863002: [Offline pages] Removing active_request_ from request coordinator (Closed)
Patch Set: Removing extra dcheck Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/json/json_writer.h" 7 #include "base/json/json_writer.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 "base/time/time.h" 10 #include "base/time/time.h"
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 loader_.get()->LoadPage(request.url()); 183 loader_.get()->LoadPage(request.url());
184 184
185 snapshot_controller_.reset( 185 snapshot_controller_.reset(
186 new SnapshotController(base::ThreadTaskRunnerHandle::Get(), this, 186 new SnapshotController(base::ThreadTaskRunnerHandle::Get(), this,
187 kOfflineDomContentLoadedMs, page_delay_ms_)); 187 kOfflineDomContentLoadedMs, page_delay_ms_));
188 188
189 return true; 189 return true;
190 } 190 }
191 191
192 void BackgroundLoaderOffliner::Cancel(const CancelCallback& callback) { 192 void BackgroundLoaderOffliner::Cancel(const CancelCallback& callback) {
193 DCHECK(pending_request_);
194 // We ignore the case where pending_request_ is not set, but given the checks
195 // in RequestCoordinator this should not happen.
196 if (!pending_request_)
Pete Williamson 2017/04/24 17:41:37 I know it shouldn't happen, but if it does somehow
fgorski 2017/04/24 21:40:58 Done. I addressed that by changing the interface o
197 return;
198
193 // TODO(chili): We are not able to cancel a pending 199 // TODO(chili): We are not able to cancel a pending
194 // OfflinePageModel::SaveSnapshot() operation. We will notify caller that 200 // OfflinePageModel::SaveSnapshot() operation. We will notify caller that
195 // cancel completed once the SavePage operation returns. 201 // cancel completed once the SavePage operation returns.
196 if (!pending_request_) {
197 callback.Run(0LL);
198 return;
199 }
200
201 if (save_state_ != NONE) { 202 if (save_state_ != NONE) {
202 save_state_ = DELETE_AFTER_SAVE; 203 save_state_ = DELETE_AFTER_SAVE;
203 cancel_callback_ = callback; 204 cancel_callback_ = callback;
204 return; 205 return;
205 } 206 }
206 207
207 int64_t request_id = pending_request_->request_id(); 208 SavePageRequest canceled_request(*pending_request_.get());
208 ResetState(); 209 ResetState();
209 callback.Run(request_id); 210 callback.Run(canceled_request);
210 } 211 }
211 212
212 bool BackgroundLoaderOffliner::HandleTimeout(const SavePageRequest& request) { 213 bool BackgroundLoaderOffliner::HandleTimeout(int64_t request_id) {
213 if (pending_request_) { 214 if (pending_request_) {
214 DCHECK(request.request_id() == pending_request_->request_id()); 215 DCHECK(request_id == pending_request_->request_id());
215 if (is_low_bar_met_ && 216 if (is_low_bar_met_ && (pending_request_->started_attempt_count() + 1 >=
Pete Williamson 2017/04/24 17:41:37 nit - the new line wraps make the code a bit more
fgorski 2017/04/24 21:40:58 It was through git cl format.
216 (request.started_attempt_count() + 1 >= policy_->GetMaxStartedTries() || 217 policy_->GetMaxStartedTries() ||
217 request.completed_attempt_count() + 1 >= 218 pending_request_->completed_attempt_count() + 1 >=
218 policy_->GetMaxCompletedTries())) { 219 policy_->GetMaxCompletedTries())) {
219 // If we are already in the middle of a save operation, let it finish 220 // If we are already in the middle of a save operation, let it finish
220 // but do not return SAVED_ON_LAST_RETRY 221 // but do not return SAVED_ON_LAST_RETRY
221 if (save_state_ == NONE) { 222 if (save_state_ == NONE) {
222 did_snapshot_on_last_retry_ = true; 223 did_snapshot_on_last_retry_ = true;
223 StartSnapshot(); 224 StartSnapshot();
224 } 225 }
225 return true; 226 return true;
226 } 227 }
227 } 228 }
228 return false; 229 return false;
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 int64_t offline_id) { 396 int64_t offline_id) {
396 if (!pending_request_) 397 if (!pending_request_)
397 return; 398 return;
398 399
399 SavePageRequest request(*pending_request_.get()); 400 SavePageRequest request(*pending_request_.get());
400 bool did_snapshot_on_last_retry = did_snapshot_on_last_retry_; 401 bool did_snapshot_on_last_retry = did_snapshot_on_last_retry_;
401 ResetState(); 402 ResetState();
402 403
403 if (save_state_ == DELETE_AFTER_SAVE) { 404 if (save_state_ == DELETE_AFTER_SAVE) {
404 save_state_ = NONE; 405 save_state_ = NONE;
405 cancel_callback_.Run(request.request_id()); 406 cancel_callback_.Run(request);
406 return; 407 return;
407 } 408 }
408 409
409 save_state_ = NONE; 410 save_state_ = NONE;
410 411
411 Offliner::RequestStatus save_status; 412 Offliner::RequestStatus save_status;
412 if (save_result == SavePageResult::SUCCESS) { 413 if (save_result == SavePageResult::SUCCESS) {
413 if (did_snapshot_on_last_retry) 414 if (did_snapshot_on_last_retry)
414 save_status = RequestStatus::SAVED_ON_LAST_RETRY; 415 save_status = RequestStatus::SAVED_ON_LAST_RETRY;
415 else 416 else
(...skipping 23 matching lines...) Expand all
439 content::WebContentsObserver::Observe(contents); 440 content::WebContentsObserver::Observe(contents);
440 OfflinerData::AddToWebContents(contents, this); 441 OfflinerData::AddToWebContents(contents, this);
441 } 442 }
442 443
443 void BackgroundLoaderOffliner::OnApplicationStateChange( 444 void BackgroundLoaderOffliner::OnApplicationStateChange(
444 base::android::ApplicationState application_state) { 445 base::android::ApplicationState application_state) {
445 if (pending_request_ && is_low_end_device_ && 446 if (pending_request_ && is_low_end_device_ &&
446 application_state == 447 application_state ==
447 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { 448 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) {
448 DVLOG(1) << "App became active, canceling current offlining request"; 449 DVLOG(1) << "App became active, canceling current offlining request";
449 SavePageRequest* request = pending_request_.get();
450 // This works because Bind will make a copy of request, and we
451 // should not have to worry about reset being called before cancel callback.
452 Cancel(base::Bind( 450 Cancel(base::Bind(
453 &BackgroundLoaderOffliner::HandleApplicationStateChangeCancel, 451 &BackgroundLoaderOffliner::HandleApplicationStateChangeCancel,
454 weak_ptr_factory_.GetWeakPtr(), *request)); 452 weak_ptr_factory_.GetWeakPtr()));
455 } 453 }
456 } 454 }
457 455
458 void BackgroundLoaderOffliner::HandleApplicationStateChangeCancel( 456 void BackgroundLoaderOffliner::HandleApplicationStateChangeCancel(
459 const SavePageRequest& request, 457 const SavePageRequest& canceled_request) {
460 int64_t offline_id) { 458 DCHECK_EQ(pending_request_->request_id(), canceled_request.request_id());
461 // If for some reason the request was reset during while waiting for callback 459 // If for some reason the request was reset during while waiting for callback
462 // ignore the completion callback. 460 // ignore the completion callback.
463 if (pending_request_ && pending_request_->request_id() != offline_id) 461 if (pending_request_ &&
462 pending_request_->request_id() != canceled_request.request_id()) {
464 return; 463 return;
465 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); 464 }
465 // TODO(chili, petewil): So where is the reset state called, and might that be
chili 2017/04/24 17:44:53 The reset state is handled in the cancel: Applica
fgorski 2017/04/24 21:40:58 The problem is that pending_request_ is already re
466 // the reason for wez's problems?
467 completion_callback_.Run(canceled_request,
Pete Williamson 2017/04/24 17:41:37 Was Wez using the background loader offliner? (I
fgorski 2017/04/24 21:40:58 Per our discussion it wasn't the bug filed by wez,
468 RequestStatus::FOREGROUND_CANCELED);
466 } 469 }
467 470
468 void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) { 471 void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) {
469 base::TimeTicks current_time = base::TimeTicks::Now(); 472 base::TimeTicks current_time = base::TimeTicks::Now();
470 base::TimeDelta delay_so_far = current_time - load_start_time_; 473 base::TimeDelta delay_so_far = current_time - load_start_time_;
471 // We would prefer to use int64_t here, but JSON does not support that type. 474 // We would prefer to use int64_t here, but JSON does not support that type.
472 // Given the choice between int and double, we choose to implicitly convert to 475 // Given the choice between int and double, we choose to implicitly convert to
473 // a double since it maintains more precision (we can get a longer time in 476 // a double since it maintains more precision (we can get a longer time in
474 // milliseconds than we can with a 2 bit int, 53 bits vs 32). 477 // milliseconds than we can with a 2 bit int, 53 bits vs 32).
475 double delay = delay_so_far.InMilliseconds(); 478 double delay = delay_so_far.InMilliseconds();
476 signal_data_.SetDouble(signal_name, delay); 479 signal_data_.SetDouble(signal_name, delay);
477 } 480 }
478 481
479 } // namespace offline_pages 482 } // namespace offline_pages
480 483
481 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); 484 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698