OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ |
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ | 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h" | 14 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h" |
15 #include "storage/browser/fileapi/async_file_util.h" | 15 #include "storage/browser/fileapi/async_file_util.h" |
16 | 16 |
17 namespace chromeos { | 17 namespace chromeos { |
18 namespace file_system_provider { | 18 namespace file_system_provider { |
19 | 19 |
20 // Queues arbitrary tasks. At most |max_in_parallel_| tasks will be running at | 20 // Queues arbitrary tasks. At most |max_in_parallel_| tasks will be running at |
21 // once. | 21 // once. |
22 // | 22 // |
23 // The common use case is: | 23 // The common use case is: |
24 // 1. Call NewToken() to obtain the token used bo all other methods. | 24 // 1. Call NewToken() to obtain the token used bo all other methods. |
25 // 2. Call Enqueue() to enqueue the task. | 25 // 2. Call Enqueue() to enqueue the task. |
26 // 3. Call Complete() when the task is completed. | 26 // 3. Call Complete() when the task is completed. |
27 // 4. Call Remove() to remove a completed task from the queue and run other | 27 // 4. Call Remove() to remove a completed task from the queue and run other |
28 // enqueued tasks. | 28 // enqueued tasks. |
29 // | 29 // |
30 // Enqueued tasks can be aborted with the callback returned by Enqueue() any | 30 // Enqueued tasks can be aborted with Abort() at any time until they are marked |
31 // time until they are marked as completed. Aborted tasks are automatically | 31 // as completed or removed from the queue, as long as the task supports aborting |
32 // removed from the queue if they were not executed yet, or executed but not | 32 // (it's abort callback is not NULL). Aorting does not remove the task from the |
33 // completed. | 33 // queue. |
34 // | 34 // |
35 // In most cases you'll want to call Remove() and Complete() one after the | 35 // In most cases you'll want to call Remove() and Complete() one after the |
36 // other. However, in some cases you may want to separate it. Eg. for limiting | 36 // other. However, in some cases you may want to separate it. Eg. for limiting |
37 // number of opened files, you may want to call Complete() after opening is | 37 // number of opened files, you may want to call Complete() after opening is |
38 // completed, but Remove() after the file is closed. Note, that they can be | 38 // completed, but Remove() after the file is closed. Note, that they can be |
39 // called at most once, and they must not be called for aborted tasks. | 39 // called at most once. |
40 class Queue { | 40 class Queue { |
41 public: | 41 public: |
42 typedef base::Callback<AbortCallback(void)> AbortableCallback; | 42 typedef base::Callback<AbortCallback(void)> AbortableCallback; |
43 | 43 |
44 // Creates a queue with a maximum number of tasks running in parallel. | 44 // Creates a queue with a maximum number of tasks running in parallel. |
45 explicit Queue(size_t max_in_parallel); | 45 explicit Queue(size_t max_in_parallel); |
46 | 46 |
47 virtual ~Queue(); | 47 virtual ~Queue(); |
48 | 48 |
49 // Creates a token for enqueuing (and later aborting) tasks. | 49 // Creates a token for enqueuing (and later aborting) tasks. |
50 size_t NewToken(); | 50 size_t NewToken(); |
51 | 51 |
52 // Enqueues a task using a token generated with NewToken(). The task will be | 52 // Enqueues a task using a token generated with NewToken(). The task will be |
53 // executed if there is space in the internal queue, otherwise it will wait | 53 // executed if there is space in the internal queue, otherwise it will wait |
54 // until another task is finished. Once the task is finished, Complete() and | 54 // until another task is finished. Once the task is finished, Complete() and |
55 // Remove() must be called. | 55 // Remove() must be called. The callback's abort callback may be NULL. In |
56 // | 56 // such case, Abort() must not be called. |
57 // The returned callback can be called to abort the task at any time. Once | 57 void Enqueue(size_t token, const AbortableCallback& callback); |
58 // aborted, the task is automatically removed from the queue if the task | 58 |
59 // was not executed, or executed but not completed, despite the result of | 59 // Forcibly aborts a previously enqueued task. May be called at any time as |
60 // |callback|'s aborting closure. | 60 // long as the task is still in the queue and is not marked as completed. |
61 AbortCallback Enqueue(size_t token, const AbortableCallback& callback); | 61 // Note, that Remove() must be called in order to remove the task from the |
| 62 // queue. Must not be called if the task doesn't support aborting (it's |
| 63 // abort callback is NULL). |
| 64 void Abort(size_t token); |
62 | 65 |
63 // Marks the previously enqueued task as complete. Must be called for each | 66 // Marks the previously enqueued task as complete. Must be called for each |
64 // enqueued task (unless aborted). Note, that Remove() must be called in order | 67 // enqueued task (unless aborted). Note, that Remove() must be called in order |
65 // to remove the task from the queue if it hasn't been aborted earlier. | 68 // to remove the task from the queue if it hasn't been aborted earlier. |
66 // It must not be called more than one, nor for aborted tasks. | 69 // It must not be called more than one, nor for aborted tasks. |
67 void Complete(size_t token); | 70 void Complete(size_t token); |
68 | 71 |
69 // Removes the previously enqueued and completed task from the queue. Must not | 72 // Removes the previously enqueued and completed or aborted task from the |
70 // be called for aborted, or not completed tasks. Must not be called more than | 73 // queue. Must not be called more than once. |
71 // once. | |
72 void Remove(size_t token); | 74 void Remove(size_t token); |
73 | 75 |
74 private: | 76 private: |
75 // Information about an enqueued task which hasn't been removed, nor aborted. | 77 // Information about an enqueued task which hasn't been removed, nor aborted. |
76 struct Task { | 78 struct Task { |
77 Task(); | 79 Task(); |
78 Task(size_t token, const AbortableCallback& callback); | 80 Task(size_t token, const AbortableCallback& callback); |
79 ~Task(); | 81 ~Task(); |
80 | 82 |
81 size_t token; | 83 size_t token; |
82 bool completed; | |
83 AbortableCallback callback; | 84 AbortableCallback callback; |
84 AbortCallback abort_callback; | 85 AbortCallback abort_callback; |
85 }; | 86 }; |
86 | 87 |
87 // Runs the next task from the pending queue if there is less than | 88 // Runs the next task from the pending queue if there is less than |
88 // |max_in_parallel_| tasks running at once. | 89 // |max_in_parallel_| tasks running at once. |
89 void MaybeRun(); | 90 void MaybeRun(); |
90 | 91 |
91 // Aborts a previously enqueued task. Returns the result asynchronously via | |
92 // |callback|. May be called at any time, but if already completed then | |
93 // FILE_ERROR_INVALID_OPERATION error code will be returned. | |
94 void Abort(size_t token, | |
95 const storage::AsyncFileUtil::StatusCallback& callback); | |
96 | |
97 const size_t max_in_parallel_; | 92 const size_t max_in_parallel_; |
98 size_t next_token_; | 93 size_t next_token_; |
99 std::deque<Task> pending_; | 94 std::deque<Task> pending_; |
100 std::map<int, Task> executed_; | 95 std::map<int, Task> executed_; |
| 96 std::map<int, Task> completed_; |
| 97 std::map<int, Task> aborted_; |
101 | 98 |
102 base::WeakPtrFactory<Queue> weak_ptr_factory_; | 99 base::WeakPtrFactory<Queue> weak_ptr_factory_; |
103 DISALLOW_COPY_AND_ASSIGN(Queue); | 100 DISALLOW_COPY_AND_ASSIGN(Queue); |
104 }; | 101 }; |
105 | 102 |
106 } // namespace file_system_provider | 103 } // namespace file_system_provider |
107 } // namespace chromeos | 104 } // namespace chromeos |
108 | 105 |
109 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ | 106 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ |
OLD | NEW |