Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(403)

Side by Side Diff: chrome/browser/android/preferences/pref_service_bridge.cc

Issue 2406203002: Use BCP47 compliant format for locale representation (Closed)
Patch Set: format return in description Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 #include "components/password_manager/core/common/password_manager_pref_names.h" 56 #include "components/password_manager/core/common/password_manager_pref_names.h"
57 #include "components/prefs/pref_service.h" 57 #include "components/prefs/pref_service.h"
58 #include "components/signin/core/common/signin_pref_names.h" 58 #include "components/signin/core/common/signin_pref_names.h"
59 #include "components/strings/grit/components_locale_settings.h" 59 #include "components/strings/grit/components_locale_settings.h"
60 #include "components/translate/core/browser/translate_prefs.h" 60 #include "components/translate/core/browser/translate_prefs.h"
61 #include "components/translate/core/common/translate_pref_names.h" 61 #include "components/translate/core/common/translate_pref_names.h"
62 #include "components/version_info/version_info.h" 62 #include "components/version_info/version_info.h"
63 #include "components/web_resource/web_resource_pref_names.h" 63 #include "components/web_resource/web_resource_pref_names.h"
64 #include "content/public/browser/browser_thread.h" 64 #include "content/public/browser/browser_thread.h"
65 #include "jni/PrefServiceBridge_jni.h" 65 #include "jni/PrefServiceBridge_jni.h"
66 #include "third_party/icu/source/common/unicode/uloc.h"
66 #include "ui/base/l10n/l10n_util.h" 67 #include "ui/base/l10n/l10n_util.h"
67 68
68 using base::android::AttachCurrentThread; 69 using base::android::AttachCurrentThread;
69 using base::android::CheckException; 70 using base::android::CheckException;
70 using base::android::ConvertJavaStringToUTF8; 71 using base::android::ConvertJavaStringToUTF8;
71 using base::android::ConvertUTF8ToJavaString; 72 using base::android::ConvertUTF8ToJavaString;
72 using base::android::JavaParamRef; 73 using base::android::JavaParamRef;
73 using base::android::ScopedJavaLocalRef; 74 using base::android::ScopedJavaLocalRef;
74 using base::android::ScopedJavaGlobalRef; 75 using base::android::ScopedJavaGlobalRef;
75 using content::BrowserThread; 76 using content::BrowserThread;
(...skipping 1064 matching lines...) Expand 10 before | Expand all | Expand 10 after
1140 1141
1141 // static 1142 // static
1142 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) { 1143 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) {
1143 return RegisterNativesImpl(env); 1144 return RegisterNativesImpl(env);
1144 } 1145 }
1145 1146
1146 // static 1147 // static
1147 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in 1148 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in
1148 // chrome/android/java/src/org/chromium/chrome/browser/ 1149 // chrome/android/java/src/org/chromium/chrome/browser/
1149 // physicalweb/PwsClientImpl.java 1150 // physicalweb/PwsClientImpl.java
1150 // Input |locales| is a comma separated locale representaion. Each locale 1151 // Input |locales| is a comma separated locale representation that consists of
1151 // representation should be xx_XX style, where xx is a 2-letter 1152 // language tags(BCP47 compliant format). Each language tag contains
1152 // ISO 639-1 compliant language code and XX is a 2-letter 1153 // a language code and a country code or a language code only.
1153 // ISO 3166-1 compliant country code.
1154 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( 1154 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
1155 const std::string& locales, 1155 const std::string& locales,
1156 std::string* accept_languages) { 1156 std::string* accept_languages) {
1157 std::vector<std::string> locale_list = 1157 std::vector<std::string> locale_list =
1158 base::SplitString(locales + "," + *accept_languages, ",", 1158 base::SplitString(locales + "," + *accept_languages, ",",
1159 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 1159 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
1160 1160
1161 std::set<std::string> seen_tags; 1161 std::set<std::string> seen_tags;
1162 std::vector<std::pair<std::string, std::string>> unique_locale_list; 1162 std::vector<std::pair<std::string, std::string>> unique_locale_list;
1163 for (const std::string& locale_str : locale_list) { 1163 for (const std::string& locale_str : locale_list) {
1164 // TODO(yirui): Support BCP47 compliant format including 3-letter 1164 char locale_ID[ULOC_FULLNAME_CAPACITY];
1165 // country code, '-' separator and missing country case. 1165 char language_code[ULOC_LANG_CAPACITY];
1166 if (locale_str.size() != 5u || 1166 char country_code[ULOC_COUNTRY_CAPACITY];
1167 (locale_str[2] != '_' && locale_str[2] != '-')) 1167 UErrorCode error = U_ZERO_ERROR;
1168 continue; // Skip not well formed locale. 1168 uloc_forLanguageTag(locale_str.c_str(), locale_ID, ULOC_FULLNAME_CAPACITY,
1169 nullptr, &error);
1170 uloc_getLanguage(locale_ID, language_code, ULOC_LANG_CAPACITY, &error);
1171 uloc_getCountry(locale_ID, country_code, ULOC_COUNTRY_CAPACITY, &error);
1172 std::string lang_code(language_code);
ksk1 2016/10/17 05:06:04 Using both lang_code and language_code is confusin
Yirui Huang 2016/10/17 10:40:52 Done.
1173 std::string coun_code(country_code);
1169 1174
ksk1 2016/10/17 05:06:04 if (error != U_ZERO_ERROR || lang_code.empty()) {
Yirui Huang 2016/10/17 10:40:52 U_FAILURE is used for checking.
1170 std::string lang_code(locale_str.substr(0, 2)); 1175 // Java mostly follows ISO-639-1 and ICU, except for the following four.
1171 std::string country_code(locale_str.substr(3, 2));
1172
1173 // Java mostly follows ISO-639-1 and ICU, except for the following three.
1174 // See documentation on java.util.Locale constructor for more. 1176 // See documentation on java.util.Locale constructor for more.
1175 if (lang_code == "iw") 1177 if (lang_code == "iw")
1176 lang_code = "he"; 1178 lang_code = "he";
1177 else if (lang_code == "ji") 1179 else if (lang_code == "ji")
1178 lang_code = "yi"; 1180 lang_code = "yi";
1179 else if (lang_code == "in") 1181 else if (lang_code == "in")
1180 lang_code = "id"; 1182 lang_code = "id";
1183 else if (lang_code == "tl")
1184 lang_code = "fil";
1181 1185
1182 std::string language_tag(lang_code + "-" + country_code); 1186 std::string language_tag(lang_code + "-" + coun_code);
1183 1187
1184 if (seen_tags.find(language_tag) != seen_tags.end()) 1188 if (seen_tags.find(language_tag) != seen_tags.end())
1185 continue; 1189 continue;
1186 unique_locale_list.push_back(std::make_pair(lang_code, country_code)); 1190 if (coun_code != "")
ksk1 2016/10/17 05:06:04 !coun_code.empty()
Yirui Huang 2016/10/17 10:40:52 same as in Java side, not necessary, so removed ch
1187 seen_tags.insert(language_tag); 1191 seen_tags.insert(language_tag);
1192 unique_locale_list.push_back(std::make_pair(lang_code, coun_code));
1188 } 1193 }
1189 1194
1190 // If language is not in the accept languages list, also add language 1195 // If language is not in the accept languages list, also add language
1191 // code. A language code should only be inserted after the last 1196 // code. A language code should only be inserted after the last
1192 // languageTag that contains that language. 1197 // languageTag that contains that language.
1193 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled 1198 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
1194 // with Chrome but may fail on arbitrary lists of language tags due to 1199 // with Chrome but may fail on arbitrary lists of language tags due to
1195 // differences in case and whitespace. 1200 // differences in case and whitespace.
1196 std::set<std::string> seen_languages; 1201 std::set<std::string> seen_languages;
1197 std::vector<std::string> output_list; 1202 std::vector<std::string> output_list;
1198 for (auto it = unique_locale_list.rbegin(); it != unique_locale_list.rend(); 1203 for (auto it = unique_locale_list.rbegin(); it != unique_locale_list.rend();
1199 ++it) { 1204 ++it) {
1200 if (seen_languages.find(it->first) == seen_languages.end()) { 1205 if (seen_languages.find(it->first) == seen_languages.end()) {
1201 output_list.push_back(it->first); 1206 output_list.push_back(it->first);
1202 seen_languages.insert(it->first); 1207 seen_languages.insert(it->first);
1203 } 1208 }
1204 output_list.push_back(it->first + "-" + it->second); 1209 if (!it->second.empty())
1210 output_list.push_back(it->first + "-" + it->second);
1205 } 1211 }
1206 1212
1207 std::reverse(output_list.begin(), output_list.end()); 1213 std::reverse(output_list.begin(), output_list.end());
1208 *accept_languages = base::JoinString(output_list, ","); 1214 *accept_languages = base::JoinString(output_list, ",");
1209 } 1215 }
1210 1216
1211 // static 1217 // static
1212 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( 1218 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting(
1213 ContentSettingsType content_type) { 1219 ContentSettingsType content_type) {
1214 JNIEnv* env = AttachCurrentThread(); 1220 JNIEnv* env = AttachCurrentThread();
1215 base::android::ScopedJavaLocalRef<jstring> android_permission = 1221 base::android::ScopedJavaLocalRef<jstring> android_permission =
1216 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( 1222 Java_PrefServiceBridge_getAndroidPermissionForContentSetting(
1217 env, content_type); 1223 env, content_type);
1218 if (android_permission.is_null()) 1224 if (android_permission.is_null())
1219 return std::string(); 1225 return std::string();
1220 1226
1221 return ConvertJavaStringToUTF8(android_permission); 1227 return ConvertJavaStringToUTF8(android_permission);
1222 } 1228 }
1223 1229
1224 static void SetSupervisedUserId(JNIEnv* env, 1230 static void SetSupervisedUserId(JNIEnv* env,
1225 const JavaParamRef<jobject>& obj, 1231 const JavaParamRef<jobject>& obj,
1226 const JavaParamRef<jstring>& pref) { 1232 const JavaParamRef<jstring>& pref) {
1227 GetPrefService()->SetString(prefs::kSupervisedUserId, 1233 GetPrefService()->SetString(prefs::kSupervisedUserId,
1228 ConvertJavaStringToUTF8(env, pref)); 1234 ConvertJavaStringToUTF8(env, pref));
1229 } 1235 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698