| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/translate/core/browser/translate_prefs.h" | 5 #include "components/translate/core/browser/translate_prefs.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_piece.h" |
| 10 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 13 #include "components/pref_registry/pref_registry_syncable.h" | 14 #include "components/pref_registry/pref_registry_syncable.h" |
| 14 #include "components/prefs/pref_service.h" | 15 #include "components/prefs/pref_service.h" |
| 15 #include "components/prefs/scoped_user_pref_update.h" | 16 #include "components/prefs/scoped_user_pref_update.h" |
| 16 #include "components/translate/core/browser/translate_accept_languages.h" | 17 #include "components/translate/core/browser/translate_accept_languages.h" |
| 17 #include "components/translate/core/browser/translate_download_manager.h" | 18 #include "components/translate/core/browser/translate_download_manager.h" |
| 18 #include "components/translate/core/browser/translate_experiment.h" | 19 #include "components/translate/core/browser/translate_experiment.h" |
| 19 #include "components/translate/core/common/translate_util.h" | 20 #include "components/translate/core/common/translate_util.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 // | 54 // |
| 54 // * translate_last_denied_time | 55 // * translate_last_denied_time |
| 55 // * translate_too_often_denied | 56 // * translate_too_often_denied |
| 56 // * translate_language_blacklist | 57 // * translate_language_blacklist |
| 57 | 58 |
| 58 namespace { | 59 namespace { |
| 59 | 60 |
| 60 // Expands language codes to make these more suitable for Accept-Language. | 61 // Expands language codes to make these more suitable for Accept-Language. |
| 61 // Example: ['en-US', 'ja', 'en-CA'] => ['en-US', 'en', 'ja', 'en-CA']. | 62 // Example: ['en-US', 'ja', 'en-CA'] => ['en-US', 'en', 'ja', 'en-CA']. |
| 62 // 'en' won't appear twice as this function eliminates duplicates. | 63 // 'en' won't appear twice as this function eliminates duplicates. |
| 64 // The StringPieces in |expanded_languages| are references to the strings in |
| 65 // |languages|. |
| 63 void ExpandLanguageCodes(const std::vector<std::string>& languages, | 66 void ExpandLanguageCodes(const std::vector<std::string>& languages, |
| 64 std::vector<std::string>* expanded_languages) { | 67 std::vector<base::StringPiece>* expanded_languages) { |
| 65 DCHECK(expanded_languages); | 68 DCHECK(expanded_languages); |
| 66 DCHECK(expanded_languages->empty()); | 69 DCHECK(expanded_languages->empty()); |
| 67 | 70 |
| 68 // used to eliminate duplicates. | 71 // used to eliminate duplicates. |
| 69 std::set<std::string> seen; | 72 std::set<base::StringPiece> seen; |
| 70 | 73 |
| 71 for (std::vector<std::string>::const_iterator it = languages.begin(); | 74 for (const auto& language : languages) { |
| 72 it != languages.end(); ++it) { | |
| 73 const std::string& language = *it; | |
| 74 if (seen.find(language) == seen.end()) { | 75 if (seen.find(language) == seen.end()) { |
| 75 expanded_languages->push_back(language); | 76 expanded_languages->push_back(language); |
| 76 seen.insert(language); | 77 seen.insert(language); |
| 77 } | 78 } |
| 78 | 79 |
| 79 std::vector<std::string> tokens = base::SplitString( | 80 std::vector<base::StringPiece> tokens = base::SplitStringPiece( |
| 80 language, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 81 language, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 81 if (tokens.size() == 0) | 82 if (tokens.size() == 0) |
| 82 continue; | 83 continue; |
| 83 const std::string& main_part = tokens[0]; | 84 |
| 85 base::StringPiece main_part = tokens[0]; |
| 84 if (seen.find(main_part) == seen.end()) { | 86 if (seen.find(main_part) == seen.end()) { |
| 85 expanded_languages->push_back(main_part); | 87 expanded_languages->push_back(main_part); |
| 86 seen.insert(main_part); | 88 seen.insert(main_part); |
| 87 } | 89 } |
| 88 } | 90 } |
| 89 } | 91 } |
| 90 | 92 |
| 91 } // namespace | 93 } // namespace |
| 92 | 94 |
| 93 const base::Feature kTranslateUI2016Q2{"TranslateUI2016Q2", | 95 const base::Feature kTranslateUI2016Q2{"TranslateUI2016Q2", |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 void TranslatePrefs::UpdateLanguageList( | 411 void TranslatePrefs::UpdateLanguageList( |
| 410 const std::vector<std::string>& languages) { | 412 const std::vector<std::string>& languages) { |
| 411 #if defined(OS_CHROMEOS) | 413 #if defined(OS_CHROMEOS) |
| 412 std::string languages_str = base::JoinString(languages, ","); | 414 std::string languages_str = base::JoinString(languages, ","); |
| 413 prefs_->SetString(preferred_languages_pref_.c_str(), languages_str); | 415 prefs_->SetString(preferred_languages_pref_.c_str(), languages_str); |
| 414 #endif | 416 #endif |
| 415 | 417 |
| 416 // Save the same language list as accept languages preference as well, but we | 418 // Save the same language list as accept languages preference as well, but we |
| 417 // need to expand the language list, to make it more acceptable. For instance, | 419 // need to expand the language list, to make it more acceptable. For instance, |
| 418 // some web sites don't understand 'en-US' but 'en'. See crosbug.com/9884. | 420 // some web sites don't understand 'en-US' but 'en'. See crosbug.com/9884. |
| 419 std::vector<std::string> accept_languages; | 421 std::vector<base::StringPiece> accept_languages; |
| 420 ExpandLanguageCodes(languages, &accept_languages); | 422 ExpandLanguageCodes(languages, &accept_languages); |
| 421 std::string accept_languages_str = base::JoinString(accept_languages, ","); | 423 std::string accept_languages_str = base::JoinString(accept_languages, ","); |
| 422 prefs_->SetString(accept_languages_pref_.c_str(), accept_languages_str); | 424 prefs_->SetString(accept_languages_pref_.c_str(), accept_languages_str); |
| 423 } | 425 } |
| 424 | 426 |
| 425 bool TranslatePrefs::CanTranslateLanguage( | 427 bool TranslatePrefs::CanTranslateLanguage( |
| 426 TranslateAcceptLanguages* accept_languages, | 428 TranslateAcceptLanguages* accept_languages, |
| 427 const std::string& language) { | 429 const std::string& language) { |
| 428 bool can_be_accept_language = | 430 bool can_be_accept_language = |
| 429 TranslateAcceptLanguages::CanBeAcceptLanguage(language); | 431 TranslateAcceptLanguages::CanBeAcceptLanguage(language); |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 out_value->push_back(it); | 632 out_value->push_back(it); |
| 631 std::sort(out_value->begin(), out_value->end(), | 633 std::sort(out_value->begin(), out_value->end(), |
| 632 [](const LanguageAndProbability& left, | 634 [](const LanguageAndProbability& left, |
| 633 const LanguageAndProbability& right) { | 635 const LanguageAndProbability& right) { |
| 634 return left.second > right.second; | 636 return left.second > right.second; |
| 635 }); | 637 }); |
| 636 return confidence; | 638 return confidence; |
| 637 } | 639 } |
| 638 | 640 |
| 639 } // namespace translate | 641 } // namespace translate |
| OLD | NEW |