Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / cache_storage / cache_storage_scheduler.cc
blob33447b93787db708d73e5a79cb7dd1fa1c47f61a
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/message_loop/message_loop_proxy.h"
14 namespace content {
16 CacheStorageScheduler::CacheStorageScheduler() : operation_running_(false) {
19 CacheStorageScheduler::~CacheStorageScheduler() {
22 void CacheStorageScheduler::ScheduleOperation(const base::Closure& closure) {
23 pending_operations_.push_back(closure);
24 RunOperationIfIdle();
27 void CacheStorageScheduler::CompleteOperationAndRunNext() {
28 DCHECK(operation_running_);
29 operation_running_ = false;
30 RunOperationIfIdle();
33 bool CacheStorageScheduler::ScheduledOperations() const {
34 return operation_running_ || !pending_operations_.empty();
37 void CacheStorageScheduler::RunOperationIfIdle() {
38 if (!operation_running_ && !pending_operations_.empty()) {
39 operation_running_ = true;
40 // TODO(jkarlin): Run multiple operations in parallel where allowed.
41 base::Closure closure = pending_operations_.front();
42 pending_operations_.pop_front();
43 base::MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind(closure));
47 } // namespace content