#include "components/translate/core/browser/translate_ui_delegate.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_macros.h"
#include "components/language/core/browser/pref_names.h"
#include "components/language/core/common/language_experiments.h"
#include "components/translate/core/browser/translate_client.h"
#include "components/translate/core/browser/translate_download_manager.h"
#include "components/translate/core/browser/translate_driver.h"
#include "components/translate/core/browser/translate_manager.h"
#include "components/translate/core/browser/translate_metrics_logger.h"
#include "components/translate/core/browser/translate_prefs.h"
#include "components/translate/core/browser/translate_ui_languages_manager.h"
#include "components/translate/core/common/translate_util.h"
#include "net/base/url_util.h"
#include "third_party/metrics_proto/translate_event.pb.h"
namespace {
const char kShowErrorUI[] = …;
}
namespace translate {
TranslateUIDelegate::TranslateUIDelegate(
const base::WeakPtr<TranslateManager>& translate_manager,
const std::string& source_language,
const std::string& target_language)
: … { … }
TranslateUIDelegate::~TranslateUIDelegate() = default;
void TranslateUIDelegate::UpdateAndRecordSourceLanguageIndex(
size_t language_index) { … }
void TranslateUIDelegate::UpdateAndRecordSourceLanguage(
const std::string& language_code) { … }
void TranslateUIDelegate::UpdateAndRecordTargetLanguageIndex(
size_t language_index) { … }
void TranslateUIDelegate::UpdateAndRecordTargetLanguage(
const std::string& language_code) { … }
void TranslateUIDelegate::OnErrorShown(TranslateErrors error_type) { … }
const LanguageState* TranslateUIDelegate::GetLanguageState() { … }
void TranslateUIDelegate::GetContentLanguagesCodes(
std::vector<std::string>* content_codes) const { … }
void TranslateUIDelegate::Translate() { … }
void TranslateUIDelegate::RevertTranslation() { … }
void TranslateUIDelegate::TranslationDeclined(bool explicitly_closed) { … }
bool TranslateUIDelegate::IsLanguageBlocked() const { … }
void TranslateUIDelegate::SetLanguageBlocked(bool value) { … }
bool TranslateUIDelegate::IsSiteOnNeverPromptList() const { … }
bool TranslateUIDelegate::CanAddSiteToNeverPromptList() const { … }
void TranslateUIDelegate::SetNeverPromptSite(bool value) { … }
bool TranslateUIDelegate::ShouldAlwaysTranslate() const { … }
void TranslateUIDelegate::SetAlwaysTranslate(bool value) { … }
bool TranslateUIDelegate::ShouldAlwaysTranslateBeCheckedByDefault() const { … }
bool TranslateUIDelegate::ShouldShowAlwaysTranslateShortcut() const { … }
bool TranslateUIDelegate::ShouldShowNeverTranslateShortcut() const { … }
void TranslateUIDelegate::OnUIClosedByUser() { … }
void TranslateUIDelegate::ReportUIInteraction(UIInteraction ui_interaction) { … }
void TranslateUIDelegate::ReportUIChange(bool is_ui_shown) { … }
void TranslateUIDelegate::MaybeSetContentLanguages() { … }
bool TranslateUIDelegate::IsIncognito() const { … }
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
bool TranslateUIDelegate::ShouldAutoAlwaysTranslate() {
if (IsIncognito() || ShouldAlwaysTranslate())
return false;
const std::string& source_language =
translate_ui_languages_manager_->GetSourceLanguageCode();
if (source_language == kUnknownLanguageCode)
return false;
bool always_translate =
(prefs_->GetTranslationAcceptedCount(source_language) >=
GetAutoAlwaysThreshold() &&
prefs_->GetTranslationAutoAlwaysCount(source_language) <
GetMaximumNumberOfAutoAlways());
if (always_translate) {
prefs_->IncrementTranslationAutoAlwaysCount(source_language);
prefs_->ResetTranslationAcceptedCount(source_language);
}
return always_translate;
}
bool TranslateUIDelegate::ShouldAutoNeverTranslate() {
if (IsIncognito())
return false;
const std::string& source_language =
translate_ui_languages_manager_->GetSourceLanguageCode();
if (!prefs_->CanTranslateLanguage(source_language))
return false;
int auto_never_count = prefs_->GetTranslationAutoNeverCount(source_language);
int off_by_one = auto_never_count == 0 ? 1 : 0;
bool never_translate =
(prefs_->GetTranslationDeniedCount(source_language) + off_by_one >=
GetAutoNeverThreshold() &&
auto_never_count < GetMaximumNumberOfAutoNever());
if (never_translate) {
prefs_->IncrementTranslationAutoNeverCount(source_language);
prefs_->ResetTranslationDeniedCount(source_language);
}
return never_translate;
}
#endif
std::string TranslateUIDelegate::GetPageHost() const { … }
const TranslateDriver* TranslateUIDelegate::GetTranslateDriver() const { … }
}