Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: net/base/directory_lister.cc

Issue 1096323003: Remove unused sorting options from DirectoryLister. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed nits Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/directory_lister.h ('k') | net/base/directory_lister_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_enumerator.h" 10 #include "base/files/file_enumerator.h"
(...skipping 27 matching lines...) Expand all
38 // Directories before regular files. 38 // Directories before regular files.
39 bool a_is_directory = a.info.IsDirectory(); 39 bool a_is_directory = a.info.IsDirectory();
40 bool b_is_directory = b.info.IsDirectory(); 40 bool b_is_directory = b.info.IsDirectory();
41 if (a_is_directory != b_is_directory) 41 if (a_is_directory != b_is_directory)
42 return a_is_directory; 42 return a_is_directory;
43 43
44 return base::i18n::LocaleAwareCompareFilenames(a.info.GetName(), 44 return base::i18n::LocaleAwareCompareFilenames(a.info.GetName(),
45 b.info.GetName()); 45 b.info.GetName());
46 } 46 }
47 47
48 bool CompareDate(const DirectoryLister::DirectoryListerData& a,
49 const DirectoryLister::DirectoryListerData& b) {
50 // Parent directory before all else.
51 if (IsDotDot(a.info.GetName()))
52 return true;
53 if (IsDotDot(b.info.GetName()))
54 return false;
55
56 // Directories before regular files.
57 bool a_is_directory = a.info.IsDirectory();
58 bool b_is_directory = b.info.IsDirectory();
59 if (a_is_directory != b_is_directory)
60 return a_is_directory;
61 return a.info.GetLastModifiedTime() > b.info.GetLastModifiedTime();
62 }
63
64 // Comparator for sorting find result by paths. This uses the locale-aware
65 // comparison function on the filenames for sorting in the user's locale.
66 // Static.
67 bool CompareFullPath(const DirectoryLister::DirectoryListerData& a,
68 const DirectoryLister::DirectoryListerData& b) {
69 return base::i18n::LocaleAwareCompareFilenames(a.path, b.path);
70 }
71
72 void SortData(std::vector<DirectoryLister::DirectoryListerData>* data, 48 void SortData(std::vector<DirectoryLister::DirectoryListerData>* data,
73 DirectoryLister::SortType sort_type) { 49 DirectoryLister::ListingType listing_type) {
74 // Sort the results. See the TODO below (this sort should be removed and we 50 // Sort the results. See the TODO below (this sort should be removed and we
75 // should do it from JS). 51 // should do it from JS).
76 if (sort_type == DirectoryLister::DATE) { 52 if (listing_type == DirectoryLister::ALPHA_DIRS_FIRST) {
77 std::sort(data->begin(), data->end(), CompareDate);
78 } else if (sort_type == DirectoryLister::FULL_PATH) {
79 std::sort(data->begin(), data->end(), CompareFullPath);
80 } else if (sort_type == DirectoryLister::ALPHA_DIRS_FIRST) {
81 std::sort(data->begin(), data->end(), CompareAlphaDirsFirst); 53 std::sort(data->begin(), data->end(), CompareAlphaDirsFirst);
82 } else { 54 } else if (listing_type != DirectoryLister::NO_SORT &&
83 DCHECK_EQ(DirectoryLister::NO_SORT, sort_type); 55 listing_type != DirectoryLister::NO_SORT_RECURSIVE) {
56 NOTREACHED();
84 } 57 }
85 } 58 }
86 59
87 } // namespace 60 } // namespace
88 61
89 DirectoryLister::DirectoryLister(const base::FilePath& dir, 62 DirectoryLister::DirectoryLister(const base::FilePath& dir,
90 DirectoryListerDelegate* delegate) 63 DirectoryListerDelegate* delegate)
91 : delegate_(delegate) { 64 : delegate_(delegate) {
92 core_ = new Core(dir, false, ALPHA_DIRS_FIRST, this); 65 core_ = new Core(dir, ALPHA_DIRS_FIRST, this);
93 DCHECK(delegate_); 66 DCHECK(delegate_);
94 DCHECK(!dir.value().empty()); 67 DCHECK(!dir.value().empty());
95 } 68 }
96 69
97 DirectoryLister::DirectoryLister(const base::FilePath& dir, 70 DirectoryLister::DirectoryLister(const base::FilePath& dir,
98 bool recursive, 71 ListingType type,
99 SortType sort,
100 DirectoryListerDelegate* delegate) 72 DirectoryListerDelegate* delegate)
101 : delegate_(delegate) { 73 : delegate_(delegate) {
102 core_ = new Core(dir, recursive, sort, this); 74 core_ = new Core(dir, type, this);
103 DCHECK(delegate_); 75 DCHECK(delegate_);
104 DCHECK(!dir.value().empty()); 76 DCHECK(!dir.value().empty());
105 } 77 }
106 78
107 DirectoryLister::~DirectoryLister() { 79 DirectoryLister::~DirectoryLister() {
108 Cancel(); 80 Cancel();
109 } 81 }
110 82
111 bool DirectoryLister::Start() { 83 bool DirectoryLister::Start() {
112 return base::WorkerPool::PostTask( 84 return base::WorkerPool::PostTask(
113 FROM_HERE, 85 FROM_HERE,
114 base::Bind(&Core::Start, core_), 86 base::Bind(&Core::Start, core_),
115 true); 87 true);
116 } 88 }
117 89
118 void DirectoryLister::Cancel() { 90 void DirectoryLister::Cancel() {
119 core_->CancelOnOriginThread(); 91 core_->CancelOnOriginThread();
120 } 92 }
121 93
122 DirectoryLister::Core::Core(const base::FilePath& dir, 94 DirectoryLister::Core::Core(const base::FilePath& dir,
123 bool recursive, 95 ListingType type,
124 SortType sort,
125 DirectoryLister* lister) 96 DirectoryLister* lister)
126 : dir_(dir), 97 : dir_(dir),
127 recursive_(recursive), 98 type_(type),
128 sort_(sort),
129 origin_loop_(base::MessageLoopProxy::current()), 99 origin_loop_(base::MessageLoopProxy::current()),
130 lister_(lister), 100 lister_(lister),
131 cancelled_(0) { 101 cancelled_(0) {
132 DCHECK(lister_); 102 DCHECK(lister_);
133 } 103 }
134 104
135 DirectoryLister::Core::~Core() {} 105 DirectoryLister::Core::~Core() {}
136 106
137 void DirectoryLister::Core::CancelOnOriginThread() { 107 void DirectoryLister::Core::CancelOnOriginThread() {
138 DCHECK(origin_loop_->BelongsToCurrentThread()); 108 DCHECK(origin_loop_->BelongsToCurrentThread());
(...skipping 10 matching lines...) Expand all
149 119
150 if (!base::DirectoryExists(dir_)) { 120 if (!base::DirectoryExists(dir_)) {
151 origin_loop_->PostTask( 121 origin_loop_->PostTask(
152 FROM_HERE, 122 FROM_HERE,
153 base::Bind(&Core::DoneOnOriginThread, this, 123 base::Bind(&Core::DoneOnOriginThread, this,
154 base::Passed(directory_list.Pass()), ERR_FILE_NOT_FOUND)); 124 base::Passed(directory_list.Pass()), ERR_FILE_NOT_FOUND));
155 return; 125 return;
156 } 126 }
157 127
158 int types = base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES; 128 int types = base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES;
159 if (!recursive_) 129 bool recursive;
130 if (NO_SORT_RECURSIVE != type_) {
160 types |= base::FileEnumerator::INCLUDE_DOT_DOT; 131 types |= base::FileEnumerator::INCLUDE_DOT_DOT;
161 132 recursive = false;
162 base::FileEnumerator file_enum(dir_, recursive_, types); 133 } else {
134 recursive = true;
135 }
136 base::FileEnumerator file_enum(dir_, recursive, types);
163 137
164 base::FilePath path; 138 base::FilePath path;
165 while (!(path = file_enum.Next()).empty()) { 139 while (!(path = file_enum.Next()).empty()) {
166 // Abort on cancellation. This is purely for performance reasons. 140 // Abort on cancellation. This is purely for performance reasons.
167 // Correctness guarantees are made by checks in DoneOnOriginThread. 141 // Correctness guarantees are made by checks in DoneOnOriginThread.
168 if (IsCancelled()) 142 if (IsCancelled())
169 return; 143 return;
170 144
171 DirectoryListerData data; 145 DirectoryListerData data;
172 data.info = file_enum.GetInfo(); 146 data.info = file_enum.GetInfo();
173 data.path = path; 147 data.path = path;
174 directory_list->push_back(data); 148 directory_list->push_back(data);
175 149
176 /* TODO(brettw) bug 24107: It would be nice to send incremental updates. 150 /* TODO(brettw) bug 24107: It would be nice to send incremental updates.
177 We gather them all so they can be sorted, but eventually the sorting 151 We gather them all so they can be sorted, but eventually the sorting
178 should be done from JS to give more flexibility in the page. When we do 152 should be done from JS to give more flexibility in the page. When we do
179 that, we can uncomment this to send incremental updates to the page. 153 that, we can uncomment this to send incremental updates to the page.
180 154
181 const int kFilesPerEvent = 8; 155 const int kFilesPerEvent = 8;
182 if (file_data.size() < kFilesPerEvent) 156 if (file_data.size() < kFilesPerEvent)
183 continue; 157 continue;
184 158
185 origin_loop_->PostTask( 159 origin_loop_->PostTask(
186 FROM_HERE, 160 FROM_HERE,
187 base::Bind(&DirectoryLister::Core::SendData, file_data)); 161 base::Bind(&DirectoryLister::Core::SendData, file_data));
188 file_data.clear(); 162 file_data.clear();
189 */ 163 */
190 } 164 }
191 165
192 SortData(directory_list.get(), sort_); 166 SortData(directory_list.get(), type_);
193 167
194 origin_loop_->PostTask( 168 origin_loop_->PostTask(
195 FROM_HERE, 169 FROM_HERE,
196 base::Bind(&Core::DoneOnOriginThread, this, 170 base::Bind(&Core::DoneOnOriginThread, this,
197 base::Passed(directory_list.Pass()), OK)); 171 base::Passed(directory_list.Pass()), OK));
198 } 172 }
199 173
200 bool DirectoryLister::Core::IsCancelled() const { 174 bool DirectoryLister::Core::IsCancelled() const {
201 return !!base::subtle::NoBarrier_Load(&cancelled_); 175 return !!base::subtle::NoBarrier_Load(&cancelled_);
202 } 176 }
(...skipping 17 matching lines...) Expand all
220 194
221 void DirectoryLister::OnListFile(const DirectoryListerData& data) { 195 void DirectoryLister::OnListFile(const DirectoryListerData& data) {
222 delegate_->OnListFile(data); 196 delegate_->OnListFile(data);
223 } 197 }
224 198
225 void DirectoryLister::OnListDone(int error) { 199 void DirectoryLister::OnListDone(int error) {
226 delegate_->OnListDone(error); 200 delegate_->OnListDone(error);
227 } 201 }
228 202
229 } // namespace net 203 } // namespace net
OLDNEW
« no previous file with comments | « net/base/directory_lister.h ('k') | net/base/directory_lister_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698