Disable FileSystemProviderApiTest.BigFile
[chromium-blink-merge.git] / net / base / directory_lister.h
blob51387e806557f10d3f51462eefd17d86af456120
1 // Copyright (c) 2012 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 NET_BASE_DIRECTORY_LISTER_H_
6 #define NET_BASE_DIRECTORY_LISTER_H_
8 #include <vector>
10 #include "base/atomicops.h"
11 #include "base/files/file_enumerator.h"
12 #include "base/files/file_path.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop/message_loop_proxy.h"
17 #include "net/base/net_export.h"
19 namespace net {
21 // This class provides an API for asynchronously listing the contents of a
22 // directory on the filesystem. It runs a task on a background thread, and
23 // enumerates all files in the specified directory on that thread. Destroying
24 // the lister cancels the list operation. The DirectoryLister must only be
25 // used on a thread with a MessageLoop.
26 class NET_EXPORT DirectoryLister {
27 public:
28 // Represents one file found.
29 struct DirectoryListerData {
30 base::FileEnumerator::FileInfo info;
31 base::FilePath path;
34 // Implement this class to receive directory entries.
35 class DirectoryListerDelegate {
36 public:
37 // Called for each file found by the lister.
38 virtual void OnListFile(const DirectoryListerData& data) = 0;
40 // Called when the listing is complete.
41 virtual void OnListDone(int error) = 0;
43 protected:
44 virtual ~DirectoryListerDelegate() {}
47 // Sort options
48 // ALPHA_DIRS_FIRST is the default sort :
49 // directories first in name order, then files by name order
50 // FULL_PATH sorts by paths as strings, ignoring files v. directories
51 // DATE sorts by last modified date
52 // TODO(mmenke): Only NO_SORT and ALPHA_DIRS_FIRST appear to be used in
53 // production code, and there's very little testing of some of these
54 // options. Remove unused options, improve testing of the others.
55 enum SortType {
56 NO_SORT,
57 DATE,
58 ALPHA_DIRS_FIRST,
59 FULL_PATH
62 DirectoryLister(const base::FilePath& dir,
63 DirectoryListerDelegate* delegate);
65 DirectoryLister(const base::FilePath& dir,
66 bool recursive,
67 SortType sort,
68 DirectoryListerDelegate* delegate);
70 // Will invoke Cancel().
71 ~DirectoryLister();
73 // Call this method to start the directory enumeration thread.
74 bool Start();
76 // Call this method to asynchronously stop directory enumeration. The
77 // delegate will not be called back.
78 void Cancel();
80 private:
81 typedef std::vector<DirectoryListerData> DirectoryList;
83 // Class responsible for retrieving and sorting the actual directory list on
84 // a worker pool thread. Created on the DirectoryLister's thread. As it's
85 // refcounted, it's destroyed when the final reference is released, which may
86 // happen on either thread.
88 // It's kept alive during the calls to Start() and DoneOnOriginThread() by the
89 // reference owned by the callback itself.
90 class Core : public base::RefCountedThreadSafe<Core> {
91 public:
92 Core(const base::FilePath& dir,
93 bool recursive,
94 SortType sort,
95 DirectoryLister* lister);
97 // May only be called on a worker pool thread.
98 void Start();
100 // Must be called on the origin thread.
101 void CancelOnOriginThread();
103 private:
104 friend class base::RefCountedThreadSafe<Core>;
105 class DataEvent;
107 ~Core();
109 // Called on both threads.
110 bool IsCancelled() const;
112 // Called on origin thread.
113 void DoneOnOriginThread(scoped_ptr<DirectoryList> directory_list,
114 int error) const;
116 const base::FilePath dir_;
117 const bool recursive_;
118 const SortType sort_;
119 const scoped_refptr<base::MessageLoopProxy> origin_loop_;
121 // Only used on the origin thread.
122 DirectoryLister* lister_;
124 // Set to 1 on cancellation. Used both to abort listing files early on the
125 // worker pool thread for performance reasons and to ensure |lister_| isn't
126 // called after cancellation on the origin thread.
127 base::subtle::Atomic32 cancelled_;
129 DISALLOW_COPY_AND_ASSIGN(Core);
132 // Call into the corresponding DirectoryListerDelegate. Must not be called
133 // after cancellation.
134 void OnListFile(const DirectoryListerData& data);
135 void OnListDone(int error);
137 scoped_refptr<Core> core_;
138 DirectoryListerDelegate* const delegate_;
140 DISALLOW_COPY_AND_ASSIGN(DirectoryLister);
143 } // namespace net
145 #endif // NET_BASE_DIRECTORY_LISTER_H_