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 #include "net/base/directory_lister.h" | 5 #include "net/base/directory_lister.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 else if (sort_type == DirectoryLister::ALPHA_DIRS_FIRST) | 80 else if (sort_type == DirectoryLister::ALPHA_DIRS_FIRST) |
81 std::sort(data->begin(), data->end(), CompareAlphaDirsFirst); | 81 std::sort(data->begin(), data->end(), CompareAlphaDirsFirst); |
82 else | 82 else |
83 DCHECK_EQ(DirectoryLister::NO_SORT, sort_type); | 83 DCHECK_EQ(DirectoryLister::NO_SORT, sort_type); |
84 } | 84 } |
85 | 85 |
86 } // namespace | 86 } // namespace |
87 | 87 |
88 DirectoryLister::DirectoryLister(const base::FilePath& dir, | 88 DirectoryLister::DirectoryLister(const base::FilePath& dir, |
89 DirectoryListerDelegate* delegate) | 89 DirectoryListerDelegate* delegate) |
90 : core_(new Core(dir, false, ALPHA_DIRS_FIRST, this)), | 90 : core_(new Core(dir, false, ALPHA_DIRS_FIRST, this)), delegate_(delegate) { |
91 delegate_(delegate) { | |
92 DCHECK(delegate_); | 91 DCHECK(delegate_); |
93 DCHECK(!dir.value().empty()); | 92 DCHECK(!dir.value().empty()); |
94 } | 93 } |
95 | 94 |
96 DirectoryLister::DirectoryLister(const base::FilePath& dir, | 95 DirectoryLister::DirectoryLister(const base::FilePath& dir, |
97 bool recursive, | 96 bool recursive, |
98 SortType sort, | 97 SortType sort, |
99 DirectoryListerDelegate* delegate) | 98 DirectoryListerDelegate* delegate) |
100 : core_(new Core(dir, recursive, sort, this)), | 99 : core_(new Core(dir, recursive, sort, this)), delegate_(delegate) { |
101 delegate_(delegate) { | |
102 DCHECK(delegate_); | 100 DCHECK(delegate_); |
103 DCHECK(!dir.value().empty()); | 101 DCHECK(!dir.value().empty()); |
104 } | 102 } |
105 | 103 |
106 DirectoryLister::~DirectoryLister() { | 104 DirectoryLister::~DirectoryLister() { |
107 Cancel(); | 105 Cancel(); |
108 } | 106 } |
109 | 107 |
110 bool DirectoryLister::Start() { | 108 bool DirectoryLister::Start() { |
111 return core_->Start(); | 109 return core_->Start(); |
112 } | 110 } |
113 | 111 |
114 void DirectoryLister::Cancel() { | 112 void DirectoryLister::Cancel() { |
115 return core_->Cancel(); | 113 return core_->Cancel(); |
116 } | 114 } |
117 | 115 |
118 DirectoryLister::Core::Core(const base::FilePath& dir, | 116 DirectoryLister::Core::Core(const base::FilePath& dir, |
119 bool recursive, | 117 bool recursive, |
120 SortType sort, | 118 SortType sort, |
121 DirectoryLister* lister) | 119 DirectoryLister* lister) |
122 : dir_(dir), | 120 : dir_(dir), recursive_(recursive), sort_(sort), lister_(lister) { |
123 recursive_(recursive), | |
124 sort_(sort), | |
125 lister_(lister) { | |
126 DCHECK(lister_); | 121 DCHECK(lister_); |
127 } | 122 } |
128 | 123 |
129 DirectoryLister::Core::~Core() {} | 124 DirectoryLister::Core::~Core() { |
| 125 } |
130 | 126 |
131 bool DirectoryLister::Core::Start() { | 127 bool DirectoryLister::Core::Start() { |
132 origin_loop_ = base::MessageLoopProxy::current(); | 128 origin_loop_ = base::MessageLoopProxy::current(); |
133 | 129 |
134 return base::WorkerPool::PostTask( | 130 return base::WorkerPool::PostTask( |
135 FROM_HERE, base::Bind(&Core::StartInternal, this), true); | 131 FROM_HERE, base::Bind(&Core::StartInternal, this), true); |
136 } | 132 } |
137 | 133 |
138 void DirectoryLister::Core::Cancel() { | 134 void DirectoryLister::Core::Cancel() { |
139 lister_ = NULL; | 135 lister_ = NULL; |
140 } | 136 } |
141 | 137 |
142 void DirectoryLister::Core::StartInternal() { | 138 void DirectoryLister::Core::StartInternal() { |
143 | |
144 if (!base::DirectoryExists(dir_)) { | 139 if (!base::DirectoryExists(dir_)) { |
145 origin_loop_->PostTask( | 140 origin_loop_->PostTask( |
146 FROM_HERE, | 141 FROM_HERE, |
147 base::Bind(&DirectoryLister::Core::OnDone, this, ERR_FILE_NOT_FOUND)); | 142 base::Bind(&DirectoryLister::Core::OnDone, this, ERR_FILE_NOT_FOUND)); |
148 return; | 143 return; |
149 } | 144 } |
150 | 145 |
151 int types = base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES; | 146 int types = base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES; |
152 if (!recursive_) | 147 if (!recursive_) |
153 types |= base::FileEnumerator::INCLUDE_DOT_DOT; | 148 types |= base::FileEnumerator::INCLUDE_DOT_DOT; |
(...skipping 19 matching lines...) Expand all Loading... |
173 | 168 |
174 origin_loop_->PostTask( | 169 origin_loop_->PostTask( |
175 FROM_HERE, | 170 FROM_HERE, |
176 base::Bind(&DirectoryLister::Core::SendData, file_data)); | 171 base::Bind(&DirectoryLister::Core::SendData, file_data)); |
177 file_data.clear(); | 172 file_data.clear(); |
178 */ | 173 */ |
179 } | 174 } |
180 | 175 |
181 SortData(&file_data, sort_); | 176 SortData(&file_data, sort_); |
182 origin_loop_->PostTask( | 177 origin_loop_->PostTask( |
183 FROM_HERE, | 178 FROM_HERE, base::Bind(&DirectoryLister::Core::SendData, this, file_data)); |
184 base::Bind(&DirectoryLister::Core::SendData, this, file_data)); | |
185 | 179 |
186 origin_loop_->PostTask( | 180 origin_loop_->PostTask(FROM_HERE, |
187 FROM_HERE, | 181 base::Bind(&DirectoryLister::Core::OnDone, this, OK)); |
188 base::Bind(&DirectoryLister::Core::OnDone, this, OK)); | |
189 } | 182 } |
190 | 183 |
191 void DirectoryLister::Core::SendData( | 184 void DirectoryLister::Core::SendData( |
192 const std::vector<DirectoryLister::DirectoryListerData>& data) { | 185 const std::vector<DirectoryLister::DirectoryListerData>& data) { |
193 DCHECK(origin_loop_->BelongsToCurrentThread()); | 186 DCHECK(origin_loop_->BelongsToCurrentThread()); |
194 // We need to check for cancellation (indicated by NULL'ing of |lister_|) | 187 // We need to check for cancellation (indicated by NULL'ing of |lister_|) |
195 // which can happen during each callback. | 188 // which can happen during each callback. |
196 for (size_t i = 0; lister_ && i < data.size(); ++i) | 189 for (size_t i = 0; lister_ && i < data.size(); ++i) |
197 lister_->OnReceivedData(data[i]); | 190 lister_->OnReceivedData(data[i]); |
198 } | 191 } |
199 | 192 |
200 void DirectoryLister::Core::OnDone(int error) { | 193 void DirectoryLister::Core::OnDone(int error) { |
201 DCHECK(origin_loop_->BelongsToCurrentThread()); | 194 DCHECK(origin_loop_->BelongsToCurrentThread()); |
202 if (lister_) | 195 if (lister_) |
203 lister_->OnDone(error); | 196 lister_->OnDone(error); |
204 } | 197 } |
205 | 198 |
206 void DirectoryLister::OnReceivedData(const DirectoryListerData& data) { | 199 void DirectoryLister::OnReceivedData(const DirectoryListerData& data) { |
207 delegate_->OnListFile(data); | 200 delegate_->OnListFile(data); |
208 } | 201 } |
209 | 202 |
210 void DirectoryLister::OnDone(int error) { | 203 void DirectoryLister::OnDone(int error) { |
211 delegate_->OnListDone(error); | 204 delegate_->OnListDone(error); |
212 } | 205 } |
213 | 206 |
214 } // namespace net | 207 } // namespace net |
OLD | NEW |