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 "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 10 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
(...skipping 15 matching lines...) Expand all Loading... | |
26 | 26 |
27 if (a_file_info.last_accessed() != b_file_info.last_accessed()) | 27 if (a_file_info.last_accessed() != b_file_info.last_accessed()) |
28 return a_file_info.last_accessed() > b_file_info.last_accessed(); | 28 return a_file_info.last_accessed() > b_file_info.last_accessed(); |
29 | 29 |
30 // When the entries have the same last access time (which happens quite often | 30 // When the entries have the same last access time (which happens quite often |
31 // because Drive server doesn't set the field until an entry is viewed via | 31 // because Drive server doesn't set the field until an entry is viewed via |
32 // drive.google.com), we use last modified time as the tie breaker. | 32 // drive.google.com), we use last modified time as the tie breaker. |
33 return a_file_info.last_modified() > b_file_info.last_modified(); | 33 return a_file_info.last_modified() > b_file_info.last_modified(); |
34 } | 34 } |
35 | 35 |
36 // Returns true if |entry| is eligible for the search |options| and should be | |
37 // tested for the match with the query. | |
38 // If SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS is requested, the hosted | |
39 // documents are skipped. If SEARCH_METADATA_EXCLUDE_DIRECTORIES is requested, | |
40 // the directories are skipped. If SEARCH_METADATA_SHARED_WITH_ME is requested, | |
41 // only the entries with shared-with-me label will be tested. | |
42 bool IsEligibleEntry(const DriveEntryProto& entry, int options) { | |
43 if ((options & SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS) && | |
44 entry.file_specific_info().is_hosted_document()) | |
45 return false; | |
46 | |
47 if ((options & SEARCH_METADATA_EXCLUDE_DIRECTORIES) && | |
48 entry.file_info().is_directory()) | |
49 return false; | |
50 | |
51 if (options & SEARCH_METADATA_SHARED_WITH_ME) | |
52 return entry.shared_with_me(); | |
53 | |
54 return true; | |
55 } | |
56 | |
57 // Returns true if this search needs to traverse the tree under |entry|. | |
58 bool ShouldReadDirectory(const DriveEntryProto& entry, int options) { | |
59 // Cannot read non-directory. | |
60 if (!entry.file_info().is_directory()) | |
61 return false; | |
62 | |
63 // This is a directory. | |
64 // Shared-with-me search do not go into a share-with-me directory. | |
65 if (options & SEARCH_METADATA_SHARED_WITH_ME) | |
66 return !entry.shared_with_me(); | |
67 | |
68 return true; | |
69 } | |
70 | |
36 } // namespace | 71 } // namespace |
37 | 72 |
38 // Helper class for searching the local resource metadata. | 73 // Helper class for searching the local resource metadata. |
39 class SearchMetadataHelper { | 74 class SearchMetadataHelper { |
40 public: | 75 public: |
41 SearchMetadataHelper(DriveResourceMetadata* resource_metadata, | 76 SearchMetadataHelper(DriveResourceMetadata* resource_metadata, |
42 const std::string& query, | 77 const std::string& query, |
43 int options, | 78 int options, |
44 int at_most_num_matches, | 79 int at_most_num_matches, |
45 const SearchMetadataCallback& callback) | 80 const SearchMetadataCallback& callback) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 } | 115 } |
81 DCHECK(entries); | 116 DCHECK(entries); |
82 | 117 |
83 --num_pending_reads_; | 118 --num_pending_reads_; |
84 for (size_t i = 0; i < entries->size(); ++i) { | 119 for (size_t i = 0; i < entries->size(); ++i) { |
85 const DriveEntryProto& entry = entries->at(i); | 120 const DriveEntryProto& entry = entries->at(i); |
86 const base::FilePath current_path = parent_path.Append( | 121 const base::FilePath current_path = parent_path.Append( |
87 base::FilePath::FromUTF8Unsafe(entry.base_name())); | 122 base::FilePath::FromUTF8Unsafe(entry.base_name())); |
88 | 123 |
89 // Recursively reading the sub directory. | 124 // Recursively reading the sub directory. |
90 if (entry.file_info().is_directory()) { | 125 if (ShouldReadDirectory(entry, options_)) { |
91 ++num_pending_reads_; | 126 ++num_pending_reads_; |
92 resource_metadata_->ReadDirectoryByPath( | 127 resource_metadata_->ReadDirectoryByPath( |
93 current_path, | 128 current_path, |
94 base::Bind(&SearchMetadataHelper::DidReadDirectoryByPath, | 129 base::Bind(&SearchMetadataHelper::DidReadDirectoryByPath, |
95 weak_ptr_factory_.GetWeakPtr(), | 130 weak_ptr_factory_.GetWeakPtr(), |
96 current_path)); | 131 current_path)); |
97 } | 132 } |
98 | 133 |
99 // Skip the hosted document if |options| has | 134 // Add it to the search result if the entry is eligible for the given |
100 // SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS, which is set by | 135 // |options| and matches the query. The base name of the entry must |
101 // DriveFileSystem::SearchMetadata() based on the preference. | 136 // contains |query| to match the query. |
102 if ((options_ & SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS) && | |
103 entry.file_specific_info().is_hosted_document()) | |
104 continue; | |
105 | |
106 // Skip the directory if SEARCH_METADATA_EXCLUDE_DIRECTORIES is requested. | |
107 if ((options_ & SEARCH_METADATA_EXCLUDE_DIRECTORIES) && | |
108 entry.file_info().is_directory()) | |
109 continue; | |
110 | |
111 // Add it to the search result if the base name of the file contains | |
112 // the query. | |
113 std::string highlighted; | 137 std::string highlighted; |
114 if (FindAndHighlight(entry.base_name(), query_, &highlighted)) { | 138 if (IsEligibleEntry(entry, options_) && |
139 FindAndHighlight(entry.base_name(), query_, &highlighted)) { | |
115 results_->push_back( | 140 results_->push_back( |
116 MetadataSearchResult(current_path, entry, highlighted)); | 141 MetadataSearchResult(current_path, entry, highlighted)); |
117 } | 142 } |
143 | |
yoshiki
2013/04/10 04:29:49
nit: remove
Haruki Sato
2013/04/10 04:51:13
Done.
| |
118 } | 144 } |
119 | 145 |
120 if (num_pending_reads_ == 0) { | 146 if (num_pending_reads_ == 0) { |
121 // Search is complete. Send the result to the callback. | 147 // Search is complete. Send the result to the callback. |
122 std::sort(results_->begin(), results_->end(), &CompareByTimestamp); | 148 std::sort(results_->begin(), results_->end(), &CompareByTimestamp); |
123 if (results_->size() > static_cast<size_t>(at_most_num_matches_)) { | 149 if (results_->size() > static_cast<size_t>(at_most_num_matches_)) { |
124 // Don't use resize() as it requires a default constructor. | 150 // Don't use resize() as it requires a default constructor. |
125 results_->erase(results_->begin() + at_most_num_matches_, | 151 results_->erase(results_->begin() + at_most_num_matches_, |
126 results_->end()); | 152 results_->end()); |
127 } | 153 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 if (num_matches == 0) | 224 if (num_matches == 0) |
199 return false; | 225 return false; |
200 | 226 |
201 std::string remaining_piece = text.substr(cursor); | 227 std::string remaining_piece = text.substr(cursor); |
202 highlighted_text->append(net::EscapeForHTML(remaining_piece)); | 228 highlighted_text->append(net::EscapeForHTML(remaining_piece)); |
203 | 229 |
204 return true; | 230 return true; |
205 } | 231 } |
206 | 232 |
207 } // namespace drive | 233 } // namespace drive |
OLD | NEW |