Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / queue.h
blobadc1684e138ca12ead4f215f398c107c50f7599d
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.
28 // If the task supports aborting (it's abort callback is not NULL), then an
29 // enqueued task can be aborted with Abort() at any time as long as the task is
30 // not completed.
32 // Once a task is executed, it must be marked as completed with Complete(). If
33 // it's aborted before executing, no call to Complete() can happen. Simply
34 // saying, just call Complete() from the completion callback of the task.
35 class Queue {
36 public:
37 typedef base::Callback<AbortCallback(void)> AbortableCallback;
39 // Creates a queue with a maximum number of tasks running in parallel.
40 explicit Queue(size_t max_in_parallel);
42 virtual ~Queue();
44 // Creates a token for enqueuing (and later aborting) tasks.
45 size_t NewToken();
47 // Enqueues a task using a token generated with NewToken(). The task will be
48 // executed if there is space in the internal queue, otherwise it will wait
49 // until another task is finished. Once the task is finished, Complete() must
50 // be called. The callback's abort callback may be NULL. In such case, Abort()
51 // must not be called.
52 void Enqueue(size_t token, const AbortableCallback& callback);
54 // Forcibly aborts a previously enqueued task. May be called at any time as
55 // long as the task is still in the queue and is not marked as completed.
56 void Abort(size_t token);
58 // Marks an executed task with |token| as completed. Must be called once the
59 // task is executed. Simply saying, in most cases it should be just called
60 // from the task's completion callback.
61 void Complete(size_t token);
63 private:
64 // Information about an enqueued task which hasn't been removed, nor aborted.
65 struct Task {
66 Task();
67 Task(size_t token, const AbortableCallback& callback);
68 ~Task();
70 size_t token;
71 AbortableCallback callback;
72 AbortCallback abort_callback;
75 // Runs the next task from the pending queue if there is less than
76 // |max_in_parallel_| tasks running at once.
77 void MaybeRun();
79 const size_t max_in_parallel_;
80 size_t next_token_;
81 std::deque<Task> pending_;
82 std::map<int, Task> executed_;
84 base::WeakPtrFactory<Queue> weak_ptr_factory_;
85 DISALLOW_COPY_AND_ASSIGN(Queue);
88 } // namespace file_system_provider
89 } // namespace chromeos
91 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_