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

Side by Side Diff: net/http/http_util_icu.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
« no previous file with comments | « net/http/http_util_icu.h ('k') | net/http/http_util_icu_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 namespace http_util_icu {
20
21 // The input is a comma separated languages list, this function allows
22 // whitespace between each languages. It will come from the preference and a
23 // user does not manually edit the preference file. The accept languages that
24 // customized in Android Webview from developers are not processed here.
25 std::string GenerateAcceptLanguageHeader(const std::string& raw_language_list) {
26 std::vector<std::string> locales_list = base::SplitString(
27 raw_language_list, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
28
29 // If language is not in the accept languages list, also add language
30 // code. A language code should only be inserted after the last
31 // languageTag that contains that language.
32 std::set<std::string> seen_languages;
33 std::vector<std::string> output_list;
34 for (auto it = locales_list.rbegin(); it != locales_list.rend(); ++it) {
35 char locale_ID[ULOC_FULLNAME_CAPACITY] = {};
36 char language_code[ULOC_LANG_CAPACITY] = {};
37
38 UErrorCode error = U_ZERO_ERROR;
39 std::string locale_string = *it;
40 uloc_forLanguageTag(locale_string.c_str(), locale_ID,
41 ULOC_FULLNAME_CAPACITY, nullptr, &error);
42 if (U_FAILURE(error)) {
43 LOG(ERROR) << "Ignoring invalid locale representation " << locale_string;
44 continue;
45 }
46
47 error = U_ZERO_ERROR;
48 uloc_getLanguage(locale_ID, language_code, ULOC_LANG_CAPACITY, &error);
49 if (U_FAILURE(error)) {
50 LOG(ERROR) << "Ignoring invalid locale representation " << locale_string;
51 continue;
52 }
53
54 if (seen_languages.find(language_code) == seen_languages.end()) {
55 output_list.push_back(language_code);
56 seen_languages.insert(language_code);
57 }
58
59 if (language_code != *it)
60 output_list.push_back(locale_string);
61 }
62
63 std::reverse(output_list.begin(), output_list.end());
64
65 // We use integers for q-value and q-value decrement that are 10 times larger
66 // than actual values to avoid a problem with comparing two floating point
67 // numbers.
68 const unsigned int kQvalueDecrement10 = 2;
69 unsigned int qvalue10 = 10;
70 std::string lang_list_with_q;
71 for (unsigned i = 0; i < output_list.size(); i++) {
72 if (qvalue10 == 10) {
73 // q=1.0 is implicit.
74 lang_list_with_q = output_list[i];
75 } else {
76 DCHECK_LT(qvalue10, 10U);
77 base::StringAppendF(&lang_list_with_q, ",%s;q=0.%d",
78 output_list[i].c_str(), qvalue10);
79 }
80 // It does not make sense to have 'q=0'.
81 if (qvalue10 > kQvalueDecrement10)
82 qvalue10 -= kQvalueDecrement10;
83 }
84 return lang_list_with_q;
85 }
86
87 } // namespace http_util_icu
88
89 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_util_icu.h ('k') | net/http/http_util_icu_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698