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: |
49 // directories first in name order, then files by name order | 49 // directories first in name order, then files by name order |
50 // FULL_PATH sorts by paths as strings, ignoring files v. directories | 50 // Listing is recursive only if listing type is NO_SORT_RECURSIVE. |
51 // DATE sorts by last modified date | 51 // TODO(mmenke): Improve testing. |
52 // TODO(mmenke): Only NO_SORT and ALPHA_DIRS_FIRST appear to be used in | 52 enum ListingType { |
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, | 53 NO_SORT, |
57 DATE, | 54 NO_SORT_RECURSIVE, |
58 ALPHA_DIRS_FIRST, | 55 ALPHA_DIRS_FIRST, |
59 FULL_PATH | |
60 }; | 56 }; |
61 | 57 |
62 DirectoryLister(const base::FilePath& dir, | 58 DirectoryLister(const base::FilePath& dir, |
63 DirectoryListerDelegate* delegate); | 59 DirectoryListerDelegate* delegate); |
64 | 60 |
65 DirectoryLister(const base::FilePath& dir, | 61 DirectoryLister(const base::FilePath& dir, |
66 bool recursive, | 62 ListingType type, |
67 SortType sort, | |
68 DirectoryListerDelegate* delegate); | 63 DirectoryListerDelegate* delegate); |
69 | 64 |
70 // Will invoke Cancel(). | 65 // Will invoke Cancel(). |
71 ~DirectoryLister(); | 66 ~DirectoryLister(); |
72 | 67 |
73 // Call this method to start the directory enumeration thread. | 68 // Call this method to start the directory enumeration thread. |
74 bool Start(); | 69 bool Start(); |
75 | 70 |
76 // Call this method to asynchronously stop directory enumeration. The | 71 // Call this method to asynchronously stop directory enumeration. The |
77 // delegate will not be called back. | 72 // delegate will not be called back. |
78 void Cancel(); | 73 void Cancel(); |
79 | 74 |
80 private: | 75 private: |
81 typedef std::vector<DirectoryListerData> DirectoryList; | 76 typedef std::vector<DirectoryListerData> DirectoryList; |
82 | 77 |
83 // Class responsible for retrieving and sorting the actual directory list on | 78 // 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 | 79 // 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 | 80 // refcounted, it's destroyed when the final reference is released, which may |
86 // happen on either thread. | 81 // happen on either thread. |
87 // | 82 // |
88 // It's kept alive during the calls to Start() and DoneOnOriginThread() by the | 83 // It's kept alive during the calls to Start() and DoneOnOriginThread() by the |
89 // reference owned by the callback itself. | 84 // reference owned by the callback itself. |
90 class Core : public base::RefCountedThreadSafe<Core> { | 85 class Core : public base::RefCountedThreadSafe<Core> { |
91 public: | 86 public: |
92 Core(const base::FilePath& dir, | 87 Core(const base::FilePath& dir, ListingType type, DirectoryLister* lister); |
93 bool recursive, | |
94 SortType sort, | |
95 DirectoryLister* lister); | |
96 | 88 |
97 // May only be called on a worker pool thread. | 89 // May only be called on a worker pool thread. |
98 void Start(); | 90 void Start(); |
99 | 91 |
100 // Must be called on the origin thread. | 92 // Must be called on the origin thread. |
101 void CancelOnOriginThread(); | 93 void CancelOnOriginThread(); |
102 | 94 |
103 private: | 95 private: |
104 friend class base::RefCountedThreadSafe<Core>; | 96 friend class base::RefCountedThreadSafe<Core>; |
105 class DataEvent; | 97 class DataEvent; |
106 | 98 |
107 ~Core(); | 99 ~Core(); |
108 | 100 |
109 // Called on both threads. | 101 // Called on both threads. |
110 bool IsCancelled() const; | 102 bool IsCancelled() const; |
111 | 103 |
112 // Called on origin thread. | 104 // Called on origin thread. |
113 void DoneOnOriginThread(scoped_ptr<DirectoryList> directory_list, | 105 void DoneOnOriginThread(scoped_ptr<DirectoryList> directory_list, |
114 int error) const; | 106 int error) const; |
115 | 107 |
116 const base::FilePath dir_; | 108 const base::FilePath dir_; |
117 const bool recursive_; | 109 const ListingType type_; |
118 const SortType sort_; | |
119 const scoped_refptr<base::MessageLoopProxy> origin_loop_; | 110 const scoped_refptr<base::MessageLoopProxy> origin_loop_; |
120 | 111 |
121 // Only used on the origin thread. | 112 // Only used on the origin thread. |
122 DirectoryLister* lister_; | 113 DirectoryLister* lister_; |
123 | 114 |
124 // Set to 1 on cancellation. Used both to abort listing files early on the | 115 // 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 | 116 // worker pool thread for performance reasons and to ensure |lister_| isn't |
126 // called after cancellation on the origin thread. | 117 // called after cancellation on the origin thread. |
127 base::subtle::Atomic32 cancelled_; | 118 base::subtle::Atomic32 cancelled_; |
128 | 119 |
129 DISALLOW_COPY_AND_ASSIGN(Core); | 120 DISALLOW_COPY_AND_ASSIGN(Core); |
130 }; | 121 }; |
131 | 122 |
132 // Call into the corresponding DirectoryListerDelegate. Must not be called | 123 // Call into the corresponding DirectoryListerDelegate. Must not be called |
133 // after cancellation. | 124 // after cancellation. |
134 void OnListFile(const DirectoryListerData& data); | 125 void OnListFile(const DirectoryListerData& data); |
135 void OnListDone(int error); | 126 void OnListDone(int error); |
136 | 127 |
137 scoped_refptr<Core> core_; | 128 scoped_refptr<Core> core_; |
138 DirectoryListerDelegate* const delegate_; | 129 DirectoryListerDelegate* const delegate_; |
139 | 130 |
140 DISALLOW_COPY_AND_ASSIGN(DirectoryLister); | 131 DISALLOW_COPY_AND_ASSIGN(DirectoryLister); |
141 }; | 132 }; |
142 | 133 |
143 } // namespace net | 134 } // namespace net |
144 | 135 |
145 #endif // NET_BASE_DIRECTORY_LISTER_H_ | 136 #endif // NET_BASE_DIRECTORY_LISTER_H_ |
OLD | NEW |