OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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. | |
4 | |
5 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | |
6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/strings/string16.h" | |
14 #include "components/query_parser/query_parser.h" | |
15 | |
16 class BookmarkClient; | |
17 class BookmarkNode; | |
18 struct BookmarkMatch; | |
19 | |
20 // BookmarkIndex maintains an index of the titles and URLs of bookmarks for | |
21 // quick look up. BookmarkIndex is owned and maintained by BookmarkModel, you | |
22 // shouldn't need to interact directly with BookmarkIndex. | |
23 // | |
24 // BookmarkIndex maintains the index (index_) as a map of sets. The map (type | |
25 // Index) maps from a lower case string to the set (type NodeSet) of | |
26 // BookmarkNodes that contain that string in their title or URL. | |
27 class BookmarkIndex { | |
28 public: | |
29 // |index_urls| says whether URLs should be stored in the index in addition | |
30 // to bookmark titles. |languages| used to help parse IDNs in URLs for the | |
31 // bookmark index. | |
32 BookmarkIndex(BookmarkClient* client, | |
33 bool index_urls, | |
34 const std::string& languages); | |
35 ~BookmarkIndex(); | |
36 | |
37 // Invoked when a bookmark has been added to the model. | |
38 void Add(const BookmarkNode* node); | |
39 | |
40 // Invoked when a bookmark has been removed from the model. | |
41 void Remove(const BookmarkNode* node); | |
42 | |
43 // Returns up to |max_count| of bookmarks containing each term from | |
44 // the text |query| in either the title or the URL. | |
45 void GetBookmarksMatching( | |
46 const base::string16& query, | |
47 size_t max_count, | |
48 std::vector<BookmarkMatch>* results); | |
49 | |
50 private: | |
51 typedef std::vector<const BookmarkNode*> Nodes; | |
52 typedef std::set<const BookmarkNode*> NodeSet; | |
53 typedef std::map<base::string16, NodeSet> Index; | |
54 | |
55 struct Match; | |
56 typedef std::vector<Match> Matches; | |
57 | |
58 // Extracts |matches.nodes| into Nodes, sorts the pairs in decreasing order of | |
59 // typed count (if supported by the client), and then de-dupes the matches. | |
60 void SortMatches(const Matches& matches, Nodes* sorted_nodes) const; | |
61 | |
62 // Add |node| to |results| if the node matches the query. | |
63 void AddMatchToResults( | |
64 const BookmarkNode* node, | |
65 query_parser::QueryParser* parser, | |
66 const query_parser::QueryNodeStarVector& query_nodes, | |
67 std::vector<BookmarkMatch>* results); | |
68 | |
69 // Populates |matches| for the specified term. If |first_term| is true, this | |
70 // is the first term in the query. Returns true if there is at least one node | |
71 // matching the term. | |
72 bool GetBookmarksMatchingTerm(const base::string16& term, | |
73 bool first_term, | |
74 Matches* matches); | |
75 | |
76 // Iterates over |matches| updating each Match's nodes to contain the | |
77 // intersection of the Match's current nodes and the nodes at |index_i|. | |
78 // If the intersection is empty, the Match is removed. | |
79 // | |
80 // This is invoked from GetBookmarksMatchingTerm. | |
81 void CombineMatchesInPlace(const Index::const_iterator& index_i, | |
82 Matches* matches); | |
83 | |
84 // Iterates over |current_matches| calculating the intersection between the | |
85 // Match's nodes and the nodes at |index_i|. If the intersection between the | |
86 // two is non-empty, a new match is added to |result|. | |
87 // | |
88 // This differs from CombineMatchesInPlace in that if the intersection is | |
89 // non-empty the result is added to result, not combined in place. This | |
90 // variant is used for prefix matching. | |
91 // | |
92 // This is invoked from GetBookmarksMatchingTerm. | |
93 void CombineMatches(const Index::const_iterator& index_i, | |
94 const Matches& current_matches, | |
95 Matches* result); | |
96 | |
97 // Returns the set of query words from |query|. | |
98 std::vector<base::string16> ExtractQueryWords(const base::string16& query); | |
99 | |
100 // Adds |node| to |index_|. | |
101 void RegisterNode(const base::string16& term, const BookmarkNode* node); | |
102 | |
103 // Removes |node| from |index_|. | |
104 void UnregisterNode(const base::string16& term, const BookmarkNode* node); | |
105 | |
106 Index index_; | |
107 | |
108 BookmarkClient* const client_; | |
109 | |
110 // Languages used to help parse IDNs in URLs for the bookmark index. | |
111 const std::string languages_; | |
112 | |
113 // True if URLs are stored in the index as well as bookmark titles. | |
114 const bool index_urls_; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); | |
117 }; | |
118 | |
119 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | |
OLD | NEW |