[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / cache_storage / cache_storage_scheduler.cc
blob86db5f2dc8f8b95967e395f2aeda4c7de8f045e5
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
3 // found in the LICENSE file.
5 #include "content/browser/cache_storage/cache_storage_scheduler.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
15 namespace content {
17 CacheStorageScheduler::CacheStorageScheduler() : operation_running_(false) {
20 CacheStorageScheduler::~CacheStorageScheduler() {
23 void CacheStorageScheduler::ScheduleOperation(const base::Closure& closure) {
24 pending_operations_.push_back(closure);
25 RunOperationIfIdle();
28 void CacheStorageScheduler::CompleteOperationAndRunNext() {
29 DCHECK(operation_running_);
30 operation_running_ = false;
31 RunOperationIfIdle();
34 bool CacheStorageScheduler::ScheduledOperations() const {
35 return operation_running_ || !pending_operations_.empty();
38 void CacheStorageScheduler::RunOperationIfIdle() {
39 if (!operation_running_ && !pending_operations_.empty()) {
40 operation_running_ = true;
41 // TODO(jkarlin): Run multiple operations in parallel where allowed.
42 base::Closure closure = pending_operations_.front();
43 pending_operations_.pop_front();
44 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
45 base::Bind(closure));
49 } // namespace content