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

Side by Side Diff: net/http/http_util_icu.cc

Issue 2559243003: Extend with a language code in http accept languages
Patch Set: Removed unnecessary imports and checked develop path 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 <vector>
10
11 #include "base/logging.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/stringprintf.h"
14 #include "third_party/icu/source/common/unicode/uloc.h"
15
16 namespace net {
17
18 // The input is a comma separated languages list, this function allows
19 // whitespace between each languages. It will come from the preference and a
20 // user does not manually edit the preference file. The accept languages that
21 // customixed from developers are not processed here.
Seigo Nonaka 2016/12/12 07:29:26 nit: /customixed/customized/ And it may be good to
Yirui Huang 2016/12/12 07:41:35 Modified with Android Webview. "+ including those
22 std::string HttpUtilIcu::GenerateAcceptLanguageHeader(
23 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);
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;
31 std::vector<std::string> output_list;
32 for (auto it = locales_list.rbegin(); it != locales_list.rend(); ++it) {
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());
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 }
84 }
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