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.
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
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.
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
);
44 // Creates a token for enqueuing (and later aborting) tasks.
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
);
64 // Information about an enqueued task which hasn't been removed, nor aborted.
67 Task(size_t token
, const AbortableCallback
& callback
);
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.
79 const size_t max_in_parallel_
;
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_