Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: chrome/browser/chromeos/file_system_provider/queue.h

Issue 971303003: Simplify file_system_provider::Queue. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_
7 7
8 #include <deque> 8 #include <deque>
9 #include <map> 9 #include <map>
10 10
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h" 14 #include "chrome/browser/chromeos/file_system_provider/abort_callback.h"
15 #include "storage/browser/fileapi/async_file_util.h" 15 #include "storage/browser/fileapi/async_file_util.h"
16 16
17 namespace chromeos { 17 namespace chromeos {
18 namespace file_system_provider { 18 namespace file_system_provider {
19 19
20 // Queues arbitrary tasks. At most |max_in_parallel_| tasks will be running at 20 // Queues arbitrary tasks. At most |max_in_parallel_| tasks will be running at
21 // once. 21 // once.
22 // 22 //
23 // The common use case is: 23 // The common use case is:
24 // 1. Call NewToken() to obtain the token used bo all other methods. 24 // 1. Call NewToken() to obtain the token used bo all other methods.
25 // 2. Call Enqueue() to enqueue the task. 25 // 2. Call Enqueue() to enqueue the task.
26 // 3. Call Complete() when the task is completed. 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 // 27 //
30 // Enqueued tasks can be aborted with Abort() at any time until they are marked 28 // If the task supports aborting (it's abort callback is not NULL), then an
31 // as completed or removed from the queue, as long as the task supports aborting 29 // enqueued task can be aborted with Abort() at any time as long as the task is
32 // (it's abort callback is not NULL). Aorting does not remove the task from the 30 // not completed.
33 // queue.
34 // 31 //
35 // In most cases you'll want to call Remove() and Complete() one after the 32 // Once a task is executed, it must be marked as completed with Complete(). If
36 // other. However, in some cases you may want to separate it. Eg. for limiting 33 // it's aborted before executing, no call to Complete() can happen. Simply
37 // number of opened files, you may want to call Complete() after opening is 34 // saying, just call Complete() from the completion callback of the task.
38 // completed, but Remove() after the file is closed. Note, that they can be
39 // called at most once.
40 class Queue { 35 class Queue {
41 public: 36 public:
42 typedef base::Callback<AbortCallback(void)> AbortableCallback; 37 typedef base::Callback<AbortCallback(void)> AbortableCallback;
43 38
44 // Creates a queue with a maximum number of tasks running in parallel. 39 // Creates a queue with a maximum number of tasks running in parallel.
45 explicit Queue(size_t max_in_parallel); 40 explicit Queue(size_t max_in_parallel);
46 41
47 virtual ~Queue(); 42 virtual ~Queue();
48 43
49 // Creates a token for enqueuing (and later aborting) tasks. 44 // Creates a token for enqueuing (and later aborting) tasks.
50 size_t NewToken(); 45 size_t NewToken();
51 46
52 // Enqueues a task using a token generated with NewToken(). The task will be 47 // 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 48 // 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 49 // until another task is finished. Once the task is finished, Complete() must
55 // Remove() must be called. The callback's abort callback may be NULL. In 50 // be called. The callback's abort callback may be NULL. In such case, Abort()
56 // such case, Abort() must not be called. 51 // must not be called.
57 void Enqueue(size_t token, const AbortableCallback& callback); 52 void Enqueue(size_t token, const AbortableCallback& callback);
58 53
59 // Forcibly aborts a previously enqueued task. May be called at any time as 54 // 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. 55 // 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); 56 void Abort(size_t token);
65 57
66 // Returns true if the task which is in the queue with |token| has been 58 // Marks an executed task with |token| as completed. Must be called once the
67 // aborted. This method must not be called for tasks which are not in the 59 // task is executed. Simply saying, in most cases it should be just called
68 // queue. 60 // from the task's completion callback.
69 bool IsAborted(size_t token);
70
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); 61 void Complete(size_t token);
76 62
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);
80
81 private: 63 private:
82 // Information about an enqueued task which hasn't been removed, nor aborted. 64 // Information about an enqueued task which hasn't been removed, nor aborted.
83 struct Task { 65 struct Task {
84 Task(); 66 Task();
85 Task(size_t token, const AbortableCallback& callback); 67 Task(size_t token, const AbortableCallback& callback);
86 ~Task(); 68 ~Task();
87 69
88 size_t token; 70 size_t token;
89 AbortableCallback callback; 71 AbortableCallback callback;
90 AbortCallback abort_callback; 72 AbortCallback abort_callback;
91 }; 73 };
92 74
93 // Runs the next task from the pending queue if there is less than 75 // Runs the next task from the pending queue if there is less than
94 // |max_in_parallel_| tasks running at once. 76 // |max_in_parallel_| tasks running at once.
95 void MaybeRun(); 77 void MaybeRun();
96 78
97 const size_t max_in_parallel_; 79 const size_t max_in_parallel_;
98 size_t next_token_; 80 size_t next_token_;
99 std::deque<Task> pending_; 81 std::deque<Task> pending_;
100 std::map<int, Task> executed_; 82 std::map<int, Task> executed_;
101 std::map<int, Task> completed_;
102 std::map<int, Task> aborted_;
103 83
104 base::WeakPtrFactory<Queue> weak_ptr_factory_; 84 base::WeakPtrFactory<Queue> weak_ptr_factory_;
105 DISALLOW_COPY_AND_ASSIGN(Queue); 85 DISALLOW_COPY_AND_ASSIGN(Queue);
106 }; 86 };
107 87
108 } // namespace file_system_provider 88 } // namespace file_system_provider
109 } // namespace chromeos 89 } // namespace chromeos
110 90
111 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_ 91 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_QUEUE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698