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_
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"
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
{
28 // Represents one file found.
29 struct DirectoryListerData
{
30 base::FileEnumerator::FileInfo info
;
34 // Implement this class to receive directory entries.
35 class DirectoryListerDelegate
{
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;
44 virtual ~DirectoryListerDelegate() {}
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.
62 DirectoryLister(const base::FilePath
& dir
,
63 DirectoryListerDelegate
* delegate
);
65 DirectoryLister(const base::FilePath
& dir
,
68 DirectoryListerDelegate
* delegate
);
70 // Will invoke Cancel().
73 // Call this method to start the directory enumeration thread.
76 // Call this method to asynchronously stop directory enumeration. The
77 // delegate will not be called back.
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
> {
92 Core(const base::FilePath
& dir
,
95 DirectoryLister
* lister
);
97 // May only be called on a worker pool thread.
100 // Must be called on the origin thread.
101 void CancelOnOriginThread();
104 friend class base::RefCountedThreadSafe
<Core
>;
109 // Called on both threads.
110 bool IsCancelled() const;
112 // Called on origin thread.
113 void DoneOnOriginThread(scoped_ptr
<DirectoryList
> directory_list
,
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
);
145 #endif // NET_BASE_DIRECTORY_LISTER_H_