OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "chrome/browser/search_engines/template_url_service_android.h" | 5 #include "chrome/browser/search_engines/template_url_service_android.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 if (weak_java_obj_.get(env).is_null()) | 136 if (weak_java_obj_.get(env).is_null()) |
137 return; | 137 return; |
138 LoadTemplateURLs(); | 138 LoadTemplateURLs(); |
139 | 139 |
140 Java_TemplateUrlService_templateUrlServiceLoaded(env, | 140 Java_TemplateUrlService_templateUrlServiceLoaded(env, |
141 weak_java_obj_.get(env)); | 141 weak_java_obj_.get(env)); |
142 } | 142 } |
143 | 143 |
144 void TemplateUrlServiceAndroid::LoadTemplateURLs() { | 144 void TemplateUrlServiceAndroid::LoadTemplateURLs() { |
145 template_urls_ = template_url_service_->GetTemplateURLs(); | 145 template_urls_ = template_url_service_->GetTemplateURLs(); |
146 TemplateURL* default_search_provider = | 146 auto isDefault = [&](TemplateURL* url) { |
147 template_url_service_->GetDefaultSearchProvider(); | 147 return template_url_service_->ShowInDefaultList(url); |
148 | 148 }; |
149 auto comp = [&](const TemplateURL* lhs, const TemplateURL* rhs) { | 149 auto defaultComp = [&](const TemplateURL* lhs, const TemplateURL* rhs) { |
150 bool rhs_prepopulated = | 150 bool rhs_prepopulated = |
151 template_url_service_->IsPrepopulatedOrCreatedByPolicy(rhs); | 151 template_url_service_->IsPrepopulatedOrCreatedByPolicy(rhs); |
152 if (template_url_service_->IsPrepopulatedOrCreatedByPolicy(lhs)) { | 152 if (template_url_service_->IsPrepopulatedOrCreatedByPolicy(lhs)) { |
153 return !rhs_prepopulated || | 153 return !rhs_prepopulated || |
154 (lhs->prepopulate_id() < rhs->prepopulate_id()); | 154 (lhs->prepopulate_id() < rhs->prepopulate_id()); |
155 } | 155 } |
156 return (lhs == default_search_provider && !rhs_prepopulated); | 156 return false; |
157 }; | 157 }; |
158 std::sort(template_urls_.begin(), template_urls_.end(), comp); | 158 auto customComp = [&](const TemplateURL* lhs, const TemplateURL* rhs) { |
159 return lhs->last_visited().ToTimeT() > rhs->last_visited().ToTimeT(); | |
160 }; | |
161 auto isRecent = [](TemplateURL* url) { | |
162 return url->last_visited().ToTimeT() < | |
163 (base::Time::Now() - base::TimeDelta::FromDays(2)).ToTimeT(); | |
164 }; | |
165 std::vector<TemplateURL*>::iterator bound = | |
166 std::partition(template_urls_.begin(), template_urls_.end(), isDefault); | |
167 std::sort(template_urls_.begin(), bound, defaultComp); | |
168 std::sort(bound, template_urls_.end(), customComp); | |
169 template_urls_.erase(std::remove_if(bound, template_urls_.end(), isRecent), | |
170 template_urls_.end()); | |
171 int num_remove = CountRemovedSearchEngineNum(bound); | |
172 template_urls_.resize(template_urls_.size() - num_remove); | |
173 } | |
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
| |
174 | |
175 int TemplateUrlServiceAndroid::CountRemovedSearchEngineNum( | |
176 std::vector<TemplateURL*>::iterator bound) { | |
177 int num_custom_engine = std::distance(bound, template_urls_.end()); | |
178 return std::max(num_custom_engine - 3, 0); | |
159 } | 179 } |
160 | 180 |
161 void TemplateUrlServiceAndroid::OnTemplateURLServiceChanged() { | 181 void TemplateUrlServiceAndroid::OnTemplateURLServiceChanged() { |
162 JNIEnv* env = base::android::AttachCurrentThread(); | 182 JNIEnv* env = base::android::AttachCurrentThread(); |
163 if (weak_java_obj_.get(env).is_null()) | 183 if (weak_java_obj_.get(env).is_null()) |
164 return; | 184 return; |
165 LoadTemplateURLs(); | 185 LoadTemplateURLs(); |
166 | 186 |
167 Java_TemplateUrlService_onTemplateURLServiceChanged(env, | 187 Java_TemplateUrlService_onTemplateURLServiceChanged(env, |
168 weak_java_obj_.get(env)); | 188 weak_java_obj_.get(env)); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
287 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 307 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
288 TemplateUrlServiceAndroid* template_url_service_android = | 308 TemplateUrlServiceAndroid* template_url_service_android = |
289 new TemplateUrlServiceAndroid(env, obj); | 309 new TemplateUrlServiceAndroid(env, obj); |
290 return reinterpret_cast<intptr_t>(template_url_service_android); | 310 return reinterpret_cast<intptr_t>(template_url_service_android); |
291 } | 311 } |
292 | 312 |
293 // static | 313 // static |
294 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { | 314 bool TemplateUrlServiceAndroid::Register(JNIEnv* env) { |
295 return RegisterNativesImpl(env); | 315 return RegisterNativesImpl(env); |
296 } | 316 } |
OLD | NEW |