Put screenshot.py back to work
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_index.h
blobe3d8dd50070ea59218f44b5c35bc049ec8a5d335
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/strings/string16.h"
15 #include "components/query_parser/query_parser.h"
17 class BookmarkNode;
19 namespace bookmarks {
21 class BookmarkClient;
22 struct BookmarkMatch;
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.
31 class BookmarkIndex {
32 public:
33 // |languages| is used to help parse IDNs in URLs for the bookmark index.
34 BookmarkIndex(BookmarkClient* client,
35 const std::string& languages);
36 ~BookmarkIndex();
38 // Invoked when a bookmark has been added to the model.
39 void Add(const BookmarkNode* node);
41 // Invoked when a bookmark has been removed from the model.
42 void Remove(const BookmarkNode* node);
44 // Returns up to |max_count| of bookmarks containing each term from the text
45 // |query| in either the title or the URL.
46 void GetBookmarksMatching(const base::string16& query,
47 size_t max_count,
48 query_parser::MatchingAlgorithm matching_algorithm,
49 std::vector<BookmarkMatch>* results);
51 private:
52 typedef std::vector<const BookmarkNode*> Nodes;
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 // Extracts |matches.nodes| into Nodes, sorts the pairs in decreasing order of
60 // typed count (if supported by the client), and then de-dupes the matches.
61 void SortMatches(const Matches& matches, Nodes* sorted_nodes) const;
63 // Add |node| to |results| if the node matches the query.
64 void AddMatchToResults(
65 const BookmarkNode* node,
66 query_parser::QueryParser* parser,
67 const query_parser::QueryNodeStarVector& query_nodes,
68 std::vector<BookmarkMatch>* results);
70 // Populates |matches| for the specified term. If |first_term| is true, this
71 // is the first term in the query. Returns true if there is at least one node
72 // matching the term.
73 bool GetBookmarksMatchingTerm(
74 const base::string16& term,
75 bool first_term,
76 query_parser::MatchingAlgorithm matching_algorithm,
77 Matches* matches);
79 // Iterates over |matches| updating each Match's nodes to contain the
80 // intersection of the Match's current nodes and the nodes at |index_i|.
81 // If the intersection is empty, the Match is removed.
83 // This is invoked from GetBookmarksMatchingTerm.
84 void CombineMatchesInPlace(const Index::const_iterator& index_i,
85 Matches* matches);
87 // Iterates over |current_matches| calculating the intersection between the
88 // Match's nodes and the nodes at |index_i|. If the intersection between the
89 // two is non-empty, a new match is added to |result|.
91 // This differs from CombineMatchesInPlace in that if the intersection is
92 // non-empty the result is added to result, not combined in place. This
93 // variant is used for prefix matching.
95 // This is invoked from GetBookmarksMatchingTerm.
96 void CombineMatches(const Index::const_iterator& index_i,
97 const Matches& current_matches,
98 Matches* result);
100 // Returns the set of query words from |query|.
101 std::vector<base::string16> ExtractQueryWords(const base::string16& query);
103 // Adds |node| to |index_|.
104 void RegisterNode(const base::string16& term, const BookmarkNode* node);
106 // Removes |node| from |index_|.
107 void UnregisterNode(const base::string16& term, const BookmarkNode* node);
109 Index index_;
111 BookmarkClient* const client_;
113 // Languages used to help parse IDNs in URLs for the bookmark index.
114 const std::string languages_;
116 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex);
119 } // namespace bookmarks
121 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_INDEX_H_