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

Side by Side Diff: content/browser/background_fetch/background_fetch_data_manager.cc

Issue 2978603003: [Background Fetch] Tidy up getting/activating pending requests (Closed)
Patch Set: Created 3 years, 5 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "content/browser/background_fetch/background_fetch_data_manager.h" 5 #include "content/browser/background_fetch/background_fetch_data_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <queue> 8 #include <queue>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 pending_requests_.push(std::move(request)); 47 pending_requests_.push(std::move(request));
48 } 48 }
49 } 49 }
50 50
51 ~RegistrationData() = default; 51 ~RegistrationData() = default;
52 52
53 // Returns whether there are remaining requests on the request queue. 53 // Returns whether there are remaining requests on the request queue.
54 bool HasPendingRequests() const { return !pending_requests_.empty(); } 54 bool HasPendingRequests() const { return !pending_requests_.empty(); }
55 55
56 // Consumes a request from the queue that is to be fetched. 56 // Consumes a request from the queue that is to be fetched.
57 scoped_refptr<BackgroundFetchRequestInfo> GetPendingRequest() { 57 scoped_refptr<BackgroundFetchRequestInfo> ActivateNextPendingRequest() {
58 DCHECK(!pending_requests_.empty()); 58 DCHECK(!pending_requests_.empty());
59 59
60 auto request = pending_requests_.front(); 60 auto request = pending_requests_.front();
61 pending_requests_.pop(); 61 pending_requests_.pop();
62 62
63 // The |request| is considered to be active now. 63 // The |request| is considered to be active now.
64 active_requests_.push_back(request); 64 active_requests_.push_back(request);
65 65
66 return request; 66 return request;
67 } 67 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 } 139 }
140 140
141 void BackgroundFetchDataManager::CreateRegistration( 141 void BackgroundFetchDataManager::CreateRegistration(
142 const BackgroundFetchRegistrationId& registration_id, 142 const BackgroundFetchRegistrationId& registration_id,
143 const std::vector<ServiceWorkerFetchRequest>& requests, 143 const std::vector<ServiceWorkerFetchRequest>& requests,
144 const BackgroundFetchOptions& options, 144 const BackgroundFetchOptions& options,
145 CreateRegistrationCallback callback) { 145 CreateRegistrationCallback callback) {
146 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 146 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
147 147
148 if (registrations_.find(registration_id) != registrations_.end()) { 148 if (registrations_.find(registration_id) != registrations_.end()) {
149 std::move(callback).Run( 149 std::move(callback).Run(blink::mojom::BackgroundFetchError::DUPLICATED_TAG);
150 blink::mojom::BackgroundFetchError::DUPLICATED_TAG,
151 std::vector<scoped_refptr<BackgroundFetchRequestInfo>>());
152 return; 150 return;
153 } 151 }
154 152
155 std::unique_ptr<RegistrationData> registration_data = 153 // Create the |RegistrationData|, and store it for easy access.
156 base::MakeUnique<RegistrationData>(requests, options); 154 registrations_.insert(std::make_pair(
157 155 registration_id, base::MakeUnique<RegistrationData>(requests, options)));
158 // Create a vector with the initial requests to feed the Job Controller with.
159 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests;
160 for (size_t i = 0; i < kMaximumBackgroundFetchParallelRequests; ++i) {
161 if (!registration_data->HasPendingRequests())
162 break;
163
164 initial_requests.push_back(registration_data->GetPendingRequest());
165 }
166
167 // Store the created |registration_data| so that we can easily access it.
168 registrations_.insert(
169 std::make_pair(registration_id, std::move(registration_data)));
170 156
171 // Inform the |callback| of the newly created registration. 157 // Inform the |callback| of the newly created registration.
172 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, 158 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE);
173 std::move(initial_requests)); 159 }
160
161 void BackgroundFetchDataManager::ActivateNextRequest(
162 const BackgroundFetchRegistrationId& registration_id,
163 NextRequestCallback callback) {
164 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
165
166 auto iter = registrations_.find(registration_id);
167 DCHECK(iter != registrations_.end());
168
169 RegistrationData* registration_data = iter->second.get();
170
171 scoped_refptr<BackgroundFetchRequestInfo> next_request;
172 if (registration_data->HasPendingRequests())
173 next_request = registration_data->ActivateNextPendingRequest();
174
175 std::move(callback).Run(std::move(next_request));
174 } 176 }
175 177
176 void BackgroundFetchDataManager::MarkRequestAsStarted( 178 void BackgroundFetchDataManager::MarkRequestAsStarted(
177 const BackgroundFetchRegistrationId& registration_id, 179 const BackgroundFetchRegistrationId& registration_id,
178 BackgroundFetchRequestInfo* request, 180 BackgroundFetchRequestInfo* request,
179 const std::string& download_guid) { 181 const std::string& download_guid) {
180 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 182 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
181 183
182 auto iter = registrations_.find(registration_id); 184 auto iter = registrations_.find(registration_id);
183 DCHECK(iter != registrations_.end()); 185 DCHECK(iter != registrations_.end());
184 186
185 RegistrationData* registration_data = iter->second.get(); 187 RegistrationData* registration_data = iter->second.get();
186 registration_data->MarkRequestAsStarted(request, download_guid); 188 registration_data->MarkRequestAsStarted(request, download_guid);
187 } 189 }
188 190
189 void BackgroundFetchDataManager::MarkRequestAsCompleteAndGetNextRequest( 191 void BackgroundFetchDataManager::MarkRequestAsCompleteAndActivateNextRequest(
190 const BackgroundFetchRegistrationId& registration_id, 192 const BackgroundFetchRegistrationId& registration_id,
191 BackgroundFetchRequestInfo* request, 193 BackgroundFetchRequestInfo* request,
192 NextRequestCallback callback) { 194 NextRequestCallback callback) {
193 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 195 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
194 196
195 auto iter = registrations_.find(registration_id); 197 auto iter = registrations_.find(registration_id);
196 DCHECK(iter != registrations_.end()); 198 DCHECK(iter != registrations_.end());
197 199
198 RegistrationData* registration_data = iter->second.get(); 200 RegistrationData* registration_data = iter->second.get();
199 registration_data->MarkRequestAsComplete(request); 201 registration_data->MarkRequestAsComplete(request);
200 202
201 scoped_refptr<BackgroundFetchRequestInfo> next_request; 203 ActivateNextRequest(registration_id, std::move(callback));
202 if (registration_data->HasPendingRequests())
203 next_request = registration_data->GetPendingRequest();
204
205 std::move(callback).Run(std::move(next_request));
206 } 204 }
207 205
208 void BackgroundFetchDataManager::GetSettledFetchesForRegistration( 206 void BackgroundFetchDataManager::GetSettledFetchesForRegistration(
209 const BackgroundFetchRegistrationId& registration_id, 207 const BackgroundFetchRegistrationId& registration_id,
210 SettledFetchesCallback callback) { 208 SettledFetchesCallback callback) {
211 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 209 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
212 210
213 auto iter = registrations_.find(registration_id); 211 auto iter = registrations_.find(registration_id);
214 DCHECK(iter != registrations_.end()); 212 DCHECK(iter != registrations_.end());
215 213
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 std::move(callback).Run(blink::mojom::BackgroundFetchError::INVALID_TAG); 291 std::move(callback).Run(blink::mojom::BackgroundFetchError::INVALID_TAG);
294 return; 292 return;
295 } 293 }
296 294
297 registrations_.erase(iter); 295 registrations_.erase(iter);
298 296
299 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE); 297 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE);
300 } 298 }
301 299
302 } // namespace content 300 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698