| 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/files/file_enumerator.h" | 11 #include "base/files/file_enumerator.h" |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" | 16 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
| 15 | 18 |
| 16 namespace net { | 19 namespace net { |
| 17 | 20 |
| 18 // | 21 // This class provides an API for asynchronously listing the contents of a |
| 19 // This class provides an API for listing the contents of a directory on the | 22 // directory on the filesystem. It runs a task on a background thread, and |
| 20 // filesystem asynchronously. It spawns a background thread, and enumerates | 23 // enumerates all files in the specified directory on that thread. Destroying |
| 21 // the specified directory on that thread. It marshalls WIN32_FIND_DATA | 24 // the lister cancels the list operation. The DirectoryLister must only be |
| 22 // structs over to the main application thread. The consumer of this class | 25 // used on a thread with a MessageLoop. |
| 23 // is insulated from any of the multi-threading details. | |
| 24 // | |
| 25 class NET_EXPORT DirectoryLister { | 26 class NET_EXPORT DirectoryLister { |
| 26 public: | 27 public: |
| 27 // Represents one file found. | 28 // Represents one file found. |
| 28 struct DirectoryListerData { | 29 struct DirectoryListerData { |
| 29 base::FileEnumerator::FileInfo info; | 30 base::FileEnumerator::FileInfo info; |
| 30 base::FilePath path; | 31 base::FilePath path; |
| 31 }; | 32 }; |
| 32 | 33 |
| 33 // Implement this class to receive directory entries. | 34 // Implement this class to receive directory entries. |
| 34 class DirectoryListerDelegate { | 35 class DirectoryListerDelegate { |
| 35 public: | 36 public: |
| 36 // Called for each file found by the lister. | 37 // Called for each file found by the lister. |
| 37 virtual void OnListFile(const DirectoryListerData& data) = 0; | 38 virtual void OnListFile(const DirectoryListerData& data) = 0; |
| 38 | 39 |
| 39 // Called when the listing is complete. | 40 // Called when the listing is complete. |
| 40 virtual void OnListDone(int error) = 0; | 41 virtual void OnListDone(int error) = 0; |
| 41 | 42 |
| 42 protected: | 43 protected: |
| 43 virtual ~DirectoryListerDelegate() {} | 44 virtual ~DirectoryListerDelegate() {} |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 // Sort options | 47 // Sort options |
| 47 // ALPHA_DIRS_FIRST is the default sort : | 48 // ALPHA_DIRS_FIRST is the default sort : |
| 48 // directories first in name order, then files by name order | 49 // directories first in name order, then files by name order |
| 49 // FULL_PATH sorts by paths as strings, ignoring files v. directories | 50 // FULL_PATH sorts by paths as strings, ignoring files v. directories |
| 50 // DATE sorts by last modified date | 51 // DATE sorts by last modified date |
| 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. |
| 51 enum SortType { | 55 enum SortType { |
| 52 NO_SORT, | 56 NO_SORT, |
| 53 DATE, | 57 DATE, |
| 54 ALPHA_DIRS_FIRST, | 58 ALPHA_DIRS_FIRST, |
| 55 FULL_PATH | 59 FULL_PATH |
| 56 }; | 60 }; |
| 57 | 61 |
| 58 DirectoryLister(const base::FilePath& dir, | 62 DirectoryLister(const base::FilePath& dir, |
| 59 DirectoryListerDelegate* delegate); | 63 DirectoryListerDelegate* delegate); |
| 60 | 64 |
| 61 DirectoryLister(const base::FilePath& dir, | 65 DirectoryLister(const base::FilePath& dir, |
| 62 bool recursive, | 66 bool recursive, |
| 63 SortType sort, | 67 SortType sort, |
| 64 DirectoryListerDelegate* delegate); | 68 DirectoryListerDelegate* delegate); |
| 65 | 69 |
| 66 // Will invoke Cancel(). | 70 // Will invoke Cancel(). |
| 67 ~DirectoryLister(); | 71 ~DirectoryLister(); |
| 68 | 72 |
| 69 // Call this method to start the directory enumeration thread. | 73 // Call this method to start the directory enumeration thread. |
| 70 bool Start(); | 74 bool Start(); |
| 71 | 75 |
| 72 // Call this method to asynchronously stop directory enumeration. The | 76 // Call this method to asynchronously stop directory enumeration. The |
| 73 // delegate will not be called back. | 77 // delegate will not be called back. |
| 74 void Cancel(); | 78 void Cancel(); |
| 75 | 79 |
| 76 private: | 80 private: |
| 81 typedef std::vector<DirectoryListerData> DirectoryList; |
| 82 |
| 83 // 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 |
| 85 // refcounted, it's destroyed when the final reference is released, which may |
| 86 // happen on either thread. |
| 87 // |
| 88 // It's kept alive during the calls to Start() and DoneOnOriginThread() by the |
| 89 // reference owned by the callback itself. |
| 77 class Core : public base::RefCountedThreadSafe<Core> { | 90 class Core : public base::RefCountedThreadSafe<Core> { |
| 78 public: | 91 public: |
| 79 Core(const base::FilePath& dir, | 92 Core(const base::FilePath& dir, |
| 80 bool recursive, | 93 bool recursive, |
| 81 SortType sort, | 94 SortType sort, |
| 82 DirectoryLister* lister); | 95 DirectoryLister* lister); |
| 83 | 96 |
| 84 bool Start(); | 97 // May only be called on a worker pool thread. |
| 98 void Start(); |
| 85 | 99 |
| 86 void Cancel(); | 100 // Must be called on the origin thread. |
| 101 void CancelOnOriginThread(); |
| 87 | 102 |
| 88 private: | 103 private: |
| 89 friend class base::RefCountedThreadSafe<Core>; | 104 friend class base::RefCountedThreadSafe<Core>; |
| 90 class DataEvent; | 105 class DataEvent; |
| 91 | 106 |
| 92 ~Core(); | 107 ~Core(); |
| 93 | 108 |
| 94 // This method runs on a WorkerPool thread. | 109 // Called on both threads. |
| 95 void StartInternal(); | 110 bool IsCancelled() const; |
| 96 | 111 |
| 97 void SendData(const std::vector<DirectoryListerData>& data); | 112 // Called on origin thread. |
| 113 void DoneOnOriginThread(scoped_ptr<DirectoryList> directory_list, |
| 114 int error) const; |
| 98 | 115 |
| 99 void OnDone(int error); | 116 const base::FilePath dir_; |
| 117 const bool recursive_; |
| 118 const SortType sort_; |
| 119 const scoped_refptr<base::MessageLoopProxy> origin_loop_; |
| 100 | 120 |
| 101 base::FilePath dir_; | 121 // Only used on the origin thread. |
| 102 bool recursive_; | 122 DirectoryLister* lister_; |
| 103 SortType sort_; | |
| 104 scoped_refptr<base::MessageLoopProxy> origin_loop_; | |
| 105 | 123 |
| 106 // |lister_| gets set to NULL when canceled. | 124 // Set to 1 on cancellation. Used both to abort listing files early on the |
| 107 DirectoryLister* lister_; | 125 // worker pool thread for performance reasons and to ensure |lister_| isn't |
| 126 // called after cancellation on the origin thread. |
| 127 base::subtle::Atomic32 cancelled_; |
| 108 | 128 |
| 109 DISALLOW_COPY_AND_ASSIGN(Core); | 129 DISALLOW_COPY_AND_ASSIGN(Core); |
| 110 }; | 130 }; |
| 111 | 131 |
| 112 void OnReceivedData(const DirectoryListerData& data); | 132 // Call into the corresponding DirectoryListerDelegate. Must not be called |
| 113 void OnDone(int error); | 133 // after cancellation. |
| 134 void OnListFile(const DirectoryListerData& data); |
| 135 void OnListDone(int error); |
| 114 | 136 |
| 115 const scoped_refptr<Core> core_; | 137 scoped_refptr<Core> core_; |
| 116 DirectoryListerDelegate* const delegate_; | 138 DirectoryListerDelegate* const delegate_; |
| 117 | 139 |
| 118 DISALLOW_COPY_AND_ASSIGN(DirectoryLister); | 140 DISALLOW_COPY_AND_ASSIGN(DirectoryLister); |
| 119 }; | 141 }; |
| 120 | 142 |
| 121 } // namespace net | 143 } // namespace net |
| 122 | 144 |
| 123 #endif // NET_BASE_DIRECTORY_LISTER_H_ | 145 #endif // NET_BASE_DIRECTORY_LISTER_H_ |
| OLD | NEW |