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/files/file_enumerator.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "net/base/net_export.h"
19 // This class provides an API for listing the contents of a directory on the
20 // filesystem asynchronously. It spawns a background thread, and enumerates
21 // the specified directory on that thread. It marshalls WIN32_FIND_DATA
22 // structs over to the main application thread. The consumer of this class
23 // is insulated from any of the multi-threading details.
25 class NET_EXPORT DirectoryLister {
27 // Represents one file found.
28 struct DirectoryListerData {
29 base::FileEnumerator::FileInfo info;
33 // Implement this class to receive directory entries.
34 class DirectoryListerDelegate {
36 // Called for each file found by the lister.
37 virtual void OnListFile(const DirectoryListerData& data) = 0;
39 // Called when the listing is complete.
40 virtual void OnListDone(int error) = 0;
43 virtual ~DirectoryListerDelegate() {}
47 // ALPHA_DIRS_FIRST is the default sort :
48 // directories first in name order, then files by name order
49 // FULL_PATH sorts by paths as strings, ignoring files v. directories
50 // DATE sorts by last modified date
58 DirectoryLister(const base::FilePath& dir,
59 DirectoryListerDelegate* delegate);
61 DirectoryLister(const base::FilePath& dir,
64 DirectoryListerDelegate* delegate);
66 // Will invoke Cancel().
69 // Call this method to start the directory enumeration thread.
72 // Call this method to asynchronously stop directory enumeration. The
73 // delegate will not be called back.
77 class Core : public base::RefCountedThreadSafe<Core> {
79 Core(const base::FilePath& dir,
82 DirectoryLister* lister);
89 friend class base::RefCountedThreadSafe<Core>;
94 // This method runs on a WorkerPool thread.
97 void SendData(const std::vector<DirectoryListerData>& data);
99 void OnDone(int error);
104 scoped_refptr<base::MessageLoopProxy> origin_loop_;
106 // |lister_| gets set to NULL when canceled.
107 DirectoryLister* lister_;
109 DISALLOW_COPY_AND_ASSIGN(Core);
112 void OnReceivedData(const DirectoryListerData& data);
113 void OnDone(int error);
115 const scoped_refptr<Core> core_;
116 DirectoryListerDelegate* const delegate_;
118 DISALLOW_COPY_AND_ASSIGN(DirectoryLister);
123 #endif // NET_BASE_DIRECTORY_LISTER_H_