| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <windows.h> | |
| 9 #include <string> | 8 #include <string> |
| 10 | 9 |
| 10 #include "base/file_util.h" |
| 11 #include "base/platform_thread.h" |
| 11 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
| 12 | 13 |
| 13 class MessageLoop; | 14 class MessageLoop; |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 // | 18 // |
| 18 // This class provides an API for listing the contents of a directory on the | 19 // This class provides an API for listing the contents of a directory on the |
| 19 // filesystem asynchronously. It spawns a background thread, and enumerates | 20 // filesystem asynchronously. It spawns a background thread, and enumerates |
| 20 // the specified directory on that thread. It marshalls WIN32_FIND_DATA | 21 // the specified directory on that thread. It marshalls WIN32_FIND_DATA |
| 21 // structs over to the main application thread. The consumer of this class | 22 // structs over to the main application thread. The consumer of this class |
| 22 // is insulated from any of the multi-threading details. | 23 // is insulated from any of the multi-threading details. |
| 23 // | 24 // |
| 24 class DirectoryLister : public base::RefCountedThreadSafe<DirectoryLister> { | 25 class DirectoryLister : public base::RefCountedThreadSafe<DirectoryLister>, |
| 26 public PlatformThread::Delegate { |
| 25 public: | 27 public: |
| 26 // Implement this class to receive directory entries. | 28 // Implement this class to receive directory entries. |
| 27 class Delegate { | 29 class DirectoryListerDelegate { |
| 28 public: | 30 public: |
| 29 virtual void OnListFile(const WIN32_FIND_DATA& data) = 0; | 31 virtual void OnListFile( |
| 32 const file_util::FileEnumerator::FindInfo& data) = 0; |
| 30 virtual void OnListDone(int error) = 0; | 33 virtual void OnListDone(int error) = 0; |
| 31 }; | 34 }; |
| 32 | 35 |
| 33 DirectoryLister(const std::wstring& dir, Delegate* delegate); | 36 DirectoryLister(const std::wstring& dir, DirectoryListerDelegate* delegate); |
| 34 ~DirectoryLister(); | 37 ~DirectoryLister(); |
| 35 | 38 |
| 36 // Call this method to start the directory enumeration thread. | 39 // Call this method to start the directory enumeration thread. |
| 37 bool Start(); | 40 bool Start(); |
| 38 | 41 |
| 39 // Call this method to asynchronously stop directory enumeration. The | 42 // Call this method to asynchronously stop directory enumeration. The |
| 40 // delegate will receive the OnListDone notification with an error code of | 43 // delegate will receive the OnListDone notification with an error code of |
| 41 // ERROR_OPERATION_ABORTED. | 44 // ERROR_OPERATION_ABORTED. |
| 42 void Cancel(); | 45 void Cancel(); |
| 43 | 46 |
| 44 // The delegate pointer may be modified at any time. | 47 // The delegate pointer may be modified at any time. |
| 45 Delegate* delegate() const { return delegate_; } | 48 DirectoryListerDelegate* delegate() const { return delegate_; } |
| 46 void set_delegate(Delegate* d) { delegate_ = d; } | 49 void set_delegate(DirectoryListerDelegate* d) { delegate_ = d; } |
| 47 | 50 |
| 48 // Returns the directory being enumerated. | 51 // Returns the directory being enumerated. |
| 49 const std::wstring& directory() const { return dir_; } | 52 const std::wstring& directory() const { return dir_; } |
| 50 | 53 |
| 51 // Returns true if the directory enumeration was canceled. | 54 // Returns true if the directory enumeration was canceled. |
| 52 bool was_canceled() const { return canceled_; } | 55 bool was_canceled() const { return canceled_; } |
| 53 | 56 |
| 57 // PlatformThread::Delegate implementation |
| 58 void ThreadMain(); |
| 59 |
| 54 private: | 60 private: |
| 55 friend class DirectoryDataEvent; | 61 friend class DirectoryDataEvent; |
| 62 friend class ThreadDelegate; |
| 56 | 63 |
| 57 void OnReceivedData(const WIN32_FIND_DATA* data, int count); | 64 void OnReceivedData(const file_util::FileEnumerator::FindInfo* data, |
| 65 int count); |
| 58 void OnDone(int error); | 66 void OnDone(int error); |
| 59 | 67 |
| 60 static unsigned __stdcall ThreadFunc(void* param); | |
| 61 | |
| 62 std::wstring dir_; | 68 std::wstring dir_; |
| 63 Delegate* delegate_; | 69 DirectoryListerDelegate* delegate_; |
| 64 MessageLoop* message_loop_; | 70 MessageLoop* message_loop_; |
| 65 HANDLE thread_; | 71 PlatformThreadHandle thread_; |
| 66 bool canceled_; | 72 bool canceled_; |
| 67 }; | 73 }; |
| 68 | 74 |
| 69 } // namespace net | 75 } // namespace net |
| 70 | 76 |
| 71 #endif // NET_BASE_DIRECTORY_LISTER_H__ | 77 #endif // NET_BASE_DIRECTORY_LISTER_H__ |
| 72 | 78 |
| OLD | NEW |