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

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

Issue 2559243003: Extend with a language code in http accept languages
Patch Set: Rebased, fixed nits and handed over this CL :( Created 4 years 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>
(...skipping 1120 matching lines...) Expand 10 before | Expand all | Expand 10 after
1131 // language tags (BCP47 compliant format). Each language tag contains a language 1131 // language tags (BCP47 compliant format). Each language tag contains a language
1132 // code and a country code or a language code only. 1132 // code and a country code or a language code only.
1133 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( 1133 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
1134 const std::string& locales, 1134 const std::string& locales,
1135 std::string* accept_languages) { 1135 std::string* accept_languages) {
1136 std::vector<std::string> locale_list = 1136 std::vector<std::string> locale_list =
1137 base::SplitString(locales + "," + *accept_languages, ",", 1137 base::SplitString(locales + "," + *accept_languages, ",",
1138 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 1138 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
1139 1139
1140 std::set<std::string> seen_tags; 1140 std::set<std::string> seen_tags;
1141 std::vector<std::pair<std::string, std::string>> unique_locale_list; 1141 std::vector<std::string> unique_locale_list;
1142 for (const std::string& locale_str : locale_list) { 1142 for (const std::string& locale_str : locale_list) {
1143 char locale_ID[ULOC_FULLNAME_CAPACITY] = {}; 1143 char locale_ID[ULOC_FULLNAME_CAPACITY] = {};
1144 char language_code_buffer[ULOC_LANG_CAPACITY] = {}; 1144 char language_code_buffer[ULOC_LANG_CAPACITY] = {};
1145 char country_code_buffer[ULOC_COUNTRY_CAPACITY] = {}; 1145 char country_code_buffer[ULOC_COUNTRY_CAPACITY] = {};
1146 1146
1147 UErrorCode error = U_ZERO_ERROR; 1147 UErrorCode error = U_ZERO_ERROR;
1148 uloc_forLanguageTag(locale_str.c_str(), locale_ID, ULOC_FULLNAME_CAPACITY, 1148 uloc_forLanguageTag(locale_str.c_str(), locale_ID, ULOC_FULLNAME_CAPACITY,
1149 nullptr, &error); 1149 nullptr, &error);
1150 if (U_FAILURE(error)) { 1150 if (U_FAILURE(error)) {
1151 LOG(ERROR) << "Ignoring invalid locale representation " << locale_str; 1151 LOG(ERROR) << "Ignoring invalid locale representation " << locale_str;
(...skipping 17 matching lines...) Expand all
1169 } 1169 }
1170 1170
1171 std::string language_code(language_code_buffer); 1171 std::string language_code(language_code_buffer);
1172 std::string country_code(country_code_buffer); 1172 std::string country_code(country_code_buffer);
1173 std::string language_tag(language_code + "-" + country_code); 1173 std::string language_tag(language_code + "-" + country_code);
1174 1174
1175 if (seen_tags.find(language_tag) != seen_tags.end()) 1175 if (seen_tags.find(language_tag) != seen_tags.end())
1176 continue; 1176 continue;
1177 1177
1178 seen_tags.insert(language_tag); 1178 seen_tags.insert(language_tag);
1179 unique_locale_list.push_back(std::make_pair(language_code, country_code)); 1179 if (!country_code.empty()) {
1180 unique_locale_list.push_back(language_tag);
1181 } else {
1182 unique_locale_list.push_back(language_code);
1183 }
1180 } 1184 }
1181 1185 *accept_languages = base::JoinString(unique_locale_list, ",");
1182 // If language is not in the accept languages list, also add language
1183 // code. A language code should only be inserted after the last
1184 // languageTag that contains that language.
1185 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
1186 // with Chrome but may fail on arbitrary lists of language tags due to
1187 // differences in case and whitespace.
1188 std::set<std::string> seen_languages;
1189 std::vector<std::string> output_list;
1190 for (auto it = unique_locale_list.rbegin(); it != unique_locale_list.rend();
1191 ++it) {
1192 if (seen_languages.find(it->first) == seen_languages.end()) {
1193 output_list.push_back(it->first);
1194 seen_languages.insert(it->first);
1195 }
1196 if (!it->second.empty())
1197 output_list.push_back(it->first + "-" + it->second);
1198 }
1199
1200 std::reverse(output_list.begin(), output_list.end());
1201 *accept_languages = base::JoinString(output_list, ",");
1202 } 1186 }
1203 1187
1204 // static 1188 // static
1205 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( 1189 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting(
1206 ContentSettingsType content_type) { 1190 ContentSettingsType content_type) {
1207 JNIEnv* env = AttachCurrentThread(); 1191 JNIEnv* env = AttachCurrentThread();
1208 base::android::ScopedJavaLocalRef<jstring> android_permission = 1192 base::android::ScopedJavaLocalRef<jstring> android_permission =
1209 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( 1193 Java_PrefServiceBridge_getAndroidPermissionForContentSetting(
1210 env, content_type); 1194 env, content_type);
1211 if (android_permission.is_null()) 1195 if (android_permission.is_null())
1212 return std::string(); 1196 return std::string();
1213 1197
1214 return ConvertJavaStringToUTF8(android_permission); 1198 return ConvertJavaStringToUTF8(android_permission);
1215 } 1199 }
1216 1200
1217 static void SetSupervisedUserId(JNIEnv* env, 1201 static void SetSupervisedUserId(JNIEnv* env,
1218 const JavaParamRef<jobject>& obj, 1202 const JavaParamRef<jobject>& obj,
1219 const JavaParamRef<jstring>& pref) { 1203 const JavaParamRef<jstring>& pref) {
1220 GetPrefService()->SetString(prefs::kSupervisedUserId, 1204 GetPrefService()->SetString(prefs::kSupervisedUserId,
1221 ConvertJavaStringToUTF8(env, pref)); 1205 ConvertJavaStringToUTF8(env, pref));
1222 } 1206 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698