| OLD | NEW |
| 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/cache_storage/cache_storage_scheduler.h" | 5 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/location.h" | 10 #include "base/location.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "content/browser/cache_storage/cache_storage_histogram_macros.h" |
| 17 #include "content/browser/cache_storage/cache_storage_operation.h" |
| 13 | 18 |
| 14 namespace content { | 19 namespace content { |
| 15 | 20 |
| 16 CacheStorageScheduler::CacheStorageScheduler() | 21 CacheStorageScheduler::CacheStorageScheduler( |
| 17 : operation_running_(false), weak_ptr_factory_(this) {} | 22 CacheStorageSchedulerClient client_type) |
| 23 : client_type_(client_type), weak_ptr_factory_(this) {} |
| 18 | 24 |
| 19 CacheStorageScheduler::~CacheStorageScheduler() { | 25 CacheStorageScheduler::~CacheStorageScheduler() {} |
| 20 } | |
| 21 | 26 |
| 22 void CacheStorageScheduler::ScheduleOperation(const base::Closure& closure) { | 27 void CacheStorageScheduler::ScheduleOperation(const base::Closure& closure) { |
| 23 pending_operations_.push_back(closure); | 28 CACHE_STORAGE_SCHEDULER_UMA(COUNTS_10000, "QueueLength", client_type_, |
| 29 pending_operations_.size()); |
| 30 |
| 31 pending_operations_.push_back(base::MakeUnique<CacheStorageOperation>( |
| 32 closure, client_type_, base::ThreadTaskRunnerHandle::Get())); |
| 24 RunOperationIfIdle(); | 33 RunOperationIfIdle(); |
| 25 } | 34 } |
| 26 | 35 |
| 27 void CacheStorageScheduler::CompleteOperationAndRunNext() { | 36 void CacheStorageScheduler::CompleteOperationAndRunNext() { |
| 28 DCHECK(operation_running_); | 37 DCHECK(running_operation_); |
| 29 operation_running_ = false; | 38 running_operation_.reset(); |
| 39 |
| 30 RunOperationIfIdle(); | 40 RunOperationIfIdle(); |
| 31 } | 41 } |
| 32 | 42 |
| 33 bool CacheStorageScheduler::ScheduledOperations() const { | 43 bool CacheStorageScheduler::ScheduledOperations() const { |
| 34 return operation_running_ || !pending_operations_.empty(); | 44 return running_operation_ || !pending_operations_.empty(); |
| 35 } | 45 } |
| 36 | 46 |
| 37 void CacheStorageScheduler::RunOperationIfIdle() { | 47 void CacheStorageScheduler::RunOperationIfIdle() { |
| 38 if (!operation_running_ && !pending_operations_.empty()) { | 48 if (!running_operation_ && !pending_operations_.empty()) { |
| 39 operation_running_ = true; | |
| 40 // TODO(jkarlin): Run multiple operations in parallel where allowed. | 49 // TODO(jkarlin): Run multiple operations in parallel where allowed. |
| 41 base::Closure closure = pending_operations_.front(); | 50 running_operation_ = std::move(pending_operations_.front()); |
| 42 pending_operations_.pop_front(); | 51 pending_operations_.pop_front(); |
| 43 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 52 |
| 44 base::Bind(closure)); | 53 CACHE_STORAGE_SCHEDULER_UMA( |
| 54 TIMES, "QueueDuration", client_type_, |
| 55 base::TimeTicks::Now() - running_operation_->creation_ticks()); |
| 56 |
| 57 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 58 FROM_HERE, base::Bind(&CacheStorageOperation::Run, |
| 59 running_operation_->AsWeakPtr())); |
| 45 } | 60 } |
| 46 } | 61 } |
| 47 | 62 |
| 48 } // namespace content | 63 } // namespace content |
| OLD | NEW |