Chromium Code Reviews| 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 } // namespace | |
| 36 | |
| 37 static jlong Init(JNIEnv* env, | |
| 38 const JavaParamRef<jclass>& clazz, | |
| 39 const JavaParamRef<jstring>& jlocale) { | |
| 40 return reinterpret_cast<intptr_t>( | |
| 41 new SpecialLocaleHandler(ConvertJavaStringToUTF8(env, jlocale))); | |
| 42 } | |
| 43 | |
| 44 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.
| |
| 45 : locale_(locale), | |
| 46 template_url_service_( | |
| 47 TemplateURLServiceFactory::GetForProfile(GetOriginalProfile())) {} | |
| 48 | |
| 49 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.
| |
| 50 | |
| 51 void SpecialLocaleHandler::Destroy(JNIEnv* env, | |
| 52 const JavaParamRef<jobject>& obj) { | |
| 53 delete this; | |
| 54 } | |
| 55 | |
| 56 jboolean SpecialLocaleHandler::LoadTemplateUrls( | |
| 57 JNIEnv* env, | |
| 58 const base::android::JavaParamRef<jobject>& obj) { | |
| 59 DCHECK(locale_.length() == 2); | |
| 60 | |
| 61 bool need_to_load = true; | |
| 62 std::vector<std::unique_ptr<TemplateURLData>> prepopulated_list = | |
| 63 TemplateURLPrepopulateData::GetLocalPrepopulatedEngines( | |
| 64 locale_, GetOriginalProfile()->GetPrefs(), &need_to_load); | |
| 65 | |
| 66 if (!need_to_load) | |
| 67 return false; | |
| 68 | |
| 69 for (const auto& data_url : prepopulated_list) { | |
| 70 data_url.get()->show_in_default_list = false; | |
| 71 data_url.get()->safe_for_autoreplace = true; | |
| 72 std::unique_ptr<TemplateURL> turl(new TemplateURL(*data_url)); | |
| 73 turl->SetType(TemplateURL::LOCAL); | |
| 74 TemplateURL* added_turl = template_url_service_->Add(std::move(turl)); | |
| 75 if (added_turl) { | |
| 76 prepopulate_ids_.push_back(added_turl->prepopulate_id()); | |
| 77 } | |
| 78 } | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 void SpecialLocaleHandler::RemoveTemplateUrls( | |
| 83 JNIEnv* env, | |
| 84 const base::android::JavaParamRef<jobject>& obj) { | |
| 85 while (!prepopulate_ids_.empty()) { | |
| 86 TemplateURL* turl = FindURLByPrepopulateID( | |
| 87 template_url_service_->GetTemplateURLs(), prepopulate_ids_.back()); | |
| 88 if (turl && template_url_service_->GetDefaultSearchProvider() != turl) { | |
| 89 template_url_service_->Remove(turl); | |
| 90 } | |
| 91 prepopulate_ids_.pop_back(); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void SpecialLocaleHandler::OverrideDefaultSearchProvider( | |
| 96 JNIEnv* env, | |
| 97 const base::android::JavaParamRef<jobject>& obj) { | |
| 98 // If the user has changed his default search provider, no-op. | |
| 99 TemplateURL* current_dsp = template_url_service_->GetDefaultSearchProvider(); | |
| 100 if (!current_dsp || | |
| 101 current_dsp->prepopulate_id() != TemplateURLPrepopulateData::google.id) { | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 TemplateURL* turl = | |
| 106 FindURLByPrepopulateID(template_url_service_->GetTemplateURLs(), | |
| 107 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
| |
| 108 if (turl) { | |
| 109 template_url_service_->SetUserSelectedDefaultSearchProvider(turl); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 // static | |
| 114 bool RegisterSpecialLocaleHandler(JNIEnv* env) { | |
| 115 return RegisterNativesImpl(env); | |
| 116 } | |
| OLD | NEW |