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/histogram.h" |
| 10 #include "base/string_tokenizer.h" |
| 11 #include "base/string_util.h" |
| 12 |
| 13 namespace { |
| 14 void RecordAcceptLanguage(Language language) { |
| 15 UMA_HISTOGRAM_ENUMERATION("LanguageUsageMetrics.AcceptLanguage", |
| 16 language, NUM_LANGUAGES); |
| 17 } |
| 18 } // namespace |
| 19 |
| 20 // static |
| 21 void LanguageUsageMetrics::RecordAcceptLanguages( |
| 22 const std::string& accept_languages) { |
| 23 // Rethink about the histogram memory costs when the number of languages |
| 24 // becomes too large. |
| 25 DCHECK_LE(NUM_LANGUAGES, 300); |
| 26 |
| 27 std::set<Language> languages; |
| 28 ParseAcceptLanguages(accept_languages, &languages); |
| 29 std::for_each(languages.begin(), languages.end(), RecordAcceptLanguage); |
| 30 } |
| 31 |
| 32 // static |
| 33 void LanguageUsageMetrics::RecordApplicationLanguage( |
| 34 const std::string& application_locale) { |
| 35 const Language language = ToLanguage(application_locale); |
| 36 if (language != UNKNOWN_LANGUAGE) { |
| 37 UMA_HISTOGRAM_ENUMERATION("LanguageUsageMetrics.ApplicationLanguage", |
| 38 language, NUM_LANGUAGES); |
| 39 } |
| 40 } |
| 41 |
| 42 // static |
| 43 void LanguageUsageMetrics::ParseAcceptLanguages( |
| 44 const std::string& accept_languages, std::set<Language>* languages) { |
| 45 languages->clear(); |
| 46 StringTokenizer locales(accept_languages, ","); |
| 47 while (locales.GetNext()) { |
| 48 const Language language = ToLanguage(locales.token()); |
| 49 if (language != UNKNOWN_LANGUAGE) { |
| 50 languages->insert(language); |
| 51 } |
| 52 } |
| 53 } |
| 54 |
| 55 // static |
| 56 Language LanguageUsageMetrics::ToLanguage(const std::string& locale) { |
| 57 StringTokenizer parts(locale, "-_"); |
| 58 if (!parts.GetNext()) { |
| 59 return UNKNOWN_LANGUAGE; |
| 60 } |
| 61 const std::string language_part = parts.token(); |
| 62 Language language; |
| 63 if (!LanguageFromCode(language_part.c_str(), &language)) { |
| 64 return UNKNOWN_LANGUAGE; |
| 65 } |
| 66 return language; |
| 67 } |
OLD | NEW |