OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 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 | 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/android/locale/special_locale_handler.h" | 5 #include "chrome/browser/android/locale/special_locale_handler.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/android/jni_weak_ref.h" | 9 #include "base/android/jni_weak_ref.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 const JavaParamRef<jobject>& obj) { | 58 const JavaParamRef<jobject>& obj) { |
59 DCHECK(locale_.length() == 2); | 59 DCHECK(locale_.length() == 2); |
60 | 60 |
61 std::vector<std::unique_ptr<TemplateURLData>> prepopulated_list = | 61 std::vector<std::unique_ptr<TemplateURLData>> prepopulated_list = |
62 GetLocalPrepopulatedEngines(profile_); | 62 GetLocalPrepopulatedEngines(profile_); |
63 | 63 |
64 if (prepopulated_list.empty()) | 64 if (prepopulated_list.empty()) |
65 return false; | 65 return false; |
66 | 66 |
67 for (const auto& data_url : prepopulated_list) { | 67 for (const auto& data_url : prepopulated_list) { |
68 TemplateURL* existing = template_url_service_->GetTemplateURLForKeyword( | 68 const TemplateURL* existing = |
69 data_url.get()->keyword()); | 69 template_url_service_->GetTemplateURLForKeyword( |
| 70 data_url.get()->keyword()); |
70 // Do not add local engines if there is already one. | 71 // Do not add local engines if there is already one. |
71 if (existing) | 72 if (existing) |
72 continue; | 73 continue; |
73 | 74 |
74 data_url.get()->safe_for_autoreplace = true; | 75 data_url.get()->safe_for_autoreplace = true; |
75 std::unique_ptr<TemplateURL> turl( | 76 std::unique_ptr<TemplateURL> turl( |
76 new TemplateURL(*data_url, TemplateURL::LOCAL)); | 77 new TemplateURL(*data_url, TemplateURL::LOCAL)); |
77 TemplateURL* added_turl = template_url_service_->Add(std::move(turl)); | 78 TemplateURL* added_turl = template_url_service_->Add(std::move(turl)); |
78 if (added_turl) { | 79 if (added_turl) { |
79 prepopulate_ids_.push_back(added_turl->prepopulate_id()); | 80 prepopulate_ids_.push_back(added_turl->prepopulate_id()); |
(...skipping 12 matching lines...) Expand all Loading... |
92 template_url_service_->Remove(turl); | 93 template_url_service_->Remove(turl); |
93 } | 94 } |
94 prepopulate_ids_.pop_back(); | 95 prepopulate_ids_.pop_back(); |
95 } | 96 } |
96 } | 97 } |
97 | 98 |
98 void SpecialLocaleHandler::OverrideDefaultSearchProvider( | 99 void SpecialLocaleHandler::OverrideDefaultSearchProvider( |
99 JNIEnv* env, | 100 JNIEnv* env, |
100 const JavaParamRef<jobject>& obj) { | 101 const JavaParamRef<jobject>& obj) { |
101 // If the user has changed their default search provider, no-op. | 102 // If the user has changed their default search provider, no-op. |
102 TemplateURL* current_dsp = template_url_service_->GetDefaultSearchProvider(); | 103 const TemplateURL* current_dsp = |
| 104 template_url_service_->GetDefaultSearchProvider(); |
103 if (!current_dsp || | 105 if (!current_dsp || |
104 current_dsp->prepopulate_id() != TemplateURLPrepopulateData::google.id) { | 106 current_dsp->prepopulate_id() != TemplateURLPrepopulateData::google.id) { |
105 return; | 107 return; |
106 } | 108 } |
107 | 109 |
108 TemplateURL* turl = FindURLByPrepopulateID( | 110 TemplateURL* turl = FindURLByPrepopulateID( |
109 template_url_service_->GetTemplateURLs(), GetDesignatedSearchEngine()); | 111 template_url_service_->GetTemplateURLs(), GetDesignatedSearchEngine()); |
110 if (turl) { | 112 if (turl) { |
111 template_url_service_->SetUserSelectedDefaultSearchProvider(turl); | 113 template_url_service_->SetUserSelectedDefaultSearchProvider(turl); |
112 } | 114 } |
113 } | 115 } |
114 | 116 |
115 void SpecialLocaleHandler::SetGoogleAsDefaultSearch( | 117 void SpecialLocaleHandler::SetGoogleAsDefaultSearch( |
116 JNIEnv* env, | 118 JNIEnv* env, |
117 const JavaParamRef<jobject>& obj) { | 119 const JavaParamRef<jobject>& obj) { |
118 // If the user has changed their default search provider, no-op. | 120 // If the user has changed their default search provider, no-op. |
119 TemplateURL* current_dsp = template_url_service_->GetDefaultSearchProvider(); | 121 const TemplateURL* current_dsp = |
| 122 template_url_service_->GetDefaultSearchProvider(); |
120 if (!current_dsp || | 123 if (!current_dsp || |
121 current_dsp->prepopulate_id() != GetDesignatedSearchEngine()) { | 124 current_dsp->prepopulate_id() != GetDesignatedSearchEngine()) { |
122 return; | 125 return; |
123 } | 126 } |
124 | 127 |
125 TemplateURL* turl = | 128 TemplateURL* turl = |
126 FindURLByPrepopulateID(template_url_service_->GetTemplateURLs(), | 129 FindURLByPrepopulateID(template_url_service_->GetTemplateURLs(), |
127 TemplateURLPrepopulateData::google.id); | 130 TemplateURLPrepopulateData::google.id); |
128 if (turl) { | 131 if (turl) { |
129 template_url_service_->SetUserSelectedDefaultSearchProvider(turl); | 132 template_url_service_->SetUserSelectedDefaultSearchProvider(turl); |
130 } | 133 } |
131 } | 134 } |
132 | 135 |
133 std::vector<std::unique_ptr<TemplateURLData>> | 136 std::vector<std::unique_ptr<TemplateURLData>> |
134 SpecialLocaleHandler::GetLocalPrepopulatedEngines(Profile* profile) { | 137 SpecialLocaleHandler::GetLocalPrepopulatedEngines(Profile* profile) { |
135 return TemplateURLPrepopulateData::GetLocalPrepopulatedEngines( | 138 return TemplateURLPrepopulateData::GetLocalPrepopulatedEngines( |
136 locale_, profile_->GetPrefs()); | 139 locale_, profile_->GetPrefs()); |
137 } | 140 } |
138 | 141 |
139 int SpecialLocaleHandler::GetDesignatedSearchEngine() { | 142 int SpecialLocaleHandler::GetDesignatedSearchEngine() { |
140 return TemplateURLPrepopulateData::sogou.id; | 143 return TemplateURLPrepopulateData::sogou.id; |
141 } | 144 } |
142 | 145 |
143 SpecialLocaleHandler::~SpecialLocaleHandler() {} | 146 SpecialLocaleHandler::~SpecialLocaleHandler() {} |
144 | 147 |
145 // static | 148 // static |
146 bool RegisterSpecialLocaleHandler(JNIEnv* env) { | 149 bool RegisterSpecialLocaleHandler(JNIEnv* env) { |
147 return RegisterNativesImpl(env); | 150 return RegisterNativesImpl(env); |
148 } | 151 } |
OLD | NEW |