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