#include "components/translate/core/browser/translate_prefs.h"
#include <limits>
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/i18n/rtl.h"
#include "base/json/values_util.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/language/core/browser/accept_languages_service.h"
#include "components/language/core/browser/language_prefs.h"
#include "components/language/core/browser/pref_names.h"
#include "components/language/core/common/language_experiments.h"
#include "components/language/core/common/language_util.h"
#include "components/language/core/common/locale_util.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/strings/grit/components_locale_settings.h"
#include "components/translate/core/browser/translate_download_manager.h"
#include "components/translate/core/browser/translate_pref_names.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_collator.h"
namespace translate {
namespace {
const int kForceTriggerBackoffThreshold = …;
bool ContainsSameBaseLanguage(const std::vector<std::string>& list,
std::string_view language_code) { … }
void MigrateObsoleteAlwaysTranslateLanguagesPref(PrefService* prefs) { … }
bool IsTranslateLanguage(std::string_view language) { … }
}
BASE_FEATURE(…);
BASE_FEATURE(…);
BASE_FEATURE(…);
TranslateLanguageInfo::TranslateLanguageInfo() = default;
TranslateLanguageInfo::TranslateLanguageInfo(const TranslateLanguageInfo&) =
default;
TranslateLanguageInfo::TranslateLanguageInfo(TranslateLanguageInfo&&) noexcept =
default;
TranslateLanguageInfo& TranslateLanguageInfo::operator=(
const TranslateLanguageInfo&) = default;
TranslateLanguageInfo& TranslateLanguageInfo::operator=(
TranslateLanguageInfo&&) noexcept = default;
TranslatePrefs::TranslatePrefs(PrefService* user_prefs)
: … { … }
TranslatePrefs::~TranslatePrefs() = default;
std::string TranslatePrefs::MapPreferenceName(const std::string& pref_name) { … }
bool TranslatePrefs::IsOfferTranslateEnabled() const { … }
bool TranslatePrefs::IsTranslateAllowedByPolicy() const { … }
void TranslatePrefs::SetCountry(const std::string& country) { … }
std::string TranslatePrefs::GetCountry() const { … }
void TranslatePrefs::ResetToDefaults() { … }
base::Value::List TranslatePrefs::GetDefaultBlockedLanguages() { … }
bool TranslatePrefs::IsBlockedLanguage(std::string_view input_language) const { … }
void TranslatePrefs::BlockLanguage(std::string_view input_language) { … }
void TranslatePrefs::UnblockLanguage(std::string_view input_language) { … }
void TranslatePrefs::ResetEmptyBlockedLanguagesToDefaults() { … }
void TranslatePrefs::ResetBlockedLanguagesToDefault() { … }
std::vector<std::string> TranslatePrefs::GetNeverTranslateLanguages() const { … }
void TranslatePrefs::AddToLanguageList(std::string_view input_language,
const bool force_blocked) { … }
void TranslatePrefs::RemoveFromLanguageList(std::string_view input_language) { … }
void TranslatePrefs::RearrangeLanguage(
std::string_view language,
const TranslatePrefs::RearrangeSpecifier where,
int offset,
const std::vector<std::string>& enabled_languages) { … }
void TranslatePrefs::SetLanguageOrder(
const std::vector<std::string>& new_order) { … }
void TranslatePrefs::GetLanguageInfoList(
const std::string& app_locale,
bool translate_allowed,
std::vector<TranslateLanguageInfo>* language_list) { … }
void TranslatePrefs::GetTranslatableContentLanguages(
const std::string& app_locale,
std::vector<std::string>* codes) { … }
bool TranslatePrefs::IsSiteOnNeverPromptList(std::string_view site) const { … }
void TranslatePrefs::AddSiteToNeverPromptList(std::string_view site,
base::Time time) { … }
void TranslatePrefs::AddSiteToNeverPromptList(std::string_view site) { … }
void TranslatePrefs::RemoveSiteFromNeverPromptList(std::string_view site) { … }
std::vector<std::string> TranslatePrefs::GetNeverPromptSitesBetween(
base::Time begin,
base::Time end) const { … }
void TranslatePrefs::DeleteNeverPromptSitesBetween(base::Time begin,
base::Time end) { … }
bool TranslatePrefs::IsLanguagePairOnAlwaysTranslateList(
std::string_view source_language,
std::string_view target_language) { … }
void TranslatePrefs::AddLanguagePairToAlwaysTranslateList(
std::string_view source_language,
std::string_view target_language) { … }
void TranslatePrefs::RemoveLanguagePairFromAlwaysTranslateList(
std::string_view source_language) { … }
std::vector<std::string> TranslatePrefs::GetAlwaysTranslateLanguages() const { … }
void TranslatePrefs::ClearNeverPromptSiteList() { … }
bool TranslatePrefs::HasLanguagePairsToAlwaysTranslate() const { … }
void TranslatePrefs::ClearAlwaysTranslateLanguagePairs() { … }
int TranslatePrefs::GetTranslationDeniedCount(std::string_view language) const { … }
void TranslatePrefs::IncrementTranslationDeniedCount(
std::string_view language) { … }
void TranslatePrefs::ResetTranslationDeniedCount(std::string_view language) { … }
int TranslatePrefs::GetTranslationIgnoredCount(
std::string_view language) const { … }
void TranslatePrefs::IncrementTranslationIgnoredCount(
std::string_view language) { … }
void TranslatePrefs::ResetTranslationIgnoredCount(std::string_view language) { … }
int TranslatePrefs::GetTranslationAcceptedCount(
std::string_view language) const { … }
void TranslatePrefs::IncrementTranslationAcceptedCount(
std::string_view language) { … }
void TranslatePrefs::ResetTranslationAcceptedCount(std::string_view language) { … }
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
int TranslatePrefs::GetTranslationAutoAlwaysCount(
std::string_view language) const {
const base::Value::Dict& dict =
prefs_->GetDict(kPrefTranslateAutoAlwaysCount);
return dict.FindInt(language).value_or(0);
}
void TranslatePrefs::IncrementTranslationAutoAlwaysCount(
std::string_view language) {
ScopedDictPrefUpdate update(prefs_, kPrefTranslateAutoAlwaysCount);
base::Value::Dict& dict = update.Get();
int count = dict.FindInt(language).value_or(0);
if (count < std::numeric_limits<int>::max())
dict.Set(language, count + 1);
}
void TranslatePrefs::ResetTranslationAutoAlwaysCount(
std::string_view language) {
ScopedDictPrefUpdate update(prefs_, kPrefTranslateAutoAlwaysCount);
update->Set(language, 0);
}
int TranslatePrefs::GetTranslationAutoNeverCount(
std::string_view language) const {
const base::Value::Dict& dict = prefs_->GetDict(kPrefTranslateAutoNeverCount);
return dict.FindInt(language).value_or(0);
}
void TranslatePrefs::IncrementTranslationAutoNeverCount(
std::string_view language) {
ScopedDictPrefUpdate update(prefs_, kPrefTranslateAutoNeverCount);
base::Value::Dict& dict = update.Get();
int count = dict.FindInt(language).value_or(0);
if (count < std::numeric_limits<int>::max())
dict.Set(language, count + 1);
}
void TranslatePrefs::ResetTranslationAutoNeverCount(std::string_view language) {
ScopedDictPrefUpdate update(prefs_, kPrefTranslateAutoNeverCount);
update->Set(language, 0);
}
#endif
#if BUILDFLAG(IS_ANDROID)
bool TranslatePrefs::GetAppLanguagePromptShown() const {
return prefs_->GetBoolean(language::prefs::kAppLanguagePromptShown);
}
void TranslatePrefs::SetAppLanguagePromptShown() {
prefs_->SetBoolean(language::prefs::kAppLanguagePromptShown, true);
}
#endif
void TranslatePrefs::GetLanguageList(
std::vector<std::string>* const languages) const { … }
void TranslatePrefs::GetUserSelectedLanguageList(
std::vector<std::string>* const languages) const { … }
bool TranslatePrefs::ShouldForceTriggerTranslateOnEnglishPages() { … }
bool TranslatePrefs::force_translate_on_english_for_testing_ = …;
void TranslatePrefs::SetShouldForceTriggerTranslateOnEnglishPagesForTesting() { … }
bool TranslatePrefs::CanTranslateLanguage(std::string_view language) { … }
bool TranslatePrefs::ShouldAutoTranslate(std::string_view source_language,
std::string* target_language) { … }
void TranslatePrefs::SetRecentTargetLanguage(
const std::string& target_language) { … }
void TranslatePrefs::ResetRecentTargetLanguage() { … }
std::string TranslatePrefs::GetRecentTargetLanguage() const { … }
int TranslatePrefs::GetForceTriggerOnEnglishPagesCount() const { … }
void TranslatePrefs::ReportForceTriggerOnEnglishPages() { … }
void TranslatePrefs::ReportAcceptedAfterForceTriggerOnEnglishPages() { … }
void TranslatePrefs::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) { … }
void TranslatePrefs::RegisterProfilePrefsForMigration(
user_prefs::PrefRegistrySyncable* registry) { … }
void TranslatePrefs::MigrateNeverPromptSites() { … }
bool TranslatePrefs::IsValueOnNeverPromptList(const char* pref_id,
std::string_view value) const { … }
void TranslatePrefs::AddValueToNeverPromptList(const char* pref_id,
std::string_view value) { … }
void TranslatePrefs::RemoveValueFromNeverPromptList(const char* pref_id,
std::string_view value) { … }
size_t TranslatePrefs::GetListSize(const char* pref_id) const { … }
bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const { … }
}