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

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

Issue 2393673003: Support multiple locales string for accept language list (Closed)
Patch Set: Support multiple locales string for language list 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/android/preferences/important_sites_util.h" 28 #include "chrome/browser/android/preferences/important_sites_util.h"
26 #include "chrome/browser/browser_process.h" 29 #include "chrome/browser/browser_process.h"
27 #include "chrome/browser/browsing_data/browsing_data_filter_builder.h" 30 #include "chrome/browser/browsing_data/browsing_data_filter_builder.h"
28 #include "chrome/browser/browsing_data/browsing_data_helper.h" 31 #include "chrome/browser/browsing_data/browsing_data_helper.h"
29 #include "chrome/browser/browsing_data/browsing_data_remover.h" 32 #include "chrome/browser/browsing_data/browsing_data_remover.h"
30 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" 33 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
31 #include "chrome/browser/browsing_data/registrable_domain_filter_builder.h" 34 #include "chrome/browser/browsing_data/registrable_domain_filter_builder.h"
32 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 35 #include "chrome/browser/content_settings/host_content_settings_map_factory.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 language tags. Each locale
ksk1 2016/10/07 09:16:23 comma separated locales?
Yirui Huang 2016/10/07 10:34:15 Done.
1151 // representation should be xx_XX style, where xx is a 2-letter
1152 // ISO 639-1 compliant language code and XX is a 2-letter
1153 // ISO 3166-1 compliant country code.
1147 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( 1154 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
1148 const std::string& locale, 1155 const std::string& locales,
1149 std::string* accept_languages) { 1156 std::string* accept_languages) {
1150 if (locale.size() != 5u || locale[2] != '_') // not well-formed 1157 std::vector<std::string> locale_list =
1151 return; 1158 base::SplitString(locales + "," + *accept_languages, ",",
1159 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
1152 1160
1153 std::string language(locale.substr(0, 2)); 1161 std::set<std::string> seen_tags;
1154 std::string region(locale.substr(3, 2)); 1162 std::vector<std::pair<std::string, std::string>> unique_locale_list;
1163 for (const std::string& locale_str : locale_list) {
1164 // TODO(yirui): Support BCP47 compliant format including 3-letter
1165 // country code, '-' separator and missing region case.
1166 if (locale_str.size() != 5 ||
ksk1 2016/10/07 09:16:23 != 5u
Yirui Huang 2016/10/07 10:34:15 Done.
1167 (locale_str[2] != '_' && locale_str[2] != '-'))
1168 continue; // Skip not well formed locale.
1155 1169
1156 // Java mostly follows ISO-639-1 and ICU, except for the following three. 1170 std::string lang_code = locale_str.substr(0, 2);
1157 // See documentation on java.util.Locale constructor for more. 1171 std::string region_code = locale_str.substr(3, 2);
ksk1 2016/10/07 09:16:23 county_code We call it country code in the method
Yirui Huang 2016/10/07 10:34:15 Done.
1158 if (language == "iw") { 1172
1159 language = "he"; 1173 // Java mostly follows ISO-639-1 and ICU, except for the following three.
1160 } else if (language == "ji") { 1174 // See documentation on java.util.Locale constructor for more.
1161 language = "yi"; 1175 if (lang_code == "iw")
1162 } else if (language == "in") { 1176 lang_code = "he";
1163 language = "id"; 1177 else if (lang_code == "ji")
1178 lang_code = "yi";
1179 else if (lang_code == "in")
1180 lang_code = "id";
1181
1182 std::string language_tag = lang_code + "-" + region_code;
1183
1184 if (seen_tags.find(language_tag) != seen_tags.end())
1185 continue;
1186 unique_locale_list.push_back(std::make_pair(lang_code, region_code));
1187 seen_tags.insert(language_tag);
1164 } 1188 }
1165 1189
1166 std::string language_region(language + "-" + region); 1190 // If language is not in the accept languages list, also add language
1191 // code. A language code should only be inserted after the last
1192 // languageTag that contains that language.
1193 // 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
1195 // differences in case and whitespace.
1196 std::set<std::string> seen_languages;
1197 std::vector<std::string> output_list;
1198 for (auto it = unique_locale_list.rbegin(); it != unique_locale_list.rend();
1199 ++it) {
1200 if (seen_languages.find(it->first) == seen_languages.end()) {
1201 output_list.push_back(it->first);
1202 seen_languages.insert(it->first);
1203 }
1204 output_list.push_back(it->first + "-" + it->second);
1205 }
1167 1206
1168 if (accept_languages->find(language_region) == std::string::npos) { 1207 std::reverse(output_list.begin(), output_list.end());
1169 std::vector<std::string> parts; 1208 *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 } 1209 }
1184 1210
1185 // static 1211 // static
1186 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( 1212 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting(
1187 ContentSettingsType content_type) { 1213 ContentSettingsType content_type) {
1188 JNIEnv* env = AttachCurrentThread(); 1214 JNIEnv* env = AttachCurrentThread();
1189 base::android::ScopedJavaLocalRef<jstring> android_permission = 1215 base::android::ScopedJavaLocalRef<jstring> android_permission =
1190 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( 1216 Java_PrefServiceBridge_getAndroidPermissionForContentSetting(
1191 env, content_type); 1217 env, content_type);
1192 if (android_permission.is_null()) 1218 if (android_permission.is_null())
1193 return std::string(); 1219 return std::string();
1194 1220
1195 return ConvertJavaStringToUTF8(android_permission); 1221 return ConvertJavaStringToUTF8(android_permission);
1196 } 1222 }
1197 1223
1198 static void SetSupervisedUserId(JNIEnv* env, 1224 static void SetSupervisedUserId(JNIEnv* env,
1199 const JavaParamRef<jobject>& obj, 1225 const JavaParamRef<jobject>& obj,
1200 const JavaParamRef<jstring>& pref) { 1226 const JavaParamRef<jstring>& pref) {
1201 GetPrefService()->SetString(prefs::kSupervisedUserId, 1227 GetPrefService()->SetString(prefs::kSupervisedUserId,
1202 ConvertJavaStringToUTF8(env, pref)); 1228 ConvertJavaStringToUTF8(env, pref));
1203 } 1229 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698