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