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_split.h" | 10 #include "base/strings/string_split.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 "translate_ignored_count_for_language"; | 29 "translate_ignored_count_for_language"; |
30 const char TranslatePrefs::kPrefTranslateAcceptedCount[] = | 30 const char TranslatePrefs::kPrefTranslateAcceptedCount[] = |
31 "translate_accepted_count"; | 31 "translate_accepted_count"; |
32 const char TranslatePrefs::kPrefTranslateBlockedLanguages[] = | 32 const char TranslatePrefs::kPrefTranslateBlockedLanguages[] = |
33 "translate_blocked_languages"; | 33 "translate_blocked_languages"; |
34 const char TranslatePrefs::kPrefTranslateLastDeniedTimeForLanguage[] = | 34 const char TranslatePrefs::kPrefTranslateLastDeniedTimeForLanguage[] = |
35 "translate_last_denied_time_for_language"; | 35 "translate_last_denied_time_for_language"; |
36 const char TranslatePrefs::kPrefTranslateTooOftenDeniedForLanguage[] = | 36 const char TranslatePrefs::kPrefTranslateTooOftenDeniedForLanguage[] = |
37 "translate_too_often_denied_for_language"; | 37 "translate_too_often_denied_for_language"; |
38 | 38 |
39 // This property is deprecated but there is still some usages. Don't use this | |
40 // for new code. | |
41 static const char kPrefTranslateLanguageBlacklist[] = | |
42 "translate_language_blacklist"; | |
43 | |
44 // The below properties used to be used but now are deprecated. Don't use them | 39 // The below properties used to be used but now are deprecated. Don't use them |
45 // since an old profile might have some values there. | 40 // since an old profile might have some values there. |
46 // | 41 // |
47 // * translate_last_denied_time | 42 // * translate_last_denied_time |
48 // * translate_too_often_denied | 43 // * translate_too_often_denied |
| 44 // * translate_language_blacklist |
49 | 45 |
50 namespace { | 46 namespace { |
51 | 47 |
52 void GetBlacklistedLanguages(const PrefService* prefs, | |
53 std::vector<std::string>* languages) { | |
54 DCHECK(languages); | |
55 DCHECK(languages->empty()); | |
56 | |
57 const char* key = kPrefTranslateLanguageBlacklist; | |
58 const base::ListValue* list = prefs->GetList(key); | |
59 for (base::ListValue::const_iterator it = list->begin(); it != list->end(); | |
60 ++it) { | |
61 std::string lang; | |
62 (*it)->GetAsString(&lang); | |
63 languages->push_back(lang); | |
64 } | |
65 } | |
66 | |
67 // Expands language codes to make these more suitable for Accept-Language. | 48 // Expands language codes to make these more suitable for Accept-Language. |
68 // Example: ['en-US', 'ja', 'en-CA'] => ['en-US', 'en', 'ja', 'en-CA']. | 49 // Example: ['en-US', 'ja', 'en-CA'] => ['en-US', 'en', 'ja', 'en-CA']. |
69 // 'en' won't appear twice as this function eliminates duplicates. | 50 // 'en' won't appear twice as this function eliminates duplicates. |
70 void ExpandLanguageCodes(const std::vector<std::string>& languages, | 51 void ExpandLanguageCodes(const std::vector<std::string>& languages, |
71 std::vector<std::string>* expanded_languages) { | 52 std::vector<std::string>* expanded_languages) { |
72 DCHECK(expanded_languages); | 53 DCHECK(expanded_languages); |
73 DCHECK(expanded_languages->empty()); | 54 DCHECK(expanded_languages->empty()); |
74 | 55 |
75 // used to eliminate duplicates. | 56 // used to eliminate duplicates. |
76 std::set<std::string> seen; | 57 std::set<std::string> seen; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 languages.end()) { | 192 languages.end()) { |
212 languages.push_back(language); | 193 languages.push_back(language); |
213 UpdateLanguageList(languages); | 194 UpdateLanguageList(languages); |
214 } | 195 } |
215 } | 196 } |
216 | 197 |
217 void TranslatePrefs::UnblockLanguage(const std::string& original_language) { | 198 void TranslatePrefs::UnblockLanguage(const std::string& original_language) { |
218 RemoveValueFromBlacklist(kPrefTranslateBlockedLanguages, original_language); | 199 RemoveValueFromBlacklist(kPrefTranslateBlockedLanguages, original_language); |
219 } | 200 } |
220 | 201 |
221 void TranslatePrefs::RemoveLanguageFromLegacyBlacklist( | |
222 const std::string& original_language) { | |
223 RemoveValueFromBlacklist(kPrefTranslateLanguageBlacklist, original_language); | |
224 } | |
225 | |
226 bool TranslatePrefs::IsSiteBlacklisted(const std::string& site) const { | 202 bool TranslatePrefs::IsSiteBlacklisted(const std::string& site) const { |
227 return IsValueBlacklisted(kPrefTranslateSiteBlacklist, site); | 203 return IsValueBlacklisted(kPrefTranslateSiteBlacklist, site); |
228 } | 204 } |
229 | 205 |
230 void TranslatePrefs::BlacklistSite(const std::string& site) { | 206 void TranslatePrefs::BlacklistSite(const std::string& site) { |
231 BlacklistValue(kPrefTranslateSiteBlacklist, site); | 207 BlacklistValue(kPrefTranslateSiteBlacklist, site); |
232 } | 208 } |
233 | 209 |
234 void TranslatePrefs::RemoveSiteFromBlacklist(const std::string& site) { | 210 void TranslatePrefs::RemoveSiteFromBlacklist(const std::string& site) { |
235 RemoveValueFromBlacklist(kPrefTranslateSiteBlacklist, site); | 211 RemoveValueFromBlacklist(kPrefTranslateSiteBlacklist, site); |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 if (dict && dict->GetString(original_language, target_language)) { | 444 if (dict && dict->GetString(original_language, target_language)) { |
469 DCHECK(!target_language->empty()); | 445 DCHECK(!target_language->empty()); |
470 return !target_language->empty(); | 446 return !target_language->empty(); |
471 } | 447 } |
472 return false; | 448 return false; |
473 } | 449 } |
474 | 450 |
475 // static | 451 // static |
476 void TranslatePrefs::RegisterProfilePrefs( | 452 void TranslatePrefs::RegisterProfilePrefs( |
477 user_prefs::PrefRegistrySyncable* registry) { | 453 user_prefs::PrefRegistrySyncable* registry) { |
478 registry->RegisterListPref(kPrefTranslateLanguageBlacklist, | |
479 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
480 registry->RegisterListPref(kPrefTranslateSiteBlacklist, | 454 registry->RegisterListPref(kPrefTranslateSiteBlacklist, |
481 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 455 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
482 registry->RegisterDictionaryPref( | 456 registry->RegisterDictionaryPref( |
483 kPrefTranslateWhitelists, | 457 kPrefTranslateWhitelists, |
484 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 458 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
485 registry->RegisterDictionaryPref( | 459 registry->RegisterDictionaryPref( |
486 kPrefTranslateDeniedCount, | 460 kPrefTranslateDeniedCount, |
487 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 461 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
488 registry->RegisterDictionaryPref( | 462 registry->RegisterDictionaryPref( |
489 kPrefTranslateIgnoredCount, | 463 kPrefTranslateIgnoredCount, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 std::string target_lang; | 506 std::string target_lang; |
533 if (list->empty() || | 507 if (list->empty() || |
534 !list->GetString(list->GetSize() - 1, &target_lang) || | 508 !list->GetString(list->GetSize() - 1, &target_lang) || |
535 target_lang.empty()) { | 509 target_lang.empty()) { |
536 dict->Remove(key, NULL); | 510 dict->Remove(key, NULL); |
537 } else { | 511 } else { |
538 dict->SetString(key, target_lang); | 512 dict->SetString(key, target_lang); |
539 } | 513 } |
540 } | 514 } |
541 } | 515 } |
542 | |
543 // Get the union of the blacklist and the Accept languages, and set this to | |
544 // the new language set 'translate_blocked_languages'. This is used for the | |
545 // settings UI for Translate and configration to determine which langauage | |
546 // should be translated instead of the blacklist. The blacklist is no longer | |
547 // used after launching the settings UI. | |
548 // After that, Set 'translate_languages_not_translate' to Accept languages to | |
549 // enable settings for users. | |
550 bool merged = user_prefs->HasPrefPath(kPrefTranslateBlockedLanguages); | |
551 | |
552 if (!merged) { | |
553 std::vector<std::string> blacklisted_languages; | |
554 GetBlacklistedLanguages(user_prefs, &blacklisted_languages); | |
555 | |
556 std::vector<std::string> accept_languages = | |
557 base::SplitString(user_prefs->GetString(accept_languages_pref), ",", | |
558 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
559 | |
560 std::vector<std::string> blocked_languages; | |
561 CreateBlockedLanguages(&blocked_languages, blacklisted_languages, | |
562 accept_languages); | |
563 | |
564 // Create the new preference kPrefTranslateBlockedLanguages. | |
565 { | |
566 base::ListValue blocked_languages_list; | |
567 for (std::vector<std::string>::const_iterator it = | |
568 blocked_languages.begin(); | |
569 it != blocked_languages.end(); ++it) { | |
570 blocked_languages_list.Append(new base::StringValue(*it)); | |
571 } | |
572 ListPrefUpdate update(user_prefs, kPrefTranslateBlockedLanguages); | |
573 base::ListValue* list = update.Get(); | |
574 DCHECK(list != NULL); | |
575 list->Swap(&blocked_languages_list); | |
576 } | |
577 | |
578 // Update kAcceptLanguages | |
579 for (std::vector<std::string>::const_iterator it = | |
580 blocked_languages.begin(); | |
581 it != blocked_languages.end(); ++it) { | |
582 std::string lang = *it; | |
583 translate::ToChromeLanguageSynonym(&lang); | |
584 bool not_found = | |
585 std::find(accept_languages.begin(), accept_languages.end(), lang) == | |
586 accept_languages.end(); | |
587 if (not_found) | |
588 accept_languages.push_back(lang); | |
589 } | |
590 | |
591 std::string new_accept_languages_str = | |
592 base::JoinString(accept_languages, ","); | |
593 user_prefs->SetString(accept_languages_pref, new_accept_languages_str); | |
594 } | |
595 } | |
596 | |
597 // static | |
598 void TranslatePrefs::CreateBlockedLanguages( | |
599 std::vector<std::string>* blocked_languages, | |
600 const std::vector<std::string>& blacklisted_languages, | |
601 const std::vector<std::string>& accept_languages) { | |
602 DCHECK(blocked_languages); | |
603 DCHECK(blocked_languages->empty()); | |
604 | |
605 std::set<std::string> result; | |
606 | |
607 for (std::vector<std::string>::const_iterator it = | |
608 blacklisted_languages.begin(); | |
609 it != blacklisted_languages.end(); ++it) { | |
610 result.insert(*it); | |
611 } | |
612 | |
613 const std::string& app_locale = | |
614 TranslateDownloadManager::GetInstance()->application_locale(); | |
615 std::string ui_lang = TranslateDownloadManager::GetLanguageCode(app_locale); | |
616 bool is_ui_english = | |
617 ui_lang == "en" || | |
618 base::StartsWith(ui_lang, "en-", base::CompareCase::INSENSITIVE_ASCII); | |
619 | |
620 for (std::vector<std::string>::const_iterator it = accept_languages.begin(); | |
621 it != accept_languages.end(); ++it) { | |
622 std::string lang = *it; | |
623 translate::ToTranslateLanguageSynonym(&lang); | |
624 | |
625 // Regarding http://crbug.com/36182, even though English exists in Accept | |
626 // language list, English could be translated on non-English locale. | |
627 if (lang == "en" && !is_ui_english) | |
628 continue; | |
629 | |
630 result.insert(lang); | |
631 } | |
632 | |
633 blocked_languages->insert(blocked_languages->begin(), result.begin(), | |
634 result.end()); | |
635 } | 516 } |
636 | 517 |
637 bool TranslatePrefs::IsValueInList(const base::ListValue* list, | 518 bool TranslatePrefs::IsValueInList(const base::ListValue* list, |
638 const std::string& in_value) const { | 519 const std::string& in_value) const { |
639 for (size_t i = 0; i < list->GetSize(); ++i) { | 520 for (size_t i = 0; i < list->GetSize(); ++i) { |
640 std::string value; | 521 std::string value; |
641 if (list->GetString(i, &value) && value == in_value) | 522 if (list->GetString(i, &value) && value == in_value) |
642 return true; | 523 return true; |
643 } | 524 } |
644 return false; | 525 return false; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 const base::ListValue* blacklist = prefs_->GetList(pref_id); | 560 const base::ListValue* blacklist = prefs_->GetList(pref_id); |
680 return (blacklist == NULL || blacklist->empty()); | 561 return (blacklist == NULL || blacklist->empty()); |
681 } | 562 } |
682 | 563 |
683 bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const { | 564 bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const { |
684 const base::DictionaryValue* dict = prefs_->GetDictionary(pref_id); | 565 const base::DictionaryValue* dict = prefs_->GetDictionary(pref_id); |
685 return (dict == NULL || dict->empty()); | 566 return (dict == NULL || dict->empty()); |
686 } | 567 } |
687 | 568 |
688 } // namespace translate | 569 } // namespace translate |
OLD | NEW |