1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_INDEX_H_
6 #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_INDEX_H_
13 #include "base/basictypes.h"
14 #include "base/strings/string16.h"
15 #include "components/query_parser/query_parser.h"
24 // BookmarkIndex maintains an index of the titles and URLs of bookmarks for
25 // quick look up. BookmarkIndex is owned and maintained by BookmarkModel, you
26 // shouldn't need to interact directly with BookmarkIndex.
28 // BookmarkIndex maintains the index (index_) as a map of sets. The map (type
29 // Index) maps from a lower case string to the set (type NodeSet) of
30 // BookmarkNodes that contain that string in their title or URL.
33 // |index_urls| says whether URLs should be stored in the index in addition
34 // to bookmark titles. |languages| used to help parse IDNs in URLs for the
36 BookmarkIndex(BookmarkClient* client,
38 const std::string& languages);
41 // Invoked when a bookmark has been added to the model.
42 void Add(const BookmarkNode* node);
44 // Invoked when a bookmark has been removed from the model.
45 void Remove(const BookmarkNode* node);
47 // Returns up to |max_count| of bookmarks containing each term from
48 // the text |query| in either the title or the URL.
49 void GetBookmarksMatching(
50 const base::string16& query,
52 std::vector<BookmarkMatch>* results);
55 typedef std::vector<const BookmarkNode*> Nodes;
56 typedef std::set<const BookmarkNode*> NodeSet;
57 typedef std::map<base::string16, NodeSet> Index;
60 typedef std::vector<Match> Matches;
62 // Extracts |matches.nodes| into Nodes, sorts the pairs in decreasing order of
63 // typed count (if supported by the client), and then de-dupes the matches.
64 void SortMatches(const Matches& matches, Nodes* sorted_nodes) const;
66 // Add |node| to |results| if the node matches the query.
67 void AddMatchToResults(
68 const BookmarkNode* node,
69 query_parser::QueryParser* parser,
70 const query_parser::QueryNodeStarVector& query_nodes,
71 std::vector<BookmarkMatch>* results);
73 // Populates |matches| for the specified term. If |first_term| is true, this
74 // is the first term in the query. Returns true if there is at least one node
76 bool GetBookmarksMatchingTerm(const base::string16& term,
80 // Iterates over |matches| updating each Match's nodes to contain the
81 // intersection of the Match's current nodes and the nodes at |index_i|.
82 // If the intersection is empty, the Match is removed.
84 // This is invoked from GetBookmarksMatchingTerm.
85 void CombineMatchesInPlace(const Index::const_iterator& index_i,
88 // Iterates over |current_matches| calculating the intersection between the
89 // Match's nodes and the nodes at |index_i|. If the intersection between the
90 // two is non-empty, a new match is added to |result|.
92 // This differs from CombineMatchesInPlace in that if the intersection is
93 // non-empty the result is added to result, not combined in place. This
94 // variant is used for prefix matching.
96 // This is invoked from GetBookmarksMatchingTerm.
97 void CombineMatches(const Index::const_iterator& index_i,
98 const Matches& current_matches,
101 // Returns the set of query words from |query|.
102 std::vector<base::string16> ExtractQueryWords(const base::string16& query);
104 // Adds |node| to |index_|.
105 void RegisterNode(const base::string16& term, const BookmarkNode* node);
107 // Removes |node| from |index_|.
108 void UnregisterNode(const base::string16& term, const BookmarkNode* node);
112 BookmarkClient* const client_;
114 // Languages used to help parse IDNs in URLs for the bookmark index.
115 const std::string languages_;
117 // True if URLs are stored in the index as well as bookmark titles.
118 const bool index_urls_;
120 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex);
123 } // namespace bookmarks
125 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_INDEX_H_