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> |
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/logging.h" | |
21 #include "base/metrics/histogram_macros.h" | 22 #include "base/metrics/histogram_macros.h" |
22 #include "base/scoped_observer.h" | 23 #include "base/scoped_observer.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" |
(...skipping 1025 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1056 | 1057 |
1057 static void SetEulaAccepted(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 1058 static void SetEulaAccepted(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
1058 g_browser_process->local_state()->SetBoolean(prefs::kEulaAccepted, true); | 1059 g_browser_process->local_state()->SetBoolean(prefs::kEulaAccepted, true); |
1059 } | 1060 } |
1060 | 1061 |
1061 static void ResetAcceptLanguages(JNIEnv* env, | 1062 static void ResetAcceptLanguages(JNIEnv* env, |
1062 const JavaParamRef<jobject>& obj, | 1063 const JavaParamRef<jobject>& obj, |
1063 const JavaParamRef<jstring>& default_locale) { | 1064 const JavaParamRef<jstring>& default_locale) { |
1064 std::string accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES)); | 1065 std::string accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES)); |
1065 std::string locale_string(ConvertJavaStringToUTF8(env, default_locale)); | 1066 std::string locale_string(ConvertJavaStringToUTF8(env, default_locale)); |
1066 | |
1067 PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(locale_string, | 1067 PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(locale_string, |
1068 &accept_languages); | 1068 &accept_languages); |
1069 GetPrefService()->SetString(prefs::kAcceptLanguages, accept_languages); | 1069 GetPrefService()->SetString(prefs::kAcceptLanguages, accept_languages); |
1070 } | 1070 } |
1071 | 1071 |
1072 // Sends all information about the different versions to Java. | 1072 // Sends all information about the different versions to Java. |
1073 // From browser_about_handler.cc | 1073 // From browser_about_handler.cc |
1074 static ScopedJavaLocalRef<jobject> GetAboutVersionStrings( | 1074 static ScopedJavaLocalRef<jobject> GetAboutVersionStrings( |
1075 JNIEnv* env, | 1075 JNIEnv* env, |
1076 const JavaParamRef<jobject>& obj) { | 1076 const JavaParamRef<jobject>& obj) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1138 // static | 1138 // static |
1139 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) { | 1139 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) { |
1140 return RegisterNativesImpl(env); | 1140 return RegisterNativesImpl(env); |
1141 } | 1141 } |
1142 | 1142 |
1143 // static | 1143 // static |
1144 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in | 1144 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in |
1145 // chrome/android/java/src/org/chromium/chrome/browser/ | 1145 // chrome/android/java/src/org/chromium/chrome/browser/ |
1146 // physicalweb/PwsClientImpl.java | 1146 // physicalweb/PwsClientImpl.java |
1147 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( | 1147 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( |
1148 const std::string& locale, | 1148 const std::string& locale, |
Seigo Nonaka
2016/10/06 02:02:18
This is now comma-separated multiple locales. Coul
Yirui Huang
2016/10/06 02:29:11
Done.
| |
1149 std::string* accept_languages) { | 1149 std::string* accept_languages) { |
1150 if (locale.size() != 5u || locale[2] != '_') // not well-formed | 1150 std::vector<std::string> languages; |
1151 return; | 1151 std::size_t max_check_length = locale.length(); |
Seigo Nonaka
2016/10/06 02:02:18
I think you can just use size_t here (dropping std
Yirui Huang
2016/10/06 02:29:11
Done.
| |
1152 | 1152 std::size_t check = 0; |
1153 std::string language(locale.substr(0, 2)); | 1153 // Check see if it is a Locale List or single Locale |
1154 std::string region(locale.substr(3, 2)); | 1154 if (locale.length() > 5) { |
Seigo Nonaka
2016/10/06 02:02:18
BTW, I think you can use StringSplit here.
https:/
Yirui Huang
2016/10/06 02:29:11
Done.
| |
1155 | 1155 while (locale.find(",", check) != std::string::npos) { |
1156 // Java mostly follows ISO-639-1 and ICU, except for the following three. | 1156 std::size_t found = locale.find(",", check); |
1157 // See documentation on java.util.Locale constructor for more. | 1157 languages.push_back(locale.substr(check, found - check)); |
1158 if (language == "iw") { | 1158 check = found + 1; |
1159 language = "he"; | 1159 // Check the last element |
1160 } else if (language == "ji") { | 1160 if (max_check_length - check <= 5) { |
1161 language = "yi"; | 1161 languages.push_back(locale.substr(check)); |
1162 } else if (language == "in") { | 1162 break; |
1163 language = "id"; | 1163 } |
1164 } | |
1165 } else { | |
1166 languages.push_back(locale); | |
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 < languages.size(); i++) { | |
1171 // Since in Android Language Input, region is always attached with language. | |
1172 // Therefore, an unwell-formed input, such as 'ms', should not be expected. | |
1173 DCHECK(languages[i].size() == 5u && languages[i][2] == '_'); | |
Seigo Nonaka
2016/10/06 02:02:18
As we chatted offline, sorry DCHECK is bit aggress
Yirui Huang
2016/10/06 02:29:11
Done.
| |
1174 std::string language(languages[i].substr(0, 2)); | |
1175 std::string region(languages[i].substr(3, 2)); | |
1167 | 1176 |
1168 if (accept_languages->find(language_region) == std::string::npos) { | 1177 // Java mostly follows ISO-639-1 and ICU, except for the following three. |
1169 std::vector<std::string> parts; | 1178 // See documentation on java.util.Locale constructor for more. |
1170 parts.push_back(language_region); | 1179 if (language == "iw") { |
1171 // If language is not in the accept languages list, also add language code. | 1180 language = "he"; |
1172 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled | 1181 } else if (language == "ji") { |
1173 // with Chrome but may fail on arbitrary lists of language tags due to | 1182 language = "yi"; |
1174 // differences in case and whitespace. | 1183 } else if (language == "in") { |
1175 if (accept_languages->find(language + ",") == std::string::npos && | 1184 language = "id"; |
1176 !std::equal(language.rbegin(), language.rend(), | |
1177 accept_languages->rbegin())) { | |
1178 parts.push_back(language); | |
1179 } | 1185 } |
1180 parts.push_back(*accept_languages); | 1186 |
1181 *accept_languages = base::JoinString(parts, ","); | 1187 std::string language_region(language + "-" + region); |
1188 | |
1189 if (accept_languages->find(language_region) == std::string::npos) { | |
1190 parts.push_back(language_region); | |
1191 // If language is not in the accept languages list, also add language | |
1192 // code. | |
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 if ((accept_languages->find(language) == std::string::npos || | |
1197 accept_languages->find(language + ",") == std::string::npos) && | |
1198 !std::equal(language.rbegin(), language.rend(), | |
1199 accept_languages->rbegin())) { | |
1200 parts.push_back(language); | |
1201 } | |
1202 } | |
1182 } | 1203 } |
1204 parts.push_back(*accept_languages); | |
1205 *accept_languages = base::JoinString(parts, ","); | |
1183 } | 1206 } |
1184 | 1207 |
1185 // static | 1208 // static |
1186 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( | 1209 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( |
1187 ContentSettingsType content_type) { | 1210 ContentSettingsType content_type) { |
1188 JNIEnv* env = AttachCurrentThread(); | 1211 JNIEnv* env = AttachCurrentThread(); |
1189 base::android::ScopedJavaLocalRef<jstring> android_permission = | 1212 base::android::ScopedJavaLocalRef<jstring> android_permission = |
1190 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( | 1213 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( |
1191 env, content_type); | 1214 env, content_type); |
1192 if (android_permission.is_null()) | 1215 if (android_permission.is_null()) |
1193 return std::string(); | 1216 return std::string(); |
1194 | 1217 |
1195 return ConvertJavaStringToUTF8(android_permission); | 1218 return ConvertJavaStringToUTF8(android_permission); |
1196 } | 1219 } |
1197 | 1220 |
1198 static void SetSupervisedUserId(JNIEnv* env, | 1221 static void SetSupervisedUserId(JNIEnv* env, |
1199 const JavaParamRef<jobject>& obj, | 1222 const JavaParamRef<jobject>& obj, |
1200 const JavaParamRef<jstring>& pref) { | 1223 const JavaParamRef<jstring>& pref) { |
1201 GetPrefService()->SetString(prefs::kSupervisedUserId, | 1224 GetPrefService()->SetString(prefs::kSupervisedUserId, |
1202 ConvertJavaStringToUTF8(env, pref)); | 1225 ConvertJavaStringToUTF8(env, pref)); |
1203 } | 1226 } |
OLD | NEW |