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_
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"
18 namespace file_system_provider
{
20 // Queues arbitrary tasks. At most |max_in_parallel_| tasks will be running at
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
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
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.
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
);
49 // Creates a token for enqueuing (and later aborting) tasks.
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
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
);
82 // Information about an enqueued task which hasn't been removed, nor aborted.
85 Task(size_t token
, const AbortableCallback
& callback
);
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.
97 const size_t max_in_parallel_
;
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_