OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/browser/chromeos/drive/search_metadata.h" | 5 #include "chrome/browser/chromeos/drive/search_metadata.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <queue> | 8 #include <queue> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 ResourceMetadata* resource_metadata, | 121 ResourceMetadata* resource_metadata, |
122 FileCache* cache, | 122 FileCache* cache, |
123 const std::string& query, | 123 const std::string& query, |
124 int options, | 124 int options, |
125 size_t at_most_num_matches, | 125 size_t at_most_num_matches, |
126 ScopedPriorityQueue<MetadataSearchResult, | 126 ScopedPriorityQueue<MetadataSearchResult, |
127 MetadataSearchResultComparator>* result_candidates, | 127 MetadataSearchResultComparator>* result_candidates, |
128 const ResourceEntry& entry) { | 128 const ResourceEntry& entry) { |
129 DCHECK_GE(at_most_num_matches, result_candidates->size()); | 129 DCHECK_GE(at_most_num_matches, result_candidates->size()); |
130 | 130 |
| 131 // If the candidate set is already full, and this |entry| is old, do nothing. |
| 132 // We perform this check first in order to avoid the costly find-and-highlight |
| 133 // or FilePath lookup as much as possible. |
| 134 if (result_candidates->size() == at_most_num_matches && |
| 135 !CompareByTimestamp(entry, result_candidates->top()->entry)) |
| 136 return; |
| 137 |
131 // Add |entry| to the result if the entry is eligible for the given | 138 // Add |entry| to the result if the entry is eligible for the given |
132 // |options| and matches the query. The base name of the entry must | 139 // |options| and matches the query. The base name of the entry must |
133 // contain |query| to match the query. | 140 // contain |query| to match the query. |
134 std::string highlighted; | 141 std::string highlighted; |
135 if (!IsEligibleEntry(entry, cache, options) || | 142 if (!IsEligibleEntry(entry, cache, options) || |
136 !FindAndHighlight(entry.base_name(), query, &highlighted)) | 143 !FindAndHighlight(entry.base_name(), query, &highlighted)) |
137 return; | 144 return; |
138 | 145 |
139 base::FilePath path = resource_metadata->GetFilePath(entry.resource_id()); | 146 base::FilePath path = resource_metadata->GetFilePath(entry.resource_id()); |
140 if (path.empty()) | 147 if (path.empty()) |
141 return; | 148 return; |
142 | 149 |
143 // Make space for |entry| when appropriate. | 150 // Make space for |entry| when appropriate. |
144 if (result_candidates->size() == at_most_num_matches && | 151 if (result_candidates->size() == at_most_num_matches) |
145 CompareByTimestamp(entry, result_candidates->top()->entry)) | |
146 result_candidates->pop(); | 152 result_candidates->pop(); |
147 | 153 result_candidates->push(new MetadataSearchResult(path, entry, highlighted)); |
148 // Add |entry| to the result when appropriate. | |
149 if (result_candidates->size() < at_most_num_matches) | |
150 result_candidates->push(new MetadataSearchResult(path, entry, highlighted)); | |
151 } | 154 } |
152 | 155 |
153 // Implements SearchMetadata(). | 156 // Implements SearchMetadata(). |
154 scoped_ptr<MetadataSearchResultVector> SearchMetadataOnBlockingPool( | 157 scoped_ptr<MetadataSearchResultVector> SearchMetadataOnBlockingPool( |
155 ResourceMetadata* resource_metadata, | 158 ResourceMetadata* resource_metadata, |
156 FileCache* cache, | 159 FileCache* cache, |
157 const std::string& query, | 160 const std::string& query, |
158 int options, | 161 int options, |
159 int at_most_num_matches) { | 162 int at_most_num_matches) { |
160 ScopedPriorityQueue<MetadataSearchResult, | 163 ScopedPriorityQueue<MetadataSearchResult, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 highlighted_text->append(net::EscapeForHTML(UTF16ToUTF8(pre))); | 234 highlighted_text->append(net::EscapeForHTML(UTF16ToUTF8(pre))); |
232 highlighted_text->append("<b>"); | 235 highlighted_text->append("<b>"); |
233 highlighted_text->append(net::EscapeForHTML(UTF16ToUTF8(match))); | 236 highlighted_text->append(net::EscapeForHTML(UTF16ToUTF8(match))); |
234 highlighted_text->append("</b>"); | 237 highlighted_text->append("</b>"); |
235 highlighted_text->append(net::EscapeForHTML(UTF16ToUTF8(post))); | 238 highlighted_text->append(net::EscapeForHTML(UTF16ToUTF8(post))); |
236 return true; | 239 return true; |
237 } | 240 } |
238 | 241 |
239 } // namespace internal | 242 } // namespace internal |
240 } // namespace drive | 243 } // namespace drive |
OLD | NEW |