| 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" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 // static | 28 // static |
| 29 void LanguageUsageMetrics::RecordApplicationLanguage( | 29 void LanguageUsageMetrics::RecordApplicationLanguage( |
| 30 const std::string& application_locale) { | 30 const std::string& application_locale) { |
| 31 const int language_code = ToLanguageCode(application_locale); | 31 const int language_code = ToLanguageCode(application_locale); |
| 32 if (language_code != 0) | 32 if (language_code != 0) |
| 33 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.ApplicationLanguage", | 33 UMA_HISTOGRAM_SPARSE_SLOWLY("LanguageUsage.ApplicationLanguage", |
| 34 language_code); | 34 language_code); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // static | |
| 38 void LanguageUsageMetrics::ParseAcceptLanguages( | |
| 39 const std::string& accept_languages, | |
| 40 std::set<int>* languages) { | |
| 41 languages->clear(); | |
| 42 base::StringTokenizer locales(accept_languages, ","); | |
| 43 while (locales.GetNext()) { | |
| 44 const int language_code = ToLanguageCode(locales.token()); | |
| 45 if (language_code != 0) | |
| 46 languages->insert(language_code); | |
| 47 } | |
| 48 } | |
| 49 | 37 |
| 50 // static | 38 // static |
| 51 int LanguageUsageMetrics::ToLanguageCode(const std::string& locale) { | 39 int LanguageUsageMetrics::ToLanguageCode(const std::string& locale) { |
| 52 base::StringTokenizer parts(locale, "-_"); | 40 base::StringTokenizer parts(locale, "-_"); |
| 53 if (!parts.GetNext()) | 41 if (!parts.GetNext()) |
| 54 return 0; | 42 return 0; |
| 55 | 43 |
| 56 std::string language_part = parts.token(); | 44 std::string language_part = parts.token(); |
| 57 StringToLowerASCII(&language_part); | 45 StringToLowerASCII(&language_part); |
| 58 | 46 |
| 59 int language_code = 0; | 47 int language_code = 0; |
| 60 for (std::string::iterator it = language_part.begin(); | 48 for (std::string::iterator it = language_part.begin(); |
| 61 it != language_part.end(); ++it) { | 49 it != language_part.end(); ++it) { |
| 62 char ch = *it; | 50 char ch = *it; |
| 63 if (ch < 'a' || 'z' < ch) | 51 if (ch < 'a' || 'z' < ch) |
| 64 return 0; | 52 return 0; |
| 65 | 53 |
| 66 language_code <<= 8; | 54 language_code <<= 8; |
| 67 language_code += ch; | 55 language_code += ch; |
| 68 } | 56 } |
| 69 | 57 |
| 70 return language_code; | 58 return language_code; |
| 71 } | 59 } |
| 60 |
| 61 // static |
| 62 void LanguageUsageMetrics::ParseAcceptLanguages( |
| 63 const std::string& accept_languages, |
| 64 std::set<int>* languages) { |
| 65 languages->clear(); |
| 66 base::StringTokenizer locales(accept_languages, ","); |
| 67 while (locales.GetNext()) { |
| 68 const int language_code = ToLanguageCode(locales.token()); |
| 69 if (language_code != 0) |
| 70 languages->insert(language_code); |
| 71 } |
| 72 } |
| OLD | NEW |