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 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_OPERATION_H_
6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_OPERATION_H_
7 
8 #include "base/bind.h"
9 #include "base/callback.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/time/time.h"
14 #include "content/browser/cache_storage/cache_storage_scheduler_types.h"
15 #include "content/common/content_export.h"
16 
17 namespace content {
18 
19 // An operation to run in the CacheStorageScheduler. It's mostly just a closure
20 // to run plus a bunch of metrics data.
21 class CONTENT_EXPORT CacheStorageOperation {
22  public:
23   CacheStorageOperation(base::OnceClosure closure,
24                         CacheStorageSchedulerId id,
25                         CacheStorageSchedulerClient client_type,
26                         CacheStorageSchedulerMode mode,
27                         CacheStorageSchedulerOp op_type,
28                         CacheStorageSchedulerPriority priority,
29                         scoped_refptr<base::SequencedTaskRunner> task_runner);
30 
31   ~CacheStorageOperation();
32 
33   // Run the closure passed to the constructor.
34   void Run();
35 
creation_ticks()36   base::TimeTicks creation_ticks() const { return creation_ticks_; }
id()37   CacheStorageSchedulerId id() const { return id_; }
mode()38   CacheStorageSchedulerMode mode() const { return mode_; }
op_type()39   CacheStorageSchedulerOp op_type() const { return op_type_; }
priority()40   CacheStorageSchedulerPriority priority() const { return priority_; }
AsWeakPtr()41   base::WeakPtr<CacheStorageOperation> AsWeakPtr() {
42     return weak_ptr_factory_.GetWeakPtr();
43   }
44 
45  private:
46   void NotifyOperationSlow();
47 
48   // The operation's closure to run.
49   base::OnceClosure closure_;
50 
51   // Ticks at time of object creation.
52   base::TimeTicks creation_ticks_;
53 
54   // Ticks at time the operation's closure is run.
55   base::TimeTicks start_ticks_;
56 
57   // If the operation took a long time to run.
58   bool was_slow_ = false;
59 
60   const CacheStorageSchedulerId id_;
61   const CacheStorageSchedulerClient client_type_;
62   const CacheStorageSchedulerMode mode_;
63   const CacheStorageSchedulerOp op_type_;
64   const CacheStorageSchedulerPriority priority_;
65   scoped_refptr<base::SequencedTaskRunner> task_runner_;
66   base::WeakPtrFactory<CacheStorageOperation> weak_ptr_factory_{this};
67 
68   DISALLOW_COPY_AND_ASSIGN(CacheStorageOperation);
69 };
70 
71 }  // namespace content
72 
73 #endif  // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_OPERATION_H_
74