Clean up URLFetcher unit tests, part 8.
[chromium-blink-merge.git] / net / base / directory_lister.h
blob02f24dfbe3448c8bac5cef5ab79a5a6f6daf48df
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 // Listing options
48 // ALPHA_DIRS_FIRST is the default listing type:
49 // directories first in name order, then files by name order
50 // Listing is recursive only if listing type is NO_SORT_RECURSIVE.
51 // TODO(mmenke): Improve testing.
52 enum ListingType {
53 NO_SORT,
54 NO_SORT_RECURSIVE,
55 ALPHA_DIRS_FIRST,
58 DirectoryLister(const base::FilePath& dir,
59 DirectoryListerDelegate* delegate);
61 DirectoryLister(const base::FilePath& dir,
62 ListingType type,
63 DirectoryListerDelegate* delegate);
65 // Will invoke Cancel().
66 ~DirectoryLister();
68 // Call this method to start the directory enumeration thread.
69 bool Start();
71 // Call this method to asynchronously stop directory enumeration. The
72 // delegate will not be called back.
73 void Cancel();
75 private:
76 typedef std::vector<DirectoryListerData> DirectoryList;
78 // Class responsible for retrieving and sorting the actual directory list on
79 // a worker pool thread. Created on the DirectoryLister's thread. As it's
80 // refcounted, it's destroyed when the final reference is released, which may
81 // happen on either thread.
83 // It's kept alive during the calls to Start() and DoneOnOriginThread() by the
84 // reference owned by the callback itself.
85 class Core : public base::RefCountedThreadSafe<Core> {
86 public:
87 Core(const base::FilePath& dir, ListingType type, DirectoryLister* lister);
89 // May only be called on a worker pool thread.
90 void Start();
92 // Must be called on the origin thread.
93 void CancelOnOriginThread();
95 private:
96 friend class base::RefCountedThreadSafe<Core>;
97 class DataEvent;
99 ~Core();
101 // Called on both threads.
102 bool IsCancelled() const;
104 // Called on origin thread.
105 void DoneOnOriginThread(scoped_ptr<DirectoryList> directory_list,
106 int error) const;
108 const base::FilePath dir_;
109 const ListingType type_;
110 const scoped_refptr<base::MessageLoopProxy> origin_loop_;
112 // Only used on the origin thread.
113 DirectoryLister* lister_;
115 // Set to 1 on cancellation. Used both to abort listing files early on the
116 // worker pool thread for performance reasons and to ensure |lister_| isn't
117 // called after cancellation on the origin thread.
118 base::subtle::Atomic32 cancelled_;
120 DISALLOW_COPY_AND_ASSIGN(Core);
123 // Call into the corresponding DirectoryListerDelegate. Must not be called
124 // after cancellation.
125 void OnListFile(const DirectoryListerData& data);
126 void OnListDone(int error);
128 scoped_refptr<Core> core_;
129 DirectoryListerDelegate* const delegate_;
131 DISALLOW_COPY_AND_ASSIGN(DirectoryLister);
134 } // namespace net
136 #endif // NET_BASE_DIRECTORY_LISTER_H_