Chromium Code Reviews| 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 #include "components/query_parser/query_parser.h" | |
| 14 | 15 |
| 15 class BookmarkNode; | 16 class BookmarkNode; |
| 16 struct BookmarkTitleMatch; | 17 struct BookmarkMatch; |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 class BrowserContext; | 20 class BrowserContext; |
| 20 } | 21 } |
| 21 | 22 |
| 22 namespace history { | 23 namespace history { |
| 23 class URLDatabase; | 24 class URLDatabase; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace query_parser { | 27 // BookmarkIndex maintains an index of the titles and URLs of bookmarks for |
| 27 class QueryNode; | 28 // quick look up. BookmarkIndex is owned and maintained by BookmarkModel, you |
| 28 class QueryParser; | |
| 29 } | |
| 30 | |
| 31 // BookmarkIndex maintains an index of the titles of bookmarks for quick | |
| 32 // look up. BookmarkIndex is owned and maintained by BookmarkModel, you | |
| 33 // shouldn't need to interact directly with BookmarkIndex. | 29 // shouldn't need to interact directly with BookmarkIndex. |
| 34 // | 30 // |
| 35 // BookmarkIndex maintains the index (index_) as a map of sets. The map (type | 31 // 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 | 32 // Index) maps from a lower case string to the set (type NodeSet) of |
| 37 // BookmarkNodes that contain that string in their title. | 33 // BookmarkNodes that contain that string in their title or URL. |
| 38 class BookmarkIndex { | 34 class BookmarkIndex { |
| 39 public: | 35 public: |
| 40 explicit BookmarkIndex(content::BrowserContext* browser_context); | 36 BookmarkIndex(content::BrowserContext* browser_context, |
| 37 const std::string& languages); | |
|
sky
2014/04/18 21:56:17
Document what languages is.
Mark P
2014/04/18 22:22:08
Done.
| |
| 41 ~BookmarkIndex(); | 38 ~BookmarkIndex(); |
| 42 | 39 |
| 43 // Invoked when a bookmark has been added to the model. | 40 // Invoked when a bookmark has been added to the model. |
| 44 void Add(const BookmarkNode* node); | 41 void Add(const BookmarkNode* node); |
| 45 | 42 |
| 46 // Invoked when a bookmark has been removed from the model. | 43 // Invoked when a bookmark has been removed from the model. |
| 47 void Remove(const BookmarkNode* node); | 44 void Remove(const BookmarkNode* node); |
| 48 | 45 |
| 49 // Returns up to |max_count| of bookmarks containing the text |query|. | 46 // Returns up to |max_count| of bookmarks containing each term from |
| 50 void GetBookmarksWithTitlesMatching( | 47 // the text |query| in either the title or the URL. |
| 48 void GetBookmarksMatching( | |
| 51 const base::string16& query, | 49 const base::string16& query, |
| 52 size_t max_count, | 50 size_t max_count, |
| 53 std::vector<BookmarkTitleMatch>* results); | 51 std::vector<BookmarkMatch>* results); |
| 54 | 52 |
| 55 private: | 53 private: |
| 56 typedef std::set<const BookmarkNode*> NodeSet; | 54 typedef std::set<const BookmarkNode*> NodeSet; |
| 57 typedef std::map<base::string16, NodeSet> Index; | 55 typedef std::map<base::string16, NodeSet> Index; |
| 58 | 56 |
| 59 struct Match; | 57 struct Match; |
| 60 typedef std::vector<Match> Matches; | 58 typedef std::vector<Match> Matches; |
| 61 | 59 |
| 62 // Pairs BookmarkNodes and the number of times the nodes' URLs were typed. | 60 // Pairs BookmarkNodes and the number of times the nodes' URLs were typed. |
| 63 // Used to sort Matches in decreasing order of typed count. | 61 // Used to sort Matches in decreasing order of typed count. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 81 // count so that the best matches will always be added to the results. | 79 // count so that the best matches will always be added to the results. |
| 82 static bool NodeTypedCountPairSortFunc(const NodeTypedCountPair& a, | 80 static bool NodeTypedCountPairSortFunc(const NodeTypedCountPair& a, |
| 83 const NodeTypedCountPair& b) { | 81 const NodeTypedCountPair& b) { |
| 84 return a.second > b.second; | 82 return a.second > b.second; |
| 85 } | 83 } |
| 86 | 84 |
| 87 // Add |node| to |results| if the node matches the query. | 85 // Add |node| to |results| if the node matches the query. |
| 88 void AddMatchToResults( | 86 void AddMatchToResults( |
| 89 const BookmarkNode* node, | 87 const BookmarkNode* node, |
| 90 query_parser::QueryParser* parser, | 88 query_parser::QueryParser* parser, |
| 91 const std::vector<query_parser::QueryNode*>& query_nodes, | 89 const query_parser::QueryNodeStarVector& query_nodes, |
| 92 std::vector<BookmarkTitleMatch>* results); | 90 std::vector<BookmarkMatch>* results); |
| 93 | 91 |
| 94 // Populates |matches| for the specified term. If |first_term| is true, this | 92 // 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 | 93 // is the first term in the query. Returns true if there is at least one node |
| 96 // matching the term. | 94 // matching the term. |
| 97 bool GetBookmarksWithTitleMatchingTerm(const base::string16& term, | 95 bool GetBookmarksMatchingTerm(const base::string16& term, |
| 98 bool first_term, | 96 bool first_term, |
| 99 Matches* matches); | 97 Matches* matches); |
| 100 | 98 |
| 101 // Iterates over |matches| updating each Match's nodes to contain the | 99 // Iterates over |matches| updating each Match's nodes to contain the |
| 102 // intersection of the Match's current nodes and the nodes at |index_i|. | 100 // intersection of the Match's current nodes and the nodes at |index_i|. |
| 103 // If the intersection is empty, the Match is removed. | 101 // If the intersection is empty, the Match is removed. |
| 104 // | 102 // |
| 105 // This is invoked from GetBookmarksWithTitleMatchingTerm. | 103 // This is invoked from GetBookmarksMatchingTerm. |
| 106 void CombineMatchesInPlace(const Index::const_iterator& index_i, | 104 void CombineMatchesInPlace(const Index::const_iterator& index_i, |
| 107 Matches* matches); | 105 Matches* matches); |
| 108 | 106 |
| 109 // Iterates over |current_matches| calculating the intersection between the | 107 // Iterates over |current_matches| calculating the intersection between the |
| 110 // Match's nodes and the nodes at |index_i|. If the intersection between the | 108 // Match's nodes and the nodes at |index_i|. If the intersection between the |
| 111 // two is non-empty, a new match is added to |result|. | 109 // two is non-empty, a new match is added to |result|. |
| 112 // | 110 // |
| 113 // This differs from CombineMatchesInPlace in that if the intersection is | 111 // This differs from CombineMatchesInPlace in that if the intersection is |
| 114 // non-empty the result is added to result, not combined in place. This | 112 // non-empty the result is added to result, not combined in place. This |
| 115 // variant is used for prefix matching. | 113 // variant is used for prefix matching. |
| 116 // | 114 // |
| 117 // This is invoked from GetBookmarksWithTitleMatchingTerm. | 115 // This is invoked from GetBookmarksMatchingTerm. |
| 118 void CombineMatches(const Index::const_iterator& index_i, | 116 void CombineMatches(const Index::const_iterator& index_i, |
| 119 const Matches& current_matches, | 117 const Matches& current_matches, |
| 120 Matches* result); | 118 Matches* result); |
| 121 | 119 |
| 122 // Returns the set of query words from |query|. | 120 // Returns the set of query words from |query|. |
| 123 std::vector<base::string16> ExtractQueryWords(const base::string16& query); | 121 std::vector<base::string16> ExtractQueryWords(const base::string16& query); |
| 124 | 122 |
| 125 // Adds |node| to |index_|. | 123 // Adds |node| to |index_|. |
| 126 void RegisterNode(const base::string16& term, const BookmarkNode* node); | 124 void RegisterNode(const base::string16& term, const BookmarkNode* node); |
| 127 | 125 |
| 128 // Removes |node| from |index_|. | 126 // Removes |node| from |index_|. |
| 129 void UnregisterNode(const base::string16& term, const BookmarkNode* node); | 127 void UnregisterNode(const base::string16& term, const BookmarkNode* node); |
| 130 | 128 |
| 131 Index index_; | 129 Index index_; |
| 132 | 130 |
| 131 // True if URLs are stored in the index as well as bookmark titles. | |
| 132 const bool index_urls_; | |
| 133 | |
| 133 content::BrowserContext* browser_context_; | 134 content::BrowserContext* browser_context_; |
| 134 | 135 |
| 136 // Languages used to help parse IDNs in URLs for the bookmark index. | |
| 137 std::string languages_; | |
| 138 | |
| 135 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); | 139 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); |
| 136 }; | 140 }; |
| 137 | 141 |
| 138 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | 142 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ |
| OLD | NEW |