| 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 17 matching lines...) Expand all Loading... |
| 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 | 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 | |
| 50 // static | |
| 51 int LanguageUsageMetrics::ToLanguageCode(const std::string& locale) { | 38 int LanguageUsageMetrics::ToLanguageCode(const std::string& locale) { |
| 52 base::StringTokenizer parts(locale, "-_"); | 39 base::StringTokenizer parts(locale, "-_"); |
| 53 if (!parts.GetNext()) | 40 if (!parts.GetNext()) |
| 54 return 0; | 41 return 0; |
| 55 | 42 |
| 56 std::string language_part = parts.token(); | 43 std::string language_part = parts.token(); |
| 57 StringToLowerASCII(&language_part); | 44 StringToLowerASCII(&language_part); |
| 58 | 45 |
| 59 int language_code = 0; | 46 int language_code = 0; |
| 60 for (std::string::iterator it = language_part.begin(); | 47 for (std::string::iterator it = language_part.begin(); |
| 61 it != language_part.end(); ++it) { | 48 it != language_part.end(); ++it) { |
| 62 char ch = *it; | 49 char ch = *it; |
| 63 if (ch < 'a' || 'z' < ch) | 50 if (ch < 'a' || 'z' < ch) |
| 64 return 0; | 51 return 0; |
| 65 | 52 |
| 66 language_code <<= 8; | 53 language_code <<= 8; |
| 67 language_code += ch; | 54 language_code += ch; |
| 68 } | 55 } |
| 69 | 56 |
| 70 return language_code; | 57 return language_code; |
| 71 } | 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 |