1 // Copyright (c) 2011 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 "chrome/browser/language_usage_metrics.h"
9 #include "base/metrics/histogram.h"
10 #include "base/string_tokenizer.h"
11 #include "base/string_util.h"
14 void RecordAcceptLanguage(Language language
) {
15 UMA_HISTOGRAM_ENUMERATION("LanguageUsageMetrics.AcceptLanguage",
16 language
, NUM_LANGUAGES
);
21 void LanguageUsageMetrics::RecordAcceptLanguages(
22 const std::string
& accept_languages
) {
23 // Rethink about the histogram memory costs when the number of languages
25 DCHECK_LE(NUM_LANGUAGES
, 300);
27 std::set
<Language
> languages
;
28 ParseAcceptLanguages(accept_languages
, &languages
);
29 std::for_each(languages
.begin(), languages
.end(), RecordAcceptLanguage
);
33 void LanguageUsageMetrics::RecordApplicationLanguage(
34 const std::string
& application_locale
) {
35 const Language language
= ToLanguage(application_locale
);
36 if (language
!= UNKNOWN_LANGUAGE
) {
37 UMA_HISTOGRAM_ENUMERATION("LanguageUsageMetrics.ApplicationLanguage",
38 language
, NUM_LANGUAGES
);
43 void LanguageUsageMetrics::ParseAcceptLanguages(
44 const std::string
& accept_languages
, std::set
<Language
>* languages
) {
46 StringTokenizer
locales(accept_languages
, ",");
47 while (locales
.GetNext()) {
48 const Language language
= ToLanguage(locales
.token());
49 if (language
!= UNKNOWN_LANGUAGE
) {
50 languages
->insert(language
);
56 Language
LanguageUsageMetrics::ToLanguage(const std::string
& locale
) {
57 StringTokenizer
parts(locale
, "-_");
58 if (!parts
.GetNext()) {
59 return UNKNOWN_LANGUAGE
;
61 const std::string language_part
= parts
.token();
63 if (!LanguageFromCode(language_part
.c_str(), &language
)) {
64 return UNKNOWN_LANGUAGE
;