Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_index.h
blob8763d93383fc34469bf6b10bac3489bf314e07f0
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/macros.h"
14 #include "base/strings/string16.h"
15 #include "components/query_parser/query_parser.h"
17 namespace bookmarks {
19 class BookmarkClient;
20 class BookmarkNode;
21 struct BookmarkMatch;
23 // BookmarkIndex maintains an index of the titles and URLs of bookmarks for
24 // quick look up. BookmarkIndex is owned and maintained by BookmarkModel, you
25 // shouldn't need to interact directly with BookmarkIndex.
27 // BookmarkIndex maintains the index (index_) as a map of sets. The map (type
28 // Index) maps from a lower case string to the set (type NodeSet) of
29 // BookmarkNodes that contain that string in their title or URL.
30 class BookmarkIndex {
31 public:
32 // |languages| is used to help parse IDNs in URLs for the bookmark index.
33 BookmarkIndex(BookmarkClient* client,
34 const std::string& languages);
35 ~BookmarkIndex();
37 // Invoked when a bookmark has been added to the model.
38 void Add(const BookmarkNode* node);
40 // Invoked when a bookmark has been removed from the model.
41 void Remove(const BookmarkNode* node);
43 // Returns up to |max_count| of bookmarks containing each term from the text
44 // |query| in either the title or the URL.
45 void GetBookmarksMatching(const base::string16& query,
46 size_t max_count,
47 query_parser::MatchingAlgorithm matching_algorithm,
48 std::vector<BookmarkMatch>* results);
50 private:
51 typedef std::vector<const BookmarkNode*> Nodes;
52 typedef std::set<const BookmarkNode*> NodeSet;
53 typedef std::map<base::string16, NodeSet> Index;
55 // Constructs |sorted_nodes| by taking the matches in |matches| and sorting
56 // them in decreasing order of typed count (if supported by the client) and
57 // deduping them.
58 void SortMatches(const NodeSet& matches, Nodes* sorted_nodes) const;
60 // Add |node| to |results| if the node matches the query.
61 void AddMatchToResults(
62 const BookmarkNode* node,
63 query_parser::QueryParser* parser,
64 const query_parser::QueryNodeStarVector& query_nodes,
65 std::vector<BookmarkMatch>* results);
67 // Populates |matches| for the specified term. If |first_term| is true, this
68 // is the first term in the query. Returns true if there is at least one node
69 // matching the term.
70 bool GetBookmarksMatchingTerm(
71 const base::string16& term,
72 bool first_term,
73 query_parser::MatchingAlgorithm matching_algorithm,
74 NodeSet* matches);
76 // Returns the set of query words from |query|.
77 std::vector<base::string16> ExtractQueryWords(const base::string16& query);
79 // Adds |node| to |index_|.
80 void RegisterNode(const base::string16& term, const BookmarkNode* node);
82 // Removes |node| from |index_|.
83 void UnregisterNode(const base::string16& term, const BookmarkNode* node);
85 Index index_;
87 BookmarkClient* const client_;
89 // Languages used to help parse IDNs in URLs for the bookmark index.
90 const std::string languages_;
92 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex);
95 } // namespace bookmarks
97 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_INDEX_H_