OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/locale/special_locale_handler.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/android/jni_weak_ref.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 13 #include "components/search_engines/prepopulated_engines.h" |
| 14 #include "components/search_engines/template_url_data.h" |
| 15 #include "components/search_engines/template_url_prepopulate_data.h" |
| 16 #include "components/search_engines/template_url_service.h" |
| 17 #include "components/search_engines/util.h" |
| 18 #include "jni/SpecialLocaleHandler_jni.h" |
| 19 |
| 20 using base::android::JavaParamRef; |
| 21 using base::android::ScopedJavaGlobalRef; |
| 22 using base::android::ScopedJavaLocalRef; |
| 23 using base::android::AttachCurrentThread; |
| 24 using base::android::ConvertJavaStringToUTF8; |
| 25 |
| 26 class PrefService; |
| 27 class TemplateURL; |
| 28 |
| 29 namespace { |
| 30 |
| 31 Profile* GetOriginalProfile() { |
| 32 return ProfileManager::GetActiveUserProfile()->GetOriginalProfile(); |
| 33 } |
| 34 |
| 35 int GetDesignatedPrepopulatedIdForLocale(const std::string& locale) { |
| 36 // TODO: Implement this as a map from locale to prepopulated engines. |
| 37 return TemplateURLPrepopulateData::sogou.id; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 static jlong Init(JNIEnv* env, |
| 43 const JavaParamRef<jclass>& clazz, |
| 44 const JavaParamRef<jstring>& jlocale) { |
| 45 return reinterpret_cast<intptr_t>( |
| 46 new SpecialLocaleHandler(ConvertJavaStringToUTF8(env, jlocale))); |
| 47 } |
| 48 |
| 49 SpecialLocaleHandler::SpecialLocaleHandler(const std::string& locale) |
| 50 : locale_(locale), |
| 51 template_url_service_( |
| 52 TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) {} |
| 53 |
| 54 void SpecialLocaleHandler::Destroy(JNIEnv* env, |
| 55 const JavaParamRef<jobject>& obj) { |
| 56 delete this; |
| 57 } |
| 58 |
| 59 jboolean SpecialLocaleHandler::LoadTemplateUrls( |
| 60 JNIEnv* env, |
| 61 const JavaParamRef<jobject>& obj) { |
| 62 DCHECK(locale_.length() == 2); |
| 63 |
| 64 bool need_to_load = true; |
| 65 std::vector<std::unique_ptr<TemplateURLData>> prepopulated_list = |
| 66 TemplateURLPrepopulateData::GetLocalPrepopulatedEngines( |
| 67 locale_, GetOriginalProfile()->GetPrefs(), &need_to_load); |
| 68 |
| 69 if (!need_to_load) |
| 70 return false; |
| 71 |
| 72 for (const auto& data_url : prepopulated_list) { |
| 73 data_url.get()->show_in_default_list = false; |
| 74 data_url.get()->safe_for_autoreplace = true; |
| 75 std::unique_ptr<TemplateURL> turl(new TemplateURL(*data_url)); |
| 76 turl->SetType(TemplateURL::LOCAL); |
| 77 TemplateURL* added_turl = template_url_service_->Add(std::move(turl)); |
| 78 if (added_turl) { |
| 79 prepopulate_ids_.push_back(added_turl->prepopulate_id()); |
| 80 } |
| 81 } |
| 82 return true; |
| 83 } |
| 84 |
| 85 void SpecialLocaleHandler::RemoveTemplateUrls( |
| 86 JNIEnv* env, |
| 87 const JavaParamRef<jobject>& obj) { |
| 88 while (!prepopulate_ids_.empty()) { |
| 89 TemplateURL* turl = FindURLByPrepopulateID( |
| 90 template_url_service_->GetTemplateURLs(), prepopulate_ids_.back()); |
| 91 if (turl && template_url_service_->GetDefaultSearchProvider() != turl) { |
| 92 template_url_service_->Remove(turl); |
| 93 } |
| 94 prepopulate_ids_.pop_back(); |
| 95 } |
| 96 } |
| 97 |
| 98 void SpecialLocaleHandler::OverrideDefaultSearchProvider( |
| 99 JNIEnv* env, |
| 100 const JavaParamRef<jobject>& obj) { |
| 101 // If the user has changed his default search provider, no-op. |
| 102 TemplateURL* current_dsp = template_url_service_->GetDefaultSearchProvider(); |
| 103 if (!current_dsp || |
| 104 current_dsp->prepopulate_id() != TemplateURLPrepopulateData::google.id) { |
| 105 return; |
| 106 } |
| 107 |
| 108 TemplateURL* turl = |
| 109 FindURLByPrepopulateID(template_url_service_->GetTemplateURLs(), |
| 110 GetDesignatedPrepopulatedIdForLocale(locale_)); |
| 111 if (turl) { |
| 112 template_url_service_->SetUserSelectedDefaultSearchProvider(turl); |
| 113 } |
| 114 } |
| 115 |
| 116 SpecialLocaleHandler::~SpecialLocaleHandler() {} |
| 117 |
| 118 // static |
| 119 bool RegisterSpecialLocaleHandler(JNIEnv* env) { |
| 120 return RegisterNativesImpl(env); |
| 121 } |
OLD | NEW |