1 // Copyright 2016 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.
4 
5 #include "content/browser/cache_storage/cache_storage_operation.h"
6 
7 #include "content/browser/cache_storage/cache_storage_histogram_utils.h"
8 
9 namespace content {
10 
11 namespace {
12 const int kNumSecondsForSlowOperation = 10;
13 }
14 
CacheStorageOperation(base::OnceClosure closure,CacheStorageSchedulerId id,CacheStorageSchedulerClient client_type,CacheStorageSchedulerMode mode,CacheStorageSchedulerOp op_type,CacheStorageSchedulerPriority priority,scoped_refptr<base::SequencedTaskRunner> task_runner)15 CacheStorageOperation::CacheStorageOperation(
16     base::OnceClosure closure,
17     CacheStorageSchedulerId id,
18     CacheStorageSchedulerClient client_type,
19     CacheStorageSchedulerMode mode,
20     CacheStorageSchedulerOp op_type,
21     CacheStorageSchedulerPriority priority,
22     scoped_refptr<base::SequencedTaskRunner> task_runner)
23     : closure_(std::move(closure)),
24       creation_ticks_(base::TimeTicks::Now()),
25       id_(id),
26       client_type_(client_type),
27       mode_(mode),
28       op_type_(op_type),
29       priority_(priority),
30       task_runner_(std::move(task_runner)) {}
31 
~CacheStorageOperation()32 CacheStorageOperation::~CacheStorageOperation() {
33   RecordCacheStorageSchedulerUMA(CacheStorageSchedulerUMA::kOperationDuration,
34                                  client_type_, op_type_,
35                                  base::TimeTicks::Now() - start_ticks_);
36 
37   if (!was_slow_)
38     RecordCacheStorageSchedulerUMA(CacheStorageSchedulerUMA::kIsOperationSlow,
39                                    client_type_, op_type_, was_slow_);
40 }
41 
Run()42 void CacheStorageOperation::Run() {
43   start_ticks_ = base::TimeTicks::Now();
44 
45   task_runner_->PostDelayedTask(
46       FROM_HERE,
47       base::BindOnce(&CacheStorageOperation::NotifyOperationSlow,
48                      weak_ptr_factory_.GetWeakPtr()),
49       base::TimeDelta::FromSeconds(kNumSecondsForSlowOperation));
50   std::move(closure_).Run();
51 }
52 
NotifyOperationSlow()53 void CacheStorageOperation::NotifyOperationSlow() {
54   was_slow_ = true;
55   RecordCacheStorageSchedulerUMA(CacheStorageSchedulerUMA::kIsOperationSlow,
56                                  client_type_, op_type_, was_slow_);
57 }
58 
59 }  // namespace content
60