| 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" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/file_enumerator.h" |
| 12 #include "base/i18n/file_util_icu.h" | 13 #include "base/i18n/file_util_icu.h" |
| 13 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 14 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 15 #include "base/threading/worker_pool.h" | 16 #include "base/threading/worker_pool.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 17 | 18 |
| 18 namespace net { | 19 namespace net { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 const int kFilesPerEvent = 8; | 23 const int kFilesPerEvent = 8; |
| 23 | 24 |
| 24 bool IsDotDot(const base::FilePath& path) { | 25 bool IsDotDot(const base::FilePath& path) { |
| 25 return FILE_PATH_LITERAL("..") == path.BaseName().value(); | 26 return FILE_PATH_LITERAL("..") == path.BaseName().value(); |
| 26 } | 27 } |
| 27 | 28 |
| 28 // Comparator for sorting lister results. This uses the locale aware filename | 29 // Comparator for sorting lister results. This uses the locale aware filename |
| 29 // comparison function on the filenames for sorting in the user's locale. | 30 // comparison function on the filenames for sorting in the user's locale. |
| 30 // Static. | 31 // Static. |
| 31 bool CompareAlphaDirsFirst(const DirectoryLister::DirectoryListerData& a, | 32 bool CompareAlphaDirsFirst(const DirectoryLister::DirectoryListerData& a, |
| 32 const DirectoryLister::DirectoryListerData& b) { | 33 const DirectoryLister::DirectoryListerData& b) { |
| 33 // Parent directory before all else. | 34 // Parent directory before all else. |
| 34 if (IsDotDot(file_util::FileEnumerator::GetFilename(a.info))) | 35 if (IsDotDot(a.info.GetName())) |
| 35 return true; | 36 return true; |
| 36 if (IsDotDot(file_util::FileEnumerator::GetFilename(b.info))) | 37 if (IsDotDot(b.info.GetName())) |
| 37 return false; | 38 return false; |
| 38 | 39 |
| 39 // Directories before regular files. | 40 // Directories before regular files. |
| 40 bool a_is_directory = file_util::FileEnumerator::IsDirectory(a.info); | 41 bool a_is_directory = a.info.IsDirectory(); |
| 41 bool b_is_directory = file_util::FileEnumerator::IsDirectory(b.info); | 42 bool b_is_directory = b.info.IsDirectory(); |
| 42 if (a_is_directory != b_is_directory) | 43 if (a_is_directory != b_is_directory) |
| 43 return a_is_directory; | 44 return a_is_directory; |
| 44 | 45 |
| 45 return file_util::LocaleAwareCompareFilenames( | 46 return file_util::LocaleAwareCompareFilenames(a.info.GetName(), |
| 46 file_util::FileEnumerator::GetFilename(a.info), | 47 b.info.GetName()); |
| 47 file_util::FileEnumerator::GetFilename(b.info)); | |
| 48 } | 48 } |
| 49 | 49 |
| 50 bool CompareDate(const DirectoryLister::DirectoryListerData& a, | 50 bool CompareDate(const DirectoryLister::DirectoryListerData& a, |
| 51 const DirectoryLister::DirectoryListerData& b) { | 51 const DirectoryLister::DirectoryListerData& b) { |
| 52 // Parent directory before all else. | 52 // Parent directory before all else. |
| 53 if (IsDotDot(file_util::FileEnumerator::GetFilename(a.info))) | 53 if (IsDotDot(a.info.GetName())) |
| 54 return true; | 54 return true; |
| 55 if (IsDotDot(file_util::FileEnumerator::GetFilename(b.info))) | 55 if (IsDotDot(b.info.GetName())) |
| 56 return false; | 56 return false; |
| 57 | 57 |
| 58 // Directories before regular files. | 58 // Directories before regular files. |
| 59 bool a_is_directory = file_util::FileEnumerator::IsDirectory(a.info); | 59 bool a_is_directory = a.info.IsDirectory(); |
| 60 bool b_is_directory = file_util::FileEnumerator::IsDirectory(b.info); | 60 bool b_is_directory = b.info.IsDirectory(); |
| 61 if (a_is_directory != b_is_directory) | 61 if (a_is_directory != b_is_directory) |
| 62 return a_is_directory; | 62 return a_is_directory; |
| 63 #if defined(OS_POSIX) | 63 return a.info.GetLastModifiedTime() > b.info.GetLastModifiedTime(); |
| 64 return a.info.stat.st_mtime > b.info.stat.st_mtime; | |
| 65 #elif defined(OS_WIN) | |
| 66 if (a.info.ftLastWriteTime.dwHighDateTime == | |
| 67 b.info.ftLastWriteTime.dwHighDateTime) { | |
| 68 return a.info.ftLastWriteTime.dwLowDateTime > | |
| 69 b.info.ftLastWriteTime.dwLowDateTime; | |
| 70 } else { | |
| 71 return a.info.ftLastWriteTime.dwHighDateTime > | |
| 72 b.info.ftLastWriteTime.dwHighDateTime; | |
| 73 } | |
| 74 #endif | |
| 75 } | 64 } |
| 76 | 65 |
| 77 // Comparator for sorting find result by paths. This uses the locale-aware | 66 // Comparator for sorting find result by paths. This uses the locale-aware |
| 78 // comparison function on the filenames for sorting in the user's locale. | 67 // comparison function on the filenames for sorting in the user's locale. |
| 79 // Static. | 68 // Static. |
| 80 bool CompareFullPath(const DirectoryLister::DirectoryListerData& a, | 69 bool CompareFullPath(const DirectoryLister::DirectoryListerData& a, |
| 81 const DirectoryLister::DirectoryListerData& b) { | 70 const DirectoryLister::DirectoryListerData& b) { |
| 82 return file_util::LocaleAwareCompareFilenames(a.path, b.path); | 71 return file_util::LocaleAwareCompareFilenames(a.path, b.path); |
| 83 } | 72 } |
| 84 | 73 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 143 |
| 155 void DirectoryLister::Core::StartInternal() { | 144 void DirectoryLister::Core::StartInternal() { |
| 156 | 145 |
| 157 if (!file_util::DirectoryExists(dir_)) { | 146 if (!file_util::DirectoryExists(dir_)) { |
| 158 origin_loop_->PostTask( | 147 origin_loop_->PostTask( |
| 159 FROM_HERE, | 148 FROM_HERE, |
| 160 base::Bind(&DirectoryLister::Core::OnDone, this, ERR_FILE_NOT_FOUND)); | 149 base::Bind(&DirectoryLister::Core::OnDone, this, ERR_FILE_NOT_FOUND)); |
| 161 return; | 150 return; |
| 162 } | 151 } |
| 163 | 152 |
| 164 int types = file_util::FileEnumerator::FILES | | 153 int types = base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES; |
| 165 file_util::FileEnumerator::DIRECTORIES; | |
| 166 if (!recursive_) | 154 if (!recursive_) |
| 167 types |= file_util::FileEnumerator::INCLUDE_DOT_DOT; | 155 types |= base::FileEnumerator::INCLUDE_DOT_DOT; |
| 168 | 156 |
| 169 file_util::FileEnumerator file_enum(dir_, recursive_, types); | 157 base::FileEnumerator file_enum(dir_, recursive_, types); |
| 170 | 158 |
| 171 base::FilePath path; | 159 base::FilePath path; |
| 172 std::vector<DirectoryListerData> file_data; | 160 std::vector<DirectoryListerData> file_data; |
| 173 while (lister_ && !(path = file_enum.Next()).empty()) { | 161 while (lister_ && !(path = file_enum.Next()).empty()) { |
| 174 DirectoryListerData data; | 162 DirectoryListerData data; |
| 175 file_enum.GetFindInfo(&data.info); | 163 data.info = file_enum.GetInfo(); |
| 176 data.path = path; | 164 data.path = path; |
| 177 file_data.push_back(data); | 165 file_data.push_back(data); |
| 178 | 166 |
| 179 /* TODO(brettw) bug 24107: It would be nice to send incremental updates. | 167 /* TODO(brettw) bug 24107: It would be nice to send incremental updates. |
| 180 We gather them all so they can be sorted, but eventually the sorting | 168 We gather them all so they can be sorted, but eventually the sorting |
| 181 should be done from JS to give more flexibility in the page. When we do | 169 should be done from JS to give more flexibility in the page. When we do |
| 182 that, we can uncomment this to send incremental updates to the page. | 170 that, we can uncomment this to send incremental updates to the page. |
| 183 | 171 |
| 184 if (file_data.size() < kFilesPerEvent) | 172 if (file_data.size() < kFilesPerEvent) |
| 185 continue; | 173 continue; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 206 |
| 219 void DirectoryLister::OnReceivedData(const DirectoryListerData& data) { | 207 void DirectoryLister::OnReceivedData(const DirectoryListerData& data) { |
| 220 delegate_->OnListFile(data); | 208 delegate_->OnListFile(data); |
| 221 } | 209 } |
| 222 | 210 |
| 223 void DirectoryLister::OnDone(int error) { | 211 void DirectoryLister::OnDone(int error) { |
| 224 delegate_->OnListDone(error); | 212 delegate_->OnListDone(error); |
| 225 } | 213 } |
| 226 | 214 |
| 227 } // namespace net | 215 } // namespace net |
| OLD | NEW |