Index: components/offline_pages/background/request_queue.cc |
diff --git a/components/offline_pages/background/request_queue.cc b/components/offline_pages/background/request_queue.cc |
index 5fbd0baeda553a858c2a6abfb333574c695aef68..f3cae845b337e01f720fb779c92154c2ff89bd3b 100644 |
--- a/components/offline_pages/background/request_queue.cc |
+++ b/components/offline_pages/background/request_queue.cc |
@@ -38,6 +38,17 @@ void AddRequestDone(const RequestQueue::AddRequestCallback& callback, |
callback.Run(result, request); |
} |
+// Completes the update request call. |
+void UpdateRequestDone(const RequestQueue::UpdateRequestCallback& callback, |
+ RequestQueueStore::UpdateStatus status) { |
+ RequestQueue::UpdateRequestResult result = |
+ (status == RequestQueueStore::UpdateStatus::UPDATED) |
+ ? RequestQueue::UpdateRequestResult::SUCCESS |
+ : RequestQueue::UpdateRequestResult::STORE_FAILURE; |
+ callback.Run(result); |
+} |
+ |
+ |
// Completes the remove request call. |
void RemoveRequestDone(const RequestQueue::UpdateRequestCallback& callback, |
bool success, |
@@ -48,10 +59,11 @@ void RemoveRequestDone(const RequestQueue::UpdateRequestCallback& callback, |
: RequestQueue::UpdateRequestResult::STORE_FAILURE; |
callback.Run(result); |
} |
+ |
} // namespace |
RequestQueue::RequestQueue(std::unique_ptr<RequestQueueStore> store) |
- : store_(std::move(store)) {} |
+ : store_(std::move(store)), weak_ptr_factory_(this) {} |
RequestQueue::~RequestQueue() {} |
@@ -67,8 +79,55 @@ void RequestQueue::AddRequest(const SavePageRequest& request, |
base::Bind(&AddRequestDone, callback, request)); |
} |
-void RequestQueue::UpdateRequest(const SavePageRequest& request, |
- const UpdateRequestCallback& callback) {} |
+void RequestQueue::UpdateRequest(const SavePageRequest& update_request, |
+ const UpdateRequestCallback& update_callback) { |
+ // We have to pass the update_callback *through* the get callback. We do this |
+ // by currying the update_callback as a parameter to be used when calling |
+ // GetForUpdateDone. The actual request queue store get operation will not |
+ // see this bound parameter, but just pass it along. GetForUpdateDone then |
+ // passes it into the AddOrUpdateRequest method, where it ends up calling back |
+ // to the request queue client. |
+ // TODO(petewil): This would be more efficient if the store supported a call |
+ // to get a single item by ID. Change this code to use that API when added. |
+ // crbug.com/630657. |
+ store_->GetRequests(base::Bind( |
+ &RequestQueue::GetForUpdateDone, weak_ptr_factory_.GetWeakPtr(), |
+ update_callback, update_request)); |
+} |
+ |
+// We need a different version of the GetCallback that can take the curried |
+// update_callback as a parameter, and call back into the request queue store |
+// implementation. This must be a member function because we need access to |
+// the store pointer to call AddOrUpdateRequest. |
+void RequestQueue::GetForUpdateDone( |
+ const UpdateRequestCallback& update_callback, |
+ const SavePageRequest& update_request, |
+ bool success, |
+ const std::vector<SavePageRequest>& found_requests) { |
+ // If the result was not found, return now. |
+ if (!success) { |
+ update_callback.Run( |
+ RequestQueue::UpdateRequestResult::REQUEST_DOES_NOT_EXIST); |
+ return; |
+ } |
+ // If the found result does not contain the request we are looking for, return |
+ // now. |
+ bool found = false; |
+ std::vector<SavePageRequest>::const_iterator iter; |
+ for (iter = found_requests.begin(); iter != found_requests.end(); ++iter) { |
+ if (iter->request_id() == update_request.request_id()) |
+ found = true; |
+ } |
+ if (!found) { |
+ update_callback.Run( |
+ RequestQueue::UpdateRequestResult::REQUEST_DOES_NOT_EXIST); |
+ return; |
+ } |
+ |
+ // Since the request exists, update it. |
+ store_->AddOrUpdateRequest(update_request, |
+ base::Bind(&UpdateRequestDone, update_callback)); |
+} |
void RequestQueue::RemoveRequest(int64_t request_id, |
const UpdateRequestCallback& callback) { |