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

Side by Side Diff: content/browser/background_sync/background_sync_manager.cc

Issue 2167483002: [CacheStorage] Simpler way to wrap callbacks to run pending operation completions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comment clarification Created 4 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_sync/background_sync_manager.h" 5 #include "content/browser/background_sync/background_sync_manager.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/barrier_closure.h" 9 #include "base/barrier_closure.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 DCHECK_CURRENTLY_ON(BrowserThread::IO); 196 DCHECK_CURRENTLY_ON(BrowserThread::IO);
197 197
198 if (disabled_) { 198 if (disabled_) {
199 RecordFailureAndPostError(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); 199 RecordFailureAndPostError(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
200 return; 200 return;
201 } 201 }
202 202
203 op_scheduler_.ScheduleOperation( 203 op_scheduler_.ScheduleOperation(
204 base::Bind(&BackgroundSyncManager::RegisterCheckIfHasMainFrame, 204 base::Bind(&BackgroundSyncManager::RegisterCheckIfHasMainFrame,
205 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, 205 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options,
206 MakeStatusAndRegistrationCompletion(callback))); 206 op_scheduler_.WrapCallbackToRunNext(callback)));
207 } 207 }
208 208
209 void BackgroundSyncManager::GetRegistrations( 209 void BackgroundSyncManager::GetRegistrations(
210 int64_t sw_registration_id, 210 int64_t sw_registration_id,
211 const StatusAndRegistrationsCallback& callback) { 211 const StatusAndRegistrationsCallback& callback) {
212 DCHECK_CURRENTLY_ON(BrowserThread::IO); 212 DCHECK_CURRENTLY_ON(BrowserThread::IO);
213 213
214 if (disabled_) { 214 if (disabled_) {
215 base::ThreadTaskRunnerHandle::Get()->PostTask( 215 base::ThreadTaskRunnerHandle::Get()->PostTask(
216 FROM_HERE, 216 FROM_HERE,
217 base::Bind( 217 base::Bind(
218 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, 218 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
219 base::Passed( 219 base::Passed(
220 std::unique_ptr<ScopedVector<BackgroundSyncRegistration>>( 220 std::unique_ptr<ScopedVector<BackgroundSyncRegistration>>(
221 new ScopedVector<BackgroundSyncRegistration>())))); 221 new ScopedVector<BackgroundSyncRegistration>()))));
222 return; 222 return;
223 } 223 }
224 224
225 op_scheduler_.ScheduleOperation( 225 op_scheduler_.ScheduleOperation(
226 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, 226 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl,
227 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, 227 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
228 MakeStatusAndRegistrationsCompletion(callback))); 228 op_scheduler_.WrapCallbackToRunNext(callback)));
229 } 229 }
230 230
231 void BackgroundSyncManager::OnRegistrationDeleted(int64_t sw_registration_id, 231 void BackgroundSyncManager::OnRegistrationDeleted(int64_t sw_registration_id,
232 const GURL& pattern) { 232 const GURL& pattern) {
233 DCHECK_CURRENTLY_ON(BrowserThread::IO); 233 DCHECK_CURRENTLY_ON(BrowserThread::IO);
234 234
235 // Operations already in the queue will either fail when they write to storage 235 // Operations already in the queue will either fail when they write to storage
236 // or return stale results based on registrations loaded in memory. This is 236 // or return stale results based on registrations loaded in memory. This is
237 // inconsequential since the service worker is gone. 237 // inconsequential since the service worker is gone.
238 op_scheduler_.ScheduleOperation( 238 op_scheduler_.ScheduleOperation(
(...skipping 791 matching lines...) Expand 10 before | Expand all | Expand 10 after
1030 const base::Closure& callback, 1030 const base::Closure& callback,
1031 ServiceWorkerStatusCode status_code) { 1031 ServiceWorkerStatusCode status_code) {
1032 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1032 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1033 1033
1034 if (disabled_) { 1034 if (disabled_) {
1035 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 1035 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1036 base::Bind(callback)); 1036 base::Bind(callback));
1037 return; 1037 return;
1038 } 1038 }
1039 1039
1040 op_scheduler_.ScheduleOperation(base::Bind( 1040 op_scheduler_.ScheduleOperation(
1041 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), 1041 base::Bind(&BackgroundSyncManager::EventCompleteImpl,
1042 service_worker_id, tag, status_code, MakeClosureCompletion(callback))); 1042 weak_ptr_factory_.GetWeakPtr(), service_worker_id, tag,
1043 status_code, op_scheduler_.WrapCallbackToRunNext(callback)));
1043 } 1044 }
1044 1045
1045 void BackgroundSyncManager::EventCompleteImpl( 1046 void BackgroundSyncManager::EventCompleteImpl(
1046 int64_t service_worker_id, 1047 int64_t service_worker_id,
1047 const std::string& tag, 1048 const std::string& tag,
1048 ServiceWorkerStatusCode status_code, 1049 ServiceWorkerStatusCode status_code,
1049 const base::Closure& callback) { 1050 const base::Closure& callback) {
1050 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1051 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1051 1052
1052 if (disabled_) { 1053 if (disabled_) {
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
1181 1182
1182 void BackgroundSyncManager::SetMaxSyncAttemptsImpl( 1183 void BackgroundSyncManager::SetMaxSyncAttemptsImpl(
1183 int max_attempts, 1184 int max_attempts,
1184 const base::Closure& callback) { 1185 const base::Closure& callback) {
1185 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1186 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1186 1187
1187 parameters_->max_sync_attempts = max_attempts; 1188 parameters_->max_sync_attempts = max_attempts;
1188 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); 1189 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
1189 } 1190 }
1190 1191
1191 // TODO(jkarlin): Figure out how to pass scoped_ptrs with this.
1192 template <typename CallbackT, typename... Params>
1193 void BackgroundSyncManager::CompleteOperationCallback(const CallbackT& callback,
1194 Params... parameters) {
1195 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1196
1197 callback.Run(parameters...);
1198 op_scheduler_.CompleteOperationAndRunNext();
1199 }
1200
1201 void BackgroundSyncManager::CompleteStatusAndRegistrationCallback(
1202 StatusAndRegistrationCallback callback,
1203 BackgroundSyncStatus status,
1204 std::unique_ptr<BackgroundSyncRegistration> registration) {
1205 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1206
1207 callback.Run(status, std::move(registration));
1208 op_scheduler_.CompleteOperationAndRunNext();
1209 }
1210
1211 void BackgroundSyncManager::CompleteStatusAndRegistrationsCallback(
1212 StatusAndRegistrationsCallback callback,
1213 BackgroundSyncStatus status,
1214 std::unique_ptr<ScopedVector<BackgroundSyncRegistration>> registrations) {
1215 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1216
1217 callback.Run(status, std::move(registrations));
1218 op_scheduler_.CompleteOperationAndRunNext();
1219 }
1220
1221 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { 1192 base::Closure BackgroundSyncManager::MakeEmptyCompletion() {
1222 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1193 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1223 1194 return op_scheduler_.WrapCallbackToRunNext(base::Bind(&base::DoNothing));
1224 return MakeClosureCompletion(base::Bind(base::DoNothing));
1225 }
1226
1227 base::Closure BackgroundSyncManager::MakeClosureCompletion(
1228 const base::Closure& callback) {
1229 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1230
1231 return base::Bind(
1232 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>,
1233 weak_ptr_factory_.GetWeakPtr(), callback);
1234 }
1235
1236 BackgroundSyncManager::StatusAndRegistrationCallback
1237 BackgroundSyncManager::MakeStatusAndRegistrationCompletion(
1238 const StatusAndRegistrationCallback& callback) {
1239 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1240
1241 return base::Bind(
1242 &BackgroundSyncManager::CompleteStatusAndRegistrationCallback,
1243 weak_ptr_factory_.GetWeakPtr(), callback);
1244 }
1245
1246 BackgroundSyncManager::StatusAndRegistrationsCallback
1247 BackgroundSyncManager::MakeStatusAndRegistrationsCompletion(
1248 const StatusAndRegistrationsCallback& callback) {
1249 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1250
1251 return base::Bind(
1252 &BackgroundSyncManager::CompleteStatusAndRegistrationsCallback,
1253 weak_ptr_factory_.GetWeakPtr(), callback);
1254 }
1255
1256 BackgroundSyncManager::StatusCallback
1257 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) {
1258 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1259
1260 return base::Bind(
1261 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback,
1262 BackgroundSyncStatus>,
1263 weak_ptr_factory_.GetWeakPtr(), callback);
1264 } 1195 }
1265 1196
1266 } // namespace content 1197 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/background_sync/background_sync_manager.h ('k') | content/browser/cache_storage/cache_storage.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698