ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / queue.h
blob559dcd655cfcfadf1273abf8930fb3faacf59da2
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.
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_
8 #include <deque>
9 #include <map>
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"
17 namespace chromeos {
18 namespace file_system_provider {
20 // Queues arbitrary tasks. At most |max_in_parallel_| tasks will be running at
21 // once.
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.
30 // Enqueued tasks can be aborted with Abort() at any time until they are marked
31 // as completed or removed from the queue, as long as the task supports aborting
32 // (it's abort callback is not NULL). Aorting does not remove the task from the
33 // queue.
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.
40 class Queue {
41 public:
42 typedef base::Callback<AbortCallback(void)> AbortableCallback;
44 // Creates a queue with a maximum number of tasks running in parallel.
45 explicit Queue(size_t max_in_parallel);
47 virtual ~Queue();
49 // Creates a token for enqueuing (and later aborting) tasks.
50 size_t NewToken();
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. The callback's abort callback may be NULL. In
56 // such case, Abort() must not be called.
57 void Enqueue(size_t token, const AbortableCallback& callback);
59 // Forcibly aborts a previously enqueued task. May be called at any time as
60 // long as the task is still in the queue and is not marked as completed.
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);
66 // Returns true if the task which is in the queue with |token| has been
67 // aborted. This method must not be called for tasks which are not in the
68 // queue.
69 bool IsAborted(size_t token);
71 // Marks the previously enqueued task as complete. Must be called for each
72 // enqueued task (unless aborted). Note, that Remove() must be called in order
73 // to remove the task from the queue if it hasn't been aborted earlier.
74 // It must not be called more than one, nor for aborted tasks.
75 void Complete(size_t token);
77 // Removes the previously enqueued and completed or aborted task from the
78 // queue. Must not be called more than once.
79 void Remove(size_t token);
81 private:
82 // Information about an enqueued task which hasn't been removed, nor aborted.
83 struct Task {
84 Task();
85 Task(size_t token, const AbortableCallback& callback);
86 ~Task();
88 size_t token;
89 AbortableCallback callback;
90 AbortCallback abort_callback;
93 // Runs the next task from the pending queue if there is less than
94 // |max_in_parallel_| tasks running at once.
95 void MaybeRun();
97 const size_t max_in_parallel_;
98 size_t next_token_;
99 std::deque<Task> pending_;
100 std::map<int, Task> executed_;
101 std::map<int, Task> completed_;
102 std::map<int, Task> aborted_;
104 base::WeakPtrFactory<Queue> weak_ptr_factory_;
105 DISALLOW_COPY_AND_ASSIGN(Queue);
108 } // namespace file_system_provider
109 } // namespace chromeos
111 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_