OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ |
| 7 |
| 8 #include <deque> |
| 9 #include <map> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h" |
| 15 #include "storage/browser/fileapi/async_file_util.h" |
| 16 |
| 17 namespace chromeos { |
| 18 namespace file_system_provider { |
| 19 |
| 20 // Queues arbitrary tasks. At most |max_in_parallel_| tasks will be running at |
| 21 // once. |
| 22 // |
| 23 // The common use case is: |
| 24 // 1. Call NewToken() to obtain the token used bo all other methods. |
| 25 // 2. Call Enqueue() to enqueue the task. |
| 26 // 3. Call Complete() when the task is completed. |
| 27 // 4. Call Remove() to remove a completed task from the queue and run other |
| 28 // enqueued tasks. |
| 29 // |
| 30 // Enqueued tasks can be aborted with the callback returned by Enqueue() any |
| 31 // time until they are marked as completed. Aborted tasks are automatically |
| 32 // removed from the queue if they were not executed yet, or executed but not |
| 33 // completed. |
| 34 // |
| 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 |
| 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 |
| 39 // called at most once, and they must not be called for aborted tasks. |
| 40 class Queue { |
| 41 public: |
| 42 typedef base::Callback<AbortCallback(void)> AbortableCallback; |
| 43 |
| 44 // Creates a queue with a maximum number of tasks running in parallel. |
| 45 explicit Queue(size_t max_in_parallel); |
| 46 |
| 47 virtual ~Queue(); |
| 48 |
| 49 // Creates a token for enqueuing (and later aborting) tasks. |
| 50 size_t NewToken(); |
| 51 |
| 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 |
| 54 // until another task is finished. Once the task is finished, Complete() and |
| 55 // Remove() must be called. |
| 56 // |
| 57 // The returned callback can be called to abort the task at any time. Once |
| 58 // aborted, the task is automatically removed from the queue if the task |
| 59 // was not executed, or executed but not completed, despite the result of |
| 60 // |callback|'s aborting closure. |
| 61 AbortCallback Enqueue(size_t token, const AbortableCallback& callback); |
| 62 |
| 63 // 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 |
| 65 // 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. |
| 67 void Complete(size_t token); |
| 68 |
| 69 // Removes the previously enqueued and completed task from the queue. Must not |
| 70 // be called for aborted, or not completed tasks. Must not be called more than |
| 71 // once. |
| 72 void Remove(size_t token); |
| 73 |
| 74 private: |
| 75 struct Task; |
| 76 |
| 77 // Runs the next task from the pending queue if there is less than |
| 78 // |max_in_parallel_| tasks running at once. |
| 79 void MaybeRun(); |
| 80 |
| 81 // Aborts a previously enqueued task. Returns the result asynchronously via |
| 82 // |callback|. May be called at any time, but if already completed then |
| 83 // FILE_ERROR_INVALID_OPERATION error code will be returned. |
| 84 void Abort(size_t token, |
| 85 const storage::AsyncFileUtil::StatusCallback& callback); |
| 86 |
| 87 const size_t max_in_parallel_; |
| 88 size_t next_token_; |
| 89 std::deque<Task> pending_; |
| 90 std::map<int, Task> executed_; |
| 91 |
| 92 base::WeakPtrFactory<Queue> weak_ptr_factory_; |
| 93 DISALLOW_COPY_AND_ASSIGN(Queue); |
| 94 }; |
| 95 |
| 96 } // namespace file_system_provider |
| 97 } // namespace chromeos |
| 98 |
| 99 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ |
OLD | NEW |