OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef WEBKIT_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_ | 5 #ifndef WEBKIT_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_ |
6 #define WEBKIT_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_ | 6 #define WEBKIT_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/task_runner.h" | |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
11 #include "base/time.h" | 12 #include "base/time.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 class MessageLoopProxy; | 15 class MessageLoopProxy; |
15 } | 16 } |
16 | 17 |
17 namespace dom_storage { | 18 namespace dom_storage { |
18 | 19 |
19 // Tasks must run serially with respect to one another, but may | 20 // Tasks must run serially with respect to one another, but may |
20 // execute on different OS threads. The base class is implemented | 21 // execute on different OS threads. The base class is implemented |
21 // in terms of a MessageLoopProxy for use in testing. | 22 // in terms of a MessageLoopProxy. |
22 class DomStorageTaskRunner | 23 class DomStorageTaskRunner : public base::TaskRunner { |
23 : public base::RefCountedThreadSafe<DomStorageTaskRunner> { | |
24 public: | 24 public: |
25 explicit DomStorageTaskRunner(base::MessageLoopProxy* message_loop); | 25 explicit DomStorageTaskRunner(base::MessageLoopProxy* message_loop); |
26 virtual ~DomStorageTaskRunner(); | 26 virtual ~DomStorageTaskRunner(); |
27 | 27 |
28 // Schedules a task to be run immediately. | 28 // The PostTask() method, defined by TaskRunner, Schedules a task |
29 virtual bool PostTask( | 29 // to run immediately. |
30 const tracked_objects::Location& from_here, | |
31 const base::Closure& task); | |
32 | 30 |
33 // Schedules a task to be run after a delay. | 31 // Schedules a task to be run after a delay. |
34 virtual bool PostDelayedTask( | 32 virtual bool PostDelayedTask( |
35 const tracked_objects::Location& from_here, | 33 const tracked_objects::Location& from_here, |
36 const base::Closure& task, | 34 const base::Closure& task, |
37 base::TimeDelta delay); | 35 base::TimeDelta delay); |
38 | 36 |
37 // DEPRECATED: Only here because base::TaskRunner requires it, implemented | |
38 // by calling the virtual PostDelayedTask(..., TimeDelta) variant. | |
benm (inactive)
2012/03/13 11:09:56
Why not just use this delay_ms variant and lose th
| |
39 virtual bool PostDelayedTask( | |
40 const tracked_objects::Location& from_here, | |
41 const base::Closure& task, | |
42 int64 delay_ms); | |
43 | |
44 // Only here because base::TaskRunner requires it, the return | |
45 // value is hard coded to true. | |
46 virtual bool RunsTasksOnCurrentThread() const; | |
47 | |
39 protected: | 48 protected: |
40 const scoped_refptr<base::MessageLoopProxy> message_loop_; | 49 const scoped_refptr<base::MessageLoopProxy> message_loop_; |
41 }; | 50 }; |
42 | 51 |
43 // A derived class that utlizes the SequenceWorkerPool under a | 52 // A derived class that utlizes the SequenceWorkerPool under a |
44 // dom_storage specific SequenceToken. The MessageLoopProxy | 53 // dom_storage specific SequenceToken. The MessageLoopProxy |
45 // is used to delay scheduling on the worker pool. | 54 // is used to delay scheduling on the worker pool. |
46 class DomStorageWorkerPoolTaskRunner : public DomStorageTaskRunner { | 55 class DomStorageWorkerPoolTaskRunner : public DomStorageTaskRunner { |
47 public: | 56 public: |
48 DomStorageWorkerPoolTaskRunner( | 57 DomStorageWorkerPoolTaskRunner( |
49 base::SequencedWorkerPool* sequenced_worker_pool, | 58 base::SequencedWorkerPool* sequenced_worker_pool, |
59 base::SequencedWorkerPool::SequenceToken sequence_token, | |
50 base::MessageLoopProxy* delayed_task_loop); | 60 base::MessageLoopProxy* delayed_task_loop); |
51 virtual ~DomStorageWorkerPoolTaskRunner(); | 61 virtual ~DomStorageWorkerPoolTaskRunner(); |
52 | 62 |
53 // Schedules a sequenced worker task to be run immediately. | |
54 virtual bool PostTask( | |
55 const tracked_objects::Location& from_here, | |
56 const base::Closure& task) OVERRIDE; | |
57 | |
58 // Schedules a sequenced worker task to be run after a delay. | 63 // Schedules a sequenced worker task to be run after a delay. |
59 virtual bool PostDelayedTask( | 64 virtual bool PostDelayedTask( |
60 const tracked_objects::Location& from_here, | 65 const tracked_objects::Location& from_here, |
61 const base::Closure& task, | 66 const base::Closure& task, |
62 base::TimeDelta delay) OVERRIDE; | 67 base::TimeDelta delay) OVERRIDE; |
63 | 68 |
69 base::SequencedWorkerPool::SequenceToken sequence_token() const; | |
70 | |
64 private: | 71 private: |
65 const scoped_refptr<base::SequencedWorkerPool> sequenced_worker_pool_; | 72 const scoped_refptr<base::SequencedWorkerPool> sequenced_worker_pool_; |
66 base::SequencedWorkerPool::SequenceToken sequence_token_; | 73 base::SequencedWorkerPool::SequenceToken sequence_token_; |
67 }; | 74 }; |
68 | 75 |
69 // A derived class used in unit tests that causes us to ignore the | 76 // A derived class used in unit tests that causes us to ignore the |
70 // delay in PostDelayedTask so we don't need to block in unit tests | 77 // delay in PostDelayedTask so we don't need to block in unit tests |
71 // for the timeout to expire. | 78 // for the timeout to expire. |
72 class MockDomStorageTaskRunner : public DomStorageTaskRunner { | 79 class MockDomStorageTaskRunner : public DomStorageTaskRunner { |
73 public: | 80 public: |
74 explicit MockDomStorageTaskRunner(base::MessageLoopProxy* message_loop); | 81 explicit MockDomStorageTaskRunner(base::MessageLoopProxy* message_loop); |
75 virtual ~MockDomStorageTaskRunner() { } | 82 virtual ~MockDomStorageTaskRunner() {} |
76 | 83 |
77 virtual bool PostDelayedTask( | 84 virtual bool PostDelayedTask( |
78 const tracked_objects::Location& from_here, | 85 const tracked_objects::Location& from_here, |
79 const base::Closure& task, | 86 const base::Closure& task, |
80 base::TimeDelta delay) OVERRIDE; | 87 base::TimeDelta delay) OVERRIDE; |
81 }; | 88 }; |
82 | 89 |
83 } // namespace dom_storage | 90 } // namespace dom_storage |
84 | 91 |
85 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_ | 92 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_ |
OLD | NEW |