| 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: |
| 56 typedef std::set<const BookmarkNode*> NodeSet; | 48 typedef std::set<const BookmarkNode*> NodeSet; |
| 57 typedef std::map<base::string16, NodeSet> Index; | 49 typedef std::map<base::string16, NodeSet> Index; |
| 58 | 50 |
| 59 struct Match; | 51 struct Match; |
| 60 typedef std::vector<Match> Matches; | 52 typedef std::vector<Match> Matches; |
| 61 | 53 |
| 62 // Pairs BookmarkNodes and the number of times the nodes' URLs were typed. | 54 // Pairs BookmarkNodes and the number of times the nodes' URLs were typed. |
| 63 // Used to sort Matches in decreasing order of typed count. | 55 // Used to sort Matches in decreasing order of typed count. |
| 64 typedef std::pair<const BookmarkNode*, int> NodeTypedCountPair; | 56 typedef std::pair<const BookmarkNode*, int> NodeTypedCountPair; |
| 65 typedef std::vector<NodeTypedCountPair> NodeTypedCountPairs; | 57 typedef std::vector<NodeTypedCountPair> NodeTypedCountPairs; |
| 66 | 58 |
| 67 // Extracts |matches.nodes| into NodeTypedCountPairs, sorts the pairs in | 59 // Extracts |matches.nodes| into NodeTypedCountPairs, sorts the pairs in |
| 68 // decreasing order of typed count, and then de-dupes the matches. | 60 // decreasing order of typed count, and then de-dupes the matches. |
| 69 void SortMatches(const Matches& matches, | 61 void SortMatches(const Matches& matches, |
| 70 NodeTypedCountPairs* node_typed_counts) const; | 62 NodeTypedCountPairs* node_typed_counts) const; |
| 71 | 63 |
| 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 | 64 // 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. | 65 // count so that the best matches will always be added to the results. |
| 82 static bool NodeTypedCountPairSortFunc(const NodeTypedCountPair& a, | 66 static bool NodeTypedCountPairSortFunc(const NodeTypedCountPair& a, |
| 83 const NodeTypedCountPair& b) { | 67 const NodeTypedCountPair& b) { |
| 84 return a.second > b.second; | 68 return a.second > b.second; |
| 85 } | 69 } |
| 86 | 70 |
| 87 // Add |node| to |results| if the node matches the query. | 71 // Add |node| to |results| if the node matches the query. |
| 88 void AddMatchToResults( | 72 void AddMatchToResults( |
| 89 const BookmarkNode* node, | 73 const BookmarkNode* node, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 | 105 |
| 122 // Returns the set of query words from |query|. | 106 // Returns the set of query words from |query|. |
| 123 std::vector<base::string16> ExtractQueryWords(const base::string16& query); | 107 std::vector<base::string16> ExtractQueryWords(const base::string16& query); |
| 124 | 108 |
| 125 // Adds |node| to |index_|. | 109 // Adds |node| to |index_|. |
| 126 void RegisterNode(const base::string16& term, const BookmarkNode* node); | 110 void RegisterNode(const base::string16& term, const BookmarkNode* node); |
| 127 | 111 |
| 128 // Removes |node| from |index_|. | 112 // Removes |node| from |index_|. |
| 129 void UnregisterNode(const base::string16& term, const BookmarkNode* node); | 113 void UnregisterNode(const base::string16& term, const BookmarkNode* node); |
| 130 | 114 |
| 115 BookmarkClient* client_; |
| 116 |
| 131 Index index_; | 117 Index index_; |
| 132 | 118 |
| 133 content::BrowserContext* browser_context_; | |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); | 119 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); |
| 136 }; | 120 }; |
| 137 | 121 |
| 138 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | 122 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ |
| OLD | NEW |