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

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

Issue 2559243003: Extend with a language code in http accept languages
Patch Set: Rebased separate Generate AcceptLanguage Header from http_util 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
(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>
Seigo Nonaka 2016/12/12 07:29:26 string is necessary for std::string
Yirui Huang 2016/12/12 07:41:35 Done.
10 #include <vector>
11
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_tokenizer.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/time/time.h"
Seigo Nonaka 2016/12/12 01:48:54 probably, some of headers are not necessary, e.g.
Yirui Huang 2016/12/12 06:50:41 Done.
20 #include "net/base/url_util.h"
21 #include "third_party/icu/source/common/unicode/uloc.h"
22
23 namespace net {
24
25 // The input is a comma separated languages list, this function allows
26 // whitespace between each languages. As long as it comes from the preference
27 // and a user does not manually edit the preference file, it's the case.
28 // TODO(yirui): We still need to be aware of cases, make it more robust.
Seigo Nonaka 2016/12/12 01:48:54 AndroidWebView is the only case that the developer
Yirui Huang 2016/12/12 06:50:40 The change from developers will not use http_util,
29 std::string HttpUtilIcu::GenerateAcceptLanguageHeader(
30 const std::string& raw_language_list) {
31 std::vector<std::string> locales_list = base::SplitString(
32 raw_language_list, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
33
34 // If language is not in the accept languages list, also add language
35 // code. A language code should only be inserted after the last
36 // languageTag that contains that language.
37 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
Seigo Nonaka 2016/12/12 01:48:54 I think this comment is no longer necessary.
Yirui Huang 2016/12/12 06:50:41 Done.
38 // with Chrome but may fail on arbitrary lists of language tags due to
39 // differences in case and whitespace.
40 std::set<std::string> seen_languages;
41 std::vector<std::string> output_list;
42 for (auto it = locales_list.rbegin(); it != locales_list.rend(); ++it) {
43 char locale_ID[ULOC_FULLNAME_CAPACITY] = {};
44 char language_code[ULOC_LANG_CAPACITY] = {};
45
46 UErrorCode error = U_ZERO_ERROR;
47 std::string locale_string = *it;
48 uloc_forLanguageTag(locale_string.c_str(), locale_ID,
49 ULOC_FULLNAME_CAPACITY, nullptr, &error);
50 if (U_FAILURE(error)) {
51 LOG(ERROR) << "Ignoring invalid locale representation " << locale_string;
52 continue;
53 }
54
55 error = U_ZERO_ERROR;
56 uloc_getLanguage(locale_ID, language_code, ULOC_LANG_CAPACITY, &error);
57 if (U_FAILURE(error)) {
58 LOG(ERROR) << "Ignoring invalid locale representation " << locale_string;
59 continue;
60 }
61
62 if (seen_languages.find(language_code) == seen_languages.end()) {
63 output_list.push_back(language_code);
64 seen_languages.insert(language_code);
65 }
66
67 if (language_code != *it)
68 output_list.push_back(locale_string);
69 }
70
71 std::reverse(output_list.begin(), output_list.end());
72 std::string final_language_list = base::JoinString(output_list, ",");
73
74 // We use integers for qvalue and qvalue decrement that are 10 times
75 // larger than actual values to avoid a problem with comparing
76 // two floating point numbers.
77 const unsigned int kQvalueDecrement10 = 2;
78 unsigned int qvalue10 = 10;
79 base::StringTokenizer t(final_language_list, ",");
80 std::string lang_list_with_q;
81 while (t.GetNext()) {
Seigo Nonaka 2016/12/12 01:48:54 Let's use output_list here. You joined string then
Yirui Huang 2016/12/12 06:50:40 Done.
82 std::string language = t.token();
83 if (qvalue10 == 10) {
84 // q=1.0 is implicit.
85 lang_list_with_q = language;
86 } else {
87 DCHECK_LT(qvalue10, 10U);
88 base::StringAppendF(&lang_list_with_q, ",%s;q=0.%d", language.c_str(),
89 qvalue10);
90 }
91 // It does not make sense to have 'q=0'.
92 if (qvalue10 > kQvalueDecrement10)
93 qvalue10 -= kQvalueDecrement10;
94 }
95 return lang_list_with_q;
96 }
97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698