| 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 #include "components/query_parser/query_parser.h" |
| 15 | 15 |
| 16 class BookmarkClient; |
| 16 class BookmarkNode; | 17 class BookmarkNode; |
| 17 struct BookmarkMatch; | 18 struct BookmarkMatch; |
| 18 | 19 |
| 19 namespace content { | |
| 20 class BrowserContext; | |
| 21 } | |
| 22 | |
| 23 namespace history { | |
| 24 class URLDatabase; | |
| 25 } | |
| 26 | |
| 27 // BookmarkIndex maintains an index of the titles and URLs of bookmarks for | 20 // BookmarkIndex maintains an index of the titles and URLs of bookmarks for |
| 28 // quick look up. BookmarkIndex is owned and maintained by BookmarkModel, you | 21 // quick look up. BookmarkIndex is owned and maintained by BookmarkModel, you |
| 29 // shouldn't need to interact directly with BookmarkIndex. | 22 // shouldn't need to interact directly with BookmarkIndex. |
| 30 // | 23 // |
| 31 // BookmarkIndex maintains the index (index_) as a map of sets. The map (type | 24 // BookmarkIndex maintains the index (index_) as a map of sets. The map (type |
| 32 // Index) maps from a lower case string to the set (type NodeSet) of | 25 // Index) maps from a lower case string to the set (type NodeSet) of |
| 33 // BookmarkNodes that contain that string in their title or URL. | 26 // BookmarkNodes that contain that string in their title or URL. |
| 34 class BookmarkIndex { | 27 class BookmarkIndex { |
| 35 public: | 28 public: |
| 36 // |index_urls| says whether URLs should be stored in the index in addition | 29 // |index_urls| says whether URLs should be stored in the index in addition |
| 37 // to bookmark titles. |languages| used to help parse IDNs in URLs for the | 30 // to bookmark titles. |languages| used to help parse IDNs in URLs for the |
| 38 // bookmark index. | 31 // bookmark index. |
| 39 BookmarkIndex(content::BrowserContext* browser_context, | 32 BookmarkIndex(BookmarkClient* client, |
| 40 bool index_urls, | 33 bool index_urls, |
| 41 const std::string& languages); | 34 const std::string& languages); |
| 42 ~BookmarkIndex(); | 35 ~BookmarkIndex(); |
| 43 | 36 |
| 44 // Invoked when a bookmark has been added to the model. | 37 // Invoked when a bookmark has been added to the model. |
| 45 void Add(const BookmarkNode* node); | 38 void Add(const BookmarkNode* node); |
| 46 | 39 |
| 47 // Invoked when a bookmark has been removed from the model. | 40 // Invoked when a bookmark has been removed from the model. |
| 48 void Remove(const BookmarkNode* node); | 41 void Remove(const BookmarkNode* node); |
| 49 | 42 |
| 50 // Returns up to |max_count| of bookmarks containing each term from | 43 // Returns up to |max_count| of bookmarks containing each term from |
| 51 // the text |query| in either the title or the URL. | 44 // the text |query| in either the title or the URL. |
| 52 void GetBookmarksMatching( | 45 void GetBookmarksMatching( |
| 53 const base::string16& query, | 46 const base::string16& query, |
| 54 size_t max_count, | 47 size_t max_count, |
| 55 std::vector<BookmarkMatch>* results); | 48 std::vector<BookmarkMatch>* results); |
| 56 | 49 |
| 57 private: | 50 private: |
| 51 typedef std::vector<const BookmarkNode*> Nodes; |
| 58 typedef std::set<const BookmarkNode*> NodeSet; | 52 typedef std::set<const BookmarkNode*> NodeSet; |
| 59 typedef std::map<base::string16, NodeSet> Index; | 53 typedef std::map<base::string16, NodeSet> Index; |
| 60 | 54 |
| 61 struct Match; | 55 struct Match; |
| 62 typedef std::vector<Match> Matches; | 56 typedef std::vector<Match> Matches; |
| 63 | 57 |
| 64 // Pairs BookmarkNodes and the number of times the nodes' URLs were typed. | 58 // Extracts |matches.nodes| into Nodes, sorts the pairs in decreasing order of |
| 65 // Used to sort Matches in decreasing order of typed count. | 59 // typed count (if supported by the client), and then de-dupes the matches. |
| 66 typedef std::pair<const BookmarkNode*, int> NodeTypedCountPair; | 60 void SortMatches(const Matches& matches, Nodes* sorted_nodes) const; |
| 67 typedef std::vector<NodeTypedCountPair> NodeTypedCountPairs; | |
| 68 | |
| 69 // Extracts |matches.nodes| into NodeTypedCountPairs, sorts the pairs in | |
| 70 // decreasing order of typed count, and then de-dupes the matches. | |
| 71 void SortMatches(const Matches& matches, | |
| 72 NodeTypedCountPairs* node_typed_counts) const; | |
| 73 | |
| 74 // Extracts BookmarkNodes from |match| and retrieves typed counts for each | |
| 75 // node from the in-memory database. Inserts pairs containing the node and | |
| 76 // typed count into the vector |node_typed_counts|. |node_typed_counts| is | |
| 77 // sorted in decreasing order of typed count. | |
| 78 void ExtractBookmarkNodePairs(history::URLDatabase* url_db, | |
| 79 const Match& match, | |
| 80 NodeTypedCountPairs* node_typed_counts) const; | |
| 81 | |
| 82 // Sort function for NodeTypedCountPairs. We sort in decreasing order of typed | |
| 83 // count so that the best matches will always be added to the results. | |
| 84 static bool NodeTypedCountPairSortFunc(const NodeTypedCountPair& a, | |
| 85 const NodeTypedCountPair& b) { | |
| 86 return a.second > b.second; | |
| 87 } | |
| 88 | 61 |
| 89 // Add |node| to |results| if the node matches the query. | 62 // Add |node| to |results| if the node matches the query. |
| 90 void AddMatchToResults( | 63 void AddMatchToResults( |
| 91 const BookmarkNode* node, | 64 const BookmarkNode* node, |
| 92 query_parser::QueryParser* parser, | 65 query_parser::QueryParser* parser, |
| 93 const query_parser::QueryNodeStarVector& query_nodes, | 66 const query_parser::QueryNodeStarVector& query_nodes, |
| 94 std::vector<BookmarkMatch>* results); | 67 std::vector<BookmarkMatch>* results); |
| 95 | 68 |
| 96 // Populates |matches| for the specified term. If |first_term| is true, this | 69 // Populates |matches| for the specified term. If |first_term| is true, this |
| 97 // is the first term in the query. Returns true if there is at least one node | 70 // is the first term in the query. Returns true if there is at least one node |
| (...skipping 27 matching lines...) Expand all Loading... |
| 125 std::vector<base::string16> ExtractQueryWords(const base::string16& query); | 98 std::vector<base::string16> ExtractQueryWords(const base::string16& query); |
| 126 | 99 |
| 127 // Adds |node| to |index_|. | 100 // Adds |node| to |index_|. |
| 128 void RegisterNode(const base::string16& term, const BookmarkNode* node); | 101 void RegisterNode(const base::string16& term, const BookmarkNode* node); |
| 129 | 102 |
| 130 // Removes |node| from |index_|. | 103 // Removes |node| from |index_|. |
| 131 void UnregisterNode(const base::string16& term, const BookmarkNode* node); | 104 void UnregisterNode(const base::string16& term, const BookmarkNode* node); |
| 132 | 105 |
| 133 Index index_; | 106 Index index_; |
| 134 | 107 |
| 108 BookmarkClient* const client_; |
| 109 |
| 110 // Languages used to help parse IDNs in URLs for the bookmark index. |
| 111 const std::string languages_; |
| 112 |
| 135 // True if URLs are stored in the index as well as bookmark titles. | 113 // True if URLs are stored in the index as well as bookmark titles. |
| 136 const bool index_urls_; | 114 const bool index_urls_; |
| 137 | 115 |
| 138 content::BrowserContext* browser_context_; | |
| 139 | |
| 140 // Languages used to help parse IDNs in URLs for the bookmark index. | |
| 141 const std::string languages_; | |
| 142 | |
| 143 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); | 116 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); |
| 144 }; | 117 }; |
| 145 | 118 |
| 146 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | 119 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ |
| OLD | NEW |