Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / base / directory_lister.h
bloba29ff53287ae626cdeb63136a9e16863327538be
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 "net/base/net_export.h"
18 namespace base {
19 class SingleThreadTaskRunner;
22 namespace net {
24 // This class provides an API for asynchronously listing the contents of a
25 // directory on the filesystem. It runs a task on a background thread, and
26 // enumerates all files in the specified directory on that thread. Destroying
27 // the lister cancels the list operation. The DirectoryLister must only be
28 // used on a thread with a MessageLoop.
29 class NET_EXPORT DirectoryLister {
30 public:
31 // Represents one file found.
32 struct DirectoryListerData {
33 base::FileEnumerator::FileInfo info;
34 base::FilePath path;
37 // Implement this class to receive directory entries.
38 class DirectoryListerDelegate {
39 public:
40 // Called for each file found by the lister.
41 virtual void OnListFile(const DirectoryListerData& data) = 0;
43 // Called when the listing is complete.
44 virtual void OnListDone(int error) = 0;
46 protected:
47 virtual ~DirectoryListerDelegate() {}
50 // Listing options
51 // ALPHA_DIRS_FIRST is the default listing type:
52 // directories first in name order, then files by name order
53 // Listing is recursive only if listing type is NO_SORT_RECURSIVE.
54 // TODO(mmenke): Improve testing.
55 enum ListingType {
56 NO_SORT,
57 NO_SORT_RECURSIVE,
58 ALPHA_DIRS_FIRST,
61 DirectoryLister(const base::FilePath& dir,
62 DirectoryListerDelegate* delegate);
64 DirectoryLister(const base::FilePath& dir,
65 ListingType type,
66 DirectoryListerDelegate* delegate);
68 // Will invoke Cancel().
69 ~DirectoryLister();
71 // Call this method to start the directory enumeration thread.
72 bool Start();
74 // Call this method to asynchronously stop directory enumeration. The
75 // delegate will not be called back.
76 void Cancel();
78 private:
79 typedef std::vector<DirectoryListerData> DirectoryList;
81 // Class responsible for retrieving and sorting the actual directory list on
82 // a worker pool thread. Created on the DirectoryLister's thread. As it's
83 // refcounted, it's destroyed when the final reference is released, which may
84 // happen on either thread.
86 // It's kept alive during the calls to Start() and DoneOnOriginThread() by the
87 // reference owned by the callback itself.
88 class Core : public base::RefCountedThreadSafe<Core> {
89 public:
90 Core(const base::FilePath& dir, ListingType type, DirectoryLister* lister);
92 // May only be called on a worker pool thread.
93 void Start();
95 // Must be called on the origin thread.
96 void CancelOnOriginThread();
98 private:
99 friend class base::RefCountedThreadSafe<Core>;
100 class DataEvent;
102 ~Core();
104 // Called on both threads.
105 bool IsCancelled() const;
107 // Called on origin thread.
108 void DoneOnOriginThread(scoped_ptr<DirectoryList> directory_list,
109 int error) const;
111 const base::FilePath dir_;
112 const ListingType type_;
113 const scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
115 // Only used on the origin thread.
116 DirectoryLister* lister_;
118 // Set to 1 on cancellation. Used both to abort listing files early on the
119 // worker pool thread for performance reasons and to ensure |lister_| isn't
120 // called after cancellation on the origin thread.
121 base::subtle::Atomic32 cancelled_;
123 DISALLOW_COPY_AND_ASSIGN(Core);
126 // Call into the corresponding DirectoryListerDelegate. Must not be called
127 // after cancellation.
128 void OnListFile(const DirectoryListerData& data);
129 void OnListDone(int error);
131 scoped_refptr<Core> core_;
132 DirectoryListerDelegate* const delegate_;
134 DISALLOW_COPY_AND_ASSIGN(DirectoryLister);
137 } // namespace net
139 #endif // NET_BASE_DIRECTORY_LISTER_H_