summaryrefslogtreecommitdiffstats
path: root/chrome/browser/language_usage_metrics.cc
blob: b53c25f88a5e7bc2477f45c9f4c43c729360e0e8 (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
// Copyright (c) 2011 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 "chrome/browser/language_usage_metrics.h"

#include <algorithm>

#include "base/metrics/histogram.h"
#include "base/string_tokenizer.h"
#include "base/string_util.h"

namespace {
void RecordAcceptLanguage(Language language) {
  UMA_HISTOGRAM_ENUMERATION("LanguageUsageMetrics.AcceptLanguage",
                            language, NUM_LANGUAGES);
}
}  // namespace

// static
void LanguageUsageMetrics::RecordAcceptLanguages(
    const std::string& accept_languages) {
  // Rethink about the histogram memory costs when the number of languages
  // becomes too large.
  DCHECK_LE(NUM_LANGUAGES, 300);

  std::set<Language> languages;
  ParseAcceptLanguages(accept_languages, &languages);
  std::for_each(languages.begin(), languages.end(), RecordAcceptLanguage);
}

// static
void LanguageUsageMetrics::RecordApplicationLanguage(
    const std::string& application_locale) {
  const Language language = ToLanguage(application_locale);
  if (language != UNKNOWN_LANGUAGE) {
    UMA_HISTOGRAM_ENUMERATION("LanguageUsageMetrics.ApplicationLanguage",
                              language, NUM_LANGUAGES);
  }
}

// static
void LanguageUsageMetrics::ParseAcceptLanguages(
    const std::string& accept_languages, std::set<Language>* languages) {
  languages->clear();
  StringTokenizer locales(accept_languages, ",");
  while (locales.GetNext()) {
    const Language language = ToLanguage(locales.token());
    if (language != UNKNOWN_LANGUAGE) {
      languages->insert(language);
    }
  }
}

// static
Language LanguageUsageMetrics::ToLanguage(const std::string& locale) {
  StringTokenizer parts(locale, "-_");
  if (!parts.GetNext()) {
    return UNKNOWN_LANGUAGE;
  }
  const std::string language_part = parts.token();
  Language language;
  if (!LanguageFromCode(language_part.c_str(), &language)) {
    return UNKNOWN_LANGUAGE;
  }
  return language;
}