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 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1056 | 1056 |
1057 static void SetEulaAccepted(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 1057 static void SetEulaAccepted(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
1058 g_browser_process->local_state()->SetBoolean(prefs::kEulaAccepted, true); | 1058 g_browser_process->local_state()->SetBoolean(prefs::kEulaAccepted, true); |
1059 } | 1059 } |
1060 | 1060 |
1061 static void ResetAcceptLanguages(JNIEnv* env, | 1061 static void ResetAcceptLanguages(JNIEnv* env, |
1062 const JavaParamRef<jobject>& obj, | 1062 const JavaParamRef<jobject>& obj, |
1063 const JavaParamRef<jstring>& default_locale) { | 1063 const JavaParamRef<jstring>& default_locale) { |
1064 std::string accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES)); | 1064 std::string accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES)); |
1065 std::string locale_string(ConvertJavaStringToUTF8(env, default_locale)); | 1065 std::string locale_string(ConvertJavaStringToUTF8(env, default_locale)); |
1066 | |
1067 PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(locale_string, | 1066 PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(locale_string, |
1068 &accept_languages); | 1067 &accept_languages); |
1069 GetPrefService()->SetString(prefs::kAcceptLanguages, accept_languages); | 1068 GetPrefService()->SetString(prefs::kAcceptLanguages, accept_languages); |
1070 } | 1069 } |
1071 | 1070 |
1072 // Sends all information about the different versions to Java. | 1071 // Sends all information about the different versions to Java. |
1073 // From browser_about_handler.cc | 1072 // From browser_about_handler.cc |
1074 static ScopedJavaLocalRef<jobject> GetAboutVersionStrings( | 1073 static ScopedJavaLocalRef<jobject> GetAboutVersionStrings( |
1075 JNIEnv* env, | 1074 JNIEnv* env, |
1076 const JavaParamRef<jobject>& obj) { | 1075 const JavaParamRef<jobject>& obj) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1140 return RegisterNativesImpl(env); | 1139 return RegisterNativesImpl(env); |
1141 } | 1140 } |
1142 | 1141 |
1143 // static | 1142 // static |
1144 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in | 1143 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in |
1145 // chrome/android/java/src/org/chromium/chrome/browser/ | 1144 // chrome/android/java/src/org/chromium/chrome/browser/ |
1146 // physicalweb/PwsClientImpl.java | 1145 // physicalweb/PwsClientImpl.java |
1147 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( | 1146 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( |
1148 const std::string& locale, | 1147 const std::string& locale, |
1149 std::string* accept_languages) { | 1148 std::string* accept_languages) { |
1150 if (locale.size() != 5u || locale[2] != '_') // not well-formed | 1149 std::vector<std::string> lans; |
Seigo Nonaka
2016/10/05 07:15:41
probably, languages or langs are preferred.
Yirui Huang
2016/10/06 01:17:59
Done.
| |
1151 return; | 1150 std::string languages_to_check = locale; |
1152 | 1151 // Check see if it is a Locale List or single Locale |
1153 std::string language(locale.substr(0, 2)); | 1152 if (languages_to_check.length() > 5) { |
Seigo Nonaka
2016/10/05 07:15:41
Looks like this is not a good condition for checki
Yirui Huang
2016/10/06 01:17:59
Done.
| |
1154 std::string region(locale.substr(3, 2)); | 1153 while (languages_to_check.find(",") != std::string::npos) { |
1155 | 1154 std::size_t bfound = 0; |
1156 // Java mostly follows ISO-639-1 and ICU, except for the following three. | 1155 std::size_t efound = languages_to_check.find(","); |
1157 // See documentation on java.util.Locale constructor for more. | 1156 lans.push_back(languages_to_check.substr(bfound, efound)); |
1158 if (language == "iw") { | 1157 bfound = efound + 1; |
1159 language = "he"; | 1158 languages_to_check = languages_to_check.substr(bfound); |
Seigo Nonaka
2016/10/05 07:15:41
Since languages_to_check is a std::string, you cre
Yirui Huang
2016/10/06 01:17:59
Done.
| |
1160 } else if (language == "ji") { | 1159 // Check the last element |
1161 language = "yi"; | 1160 if (languages_to_check.length() <= 5) { |
Seigo Nonaka
2016/10/05 07:15:41
Similarly, languages_to_check can be still list ev
Yirui Huang
2016/10/06 01:17:59
Done.
| |
1162 } else if (language == "in") { | 1161 lans.push_back(languages_to_check); |
1163 language = "id"; | 1162 break; |
1163 } | |
1164 } | |
1165 } else { | |
1166 lans.push_back(languages_to_check); | |
1164 } | 1167 } |
1165 | 1168 |
1166 std::string language_region(language + "-" + region); | 1169 std::vector<std::string> parts; |
1170 for (std::size_t i = 0; i < lans.size(); i++) { | |
1171 if (lans[i].size() != 5u || lans[i][2] != '_') // not well-formed | |
1172 continue; | |
1173 std::string language(lans[i].substr(0, 2)); | |
1174 std::string region(lans[i].substr(3, 2)); | |
1167 | 1175 |
1168 if (accept_languages->find(language_region) == std::string::npos) { | 1176 // Java mostly follows ISO-639-1 and ICU, except for the following three. |
1169 std::vector<std::string> parts; | 1177 // See documentation on java.util.Locale constructor for more. |
1170 parts.push_back(language_region); | 1178 if (language == "iw") { |
1171 // If language is not in the accept languages list, also add language code. | 1179 language = "he"; |
1172 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled | 1180 } else if (language == "ji") { |
1173 // with Chrome but may fail on arbitrary lists of language tags due to | 1181 language = "yi"; |
1174 // differences in case and whitespace. | 1182 } else if (language == "in") { |
1175 if (accept_languages->find(language + ",") == std::string::npos && | 1183 language = "id"; |
1176 !std::equal(language.rbegin(), language.rend(), | |
1177 accept_languages->rbegin())) { | |
1178 parts.push_back(language); | |
1179 } | 1184 } |
1180 parts.push_back(*accept_languages); | 1185 |
1181 *accept_languages = base::JoinString(parts, ","); | 1186 std::string language_region(language + "-" + region); |
1187 | |
1188 if (accept_languages->find(language_region) == std::string::npos) { | |
1189 parts.push_back(language_region); | |
1190 // If language is not in the accept languages list, also add language | |
1191 // code. | |
1192 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled | |
1193 // with Chrome but may fail on arbitrary lists of language tags due to | |
1194 // differences in case and whitespace. | |
1195 if ((accept_languages->find(language) == std::string::npos || | |
1196 accept_languages->find(language + ",") == std::string::npos) && | |
1197 !std::equal(language.rbegin(), language.rend(), | |
1198 accept_languages->rbegin())) { | |
1199 parts.push_back(language); | |
1200 } | |
1201 } | |
1182 } | 1202 } |
1203 parts.push_back(*accept_languages); | |
1204 *accept_languages = base::JoinString(parts, ","); | |
1183 } | 1205 } |
1184 | 1206 |
1185 // static | 1207 // static |
1186 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( | 1208 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( |
1187 ContentSettingsType content_type) { | 1209 ContentSettingsType content_type) { |
1188 JNIEnv* env = AttachCurrentThread(); | 1210 JNIEnv* env = AttachCurrentThread(); |
1189 base::android::ScopedJavaLocalRef<jstring> android_permission = | 1211 base::android::ScopedJavaLocalRef<jstring> android_permission = |
1190 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( | 1212 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( |
1191 env, content_type); | 1213 env, content_type); |
1192 if (android_permission.is_null()) | 1214 if (android_permission.is_null()) |
1193 return std::string(); | 1215 return std::string(); |
1194 | 1216 |
1195 return ConvertJavaStringToUTF8(android_permission); | 1217 return ConvertJavaStringToUTF8(android_permission); |
1196 } | 1218 } |
1197 | 1219 |
1198 static void SetSupervisedUserId(JNIEnv* env, | 1220 static void SetSupervisedUserId(JNIEnv* env, |
1199 const JavaParamRef<jobject>& obj, | 1221 const JavaParamRef<jobject>& obj, |
1200 const JavaParamRef<jstring>& pref) { | 1222 const JavaParamRef<jstring>& pref) { |
1201 GetPrefService()->SetString(prefs::kSupervisedUserId, | 1223 GetPrefService()->SetString(prefs::kSupervisedUserId, |
1202 ConvertJavaStringToUTF8(env, pref)); | 1224 ConvertJavaStringToUTF8(env, pref)); |
1203 } | 1225 } |
OLD | NEW |