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 std::vector<std::unique_ptr<TemplateURLData>> prepopulated_list = |
| 65 TemplateURLPrepopulateData::GetLocalPrepopulatedEngines( |
| 66 locale_, GetOriginalProfile()->GetPrefs()); |
| 67 |
| 68 if (prepopulated_list.empty()) |
| 69 return false; |
| 70 |
| 71 for (const auto& data_url : prepopulated_list) { |
| 72 data_url.get()->show_in_default_list = false; |
| 73 data_url.get()->safe_for_autoreplace = true; |
| 74 std::unique_ptr<TemplateURL> turl( |
| 75 new TemplateURL(*data_url, TemplateURL::LOCAL)); |
| 76 TemplateURL* added_turl = template_url_service_->Add(std::move(turl)); |
| 77 if (added_turl) { |
| 78 prepopulate_ids_.push_back(added_turl->prepopulate_id()); |
| 79 } |
| 80 } |
| 81 return true; |
| 82 } |
| 83 |
| 84 void SpecialLocaleHandler::RemoveTemplateUrls( |
| 85 JNIEnv* env, |
| 86 const JavaParamRef<jobject>& obj) { |
| 87 while (!prepopulate_ids_.empty()) { |
| 88 TemplateURL* turl = FindURLByPrepopulateID( |
| 89 template_url_service_->GetTemplateURLs(), prepopulate_ids_.back()); |
| 90 if (turl && template_url_service_->GetDefaultSearchProvider() != turl) { |
| 91 template_url_service_->Remove(turl); |
| 92 } |
| 93 prepopulate_ids_.pop_back(); |
| 94 } |
| 95 } |
| 96 |
| 97 void SpecialLocaleHandler::OverrideDefaultSearchProvider( |
| 98 JNIEnv* env, |
| 99 const JavaParamRef<jobject>& obj) { |
| 100 // If the user has changed his default search provider, no-op. |
| 101 TemplateURL* current_dsp = template_url_service_->GetDefaultSearchProvider(); |
| 102 if (!current_dsp || |
| 103 current_dsp->prepopulate_id() != TemplateURLPrepopulateData::google.id) { |
| 104 return; |
| 105 } |
| 106 |
| 107 TemplateURL* turl = |
| 108 FindURLByPrepopulateID(template_url_service_->GetTemplateURLs(), |
| 109 GetDesignatedPrepopulatedIdForLocale(locale_)); |
| 110 if (turl) { |
| 111 template_url_service_->SetUserSelectedDefaultSearchProvider(turl); |
| 112 } |
| 113 } |
| 114 |
| 115 SpecialLocaleHandler::~SpecialLocaleHandler() {} |
| 116 |
| 117 // static |
| 118 bool RegisterSpecialLocaleHandler(JNIEnv* env) { |
| 119 return RegisterNativesImpl(env); |
| 120 } |
OLD | NEW |