root/components/language_usage_metrics/language_usage_metrics.cc

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. RecordAcceptLanguage
  2. RecordAcceptLanguages
  3. RecordApplicationLanguage
  4. ToLanguageCode
  5. ParseAcceptLanguages

// 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 = parts.token();
  StringToLowerASCII(&language_part);

  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

/* [<][>][^][v][top][bottom][index][help] */