OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | 5 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ |
6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | 6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 | 14 |
| 15 class BookmarkClient; |
15 class BookmarkNode; | 16 class BookmarkNode; |
16 struct BookmarkTitleMatch; | 17 struct BookmarkTitleMatch; |
17 | 18 |
18 namespace content { | |
19 class BrowserContext; | |
20 } | |
21 | |
22 namespace history { | |
23 class URLDatabase; | |
24 } | |
25 | |
26 namespace query_parser { | 19 namespace query_parser { |
27 class QueryNode; | 20 class QueryNode; |
28 class QueryParser; | 21 class QueryParser; |
29 } | 22 } |
30 | 23 |
31 // BookmarkIndex maintains an index of the titles of bookmarks for quick | 24 // BookmarkIndex maintains an index of the titles of bookmarks for quick |
32 // look up. BookmarkIndex is owned and maintained by BookmarkModel, you | 25 // look up. BookmarkIndex is owned and maintained by BookmarkModel, you |
33 // shouldn't need to interact directly with BookmarkIndex. | 26 // shouldn't need to interact directly with BookmarkIndex. |
34 // | 27 // |
35 // BookmarkIndex maintains the index (index_) as a map of sets. The map (type | 28 // BookmarkIndex maintains the index (index_) as a map of sets. The map (type |
36 // Index) maps from a lower case string to the set (type NodeSet) of | 29 // Index) maps from a lower case string to the set (type NodeSet) of |
37 // BookmarkNodes that contain that string in their title. | 30 // BookmarkNodes that contain that string in their title. |
38 class BookmarkIndex { | 31 class BookmarkIndex { |
39 public: | 32 public: |
40 explicit BookmarkIndex(content::BrowserContext* browser_context); | 33 explicit BookmarkIndex(BookmarkClient* client); |
41 ~BookmarkIndex(); | 34 ~BookmarkIndex(); |
42 | 35 |
43 // Invoked when a bookmark has been added to the model. | 36 // Invoked when a bookmark has been added to the model. |
44 void Add(const BookmarkNode* node); | 37 void Add(const BookmarkNode* node); |
45 | 38 |
46 // Invoked when a bookmark has been removed from the model. | 39 // Invoked when a bookmark has been removed from the model. |
47 void Remove(const BookmarkNode* node); | 40 void Remove(const BookmarkNode* node); |
48 | 41 |
49 // Returns up to |max_count| of bookmarks containing the text |query|. | 42 // Returns up to |max_count| of bookmarks containing the text |query|. |
50 void GetBookmarksWithTitlesMatching( | 43 void GetBookmarksWithTitlesMatching(const base::string16& query, |
51 const base::string16& query, | 44 size_t max_count, |
52 size_t max_count, | 45 std::vector<BookmarkTitleMatch>* results); |
53 std::vector<BookmarkTitleMatch>* results); | |
54 | 46 |
55 private: | 47 private: |
| 48 typedef std::vector<const BookmarkNode*> Nodes; |
56 typedef std::set<const BookmarkNode*> NodeSet; | 49 typedef std::set<const BookmarkNode*> NodeSet; |
57 typedef std::map<base::string16, NodeSet> Index; | 50 typedef std::map<base::string16, NodeSet> Index; |
58 | 51 |
59 struct Match; | 52 struct Match; |
60 typedef std::vector<Match> Matches; | 53 typedef std::vector<Match> Matches; |
61 | 54 |
62 // Pairs BookmarkNodes and the number of times the nodes' URLs were typed. | 55 // Extracts |matches.nodes| into Nodes, sorts the pairs in decreasing order of |
63 // Used to sort Matches in decreasing order of typed count. | 56 // typed count (if supported by the client), and then de-dupes the matches. |
64 typedef std::pair<const BookmarkNode*, int> NodeTypedCountPair; | 57 void SortMatches(const Matches& matches, Nodes* sorted_nodes) const; |
65 typedef std::vector<NodeTypedCountPair> NodeTypedCountPairs; | |
66 | |
67 // Extracts |matches.nodes| into NodeTypedCountPairs, sorts the pairs in | |
68 // decreasing order of typed count, and then de-dupes the matches. | |
69 void SortMatches(const Matches& matches, | |
70 NodeTypedCountPairs* node_typed_counts) const; | |
71 | |
72 // Extracts BookmarkNodes from |match| and retrieves typed counts for each | |
73 // node from the in-memory database. Inserts pairs containing the node and | |
74 // typed count into the vector |node_typed_counts|. |node_typed_counts| is | |
75 // sorted in decreasing order of typed count. | |
76 void ExtractBookmarkNodePairs(history::URLDatabase* url_db, | |
77 const Match& match, | |
78 NodeTypedCountPairs* node_typed_counts) const; | |
79 | |
80 // Sort function for NodeTypedCountPairs. We sort in decreasing order of typed | |
81 // count so that the best matches will always be added to the results. | |
82 static bool NodeTypedCountPairSortFunc(const NodeTypedCountPair& a, | |
83 const NodeTypedCountPair& b) { | |
84 return a.second > b.second; | |
85 } | |
86 | 58 |
87 // Add |node| to |results| if the node matches the query. | 59 // Add |node| to |results| if the node matches the query. |
88 void AddMatchToResults( | 60 void AddMatchToResults( |
89 const BookmarkNode* node, | 61 const BookmarkNode* node, |
90 query_parser::QueryParser* parser, | 62 query_parser::QueryParser* parser, |
91 const std::vector<query_parser::QueryNode*>& query_nodes, | 63 const std::vector<query_parser::QueryNode*>& query_nodes, |
92 std::vector<BookmarkTitleMatch>* results); | 64 std::vector<BookmarkTitleMatch>* results); |
93 | 65 |
94 // Populates |matches| for the specified term. If |first_term| is true, this | 66 // Populates |matches| for the specified term. If |first_term| is true, this |
95 // is the first term in the query. Returns true if there is at least one node | 67 // is the first term in the query. Returns true if there is at least one node |
(...skipping 25 matching lines...) Expand all Loading... |
121 | 93 |
122 // Returns the set of query words from |query|. | 94 // Returns the set of query words from |query|. |
123 std::vector<base::string16> ExtractQueryWords(const base::string16& query); | 95 std::vector<base::string16> ExtractQueryWords(const base::string16& query); |
124 | 96 |
125 // Adds |node| to |index_|. | 97 // Adds |node| to |index_|. |
126 void RegisterNode(const base::string16& term, const BookmarkNode* node); | 98 void RegisterNode(const base::string16& term, const BookmarkNode* node); |
127 | 99 |
128 // Removes |node| from |index_|. | 100 // Removes |node| from |index_|. |
129 void UnregisterNode(const base::string16& term, const BookmarkNode* node); | 101 void UnregisterNode(const base::string16& term, const BookmarkNode* node); |
130 | 102 |
| 103 BookmarkClient* client_; |
| 104 |
131 Index index_; | 105 Index index_; |
132 | 106 |
133 content::BrowserContext* browser_context_; | |
134 | |
135 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); | 107 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); |
136 }; | 108 }; |
137 | 109 |
138 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | 110 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ |
OLD | NEW |