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

Unified Diff: chrome/browser/search_engines/template_url_service_android.cc

Issue 2555513003: [Android] Sort custom search engines based on last visited time and display only top 3 most recentl… (Closed)
Patch Set: Change to only display top 3 most recently visted custom search engines within 2 days. 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
Index: chrome/browser/search_engines/template_url_service_android.cc
diff --git a/chrome/browser/search_engines/template_url_service_android.cc b/chrome/browser/search_engines/template_url_service_android.cc
index 306cd44c2130303ba5a742eaabe1db46d58d08b0..eadc562f654fafe1d3517b83ab22bbe91318f791 100644
--- a/chrome/browser/search_engines/template_url_service_android.cc
+++ b/chrome/browser/search_engines/template_url_service_android.cc
@@ -143,19 +143,39 @@ void TemplateUrlServiceAndroid::OnTemplateURLServiceLoaded() {
void TemplateUrlServiceAndroid::LoadTemplateURLs() {
template_urls_ = template_url_service_->GetTemplateURLs();
- TemplateURL* default_search_provider =
- template_url_service_->GetDefaultSearchProvider();
-
- auto comp = [&](const TemplateURL* lhs, const TemplateURL* rhs) {
+ auto isDefault = [&](TemplateURL* url) {
+ return template_url_service_->ShowInDefaultList(url);
+ };
+ auto defaultComp = [&](const TemplateURL* lhs, const TemplateURL* rhs) {
bool rhs_prepopulated =
template_url_service_->IsPrepopulatedOrCreatedByPolicy(rhs);
if (template_url_service_->IsPrepopulatedOrCreatedByPolicy(lhs)) {
return !rhs_prepopulated ||
(lhs->prepopulate_id() < rhs->prepopulate_id());
}
- return (lhs == default_search_provider && !rhs_prepopulated);
+ return false;
};
- std::sort(template_urls_.begin(), template_urls_.end(), comp);
+ auto customComp = [&](const TemplateURL* lhs, const TemplateURL* rhs) {
+ return lhs->last_visited().ToTimeT() > rhs->last_visited().ToTimeT();
+ };
+ auto isRecent = [](TemplateURL* url) {
+ return url->last_visited().ToTimeT() <
+ (base::Time::Now() - base::TimeDelta::FromDays(2)).ToTimeT();
+ };
+ std::vector<TemplateURL*>::iterator bound =
+ std::partition(template_urls_.begin(), template_urls_.end(), isDefault);
+ std::sort(template_urls_.begin(), bound, defaultComp);
+ std::sort(bound, template_urls_.end(), customComp);
+ template_urls_.erase(std::remove_if(bound, template_urls_.end(), isRecent),
+ template_urls_.end());
+ int num_remove = CountRemovedSearchEngineNum(bound);
+ template_urls_.resize(template_urls_.size() - num_remove);
+}
Peter Kasting 2016/12/08 06:56:27 Functionally, this looks like it will work. Here'
ltian 2016/12/09 00:53:08 That's a great tutorial! Thanks for the great effo
+
+int TemplateUrlServiceAndroid::CountRemovedSearchEngineNum(
+ std::vector<TemplateURL*>::iterator bound) {
+ int num_custom_engine = std::distance(bound, template_urls_.end());
+ return std::max(num_custom_engine - 3, 0);
}
void TemplateUrlServiceAndroid::OnTemplateURLServiceChanged() {
« no previous file with comments | « chrome/browser/search_engines/template_url_service_android.h ('k') | components/search_engines/template_url_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698