OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/language_usage_metrics.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/metrics/sparse_histogram.h" | |
10 #include "base/strings/string_tokenizer.h" | |
11 #include "base/strings/string_util.h" | |
12 | |
13 namespace { | |
14 void RecordAcceptLanguage(int language_code) { | |
15 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.AcceptLanguage", | |
16 language_code); | |
17 } | |
18 } // namespace | |
19 | |
20 // static | |
21 void LanguageUsageMetrics::RecordAcceptLanguages( | |
22 const std::string& accept_languages) { | |
23 std::set<int> languages; | |
24 ParseAcceptLanguages(accept_languages, &languages); | |
25 std::for_each(languages.begin(), languages.end(), RecordAcceptLanguage); | |
26 } | |
27 | |
28 // static | |
29 void LanguageUsageMetrics::RecordApplicationLanguage( | |
30 const std::string& application_locale) { | |
31 const int language_code = ToLanguageCode(application_locale); | |
32 if (language_code != 0) | |
33 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.ApplicationLanguage", | |
34 language_code); | |
35 } | |
36 | |
37 // static | |
38 int LanguageUsageMetrics::ToLanguageCode(const std::string& locale) { | |
39 base::StringTokenizer parts(locale, "-_"); | |
40 if (!parts.GetNext()) | |
41 return 0; | |
42 | |
43 std::string language_part = parts.token(); | |
44 StringToLowerASCII(&language_part); | |
45 | |
46 int language_code = 0; | |
47 for (std::string::iterator it = language_part.begin(); | |
48 it != language_part.end(); ++it) { | |
49 char ch = *it; | |
50 if (ch < 'a' || 'z' < ch) | |
51 return 0; | |
52 | |
53 language_code <<= 8; | |
54 language_code += ch; | |
55 } | |
56 | |
57 return language_code; | |
58 } | |
59 | |
60 // static | |
61 void LanguageUsageMetrics::ParseAcceptLanguages( | |
62 const std::string& accept_languages, | |
63 std::set<int>* languages) { | |
64 languages->clear(); | |
65 base::StringTokenizer locales(accept_languages, ","); | |
66 while (locales.GetNext()) { | |
67 const int language_code = ToLanguageCode(locales.token()); | |
68 if (language_code != 0) | |
69 languages->insert(language_code); | |
70 } | |
71 } | |
OLD | NEW |