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

Side by Side Diff: components/offline_pages/background/request_queue_in_memory_store.cc

Issue 2219393004: Adds an observer for the request coordinator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@resumeAPI
Patch Set: Fix nits. Created 4 years, 4 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 "components/offline_pages/background/request_queue_in_memory_store.h" 5 #include "components/offline_pages/background/request_queue_in_memory_store.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/threading/thread_task_runner_handle.h" 9 #include "base/threading/thread_task_runner_handle.h"
10 #include "components/offline_pages/background/save_page_request.h" 10 #include "components/offline_pages/background/save_page_request.h"
(...skipping 22 matching lines...) Expand all
33 requests_.insert(std::make_pair(request.request_id(), request)); 33 requests_.insert(std::make_pair(request.request_id(), request));
34 base::ThreadTaskRunnerHandle::Get()->PostTask( 34 base::ThreadTaskRunnerHandle::Get()->PostTask(
35 FROM_HERE, base::Bind(callback, UpdateStatus::UPDATED)); 35 FROM_HERE, base::Bind(callback, UpdateStatus::UPDATED));
36 } 36 }
37 37
38 void RequestQueueInMemoryStore::RemoveRequests( 38 void RequestQueueInMemoryStore::RemoveRequests(
39 const std::vector<int64_t>& request_ids, 39 const std::vector<int64_t>& request_ids,
40 const RemoveCallback& callback) { 40 const RemoveCallback& callback) {
41 RequestQueue::UpdateMultipleRequestResults results; 41 RequestQueue::UpdateMultipleRequestResults results;
42 RequestQueue::UpdateRequestResult result; 42 RequestQueue::UpdateRequestResult result;
43 std::vector<SavePageRequest> requests;
43 RequestsMap::iterator iter; 44 RequestsMap::iterator iter;
45
46 // If we find a request, mark it as succeeded, and put it in the request list.
47 // Otherwise mark it as failed.
44 for (auto request_id : request_ids) { 48 for (auto request_id : request_ids) {
45 iter = requests_.find(request_id); 49 iter = requests_.find(request_id);
46 if (iter != requests_.end()) { 50 if (iter != requests_.end()) {
51 SavePageRequest request = iter->second;
47 requests_.erase(iter); 52 requests_.erase(iter);
48 result = RequestQueue::UpdateRequestResult::SUCCESS; 53 result = RequestQueue::UpdateRequestResult::SUCCESS;
54 requests.push_back(request);
49 } else { 55 } else {
50 result = RequestQueue::UpdateRequestResult::REQUEST_DOES_NOT_EXIST; 56 result = RequestQueue::UpdateRequestResult::REQUEST_DOES_NOT_EXIST;
51 } 57 }
52 results.push_back(std::make_pair(request_id, result)); 58 results.push_back(std::make_pair(request_id, result));
53 } 59 }
54 60
55 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 61 base::ThreadTaskRunnerHandle::Get()->PostTask(
56 base::Bind(callback, results)); 62 FROM_HERE, base::Bind(callback, results, requests));
57 } 63 }
58 64
59 void RequestQueueInMemoryStore::ChangeRequestsState( 65 void RequestQueueInMemoryStore::ChangeRequestsState(
60 const std::vector<int64_t>& request_ids, 66 const std::vector<int64_t>& request_ids,
61 const SavePageRequest::RequestState new_state, 67 const SavePageRequest::RequestState new_state,
62 const UpdateCallback& callback) { 68 const UpdateMultipleRequestsCallback& callback) {
63 int count = 0; 69 RequestQueue::UpdateMultipleRequestResults results;
64 70 std::vector<SavePageRequest> requests;
71 RequestQueue::UpdateRequestResult result;
65 for (int64_t request_id : request_ids) { 72 for (int64_t request_id : request_ids) {
66 auto pair = requests_.find(request_id); 73 auto pair = requests_.find(request_id);
74 // If we find this request id, modify it, and return the modified request in
75 // the request list.
67 if (pair != requests_.end()) { 76 if (pair != requests_.end()) {
68 ++count;
69 pair->second.set_request_state(new_state); 77 pair->second.set_request_state(new_state);
78 requests.push_back(pair->second);
79 result = RequestQueue::UpdateRequestResult::SUCCESS;
80 } else {
81 result = RequestQueue::UpdateRequestResult::REQUEST_DOES_NOT_EXIST;;
70 } 82 }
83
84 results.push_back(std::make_pair(request_id, result));
71 } 85 }
72 86
73 RequestQueueStore::UpdateStatus status = 87 base::ThreadTaskRunnerHandle::Get()->PostTask(
74 count > 0 ? UpdateStatus::UPDATED : UpdateStatus::FAILED; 88 FROM_HERE, base::Bind(callback, results, requests));
75
76 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
77 base::Bind(callback, status));
78 } 89 }
79 90
80 void RequestQueueInMemoryStore::Reset(const ResetCallback& callback) { 91 void RequestQueueInMemoryStore::Reset(const ResetCallback& callback) {
81 requests_.clear(); 92 requests_.clear();
82 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 93 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
83 base::Bind(callback, true)); 94 base::Bind(callback, true));
84 } 95 }
85 96
86 } // namespace offline_pages 97 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698