blob: ac44499c53ddea510f40d07e4863f4165e18638f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/language_usage_metrics/language_usage_metrics.h"
#include <algorithm>
#include "base/metrics/sparse_histogram.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
namespace {
void RecordAcceptLanguage(int language_code) {
UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.AcceptLanguage",
language_code);
}
} // namespace
namespace language_usage_metrics {
// static
void LanguageUsageMetrics::RecordAcceptLanguages(
const std::string& accept_languages) {
std::set<int> languages;
ParseAcceptLanguages(accept_languages, &languages);
std::for_each(languages.begin(), languages.end(), RecordAcceptLanguage);
}
// static
void LanguageUsageMetrics::RecordApplicationLanguage(
const std::string& application_locale) {
const int language_code = ToLanguageCode(application_locale);
if (language_code != 0)
UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.ApplicationLanguage",
language_code);
}
// static
int LanguageUsageMetrics::ToLanguageCode(const std::string& locale) {
base::StringTokenizer parts(locale, "-_");
if (!parts.GetNext())
return 0;
std::string language_part = base::ToLowerASCII(parts.token());
int language_code = 0;
for (std::string::iterator it = language_part.begin();
it != language_part.end(); ++it) {
char ch = *it;
if (ch < 'a' || 'z' < ch)
return 0;
language_code <<= 8;
language_code += ch;
}
return language_code;
}
// static
void LanguageUsageMetrics::ParseAcceptLanguages(
const std::string& accept_languages,
std::set<int>* languages) {
languages->clear();
base::StringTokenizer locales(accept_languages, ",");
while (locales.GetNext()) {
const int language_code = ToLanguageCode(locales.token());
if (language_code != 0)
languages->insert(language_code);
}
}
} // namespace language_usage_metrics
|