Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / bookmarks / bookmark_index.h
blobf406268dc138d84fc15cf3c307d1083e1b5b7ce7
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.
5 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_
6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_
8 #include <map>
9 #include <set>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/strings/string16.h"
15 class BookmarkNode;
16 struct BookmarkTitleMatch;
17 class QueryNode;
18 class QueryParser;
20 namespace content {
21 class BrowserContext;
24 namespace history {
25 class URLDatabase;
28 // BookmarkIndex maintains an index of the titles of bookmarks for quick
29 // look up. BookmarkIndex is owned and maintained by BookmarkModel, you
30 // shouldn't need to interact directly with BookmarkIndex.
32 // BookmarkIndex maintains the index (index_) as a map of sets. The map (type
33 // Index) maps from a lower case string to the set (type NodeSet) of
34 // BookmarkNodes that contain that string in their title.
35 class BookmarkIndex {
36 public:
37 explicit BookmarkIndex(content::BrowserContext* browser_context);
38 ~BookmarkIndex();
40 // Invoked when a bookmark has been added to the model.
41 void Add(const BookmarkNode* node);
43 // Invoked when a bookmark has been removed from the model.
44 void Remove(const BookmarkNode* node);
46 // Returns up to |max_count| of bookmarks containing the text |query|.
47 void GetBookmarksWithTitlesMatching(
48 const base::string16& query,
49 size_t max_count,
50 std::vector<BookmarkTitleMatch>* results);
52 private:
53 typedef std::set<const BookmarkNode*> NodeSet;
54 typedef std::map<base::string16, NodeSet> Index;
56 struct Match;
57 typedef std::vector<Match> Matches;
59 // Pairs BookmarkNodes and the number of times the nodes' URLs were typed.
60 // Used to sort Matches in decreasing order of typed count.
61 typedef std::pair<const BookmarkNode*, int> NodeTypedCountPair;
62 typedef std::vector<NodeTypedCountPair> NodeTypedCountPairs;
64 // Extracts |matches.nodes| into NodeTypedCountPairs, sorts the pairs in
65 // decreasing order of typed count, and then de-dupes the matches.
66 void SortMatches(const Matches& matches,
67 NodeTypedCountPairs* node_typed_counts) const;
69 // Extracts BookmarkNodes from |match| and retrieves typed counts for each
70 // node from the in-memory database. Inserts pairs containing the node and
71 // typed count into the vector |node_typed_counts|. |node_typed_counts| is
72 // sorted in decreasing order of typed count.
73 void ExtractBookmarkNodePairs(history::URLDatabase* url_db,
74 const Match& match,
75 NodeTypedCountPairs* node_typed_counts) const;
77 // Sort function for NodeTypedCountPairs. We sort in decreasing order of typed
78 // count so that the best matches will always be added to the results.
79 static bool NodeTypedCountPairSortFunc(const NodeTypedCountPair& a,
80 const NodeTypedCountPair& b) {
81 return a.second > b.second;
84 // Add |node| to |results| if the node matches the query.
85 void AddMatchToResults(const BookmarkNode* node,
86 QueryParser* parser,
87 const std::vector<QueryNode*>& query_nodes,
88 std::vector<BookmarkTitleMatch>* results);
90 // Populates |matches| for the specified term. If |first_term| is true, this
91 // is the first term in the query. Returns true if there is at least one node
92 // matching the term.
93 bool GetBookmarksWithTitleMatchingTerm(const base::string16& term,
94 bool first_term,
95 Matches* matches);
97 // Iterates over |matches| updating each Match's nodes to contain the
98 // intersection of the Match's current nodes and the nodes at |index_i|.
99 // If the intersection is empty, the Match is removed.
101 // This is invoked from GetBookmarksWithTitleMatchingTerm.
102 void CombineMatchesInPlace(const Index::const_iterator& index_i,
103 Matches* matches);
105 // Iterates over |current_matches| calculating the intersection between the
106 // Match's nodes and the nodes at |index_i|. If the intersection between the
107 // two is non-empty, a new match is added to |result|.
109 // This differs from CombineMatchesInPlace in that if the intersection is
110 // non-empty the result is added to result, not combined in place. This
111 // variant is used for prefix matching.
113 // This is invoked from GetBookmarksWithTitleMatchingTerm.
114 void CombineMatches(const Index::const_iterator& index_i,
115 const Matches& current_matches,
116 Matches* result);
118 // Returns the set of query words from |query|.
119 std::vector<base::string16> ExtractQueryWords(const base::string16& query);
121 // Adds |node| to |index_|.
122 void RegisterNode(const base::string16& term, const BookmarkNode* node);
124 // Removes |node| from |index_|.
125 void UnregisterNode(const base::string16& term, const BookmarkNode* node);
127 Index index_;
129 content::BrowserContext* browser_context_;
131 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex);
134 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_