Chromium Code Reviews| 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 namespace http_util_icu { | |
|
Ryan Sleevi
2016/12/15 23:44:07
line break between 17/18 and 18/19
Yirui Huang
2016/12/16 08:00:08
Done.
| |
| 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 GenerateAcceptLanguageHeader(const std::string& raw_language_list) { | |
| 24 std::vector<std::string> locales_list = base::SplitString( | |
| 25 raw_language_list, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
|
Ryan Sleevi
2016/12/15 23:44:07
The previous design was optimized to avoid needing
| |
| 26 | |
| 27 // If language is not in the accept languages list, also add language | |
| 28 // code. A language code should only be inserted after the last | |
| 29 // languageTag that contains that language. | |
| 30 std::set<std::string> seen_languages; | |
|
Ryan Sleevi
2016/12/15 23:44:07
This makes a full copy, and the std::set itself is
| |
| 31 std::vector<std::string> output_list; | |
| 32 for (auto it = locales_list.rbegin(); it != locales_list.rend(); ++it) { | |
|
Ryan Sleevi
2016/12/15 23:44:07
This forces a non-const reference, because the aut
| |
| 33 char locale_ID[ULOC_FULLNAME_CAPACITY] = {}; | |
| 34 char language_code[ULOC_LANG_CAPACITY] = {}; | |
| 35 | |
| 36 UErrorCode error = U_ZERO_ERROR; | |
| 37 std::string locale_string = *it; | |
| 38 uloc_forLanguageTag(locale_string.c_str(), locale_ID, | |
| 39 ULOC_FULLNAME_CAPACITY, nullptr, &error); | |
| 40 if (U_FAILURE(error)) { | |
| 41 LOG(ERROR) << "Ignoring invalid locale representation " << locale_string; | |
| 42 continue; | |
| 43 } | |
| 44 | |
| 45 error = U_ZERO_ERROR; | |
| 46 uloc_getLanguage(locale_ID, language_code, ULOC_LANG_CAPACITY, &error); | |
| 47 if (U_FAILURE(error)) { | |
| 48 LOG(ERROR) << "Ignoring invalid locale representation " << locale_string; | |
| 49 continue; | |
| 50 } | |
| 51 | |
| 52 if (seen_languages.find(language_code) == seen_languages.end()) { | |
| 53 output_list.push_back(language_code); | |
| 54 seen_languages.insert(language_code); | |
| 55 } | |
| 56 | |
| 57 if (language_code != *it) | |
| 58 output_list.push_back(locale_string); | |
| 59 } | |
| 60 | |
| 61 std::reverse(output_list.begin(), output_list.end()); | |
|
Ryan Sleevi
2016/12/15 23:44:07
This is not at all obvious why you reverse here.
| |
| 62 | |
| 63 // We use integers for q-value and q-value decrement that are 10 times larger | |
| 64 // than actual values to avoid a problem with comparing two floating point | |
| 65 // numbers. | |
| 66 const unsigned int kQvalueDecrement10 = 2; | |
| 67 unsigned int qvalue10 = 10; | |
| 68 std::string lang_list_with_q; | |
| 69 for (unsigned i = 0; i < output_list.size(); i++) { | |
| 70 if (qvalue10 == 10) { | |
| 71 // q=1.0 is implicit. | |
| 72 lang_list_with_q = output_list[i]; | |
| 73 } else { | |
| 74 DCHECK_LT(qvalue10, 10U); | |
| 75 base::StringAppendF(&lang_list_with_q, ",%s;q=0.%d", | |
| 76 output_list[i].c_str(), qvalue10); | |
| 77 } | |
| 78 // It does not make sense to have 'q=0'. | |
| 79 if (qvalue10 > kQvalueDecrement10) | |
| 80 qvalue10 -= kQvalueDecrement10; | |
| 81 } | |
| 82 return lang_list_with_q; | |
| 83 } | |
|
Ryan Sleevi
2016/12/15 23:44:07
linebreak between 83/84
Yirui Huang
2016/12/16 08:00:08
Done.
| |
| 84 } | |
|
Ryan Sleevi
2016/12/15 23:44:07
// namespace http_util_icu
Yirui Huang
2016/12/16 08:00:08
Done.
| |
| 85 } | |
|
Ryan Sleevi
2016/12/15 23:44:07
// namespace net
Yirui Huang
2016/12/16 08:00:08
Done.
| |
| OLD | NEW |