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

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: Add Multiple Locales For Accept Languages 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 <vector> 11 #include <vector>
12 12
13 #include "base/android/build_info.h" 13 #include "base/android/build_info.h"
14 #include "base/android/jni_android.h" 14 #include "base/android/jni_android.h"
15 #include "base/android/jni_array.h" 15 #include "base/android/jni_array.h"
16 #include "base/android/jni_string.h" 16 #include "base/android/jni_string.h"
17 #include "base/android/jni_weak_ref.h" 17 #include "base/android/jni_weak_ref.h"
18 #include "base/feature_list.h" 18 #include "base/feature_list.h"
19 #include "base/files/file_path.h" 19 #include "base/files/file_path.h"
20 #include "base/files/file_util.h" 20 #include "base/files/file_util.h"
21 #include "base/metrics/histogram_macros.h" 21 #include "base/metrics/histogram_macros.h"
22 #include "base/scoped_observer.h" 22 #include "base/scoped_observer.h"
23 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h" 24 #include "base/strings/string_util.h"
24 #include "base/values.h" 25 #include "base/values.h"
25 #include "chrome/browser/android/preferences/important_sites_util.h" 26 #include "chrome/browser/android/preferences/important_sites_util.h"
26 #include "chrome/browser/browser_process.h" 27 #include "chrome/browser/browser_process.h"
27 #include "chrome/browser/browsing_data/browsing_data_filter_builder.h" 28 #include "chrome/browser/browsing_data/browsing_data_filter_builder.h"
28 #include "chrome/browser/browsing_data/browsing_data_helper.h" 29 #include "chrome/browser/browsing_data/browsing_data_helper.h"
29 #include "chrome/browser/browsing_data/browsing_data_remover.h" 30 #include "chrome/browser/browsing_data/browsing_data_remover.h"
30 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" 31 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
31 #include "chrome/browser/browsing_data/registrable_domain_filter_builder.h" 32 #include "chrome/browser/browsing_data/registrable_domain_filter_builder.h"
32 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 33 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
(...skipping 1104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1137 1138
1138 // static 1139 // static
1139 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) { 1140 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) {
1140 return RegisterNativesImpl(env); 1141 return RegisterNativesImpl(env);
1141 } 1142 }
1142 1143
1143 // static 1144 // static
1144 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in 1145 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in
1145 // chrome/android/java/src/org/chromium/chrome/browser/ 1146 // chrome/android/java/src/org/chromium/chrome/browser/
1146 // physicalweb/PwsClientImpl.java 1147 // physicalweb/PwsClientImpl.java
1148 // Input |locales| is a comma separated language tags. Each language tag should
1149 // be xx_XX style, where xx is a 2-letter ISO 639-1 compliant language code and
ksk1 2016/10/06 08:15:22 In Java side, language tag indicates xx-XX. Here,
Yirui Huang 2016/10/07 04:14:56 currently, we will keep '_' form for the locales,
1150 // XX is a 2-letter ISO 3166-1 compliant country code.
1147 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( 1151 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
1148 const std::string& locale, 1152 const std::string& locales,
1149 std::string* accept_languages) { 1153 std::string* accept_languages) {
1150 if (locale.size() != 5u || locale[2] != '_') // not well-formed 1154 std::vector<std::string> languages = base::SplitString(locales,
ksk1 2016/10/06 08:15:22 Can this be locales? languages sounds confusing be
Yirui Huang 2016/10/07 04:14:56 Done.
1151 return; 1155 ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
1156 std::vector<std::string> parts;
1157 for (std::size_t i = 0; i < languages.size(); i++) {
Maria 2016/10/06 16:38:00 you can just use size_t here, Chrome defines it (s
Yirui Huang 2016/10/07 04:14:56 Done.
1158 if (languages[i].size() != 5u || languages[i][2] != '_')
1159 // TODO(yirui): Support BCP47 compliant format including 3-letter
1160 // country code, '-' separator and missing region case.
1161 continue;
Maria 2016/10/06 16:38:00 use {} for blocks that are more than one line (e.g
Yirui Huang 2016/10/07 04:14:56 Done.
1162 std::string language(languages[i].substr(0, 2));
1163 std::string region(languages[i].substr(3, 2));
1152 1164
1153 std::string language(locale.substr(0, 2)); 1165 // Java mostly follows ISO-639-1 and ICU, except for the following three.
1154 std::string region(locale.substr(3, 2)); 1166 // See documentation on java.util.Locale constructor for more.
1167 if (language == "iw") {
1168 language = "he";
1169 } else if (language == "ji") {
1170 language = "yi";
1171 } else if (language == "in") {
1172 language = "id";
1173 }
1155 1174
1156 // Java mostly follows ISO-639-1 and ICU, except for the following three. 1175 std::string language_region(language + "-" + region);
1157 // See documentation on java.util.Locale constructor for more. 1176
1158 if (language == "iw") { 1177 if (accept_languages->find(language_region) == std::string::npos) {
1159 language = "he"; 1178 parts.push_back(language_region);
1160 } else if (language == "ji") { 1179 // If language is not in the accept languages list, also add language
1161 language = "yi"; 1180 // code.
1162 } else if (language == "in") { 1181 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
1163 language = "id"; 1182 // with Chrome but may fail on arbitrary lists of language tags due to
1183 // differences in case and whitespace.
1184 if ((accept_languages->find(language) == std::string::npos ||
ksk1 2016/10/06 08:15:22 If I understand correctly, when (accept_languages-
Yirui Huang 2016/10/07 04:14:56 set is used for this checking process.
1185 accept_languages->find(language + ",") == std::string::npos) &&
1186 !std::equal(language.rbegin(), language.rend(),
1187 accept_languages->rbegin())) {
1188 parts.push_back(language);
1189 }
1190 }
1164 } 1191 }
1165 1192 parts.push_back(*accept_languages);
1166 std::string language_region(language + "-" + region); 1193 *accept_languages = base::JoinString(parts, ",");
1167
1168 if (accept_languages->find(language_region) == std::string::npos) {
1169 std::vector<std::string> parts;
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 } 1194 }
1184 1195
1185 // static 1196 // static
1186 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( 1197 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting(
1187 ContentSettingsType content_type) { 1198 ContentSettingsType content_type) {
1188 JNIEnv* env = AttachCurrentThread(); 1199 JNIEnv* env = AttachCurrentThread();
1189 base::android::ScopedJavaLocalRef<jstring> android_permission = 1200 base::android::ScopedJavaLocalRef<jstring> android_permission =
1190 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( 1201 Java_PrefServiceBridge_getAndroidPermissionForContentSetting(
1191 env, content_type); 1202 env, content_type);
1192 if (android_permission.is_null()) 1203 if (android_permission.is_null())
1193 return std::string(); 1204 return std::string();
1194 1205
1195 return ConvertJavaStringToUTF8(android_permission); 1206 return ConvertJavaStringToUTF8(android_permission);
1196 } 1207 }
1197 1208
1198 static void SetSupervisedUserId(JNIEnv* env, 1209 static void SetSupervisedUserId(JNIEnv* env,
1199 const JavaParamRef<jobject>& obj, 1210 const JavaParamRef<jobject>& obj,
1200 const JavaParamRef<jstring>& pref) { 1211 const JavaParamRef<jstring>& pref) {
1201 GetPrefService()->SetString(prefs::kSupervisedUserId, 1212 GetPrefService()->SetString(prefs::kSupervisedUserId,
1202 ConvertJavaStringToUTF8(env, pref)); 1213 ConvertJavaStringToUTF8(env, pref));
1203 } 1214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698