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

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

Issue 2406203002: Use BCP47 compliant format for locale representation (Closed)
Patch Set: 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>
11 #include <set>
12 #include <string>
11 #include <vector> 13 #include <vector>
12 14
13 #include "base/android/build_info.h" 15 #include "base/android/build_info.h"
14 #include "base/android/jni_android.h" 16 #include "base/android/jni_android.h"
15 #include "base/android/jni_array.h" 17 #include "base/android/jni_array.h"
16 #include "base/android/jni_string.h" 18 #include "base/android/jni_string.h"
17 #include "base/android/jni_weak_ref.h" 19 #include "base/android/jni_weak_ref.h"
18 #include "base/feature_list.h" 20 #include "base/feature_list.h"
19 #include "base/files/file_path.h" 21 #include "base/files/file_path.h"
20 #include "base/files/file_util.h" 22 #include "base/files/file_util.h"
21 #include "base/metrics/histogram_macros.h" 23 #include "base/metrics/histogram_macros.h"
22 #include "base/scoped_observer.h" 24 #include "base/scoped_observer.h"
25 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h" 26 #include "base/strings/string_util.h"
24 #include "base/values.h" 27 #include "base/values.h"
25 #include "chrome/browser/browser_process.h" 28 #include "chrome/browser/browser_process.h"
26 #include "chrome/browser/browsing_data/browsing_data_filter_builder.h" 29 #include "chrome/browser/browsing_data/browsing_data_filter_builder.h"
27 #include "chrome/browser/browsing_data/browsing_data_helper.h" 30 #include "chrome/browser/browsing_data/browsing_data_helper.h"
28 #include "chrome/browser/browsing_data/browsing_data_remover.h" 31 #include "chrome/browser/browsing_data/browsing_data_remover.h"
29 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" 32 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
30 #include "chrome/browser/browsing_data/registrable_domain_filter_builder.h" 33 #include "chrome/browser/browsing_data/registrable_domain_filter_builder.h"
31 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 34 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
32 #include "chrome/browser/engagement/important_sites_util.h" 35 #include "chrome/browser/engagement/important_sites_util.h"
(...skipping 1104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1137 1140
1138 // static 1141 // static
1139 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) { 1142 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) {
1140 return RegisterNativesImpl(env); 1143 return RegisterNativesImpl(env);
1141 } 1144 }
1142 1145
1143 // static 1146 // static
1144 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in 1147 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in
1145 // chrome/android/java/src/org/chromium/chrome/browser/ 1148 // chrome/android/java/src/org/chromium/chrome/browser/
1146 // physicalweb/PwsClientImpl.java 1149 // physicalweb/PwsClientImpl.java
1150 // Input |locales| is a comma separated locale representation that consists of
1151 // language tags(BCP47 compliant format). Each language tag contains
1152 // a language code and a country code or a language code only.
1147 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( 1153 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
1148 const std::string& locale, 1154 const std::string& locales,
1149 std::string* accept_languages) { 1155 std::string* accept_languages) {
1150 if (locale.size() != 5u || locale[2] != '_') // not well-formed 1156 std::vector<std::string> locale_list =
1151 return; 1157 base::SplitString(locales + "," + *accept_languages, ",",
1158 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
1152 1159
1153 std::string language(locale.substr(0, 2)); 1160 std::set<std::string> seen_tags;
1154 std::string region(locale.substr(3, 2)); 1161 std::vector<std::pair<std::string, std::string>> unique_locale_list;
1162 for (const std::string& locale_str : locale_list) {
1163 std::string lang_code;
1164 std::string country_code;
1165 if (locale_str.size() == 2u || locale_str.size() == 3u) {
1166 // Language tag consists of language only
1167 lang_code = locale_str;
1168 country_code = "";
1169 } else if (locale_str[2] == '-') {
1170 lang_code = locale_str.substr(0, 2);
1171 country_code = locale_str.substr(3);
1172 } else if (locale_str[3] == '-') {
1173 lang_code = locale_str.substr(0, 3);
1174 country_code = locale_str.substr(4);
1175 } else {
1176 continue;
1177 }
1155 1178
1156 // Java mostly follows ISO-639-1 and ICU, except for the following three. 1179 // Java mostly follows ISO-639-1 and ICU, except for the following three.
1157 // See documentation on java.util.Locale constructor for more. 1180 // See documentation on java.util.Locale constructor for more.
1158 if (language == "iw") { 1181 if (lang_code == "iw")
1159 language = "he"; 1182 lang_code = "he";
1160 } else if (language == "ji") { 1183 else if (lang_code == "ji")
1161 language = "yi"; 1184 lang_code = "yi";
1162 } else if (language == "in") { 1185 else if (lang_code == "in")
1163 language = "id"; 1186 lang_code = "id";
1187
1188 std::string language_tag(lang_code + "-" + country_code);
1189
1190 if (seen_tags.find(language_tag) != seen_tags.end())
1191 continue;
1192 if (country_code != "")
1193 seen_tags.insert(language_tag);
1194 unique_locale_list.push_back(std::make_pair(lang_code, country_code));
1164 } 1195 }
1165 1196
1166 std::string language_region(language + "-" + region); 1197 // If language is not in the accept languages list, also add language
1198 // code. A language code should only be inserted after the last
1199 // languageTag that contains that language.
1200 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
1201 // with Chrome but may fail on arbitrary lists of language tags due to
1202 // differences in case and whitespace.
1203 std::set<std::string> seen_languages;
1204 std::vector<std::string> output_list;
1205 for (auto it = unique_locale_list.rbegin(); it != unique_locale_list.rend();
1206 ++it) {
1207 if (seen_languages.find(it->first) == seen_languages.end()) {
1208 output_list.push_back(it->first);
1209 seen_languages.insert(it->first);
1210 }
1211 if (it->second != "")
1212 output_list.push_back(it->first + "-" + it->second);
1213 }
1167 1214
1168 if (accept_languages->find(language_region) == std::string::npos) { 1215 std::reverse(output_list.begin(), output_list.end());
1169 std::vector<std::string> parts; 1216 *accept_languages = base::JoinString(output_list, ",");
1170 parts.push_back(language_region);
1171 // If language is not in the accept languages list, also add language code.
1172 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
1173 // with Chrome but may fail on arbitrary lists of language tags due to
1174 // differences in case and whitespace.
1175 if (accept_languages->find(language + ",") == std::string::npos &&
1176 !std::equal(language.rbegin(), language.rend(),
1177 accept_languages->rbegin())) {
1178 parts.push_back(language);
1179 }
1180 parts.push_back(*accept_languages);
1181 *accept_languages = base::JoinString(parts, ",");
1182 }
1183 } 1217 }
1184 1218
1185 // static 1219 // static
1186 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( 1220 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting(
1187 ContentSettingsType content_type) { 1221 ContentSettingsType content_type) {
1188 JNIEnv* env = AttachCurrentThread(); 1222 JNIEnv* env = AttachCurrentThread();
1189 base::android::ScopedJavaLocalRef<jstring> android_permission = 1223 base::android::ScopedJavaLocalRef<jstring> android_permission =
1190 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( 1224 Java_PrefServiceBridge_getAndroidPermissionForContentSetting(
1191 env, content_type); 1225 env, content_type);
1192 if (android_permission.is_null()) 1226 if (android_permission.is_null())
1193 return std::string(); 1227 return std::string();
1194 1228
1195 return ConvertJavaStringToUTF8(android_permission); 1229 return ConvertJavaStringToUTF8(android_permission);
1196 } 1230 }
1197 1231
1198 static void SetSupervisedUserId(JNIEnv* env, 1232 static void SetSupervisedUserId(JNIEnv* env,
1199 const JavaParamRef<jobject>& obj, 1233 const JavaParamRef<jobject>& obj,
1200 const JavaParamRef<jstring>& pref) { 1234 const JavaParamRef<jstring>& pref) {
1201 GetPrefService()->SetString(prefs::kSupervisedUserId, 1235 GetPrefService()->SetString(prefs::kSupervisedUserId,
1202 ConvertJavaStringToUTF8(env, pref)); 1236 ConvertJavaStringToUTF8(env, pref));
1203 } 1237 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698