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/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
| 11 #include "base/time.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 class MessageLoopProxy; | 14 class MessageLoopProxy; |
14 } | 15 } |
15 | 16 |
16 namespace dom_storage { | 17 namespace dom_storage { |
17 | 18 |
18 // Tasks must run serially with respect to one another, but may | 19 // Tasks must run serially with respect to one another, but may |
19 // execute on different OS threads. The base class is implemented | 20 // execute on different OS threads. The base class is implemented |
20 // in terms of a MessageLoopProxy for use in testing. | 21 // in terms of a MessageLoopProxy for use in testing. |
21 class DomStorageTaskRunner | 22 class DomStorageTaskRunner |
22 : public base::RefCountedThreadSafe<DomStorageTaskRunner> { | 23 : public base::RefCountedThreadSafe<DomStorageTaskRunner> { |
23 public: | 24 public: |
24 explicit DomStorageTaskRunner(base::MessageLoopProxy* message_loop); | 25 explicit DomStorageTaskRunner(base::MessageLoopProxy* message_loop); |
25 virtual ~DomStorageTaskRunner(); | 26 virtual ~DomStorageTaskRunner(); |
26 | 27 |
27 // Schedules a task to be run immediately. | 28 // Schedules a task to be run immediately. |
28 virtual bool PostTask( | 29 virtual bool PostTask( |
29 const tracked_objects::Location& from_here, | 30 const tracked_objects::Location& from_here, |
30 const base::Closure& task); | 31 const base::Closure& task); |
31 | 32 |
32 // Schedules a task to be run after a delay. | 33 // Schedules a task to be run after a delay. |
33 virtual bool PostDelayedTask( | 34 virtual bool PostDelayedTask( |
34 const tracked_objects::Location& from_here, | 35 const tracked_objects::Location& from_here, |
35 const base::Closure& task, | 36 const base::Closure& task, |
36 base::TimeDelta delay); | 37 base::TimeDelta delay); |
37 | 38 |
38 protected: | 39 protected: |
39 scoped_refptr<base::MessageLoopProxy> message_loop_; | 40 const scoped_refptr<base::MessageLoopProxy> message_loop_; |
40 }; | 41 }; |
41 | 42 |
42 // A derived class that utlizes the SequenceWorkerPool under a | 43 // A derived class that utlizes the SequenceWorkerPool under a |
43 // dom_storage specific SequenceToken. The MessageLoopProxy | 44 // dom_storage specific SequenceToken. The MessageLoopProxy |
44 // is used to delay scheduling on the worker pool. | 45 // is used to delay scheduling on the worker pool. |
45 class DomStorageWorkerPoolTaskRunner : public DomStorageTaskRunner { | 46 class DomStorageWorkerPoolTaskRunner : public DomStorageTaskRunner { |
46 public: | 47 public: |
47 DomStorageWorkerPoolTaskRunner( | 48 DomStorageWorkerPoolTaskRunner( |
48 base::SequencedWorkerPool* sequenced_worker_pool, | 49 base::SequencedWorkerPool* sequenced_worker_pool, |
49 base::MessageLoopProxy* delayed_task_loop); | 50 base::MessageLoopProxy* delayed_task_loop); |
50 virtual ~DomStorageWorkerPoolTaskRunner(); | 51 virtual ~DomStorageWorkerPoolTaskRunner(); |
51 | 52 |
52 // Schedules a sequenced worker task to be run immediately. | 53 // Schedules a sequenced worker task to be run immediately. |
53 virtual bool PostTask( | 54 virtual bool PostTask( |
54 const tracked_objects::Location& from_here, | 55 const tracked_objects::Location& from_here, |
55 const base::Closure& task) OVERRIDE; | 56 const base::Closure& task) OVERRIDE; |
56 | 57 |
57 // Schedules a sequenced worker task to be run after a delay. | 58 // Schedules a sequenced worker task to be run after a delay. |
58 virtual bool PostDelayedTask( | 59 virtual bool PostDelayedTask( |
59 const tracked_objects::Location& from_here, | 60 const tracked_objects::Location& from_here, |
60 const base::Closure& task, | 61 const base::Closure& task, |
61 base::TimeDelta delay) OVERRIDE; | 62 base::TimeDelta delay) OVERRIDE; |
62 | 63 |
63 private: | 64 private: |
64 base::SequencedWorkerPool* sequenced_worker_pool_; // not owned | 65 const scoped_refptr<base::SequencedWorkerPool> sequenced_worker_pool_; |
65 base::SequencedWorkerPool::SequenceToken sequence_token_; | 66 base::SequencedWorkerPool::SequenceToken sequence_token_; |
66 }; | 67 }; |
67 | 68 |
68 // A derived class used in unit tests that causes us to ignore the | 69 // A derived class used in unit tests that causes us to ignore the |
69 // delay in PostDelayedTask so we don't need to block in unit tests | 70 // delay in PostDelayedTask so we don't need to block in unit tests |
70 // for the timeout to expire. | 71 // for the timeout to expire. |
71 class MockDomStorageTaskRunner : public DomStorageTaskRunner { | 72 class MockDomStorageTaskRunner : public DomStorageTaskRunner { |
72 public: | 73 public: |
73 explicit MockDomStorageTaskRunner(base::MessageLoopProxy* message_loop); | 74 explicit MockDomStorageTaskRunner(base::MessageLoopProxy* message_loop); |
74 virtual ~MockDomStorageTaskRunner() { } | 75 virtual ~MockDomStorageTaskRunner() { } |
75 | 76 |
76 virtual bool PostDelayedTask( | 77 virtual bool PostDelayedTask( |
77 const tracked_objects::Location& from_here, | 78 const tracked_objects::Location& from_here, |
78 const base::Closure& task, | 79 const base::Closure& task, |
79 base::TimeDelta delay) OVERRIDE; | 80 base::TimeDelta delay) OVERRIDE; |
80 }; | 81 }; |
81 | 82 |
82 } // namespace dom_storage | 83 } // namespace dom_storage |
83 | 84 |
84 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_ | 85 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_TASK_RUNNER_ |
OLD | NEW |