OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_BASE_DIRECTORY_LISTER_H_ | 5 #ifndef NET_BASE_DIRECTORY_LISTER_H_ |
6 #define NET_BASE_DIRECTORY_LISTER_H_ | 6 #define NET_BASE_DIRECTORY_LISTER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
(...skipping 26 matching lines...) Expand all Loading... | |
37 // Called for each file found by the lister. | 37 // Called for each file found by the lister. |
38 virtual void OnListFile(const DirectoryListerData& data) = 0; | 38 virtual void OnListFile(const DirectoryListerData& data) = 0; |
39 | 39 |
40 // Called when the listing is complete. | 40 // Called when the listing is complete. |
41 virtual void OnListDone(int error) = 0; | 41 virtual void OnListDone(int error) = 0; |
42 | 42 |
43 protected: | 43 protected: |
44 virtual ~DirectoryListerDelegate() {} | 44 virtual ~DirectoryListerDelegate() {} |
45 }; | 45 }; |
46 | 46 |
47 // Sort options | 47 // Listing options |
48 // ALPHA_DIRS_FIRST is the default sort : | 48 // ALPHA_DIRS_FIRST is the default listing type: |
mmenke
2015/04/23 14:57:00
nit: Remove extra space before listing
| |
49 // directories first in name order, then files by name order | 49 // directories first in name order, then files by name order |
mmenke
2015/04/23 14:57:00
Mind adding a comment about NO_SORT_RECURSIVE? (J
| |
50 // FULL_PATH sorts by paths as strings, ignoring files v. directories | 50 // TODO(mmenke): Improve testing. |
51 // DATE sorts by last modified date | 51 enum ListingType { |
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. | |
55 enum SortType { | |
56 NO_SORT, | 52 NO_SORT, |
57 DATE, | 53 NO_SORT_RECURSIVE, |
58 ALPHA_DIRS_FIRST, | 54 ALPHA_DIRS_FIRST, |
59 FULL_PATH | |
60 }; | 55 }; |
61 | 56 |
62 DirectoryLister(const base::FilePath& dir, | 57 DirectoryLister(const base::FilePath& dir, |
63 DirectoryListerDelegate* delegate); | 58 DirectoryListerDelegate* delegate); |
64 | 59 |
65 DirectoryLister(const base::FilePath& dir, | 60 DirectoryLister(const base::FilePath& dir, |
66 bool recursive, | 61 ListingType type, |
67 SortType sort, | |
68 DirectoryListerDelegate* delegate); | 62 DirectoryListerDelegate* delegate); |
69 | 63 |
70 // Will invoke Cancel(). | 64 // Will invoke Cancel(). |
71 ~DirectoryLister(); | 65 ~DirectoryLister(); |
72 | 66 |
73 // Call this method to start the directory enumeration thread. | 67 // Call this method to start the directory enumeration thread. |
74 bool Start(); | 68 bool Start(); |
75 | 69 |
76 // Call this method to asynchronously stop directory enumeration. The | 70 // Call this method to asynchronously stop directory enumeration. The |
77 // delegate will not be called back. | 71 // delegate will not be called back. |
78 void Cancel(); | 72 void Cancel(); |
79 | 73 |
80 private: | 74 private: |
81 typedef std::vector<DirectoryListerData> DirectoryList; | 75 typedef std::vector<DirectoryListerData> DirectoryList; |
82 | 76 |
83 // Class responsible for retrieving and sorting the actual directory list on | 77 // 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 | 78 // 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 | 79 // refcounted, it's destroyed when the final reference is released, which may |
86 // happen on either thread. | 80 // happen on either thread. |
87 // | 81 // |
88 // It's kept alive during the calls to Start() and DoneOnOriginThread() by the | 82 // It's kept alive during the calls to Start() and DoneOnOriginThread() by the |
89 // reference owned by the callback itself. | 83 // reference owned by the callback itself. |
90 class Core : public base::RefCountedThreadSafe<Core> { | 84 class Core : public base::RefCountedThreadSafe<Core> { |
91 public: | 85 public: |
92 Core(const base::FilePath& dir, | 86 Core(const base::FilePath& dir, ListingType type, DirectoryLister* lister); |
93 bool recursive, | |
94 SortType sort, | |
95 DirectoryLister* lister); | |
96 | 87 |
97 // May only be called on a worker pool thread. | 88 // May only be called on a worker pool thread. |
98 void Start(); | 89 void Start(); |
99 | 90 |
100 // Must be called on the origin thread. | 91 // Must be called on the origin thread. |
101 void CancelOnOriginThread(); | 92 void CancelOnOriginThread(); |
102 | 93 |
103 private: | 94 private: |
104 friend class base::RefCountedThreadSafe<Core>; | 95 friend class base::RefCountedThreadSafe<Core>; |
105 class DataEvent; | 96 class DataEvent; |
106 | 97 |
107 ~Core(); | 98 ~Core(); |
108 | 99 |
109 // Called on both threads. | 100 // Called on both threads. |
110 bool IsCancelled() const; | 101 bool IsCancelled() const; |
111 | 102 |
112 // Called on origin thread. | 103 // Called on origin thread. |
113 void DoneOnOriginThread(scoped_ptr<DirectoryList> directory_list, | 104 void DoneOnOriginThread(scoped_ptr<DirectoryList> directory_list, |
114 int error) const; | 105 int error) const; |
115 | 106 |
116 const base::FilePath dir_; | 107 const base::FilePath dir_; |
117 const bool recursive_; | 108 const ListingType type_; |
118 const SortType sort_; | |
119 const scoped_refptr<base::MessageLoopProxy> origin_loop_; | 109 const scoped_refptr<base::MessageLoopProxy> origin_loop_; |
120 | 110 |
121 // Only used on the origin thread. | 111 // Only used on the origin thread. |
122 DirectoryLister* lister_; | 112 DirectoryLister* lister_; |
123 | 113 |
124 // Set to 1 on cancellation. Used both to abort listing files early on the | 114 // 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 | 115 // worker pool thread for performance reasons and to ensure |lister_| isn't |
126 // called after cancellation on the origin thread. | 116 // called after cancellation on the origin thread. |
127 base::subtle::Atomic32 cancelled_; | 117 base::subtle::Atomic32 cancelled_; |
128 | 118 |
129 DISALLOW_COPY_AND_ASSIGN(Core); | 119 DISALLOW_COPY_AND_ASSIGN(Core); |
130 }; | 120 }; |
131 | 121 |
132 // Call into the corresponding DirectoryListerDelegate. Must not be called | 122 // Call into the corresponding DirectoryListerDelegate. Must not be called |
133 // after cancellation. | 123 // after cancellation. |
134 void OnListFile(const DirectoryListerData& data); | 124 void OnListFile(const DirectoryListerData& data); |
135 void OnListDone(int error); | 125 void OnListDone(int error); |
136 | 126 |
137 scoped_refptr<Core> core_; | 127 scoped_refptr<Core> core_; |
138 DirectoryListerDelegate* const delegate_; | 128 DirectoryListerDelegate* const delegate_; |
139 | 129 |
140 DISALLOW_COPY_AND_ASSIGN(DirectoryLister); | 130 DISALLOW_COPY_AND_ASSIGN(DirectoryLister); |
141 }; | 131 }; |
142 | 132 |
143 } // namespace net | 133 } // namespace net |
144 | 134 |
145 #endif // NET_BASE_DIRECTORY_LISTER_H_ | 135 #endif // NET_BASE_DIRECTORY_LISTER_H_ |
OLD | NEW |