| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> |
| 10 |
| 9 #include "base/cancellation_flag.h" | 11 #include "base/cancellation_flag.h" |
| 10 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 11 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 12 #include "base/platform_thread.h" | 14 #include "base/platform_thread.h" |
| 13 #include "base/ref_counted.h" | 15 #include "base/ref_counted.h" |
| 16 #include "base/task.h" |
| 14 | 17 |
| 15 class MessageLoop; | 18 class MessageLoop; |
| 16 | 19 |
| 17 namespace net { | 20 namespace net { |
| 18 | 21 |
| 19 // | 22 // |
| 20 // This class provides an API for listing the contents of a directory on the | 23 // This class provides an API for listing the contents of a directory on the |
| 21 // filesystem asynchronously. It spawns a background thread, and enumerates | 24 // filesystem asynchronously. It spawns a background thread, and enumerates |
| 22 // the specified directory on that thread. It marshalls WIN32_FIND_DATA | 25 // the specified directory on that thread. It marshalls WIN32_FIND_DATA |
| 23 // structs over to the main application thread. The consumer of this class | 26 // structs over to the main application thread. The consumer of this class |
| 24 // is insulated from any of the multi-threading details. | 27 // is insulated from any of the multi-threading details. |
| 25 // | 28 // |
| 26 class DirectoryLister : public base::RefCountedThreadSafe<DirectoryLister>, | 29 class DirectoryLister : public base::RefCountedThreadSafe<DirectoryLister>, |
| 27 public PlatformThread::Delegate { | 30 public PlatformThread::Delegate { |
| 28 public: | 31 public: |
| 32 // Represents one file found. |
| 33 struct DirectoryListerData { |
| 34 file_util::FileEnumerator::FindInfo info; |
| 35 FilePath path; |
| 36 }; |
| 37 |
| 29 // Implement this class to receive directory entries. | 38 // Implement this class to receive directory entries. |
| 30 class DirectoryListerDelegate { | 39 class DirectoryListerDelegate { |
| 31 public: | 40 public: |
| 32 virtual void OnListFile( | 41 // Called for each file found by the lister. |
| 33 const file_util::FileEnumerator::FindInfo& data) = 0; | 42 virtual void OnListFile(const DirectoryListerData& data) = 0; |
| 43 |
| 44 // Called when the listing is complete. |
| 34 virtual void OnListDone(int error) = 0; | 45 virtual void OnListDone(int error) = 0; |
| 35 | 46 |
| 36 protected: | 47 protected: |
| 37 virtual ~DirectoryListerDelegate() {} | 48 virtual ~DirectoryListerDelegate() {} |
| 38 }; | 49 }; |
| 39 | 50 |
| 51 // Sort options |
| 52 // ALPHA_DIRS_FIRST is the default sort : |
| 53 // directories first in name order, then files by name order |
| 54 // FULL_PATH sorts by paths as strings, ignoring files v. directories |
| 55 // DATE sorts by last modified date |
| 40 enum SORT_TYPE { | 56 enum SORT_TYPE { |
| 41 DEFAULT, | 57 NO_SORT, |
| 42 DATE, | 58 DATE, |
| 43 ALPHA | 59 ALPHA_DIRS_FIRST, |
| 60 FULL_PATH |
| 44 }; | 61 }; |
| 45 | 62 |
| 46 DirectoryLister(const FilePath& dir, | 63 DirectoryLister(const FilePath& dir, |
| 47 DirectoryListerDelegate* delegate); | 64 DirectoryListerDelegate* delegate); |
| 48 | 65 |
| 49 DirectoryLister(const FilePath& dir, | 66 DirectoryLister(const FilePath& dir, |
| 67 bool recursive, |
| 50 SORT_TYPE sort, | 68 SORT_TYPE sort, |
| 51 DirectoryListerDelegate* delegate); | 69 DirectoryListerDelegate* delegate); |
| 52 | 70 |
| 53 | 71 |
| 54 // Call this method to start the directory enumeration thread. | 72 // Call this method to start the directory enumeration thread. |
| 55 bool Start(); | 73 bool Start(); |
| 56 | 74 |
| 57 // Call this method to asynchronously stop directory enumeration. The | 75 // Call this method to asynchronously stop directory enumeration. The |
| 58 // delegate will receive the OnListDone notification with an error code of | 76 // delegate will receive the OnListDone notification with an error code of |
| 59 // net::ERR_ABORTED. | 77 // net::ERR_ABORTED. |
| 60 void Cancel(); | 78 void Cancel(); |
| 61 | 79 |
| 62 // The delegate pointer may be modified at any time. | 80 // The delegate pointer may be modified at any time. |
| 63 DirectoryListerDelegate* delegate() const { return delegate_; } | 81 DirectoryListerDelegate* delegate() const { return delegate_; } |
| 64 void set_delegate(DirectoryListerDelegate* d) { delegate_ = d; } | 82 void set_delegate(DirectoryListerDelegate* d) { delegate_ = d; } |
| 65 | 83 |
| 66 // PlatformThread::Delegate implementation | 84 // PlatformThread::Delegate implementation |
| 67 void ThreadMain(); | 85 void ThreadMain(); |
| 68 | 86 |
| 69 private: | 87 private: |
| 70 friend class base::RefCountedThreadSafe<DirectoryLister>; | 88 friend class base::RefCountedThreadSafe<DirectoryLister>; |
| 71 friend class DirectoryDataEvent; | 89 friend class DirectoryDataEvent; |
| 72 | 90 |
| 91 // Comparison methods for sorting, chosen based on |sort_|. |
| 92 static bool CompareAlphaDirsFirst(const DirectoryListerData& a, |
| 93 const DirectoryListerData& b); |
| 94 static bool CompareDate(const DirectoryListerData& a, |
| 95 const DirectoryListerData& b); |
| 96 static bool CompareFullPath(const DirectoryListerData& a, |
| 97 const DirectoryListerData& b); |
| 98 |
| 73 ~DirectoryLister(); | 99 ~DirectoryLister(); |
| 74 | 100 |
| 75 void OnReceivedData(const file_util::FileEnumerator::FindInfo* data, | 101 void OnReceivedData(const DirectoryListerData* data, int count); |
| 76 int count); | |
| 77 void OnDone(int error); | 102 void OnDone(int error); |
| 78 | 103 |
| 79 FilePath dir_; | 104 FilePath dir_; |
| 105 bool recursive_; |
| 80 DirectoryListerDelegate* delegate_; | 106 DirectoryListerDelegate* delegate_; |
| 81 SORT_TYPE sort_; | 107 SORT_TYPE sort_; |
| 82 MessageLoop* message_loop_; | 108 MessageLoop* message_loop_; |
| 83 PlatformThreadHandle thread_; | 109 PlatformThreadHandle thread_; |
| 84 base::CancellationFlag canceled_; | 110 base::CancellationFlag canceled_; |
| 85 }; | 111 }; |
| 86 | 112 |
| 87 } // namespace net | 113 } // namespace net |
| 88 | 114 |
| 89 #endif // NET_BASE_DIRECTORY_LISTER_H_ | 115 #endif // NET_BASE_DIRECTORY_LISTER_H_ |
| OLD | NEW |