Index: chrome/browser/android/locale/special_locale_handler.cc |
diff --git a/chrome/browser/android/locale/special_locale_handler.cc b/chrome/browser/android/locale/special_locale_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d059d1b675e26812191af56f54039225d9bc30f9 |
--- /dev/null |
+++ b/chrome/browser/android/locale/special_locale_handler.cc |
@@ -0,0 +1,116 @@ |
+// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/android/locale/special_locale_handler.h" |
+ |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_string.h" |
+#include "base/android/jni_weak_ref.h" |
+#include "base/memory/ptr_util.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "chrome/browser/search_engines/template_url_service_factory.h" |
+#include "components/search_engines/prepopulated_engines.h" |
+#include "components/search_engines/template_url_data.h" |
+#include "components/search_engines/template_url_prepopulate_data.h" |
+#include "components/search_engines/template_url_service.h" |
+#include "components/search_engines/util.h" |
+#include "jni/SpecialLocaleHandler_jni.h" |
+ |
+using base::android::JavaParamRef; |
+using base::android::ScopedJavaGlobalRef; |
+using base::android::ScopedJavaLocalRef; |
+using base::android::AttachCurrentThread; |
+using base::android::ConvertJavaStringToUTF8; |
+ |
+class PrefService; |
+class TemplateURL; |
+ |
+namespace { |
+ |
+Profile* GetOriginalProfile() { |
+ return ProfileManager::GetActiveUserProfile()->GetOriginalProfile(); |
+} |
+ |
+} // namespace |
+ |
+static jlong Init(JNIEnv* env, |
+ const JavaParamRef<jclass>& clazz, |
+ const JavaParamRef<jstring>& jlocale) { |
+ return reinterpret_cast<intptr_t>( |
+ new SpecialLocaleHandler(ConvertJavaStringToUTF8(env, jlocale))); |
+} |
+ |
+SpecialLocaleHandler::SpecialLocaleHandler(std::string locale) |
Maria
2016/09/16 23:44:01
This should probably be passed by reference instea
Ian Wen
2016/09/19 17:37:55
Done.
|
+ : locale_(locale), |
+ template_url_service_( |
+ TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) {} |
+ |
+SpecialLocaleHandler::~SpecialLocaleHandler() {} |
Maria
2016/09/16 23:44:01
Use the same order for methods here as you do in y
Ian Wen
2016/09/19 17:37:55
Done.
|
+ |
+void SpecialLocaleHandler::Destroy(JNIEnv* env, |
+ const JavaParamRef<jobject>& obj) { |
+ delete this; |
+} |
+ |
+jboolean SpecialLocaleHandler::LoadTemplateUrls( |
+ JNIEnv* env, |
+ const base::android::JavaParamRef<jobject>& obj) { |
+ DCHECK(locale_.length() == 2); |
+ |
+ bool need_to_load = true; |
+ std::vector<std::unique_ptr<TemplateURLData>> prepopulated_list = |
+ TemplateURLPrepopulateData::GetLocalPrepopulatedEngines( |
+ locale_, GetOriginalProfile()->GetPrefs(), &need_to_load); |
+ |
+ if (!need_to_load) |
+ return false; |
+ |
+ for (const auto& data_url : prepopulated_list) { |
+ data_url.get()->show_in_default_list = false; |
+ data_url.get()->safe_for_autoreplace = true; |
+ std::unique_ptr<TemplateURL> turl(new TemplateURL(*data_url)); |
+ turl->SetType(TemplateURL::LOCAL); |
+ TemplateURL* added_turl = template_url_service_->Add(std::move(turl)); |
+ if (added_turl) { |
+ prepopulate_ids_.push_back(added_turl->prepopulate_id()); |
+ } |
+ } |
+ return true; |
+} |
+ |
+void SpecialLocaleHandler::RemoveTemplateUrls( |
+ JNIEnv* env, |
+ const base::android::JavaParamRef<jobject>& obj) { |
+ while (!prepopulate_ids_.empty()) { |
+ TemplateURL* turl = FindURLByPrepopulateID( |
+ template_url_service_->GetTemplateURLs(), prepopulate_ids_.back()); |
+ if (turl && template_url_service_->GetDefaultSearchProvider() != turl) { |
+ template_url_service_->Remove(turl); |
+ } |
+ prepopulate_ids_.pop_back(); |
+ } |
+} |
+ |
+void SpecialLocaleHandler::OverrideDefaultSearchProvider( |
+ JNIEnv* env, |
+ const base::android::JavaParamRef<jobject>& obj) { |
+ // If the user has changed his default search provider, no-op. |
+ TemplateURL* current_dsp = template_url_service_->GetDefaultSearchProvider(); |
+ if (!current_dsp || |
+ current_dsp->prepopulate_id() != TemplateURLPrepopulateData::google.id) { |
+ return; |
+ } |
+ |
+ TemplateURL* turl = |
+ FindURLByPrepopulateID(template_url_service_->GetTemplateURLs(), |
+ TemplateURLPrepopulateData::sogou.id); |
Maria
2016/09/16 23:44:01
Shouldn't this be abstracted away somewhere in a p
Ian Wen
2016/09/19 17:37:55
I wanted to do this, but I found it very challengi
|
+ if (turl) { |
+ template_url_service_->SetUserSelectedDefaultSearchProvider(turl); |
+ } |
+} |
+ |
+// static |
+bool RegisterSpecialLocaleHandler(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |