Update V8 to version 4.7.47.
[chromium-blink-merge.git] / components / language_usage_metrics / language_usage_metrics.cc
blobac44499c53ddea510f40d07e4863f4165e18638f
1 // Copyright 2014 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.
5 #include "components/language_usage_metrics/language_usage_metrics.h"
7 #include <algorithm>
9 #include "base/metrics/sparse_histogram.h"
10 #include "base/strings/string_tokenizer.h"
11 #include "base/strings/string_util.h"
13 namespace {
14 void RecordAcceptLanguage(int language_code) {
15 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.AcceptLanguage",
16 language_code);
18 } // namespace
20 namespace language_usage_metrics {
22 // static
23 void LanguageUsageMetrics::RecordAcceptLanguages(
24 const std::string& accept_languages) {
25 std::set<int> languages;
26 ParseAcceptLanguages(accept_languages, &languages);
27 std::for_each(languages.begin(), languages.end(), RecordAcceptLanguage);
30 // static
31 void LanguageUsageMetrics::RecordApplicationLanguage(
32 const std::string& application_locale) {
33 const int language_code = ToLanguageCode(application_locale);
34 if (language_code != 0)
35 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.ApplicationLanguage",
36 language_code);
39 // static
40 int LanguageUsageMetrics::ToLanguageCode(const std::string& locale) {
41 base::StringTokenizer parts(locale, "-_");
42 if (!parts.GetNext())
43 return 0;
45 std::string language_part = base::ToLowerASCII(parts.token());
47 int language_code = 0;
48 for (std::string::iterator it = language_part.begin();
49 it != language_part.end(); ++it) {
50 char ch = *it;
51 if (ch < 'a' || 'z' < ch)
52 return 0;
54 language_code <<= 8;
55 language_code += ch;
58 return language_code;
61 // static
62 void LanguageUsageMetrics::ParseAcceptLanguages(
63 const std::string& accept_languages,
64 std::set<int>* languages) {
65 languages->clear();
66 base::StringTokenizer locales(accept_languages, ",");
67 while (locales.GetNext()) {
68 const int language_code = ToLanguageCode(locales.token());
69 if (language_code != 0)
70 languages->insert(language_code);
74 } // namespace language_usage_metrics