summaryrefslogtreecommitdiffstats
path: root/components/language_usage_metrics/language_usage_metrics.cc
diff options
context:
space:
mode:
authordroger@chromium.org <droger@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-09 19:05:05 +0000
committerdroger@chromium.org <droger@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-09 19:05:05 +0000
commit3f3a65fe54ba1ffad797a44401ffd42d9de9ffce (patch)
treeb4ef7b030c6c6a181dd222c55d108b18be42e053 /components/language_usage_metrics/language_usage_metrics.cc
parentcaace87499af099ae2bc43d03eb9444200bee2e9 (diff)
downloadchromium_src-3f3a65fe54ba1ffad797a44401ffd42d9de9ffce.zip
chromium_src-3f3a65fe54ba1ffad797a44401ffd42d9de9ffce.tar.gz
chromium_src-3f3a65fe54ba1ffad797a44401ffd42d9de9ffce.tar.bz2
Move LanguageUsageMetrics and TranslateBrowserMetrics to components
LanguageUsageMetrics is moved to its own component because it is used both in the translate component and directly in the browser. TranslateBrowserMetrics is moved to the translate component. BUG=331509 TBR=jochen Review URL: https://codereview.chromium.org/131203002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243946 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/language_usage_metrics/language_usage_metrics.cc')
-rw-r--r--components/language_usage_metrics/language_usage_metrics.cc75
1 files changed, 75 insertions, 0 deletions
diff --git a/components/language_usage_metrics/language_usage_metrics.cc b/components/language_usage_metrics/language_usage_metrics.cc
new file mode 100644
index 0000000..adb91c9
--- /dev/null
+++ b/components/language_usage_metrics/language_usage_metrics.cc
@@ -0,0 +1,75 @@
+// 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