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

Unified Diff: chrome/browser/android/offline_pages/background_loader_offliner.cc

Issue 2715433006: [Offline Pages] Turn Offliner::Cancel into an async operation to resolve conflicting assumptions (Closed)
Patch Set: forgot to init Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/offline_pages/background_loader_offliner.cc
diff --git a/chrome/browser/android/offline_pages/background_loader_offliner.cc b/chrome/browser/android/offline_pages/background_loader_offliner.cc
index ff7226aa58225ca8055255fa8dc15e3a8d0fce38..25aa7fcce716ce6c50a891b9dfd98840f5434154 100644
--- a/chrome/browser/android/offline_pages/background_loader_offliner.cc
+++ b/chrome/browser/android/offline_pages/background_loader_offliner.cc
@@ -117,18 +117,24 @@ bool BackgroundLoaderOffliner::LoadAndSave(const SavePageRequest& request,
return true;
}
-void BackgroundLoaderOffliner::Cancel() {
+void BackgroundLoaderOffliner::Cancel(const CancelCallback& callback) {
// TODO(chili): We are not able to cancel a pending
- // OfflinePageModel::SavePage() operation. We just ignore the callback.
- if (!pending_request_)
+ // OfflinePageModel::SavePage() operation. We will notify caller that
+ // cancel completed once the SavePage operation returns.
+ if (!pending_request_) {
+ callback.Run(0LL);
return;
+ }
if (save_state_ != NONE) {
save_state_ = DELETE_AFTER_SAVE;
+ cancel_callback_ = callback;
return;
}
+ int64_t request_id = pending_request_->request_id();
ResetState();
+ callback.Run(request_id);
}
bool BackgroundLoaderOffliner::HandleTimeout(const SavePageRequest& request) {
@@ -289,6 +295,7 @@ void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result,
if (save_state_ == DELETE_AFTER_SAVE) {
save_state_ = NONE;
+ cancel_callback_.Run(request.request_id());
return;
}
@@ -323,9 +330,21 @@ void BackgroundLoaderOffliner::OnApplicationStateChange(
base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) {
DVLOG(1) << "App became active, canceling current offlining request";
SavePageRequest* request = pending_request_.get();
- Cancel();
- completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED);
+ // This works because Bind will make a copy of request, and we
+ // should not have to worry about reset being called before cancel callback.
+ Cancel(base::Bind(
+ &BackgroundLoaderOffliner::HandleApplicationStateChangeCancel,
+ weak_ptr_factory_.GetWeakPtr(), *request));
}
}
+void BackgroundLoaderOffliner::HandleApplicationStateChangeCancel(
+ const SavePageRequest& request,
+ int64_t offline_id) {
+ // If for some reason the request was reset during while waiting for callback
+ // ignore the completion callback.
+ if (pending_request_ && pending_request_->request_id() != offline_id)
+ return;
+ completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED);
+}
} // namespace offline_pages

Powered by Google App Engine
This is Rietveld 408576698