Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / dom_storage / dom_storage_task_runner.h
blobf71c9c8b63eb71a30aebf46ac7b760be09092f2a
1 // Copyright 2013 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 #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_H_
6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/sequenced_task_runner.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/threading/sequenced_worker_pool.h"
12 #include "base/time/time.h"
13 #include "content/common/content_export.h"
15 namespace base {
16 class SingleThreadTaskRunner;
19 namespace content {
21 // DOMStorage uses two task sequences (primary vs commit) to avoid
22 // primary access from queuing up behind commits to disk.
23 // * Initialization, shutdown, and administrative tasks are performed as
24 // shutdown-blocking primary sequence tasks.
25 // * Tasks directly related to the javascript'able interface are performed
26 // as shutdown-blocking primary sequence tasks.
27 // TODO(michaeln): Skip tasks for reading during shutdown.
28 // * Internal tasks related to committing changes to disk are performed as
29 // shutdown-blocking commit sequence tasks.
30 class CONTENT_EXPORT DOMStorageTaskRunner
31 : public base::TaskRunner {
32 public:
33 enum SequenceID {
34 PRIMARY_SEQUENCE,
35 COMMIT_SEQUENCE
38 // The PostTask() and PostDelayedTask() methods defined by TaskRunner
39 // post shutdown-blocking tasks on the primary sequence.
40 bool PostDelayedTask(const tracked_objects::Location& from_here,
41 const base::Closure& task,
42 base::TimeDelta delay) override = 0;
44 // Posts a shutdown blocking task to |sequence_id|.
45 virtual bool PostShutdownBlockingTask(
46 const tracked_objects::Location& from_here,
47 SequenceID sequence_id,
48 const base::Closure& task) = 0;
50 // The TaskRunner override returns true if the current thread is running
51 // on the primary sequence.
52 bool RunsTasksOnCurrentThread() const override;
54 // Returns true if the current thread is running on the given |sequence_id|.
55 virtual bool IsRunningOnSequence(SequenceID sequence_id) const = 0;
56 bool IsRunningOnPrimarySequence() const {
57 return IsRunningOnSequence(PRIMARY_SEQUENCE);
59 bool IsRunningOnCommitSequence() const {
60 return IsRunningOnSequence(COMMIT_SEQUENCE);
63 protected:
64 ~DOMStorageTaskRunner() override {}
67 // A derived class used in chromium that utilizes a SequenceWorkerPool
68 // under dom_storage specific SequenceTokens. The |delayed_task_loop|
69 // is used to delay scheduling on the worker pool.
70 class CONTENT_EXPORT DOMStorageWorkerPoolTaskRunner :
71 public DOMStorageTaskRunner {
72 public:
73 DOMStorageWorkerPoolTaskRunner(
74 base::SequencedWorkerPool* sequenced_worker_pool,
75 base::SequencedWorkerPool::SequenceToken primary_sequence_token,
76 base::SequencedWorkerPool::SequenceToken commit_sequence_token,
77 base::SingleThreadTaskRunner* delayed_task_task_runner);
79 bool PostDelayedTask(const tracked_objects::Location& from_here,
80 const base::Closure& task,
81 base::TimeDelta delay) override;
83 bool PostShutdownBlockingTask(const tracked_objects::Location& from_here,
84 SequenceID sequence_id,
85 const base::Closure& task) override;
87 bool IsRunningOnSequence(SequenceID sequence_id) const override;
89 protected:
90 ~DOMStorageWorkerPoolTaskRunner() override;
92 private:
94 base::SequencedWorkerPool::SequenceToken IDtoToken(SequenceID id) const;
96 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
97 const scoped_refptr<base::SequencedWorkerPool> sequenced_worker_pool_;
98 base::SequencedWorkerPool::SequenceToken primary_sequence_token_;
99 base::SequencedWorkerPool::SequenceToken commit_sequence_token_;
102 // A derived class used in unit tests that ignores all delays so
103 // we don't block in unit tests waiting for timeouts to expire.
104 // There is no distinction between [non]-shutdown-blocking or
105 // the primary sequence vs the commit sequence in the mock,
106 // all tasks are scheduled on |message_loop| with zero delay.
107 class CONTENT_EXPORT MockDOMStorageTaskRunner :
108 public DOMStorageTaskRunner {
109 public:
110 explicit MockDOMStorageTaskRunner(base::SingleThreadTaskRunner* task_runner);
112 bool PostDelayedTask(const tracked_objects::Location& from_here,
113 const base::Closure& task,
114 base::TimeDelta delay) override;
116 bool PostShutdownBlockingTask(const tracked_objects::Location& from_here,
117 SequenceID sequence_id,
118 const base::Closure& task) override;
120 bool IsRunningOnSequence(SequenceID sequence_id) const override;
122 protected:
123 ~MockDOMStorageTaskRunner() override;
125 private:
126 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
129 } // namespace content
131 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_H_