Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Unified Diff: components/bookmarks/browser/bookmark_index.cc

Issue 2559633003: Factor out bookmark-specific sorting logic from BookmarkIndex (Closed)
Patch Set: comments, virtual destructor, explicit Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/bookmarks/browser/bookmark_index.h ('k') | components/bookmarks/browser/bookmark_model.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/bookmarks/browser/bookmark_index.cc
diff --git a/components/bookmarks/browser/bookmark_index.cc b/components/bookmarks/browser/bookmark_index.cc
index 55d514c7c7959f737048d803389922207dcb7be9..5413be635a4523aa7b169995e94547f761a6dff6 100644
--- a/components/bookmarks/browser/bookmark_index.cc
+++ b/components/bookmarks/browser/bookmark_index.cc
@@ -11,24 +11,18 @@
#include "base/stl_util.h"
#include "base/strings/utf_offset_string_conversions.h"
#include "build/build_config.h"
-#include "components/bookmarks/browser/bookmark_client.h"
#include "components/bookmarks/browser/bookmark_match.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "components/bookmarks/browser/titled_url_node.h"
+#include "components/bookmarks/browser/titled_url_node_sorter.h"
#include "components/query_parser/snippet.h"
#include "third_party/icu/source/common/unicode/normalizer2.h"
#include "third_party/icu/source/common/unicode/utypes.h"
namespace bookmarks {
-using UrlTypedCountMap = BookmarkClient::UrlTypedCountMap;
-
namespace {
-using UrlNodeMap = std::unordered_map<const GURL*, const TitledUrlNode*>;
-using UrlTypedCountPair = std::pair<const GURL*, int>;
-using UrlTypedCountPairs = std::vector<UrlTypedCountPair>;
-
// Returns a normalized version of the UTF16 string |text|. If it fails to
// normalize the string, returns |text| itself as a best-effort.
base::string16 Normalize(const base::string16& text) {
@@ -52,36 +46,10 @@ base::string16 Normalize(const base::string16& text) {
unicode_normalized_text.length());
}
-// Sort functor for UrlTypedCountPairs. We sort in decreasing order of typed
-// count so that the best matches will always be added to the results.
-struct UrlTypedCountPairSortFunctor {
- bool operator()(const UrlTypedCountPair& a,
- const UrlTypedCountPair& b) const {
- return a.second > b.second;
- }
-};
-
-// Extract the GURL stored in an UrlTypedCountPair and use it to look up the
-// corresponding TitledUrlNode.
-class UrlTypedCountPairNodeLookupFunctor {
- public:
- explicit UrlTypedCountPairNodeLookupFunctor(UrlNodeMap& url_node_map)
- : url_node_map_(url_node_map) {
- }
-
- const TitledUrlNode* operator()(const UrlTypedCountPair& pair) const {
- return url_node_map_[pair.first];
- }
-
- private:
- UrlNodeMap& url_node_map_;
-};
-
} // namespace
-BookmarkIndex::BookmarkIndex(BookmarkClient* client)
- : client_(client) {
- DCHECK(client_);
+BookmarkIndex::BookmarkIndex(std::unique_ptr<TitledUrlNodeSorter> sorter)
+ : sorter_(std::move(sorter)) {
}
BookmarkIndex::~BookmarkIndex() {
@@ -150,29 +118,8 @@ void BookmarkIndex::GetResultsMatching(
void BookmarkIndex::SortMatches(const TitledUrlNodeSet& matches,
TitledUrlNodes* sorted_nodes) const {
- sorted_nodes->reserve(matches.size());
- if (client_->SupportsTypedCountForUrls()) {
- UrlNodeMap url_node_map;
- UrlTypedCountMap url_typed_count_map;
- for (auto node : matches) {
- const GURL& url = node->GetTitledUrlNodeUrl();
- url_node_map.insert(std::make_pair(&url, node));
- url_typed_count_map.insert(std::make_pair(&url, 0));
- }
-
- client_->GetTypedCountForUrls(&url_typed_count_map);
-
- UrlTypedCountPairs url_typed_counts;
- std::copy(url_typed_count_map.begin(),
- url_typed_count_map.end(),
- std::back_inserter(url_typed_counts));
- std::sort(url_typed_counts.begin(),
- url_typed_counts.end(),
- UrlTypedCountPairSortFunctor());
- std::transform(url_typed_counts.begin(),
- url_typed_counts.end(),
- std::back_inserter(*sorted_nodes),
- UrlTypedCountPairNodeLookupFunctor(url_node_map));
+ if (sorter_) {
+ sorter_->SortMatches(matches, sorted_nodes);
} else {
sorted_nodes->insert(sorted_nodes->end(), matches.begin(), matches.end());
}
« no previous file with comments | « components/bookmarks/browser/bookmark_index.h ('k') | components/bookmarks/browser/bookmark_model.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698