| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/preferences/pref_service_bridge.h" | 5 #include "chrome/browser/android/preferences/pref_service_bridge.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 1118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1129 // language tags (BCP47 compliant format). Each language tag contains a language | 1129 // language tags (BCP47 compliant format). Each language tag contains a language |
| 1130 // code and a country code or a language code only. | 1130 // code and a country code or a language code only. |
| 1131 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( | 1131 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( |
| 1132 const std::string& locales, | 1132 const std::string& locales, |
| 1133 std::string* accept_languages) { | 1133 std::string* accept_languages) { |
| 1134 std::vector<std::string> locale_list = | 1134 std::vector<std::string> locale_list = |
| 1135 base::SplitString(locales + "," + *accept_languages, ",", | 1135 base::SplitString(locales + "," + *accept_languages, ",", |
| 1136 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 1136 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 1137 | 1137 |
| 1138 std::set<std::string> seen_tags; | 1138 std::set<std::string> seen_tags; |
| 1139 std::vector<std::pair<std::string, std::string>> unique_locale_list; | 1139 std::vector<std::string> unique_locale_list; |
| 1140 for (const std::string& locale_str : locale_list) { | 1140 for (const std::string& locale_str : locale_list) { |
| 1141 char locale_ID[ULOC_FULLNAME_CAPACITY] = {}; | 1141 char locale_ID[ULOC_FULLNAME_CAPACITY] = {}; |
| 1142 char language_code_buffer[ULOC_LANG_CAPACITY] = {}; | 1142 char language_code_buffer[ULOC_LANG_CAPACITY] = {}; |
| 1143 char country_code_buffer[ULOC_COUNTRY_CAPACITY] = {}; | 1143 char country_code_buffer[ULOC_COUNTRY_CAPACITY] = {}; |
| 1144 | 1144 |
| 1145 UErrorCode error = U_ZERO_ERROR; | 1145 UErrorCode error = U_ZERO_ERROR; |
| 1146 uloc_forLanguageTag(locale_str.c_str(), locale_ID, ULOC_FULLNAME_CAPACITY, | 1146 uloc_forLanguageTag(locale_str.c_str(), locale_ID, ULOC_FULLNAME_CAPACITY, |
| 1147 nullptr, &error); | 1147 nullptr, &error); |
| 1148 if (U_FAILURE(error)) { | 1148 if (U_FAILURE(error)) { |
| 1149 LOG(ERROR) << "Ignoring invalid locale representation " << locale_str; | 1149 LOG(ERROR) << "Ignoring invalid locale representation " << locale_str; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1167 } | 1167 } |
| 1168 | 1168 |
| 1169 std::string language_code(language_code_buffer); | 1169 std::string language_code(language_code_buffer); |
| 1170 std::string country_code(country_code_buffer); | 1170 std::string country_code(country_code_buffer); |
| 1171 std::string language_tag(language_code + "-" + country_code); | 1171 std::string language_tag(language_code + "-" + country_code); |
| 1172 | 1172 |
| 1173 if (seen_tags.find(language_tag) != seen_tags.end()) | 1173 if (seen_tags.find(language_tag) != seen_tags.end()) |
| 1174 continue; | 1174 continue; |
| 1175 | 1175 |
| 1176 seen_tags.insert(language_tag); | 1176 seen_tags.insert(language_tag); |
| 1177 unique_locale_list.push_back(std::make_pair(language_code, country_code)); | 1177 if (!country_code.empty()) { |
| 1178 unique_locale_list.push_back(language_tag); |
| 1179 } else { |
| 1180 unique_locale_list.push_back(language_code); |
| 1181 } |
| 1178 } | 1182 } |
| 1179 | 1183 *accept_languages = base::JoinString(unique_locale_list, ","); |
| 1180 // If language is not in the accept languages list, also add language | |
| 1181 // code. A language code should only be inserted after the last | |
| 1182 // languageTag that contains that language. | |
| 1183 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled | |
| 1184 // with Chrome but may fail on arbitrary lists of language tags due to | |
| 1185 // differences in case and whitespace. | |
| 1186 std::set<std::string> seen_languages; | |
| 1187 std::vector<std::string> output_list; | |
| 1188 for (auto it = unique_locale_list.rbegin(); it != unique_locale_list.rend(); | |
| 1189 ++it) { | |
| 1190 if (seen_languages.find(it->first) == seen_languages.end()) { | |
| 1191 output_list.push_back(it->first); | |
| 1192 seen_languages.insert(it->first); | |
| 1193 } | |
| 1194 if (!it->second.empty()) | |
| 1195 output_list.push_back(it->first + "-" + it->second); | |
| 1196 } | |
| 1197 | |
| 1198 std::reverse(output_list.begin(), output_list.end()); | |
| 1199 *accept_languages = base::JoinString(output_list, ","); | |
| 1200 } | 1184 } |
| 1201 | 1185 |
| 1202 // static | 1186 // static |
| 1203 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( | 1187 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( |
| 1204 ContentSettingsType content_type) { | 1188 ContentSettingsType content_type) { |
| 1205 JNIEnv* env = AttachCurrentThread(); | 1189 JNIEnv* env = AttachCurrentThread(); |
| 1206 base::android::ScopedJavaLocalRef<jstring> android_permission = | 1190 base::android::ScopedJavaLocalRef<jstring> android_permission = |
| 1207 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( | 1191 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( |
| 1208 env, content_type); | 1192 env, content_type); |
| 1209 if (android_permission.is_null()) | 1193 if (android_permission.is_null()) |
| 1210 return std::string(); | 1194 return std::string(); |
| 1211 | 1195 |
| 1212 return ConvertJavaStringToUTF8(android_permission); | 1196 return ConvertJavaStringToUTF8(android_permission); |
| 1213 } | 1197 } |
| 1214 | 1198 |
| 1215 static void SetSupervisedUserId(JNIEnv* env, | 1199 static void SetSupervisedUserId(JNIEnv* env, |
| 1216 const JavaParamRef<jobject>& obj, | 1200 const JavaParamRef<jobject>& obj, |
| 1217 const JavaParamRef<jstring>& pref) { | 1201 const JavaParamRef<jstring>& pref) { |
| 1218 GetPrefService()->SetString(prefs::kSupervisedUserId, | 1202 GetPrefService()->SetString(prefs::kSupervisedUserId, |
| 1219 ConvertJavaStringToUTF8(env, pref)); | 1203 ConvertJavaStringToUTF8(env, pref)); |
| 1220 } | 1204 } |
| OLD | NEW |