OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/http/http_util_icu.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <set> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/logging.h" |
| 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/stringprintf.h" |
| 15 #include "third_party/icu/source/common/unicode/uloc.h" |
| 16 |
| 17 namespace net { |
| 18 |
| 19 // The input is a comma separated languages list, this function allows |
| 20 // whitespace between each languages. It will come from the preference and a |
| 21 // user does not manually edit the preference file. The accept languages that |
| 22 // customized in Android Webview from developers are not processed here. |
| 23 std::string HttpUtilIcu::GenerateAcceptLanguageHeader( |
| 24 const std::string& raw_language_list) { |
| 25 std::vector<std::string> locales_list = base::SplitString( |
| 26 raw_language_list, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 27 |
| 28 // If language is not in the accept languages list, also add language |
| 29 // code. A language code should only be inserted after the last |
| 30 // languageTag that contains that language. |
| 31 std::set<std::string> seen_languages; |
| 32 std::vector<std::string> output_list; |
| 33 for (auto it = locales_list.rbegin(); it != locales_list.rend(); ++it) { |
| 34 char locale_ID[ULOC_FULLNAME_CAPACITY] = {}; |
| 35 char language_code[ULOC_LANG_CAPACITY] = {}; |
| 36 |
| 37 UErrorCode error = U_ZERO_ERROR; |
| 38 std::string locale_string = *it; |
| 39 uloc_forLanguageTag(locale_string.c_str(), locale_ID, |
| 40 ULOC_FULLNAME_CAPACITY, nullptr, &error); |
| 41 if (U_FAILURE(error)) { |
| 42 LOG(ERROR) << "Ignoring invalid locale representation " << locale_string; |
| 43 continue; |
| 44 } |
| 45 |
| 46 error = U_ZERO_ERROR; |
| 47 uloc_getLanguage(locale_ID, language_code, ULOC_LANG_CAPACITY, &error); |
| 48 if (U_FAILURE(error)) { |
| 49 LOG(ERROR) << "Ignoring invalid locale representation " << locale_string; |
| 50 continue; |
| 51 } |
| 52 |
| 53 if (seen_languages.find(language_code) == seen_languages.end()) { |
| 54 output_list.push_back(language_code); |
| 55 seen_languages.insert(language_code); |
| 56 } |
| 57 |
| 58 if (language_code != *it) |
| 59 output_list.push_back(locale_string); |
| 60 } |
| 61 |
| 62 std::reverse(output_list.begin(), output_list.end()); |
| 63 |
| 64 // We use integers for q-value and q-value decrement that are 10 times larger |
| 65 // than actual values to avoid a problem with comparing two floating point |
| 66 // numbers. |
| 67 const unsigned int kQvalueDecrement10 = 2; |
| 68 unsigned int qvalue10 = 10; |
| 69 std::string lang_list_with_q; |
| 70 for (unsigned i = 0; i < output_list.size(); i++) { |
| 71 if (qvalue10 == 10) { |
| 72 // q=1.0 is implicit. |
| 73 lang_list_with_q = output_list[i]; |
| 74 } else { |
| 75 DCHECK_LT(qvalue10, 10U); |
| 76 base::StringAppendF(&lang_list_with_q, ",%s;q=0.%d", |
| 77 output_list[i].c_str(), qvalue10); |
| 78 } |
| 79 // It does not make sense to have 'q=0'. |
| 80 if (qvalue10 > kQvalueDecrement10) |
| 81 qvalue10 -= kQvalueDecrement10; |
| 82 } |
| 83 return lang_list_with_q; |
| 84 } |
| 85 } |
OLD | NEW |