Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / dom_storage / dom_storage_task_runner.h
blob1d4485c9bd801d534b6462d814ceececbb294c26
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/threading/sequenced_worker_pool.h"
11 #include "base/time/time.h"
12 #include "content/common/content_export.h"
14 namespace base {
15 class MessageLoopProxy;
18 namespace content {
20 // DOMStorage uses two task sequences (primary vs commit) to avoid
21 // primary access from queuing up behind commits to disk.
22 // * Initialization, shutdown, and administrative tasks are performed as
23 // shutdown-blocking primary sequence tasks.
24 // * Tasks directly related to the javascript'able interface are performed
25 // as shutdown-blocking primary sequence tasks.
26 // TODO(michaeln): Skip tasks for reading during shutdown.
27 // * Internal tasks related to committing changes to disk are performed as
28 // shutdown-blocking commit sequence tasks.
29 class CONTENT_EXPORT DOMStorageTaskRunner
30 : public base::TaskRunner {
31 public:
32 enum SequenceID {
33 PRIMARY_SEQUENCE,
34 COMMIT_SEQUENCE
37 // The PostTask() and PostDelayedTask() methods defined by TaskRunner
38 // post shutdown-blocking tasks on the primary sequence.
39 bool PostDelayedTask(const tracked_objects::Location& from_here,
40 const base::Closure& task,
41 base::TimeDelta delay) override = 0;
43 // Posts a shutdown blocking task to |sequence_id|.
44 virtual bool PostShutdownBlockingTask(
45 const tracked_objects::Location& from_here,
46 SequenceID sequence_id,
47 const base::Closure& task) = 0;
49 // The TaskRunner override returns true if the current thread is running
50 // on the primary sequence.
51 bool RunsTasksOnCurrentThread() const override;
53 // Returns true if the current thread is running on the given |sequence_id|.
54 virtual bool IsRunningOnSequence(SequenceID sequence_id) const = 0;
55 bool IsRunningOnPrimarySequence() const {
56 return IsRunningOnSequence(PRIMARY_SEQUENCE);
58 bool IsRunningOnCommitSequence() const {
59 return IsRunningOnSequence(COMMIT_SEQUENCE);
62 protected:
63 ~DOMStorageTaskRunner() override {}
66 // A derived class used in chromium that utilizes a SequenceWorkerPool
67 // under dom_storage specific SequenceTokens. The |delayed_task_loop|
68 // is used to delay scheduling on the worker pool.
69 class CONTENT_EXPORT DOMStorageWorkerPoolTaskRunner :
70 public DOMStorageTaskRunner {
71 public:
72 DOMStorageWorkerPoolTaskRunner(
73 base::SequencedWorkerPool* sequenced_worker_pool,
74 base::SequencedWorkerPool::SequenceToken primary_sequence_token,
75 base::SequencedWorkerPool::SequenceToken commit_sequence_token,
76 base::MessageLoopProxy* delayed_task_loop);
78 bool PostDelayedTask(const tracked_objects::Location& from_here,
79 const base::Closure& task,
80 base::TimeDelta delay) override;
82 bool PostShutdownBlockingTask(const tracked_objects::Location& from_here,
83 SequenceID sequence_id,
84 const base::Closure& task) override;
86 bool IsRunningOnSequence(SequenceID sequence_id) const override;
88 protected:
89 ~DOMStorageWorkerPoolTaskRunner() override;
91 private:
93 base::SequencedWorkerPool::SequenceToken IDtoToken(SequenceID id) const;
95 const scoped_refptr<base::MessageLoopProxy> message_loop_;
96 const scoped_refptr<base::SequencedWorkerPool> sequenced_worker_pool_;
97 base::SequencedWorkerPool::SequenceToken primary_sequence_token_;
98 base::SequencedWorkerPool::SequenceToken commit_sequence_token_;
101 // A derived class used in unit tests that ignores all delays so
102 // we don't block in unit tests waiting for timeouts to expire.
103 // There is no distinction between [non]-shutdown-blocking or
104 // the primary sequence vs the commit sequence in the mock,
105 // all tasks are scheduled on |message_loop| with zero delay.
106 class CONTENT_EXPORT MockDOMStorageTaskRunner :
107 public DOMStorageTaskRunner {
108 public:
109 explicit MockDOMStorageTaskRunner(base::MessageLoopProxy* message_loop);
111 bool PostDelayedTask(const tracked_objects::Location& from_here,
112 const base::Closure& task,
113 base::TimeDelta delay) override;
115 bool PostShutdownBlockingTask(const tracked_objects::Location& from_here,
116 SequenceID sequence_id,
117 const base::Closure& task) override;
119 bool IsRunningOnSequence(SequenceID sequence_id) const override;
121 protected:
122 ~MockDOMStorageTaskRunner() override;
124 private:
125 const scoped_refptr<base::MessageLoopProxy> message_loop_;
128 } // namespace content
130 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_H_