summaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authordanduong <danduong@chromium.org>2015-05-01 15:57:22 -0700
committerCommit bot <commit-bot@chromium.org>2015-05-01 22:58:28 +0000
commit0883e859e7057e0dc29c805c74d9e74a867b842a (patch)
treee55e631bc6d4897b1cc119293a2c9d6356e0a74e /components
parent40de287cfbf7a843f903b72ed15c2f82cb0537ea (diff)
downloadchromium_src-0883e859e7057e0dc29c805c74d9e74a867b842a.zip
chromium_src-0883e859e7057e0dc29c805c74d9e74a867b842a.tar.gz
chromium_src-0883e859e7057e0dc29c805c74d9e74a867b842a.tar.bz2
Shortcut bookmarks search to call directly into the BookmarkModel
If the query contains a URL, this turns a O(n) algorithm to O(log(n)). BUG=483337 Review URL: https://codereview.chromium.org/1114043007 Cr-Commit-Position: refs/heads/master@{#328006}
Diffstat (limited to 'components')
-rw-r--r--components/bookmarks/browser/bookmark_utils.cc81
1 files changed, 57 insertions, 24 deletions
diff --git a/components/bookmarks/browser/bookmark_utils.cc b/components/bookmarks/browser/bookmark_utils.cc
index ccd5ebb..9138591 100644
--- a/components/bookmarks/browser/bookmark_utils.cc
+++ b/components/bookmarks/browser/bookmark_utils.cc
@@ -163,6 +163,49 @@ GURL GetUrlFromClipboard() {
return GURL(url_text);
}
+class VectorIterator {
+ public:
+ explicit VectorIterator(std::vector<const BookmarkNode*>* nodes)
+ : nodes_(nodes), current_(nodes->begin()) {}
+ bool has_next() { return (current_ != nodes_->end()); }
+ const BookmarkNode* Next() {
+ const BookmarkNode* result = *current_;
+ ++current_;
+ return result;
+ }
+
+ private:
+ std::vector<const BookmarkNode*>* nodes_;
+ std::vector<const BookmarkNode*>::iterator current_;
+
+ DISALLOW_COPY_AND_ASSIGN(VectorIterator);
+};
+
+template <class type>
+void GetBookmarksMatchingPropertiesImpl(
+ type& iterator,
+ BookmarkModel* model,
+ const QueryFields& query,
+ const std::vector<base::string16>& query_words,
+ size_t max_count,
+ const std::string& languages,
+ std::vector<const BookmarkNode*>* nodes) {
+ while (iterator.has_next()) {
+ const BookmarkNode* node = iterator.Next();
+ if ((!query_words.empty() &&
+ !DoesBookmarkContainWords(node, query_words, languages)) ||
+ model->is_permanent_node(node)) {
+ continue;
+ }
+ if (query.title && node->GetTitle() != *query.title)
+ continue;
+
+ nodes->push_back(node);
+ if (nodes->size() == max_count)
+ return;
+ }
+}
+
} // namespace
QueryFields::QueryFields() {}
@@ -361,30 +404,20 @@ void GetBookmarksMatchingProperties(BookmarkModel* model,
return;
}
- ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node());
- while (iterator.has_next()) {
- const BookmarkNode* node = iterator.Next();
- if ((!query_words.empty() &&
- !DoesBookmarkContainWords(node, query_words, languages)) ||
- model->is_permanent_node(node)) {
- continue;
- }
- if (query.url) {
- // Check against bare url spec and IDN-decoded url.
- if (!node->is_url() ||
- !(base::UTF8ToUTF16(node->url().spec()) == *query.url ||
- net::FormatUrl(
- node->url(), languages, net::kFormatUrlOmitNothing,
- net::UnescapeRule::NORMAL, NULL, NULL, NULL) == *query.url)) {
- continue;
- }
- }
- if (query.title && node->GetTitle() != *query.title)
- continue;
-
- nodes->push_back(node);
- if (nodes->size() == max_count)
- return;
+ if (query.url) {
+ // Shortcut into the BookmarkModel if searching for URL.
+ GURL url(*query.url);
+ std::vector<const BookmarkNode*> url_matched_nodes;
+ if (url.is_valid())
+ model->GetNodesByURL(url, &url_matched_nodes);
+ bookmarks::VectorIterator iterator(&url_matched_nodes);
+ GetBookmarksMatchingPropertiesImpl<bookmarks::VectorIterator>(
+ iterator, model, query, query_words, max_count, languages, nodes);
+ } else {
+ ui::TreeNodeIterator<const BookmarkNode> iterator(model->root_node());
+ GetBookmarksMatchingPropertiesImpl<
+ ui::TreeNodeIterator<const BookmarkNode>>(
+ iterator, model, query, query_words, max_count, languages, nodes);
}
}