Probably broke Win7 Tests (dbg)(6). http://build.chromium.org/p/chromium.win/builders...
[chromium-blink-merge.git] / net / base / directory_lister.h
blob5fbc9dc6fa15abd3c409aa4502327663dfa57e78
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/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"
16 namespace net {
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 {
26 public:
27 // Represents one file found.
28 struct DirectoryListerData {
29 base::FileEnumerator::FileInfo info;
30 base::FilePath path;
33 // Implement this class to receive directory entries.
34 class DirectoryListerDelegate {
35 public:
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;
42 protected:
43 virtual ~DirectoryListerDelegate() {}
46 // Sort options
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
51 enum SortType {
52 NO_SORT,
53 DATE,
54 ALPHA_DIRS_FIRST,
55 FULL_PATH
58 DirectoryLister(const base::FilePath& dir,
59 DirectoryListerDelegate* delegate);
61 DirectoryLister(const base::FilePath& dir,
62 bool recursive,
63 SortType sort,
64 DirectoryListerDelegate* delegate);
66 // Will invoke Cancel().
67 ~DirectoryLister();
69 // Call this method to start the directory enumeration thread.
70 bool Start();
72 // Call this method to asynchronously stop directory enumeration. The
73 // delegate will not be called back.
74 void Cancel();
76 private:
77 class Core : public base::RefCountedThreadSafe<Core> {
78 public:
79 Core(const base::FilePath& dir,
80 bool recursive,
81 SortType sort,
82 DirectoryLister* lister);
84 bool Start();
86 void Cancel();
88 private:
89 friend class base::RefCountedThreadSafe<Core>;
90 class DataEvent;
92 ~Core();
94 // This method runs on a WorkerPool thread.
95 void StartInternal();
97 void SendData(const std::vector<DirectoryListerData>& data);
99 void OnDone(int error);
101 base::FilePath dir_;
102 bool recursive_;
103 SortType sort_;
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);
121 } // namespace net
123 #endif // NET_BASE_DIRECTORY_LISTER_H_