blob: 190507fb3997e7487d82897183c7698634835c49 [file] [log] [blame]
[email protected]3f3a65f2014-01-09 19:05:051// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]04261032011-07-21 08:42:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]3f3a65f2014-01-09 19:05:055#include "components/language_usage_metrics/language_usage_metrics.h"
[email protected]04261032011-07-21 08:42:086
7#include <algorithm>
8
[email protected]c2922ad2013-05-20 09:32:589#include "base/metrics/sparse_histogram.h"
[email protected]f4ebe772013-02-02 00:21:3910#include "base/strings/string_tokenizer.h"
[email protected]f9b294362013-06-10 20:22:3111#include "base/strings/string_util.h"
[email protected]04261032011-07-21 08:42:0812
13namespace {
[email protected]c2922ad2013-05-20 09:32:5814void RecordAcceptLanguage(int language_code) {
15 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.AcceptLanguage",
16 language_code);
[email protected]04261032011-07-21 08:42:0817}
18} // namespace
19
[email protected]3f3a65f2014-01-09 19:05:0520namespace language_usage_metrics {
21
[email protected]04261032011-07-21 08:42:0822// static
23void LanguageUsageMetrics::RecordAcceptLanguages(
24 const std::string& accept_languages) {
[email protected]c2922ad2013-05-20 09:32:5825 std::set<int> languages;
[email protected]04261032011-07-21 08:42:0826 ParseAcceptLanguages(accept_languages, &languages);
27 std::for_each(languages.begin(), languages.end(), RecordAcceptLanguage);
28}
29
30// static
31void LanguageUsageMetrics::RecordApplicationLanguage(
32 const std::string& application_locale) {
[email protected]c2922ad2013-05-20 09:32:5833 const int language_code = ToLanguageCode(application_locale);
34 if (language_code != 0)
35 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.ApplicationLanguage",
36 language_code);
[email protected]04261032011-07-21 08:42:0837}
38
39// static
[email protected]c2922ad2013-05-20 09:32:5840int LanguageUsageMetrics::ToLanguageCode(const std::string& locale) {
[email protected]f4ebe772013-02-02 00:21:3941 base::StringTokenizer parts(locale, "-_");
[email protected]c2922ad2013-05-20 09:32:5842 if (!parts.GetNext())
43 return 0;
44
45 std::string language_part = parts.token();
[email protected]cb1f4ac2014-08-07 16:55:4246 base::StringToLowerASCII(&language_part);
[email protected]c2922ad2013-05-20 09:32:5847
48 int language_code = 0;
49 for (std::string::iterator it = language_part.begin();
50 it != language_part.end(); ++it) {
51 char ch = *it;
52 if (ch < 'a' || 'z' < ch)
53 return 0;
54
55 language_code <<= 8;
56 language_code += ch;
[email protected]04261032011-07-21 08:42:0857 }
[email protected]c2922ad2013-05-20 09:32:5858
59 return language_code;
[email protected]04261032011-07-21 08:42:0860}
[email protected]af3de3c2013-05-29 11:26:4861
62// static
63void LanguageUsageMetrics::ParseAcceptLanguages(
64 const std::string& accept_languages,
65 std::set<int>* languages) {
66 languages->clear();
67 base::StringTokenizer locales(accept_languages, ",");
68 while (locales.GetNext()) {
69 const int language_code = ToLanguageCode(locales.token());
70 if (language_code != 0)
71 languages->insert(language_code);
72 }
73}
[email protected]3f3a65f2014-01-09 19:05:0574
75} // namespace language_usage_metrics