OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/language_usage_metrics.h" | 5 #include "chrome/browser/language_usage_metrics.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/metrics/sparse_histogram.h" | 9 #include "base/metrics/sparse_histogram.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
Takashi Toyoshima
2013/05/21 11:11:43
can you remove this?
hajimehoshi
2013/05/22 04:19:27
Done.
| |
11 #include "base/strings/string_tokenizer.h" | 11 #include "base/strings/string_tokenizer.h" |
12 #include "chrome/common/metrics/metrics_util.h" | |
12 | 13 |
13 namespace { | 14 namespace { |
14 void RecordAcceptLanguage(int language_code) { | 15 void RecordAcceptLanguage(int language_code) { |
15 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.AcceptLanguage", | 16 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.AcceptLanguage", |
16 language_code); | 17 language_code); |
17 } | 18 } |
18 } // namespace | 19 } // namespace |
19 | 20 |
20 // static | 21 // static |
21 void LanguageUsageMetrics::RecordAcceptLanguages( | 22 void LanguageUsageMetrics::RecordAcceptLanguages( |
22 const std::string& accept_languages) { | 23 const std::string& accept_languages) { |
23 std::set<int> languages; | 24 std::set<int> languages; |
24 ParseAcceptLanguages(accept_languages, &languages); | 25 ParseAcceptLanguages(accept_languages, &languages); |
25 std::for_each(languages.begin(), languages.end(), RecordAcceptLanguage); | 26 std::for_each(languages.begin(), languages.end(), RecordAcceptLanguage); |
26 } | 27 } |
27 | 28 |
28 // static | 29 // static |
29 void LanguageUsageMetrics::RecordApplicationLanguage( | 30 void LanguageUsageMetrics::RecordApplicationLanguage( |
30 const std::string& application_locale) { | 31 const std::string& application_locale) { |
31 const int language_code = ToLanguageCode(application_locale); | 32 const int language_code = metrics::ToLanguageCode(application_locale); |
32 if (language_code != 0) | 33 if (language_code != 0) |
33 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.ApplicationLanguage", | 34 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.ApplicationLanguage", |
34 language_code); | 35 language_code); |
35 } | 36 } |
36 | 37 |
37 // static | 38 // static |
38 void LanguageUsageMetrics::ParseAcceptLanguages( | 39 void LanguageUsageMetrics::ParseAcceptLanguages( |
39 const std::string& accept_languages, | 40 const std::string& accept_languages, |
40 std::set<int>* languages) { | 41 std::set<int>* languages) { |
41 languages->clear(); | 42 languages->clear(); |
42 base::StringTokenizer locales(accept_languages, ","); | 43 base::StringTokenizer locales(accept_languages, ","); |
43 while (locales.GetNext()) { | 44 while (locales.GetNext()) { |
44 const int language_code = ToLanguageCode(locales.token()); | 45 const int language_code = metrics::ToLanguageCode(locales.token()); |
45 if (language_code != 0) | 46 if (language_code != 0) |
46 languages->insert(language_code); | 47 languages->insert(language_code); |
47 } | 48 } |
48 } | 49 } |
49 | |
50 // static | |
51 int LanguageUsageMetrics::ToLanguageCode(const std::string& locale) { | |
52 base::StringTokenizer parts(locale, "-_"); | |
53 if (!parts.GetNext()) | |
54 return 0; | |
55 | |
56 std::string language_part = parts.token(); | |
57 StringToLowerASCII(&language_part); | |
58 | |
59 int language_code = 0; | |
60 for (std::string::iterator it = language_part.begin(); | |
61 it != language_part.end(); ++it) { | |
62 char ch = *it; | |
63 if (ch < 'a' || 'z' < ch) | |
64 return 0; | |
65 | |
66 language_code <<= 8; | |
67 language_code += ch; | |
68 } | |
69 | |
70 return language_code; | |
71 } | |
OLD | NEW |