chromium/components/translate/core/browser/translate_ui_delegate.cc

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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

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() {
  // Don't trigger if it's off the record or already set to always translate.
  if (IsIncognito() || ShouldAlwaysTranslate())
    return false;

  const std::string& source_language =
      translate_ui_languages_manager_->GetSourceLanguageCode();
  // Don't trigger for unknown source language.
  if (source_language == kUnknownLanguageCode)
    return false;

  bool always_translate =
      (prefs_->GetTranslationAcceptedCount(source_language) >=
           GetAutoAlwaysThreshold() &&
       prefs_->GetTranslationAutoAlwaysCount(source_language) <
           GetMaximumNumberOfAutoAlways());

  if (always_translate) {
    // Auto-always will be triggered. Need to increment the auto-always
    // counter.
    prefs_->IncrementTranslationAutoAlwaysCount(source_language);
    // Reset translateAcceptedCount so that auto-always could be triggered
    // again.
    prefs_->ResetTranslationAcceptedCount(source_language);
  }
  return always_translate;
}

bool TranslateUIDelegate::ShouldAutoNeverTranslate() {
  if (IsIncognito())
    return false;

  const std::string& source_language =
      translate_ui_languages_manager_->GetSourceLanguageCode();
  // Don't trigger if this language is already blocked.
  if (!prefs_->CanTranslateLanguage(source_language))
    return false;

  int auto_never_count = prefs_->GetTranslationAutoNeverCount(source_language);

  // At the beginning (auto_never_count == 0), deniedCount starts at 0 and is
  // off-by-one (because this checking is done before increment). However,
  // after auto-never is triggered once (auto_never_count > 0), deniedCount
  // starts at 1. So there is no off-by-one by then.
  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) {
    // Auto-never will be triggered. Need to increment the auto-never counter.
    prefs_->IncrementTranslationAutoNeverCount(source_language);
    // Reset translateDeniedCount so that auto-never could be triggered again.
    prefs_->ResetTranslationDeniedCount(source_language);
  }
  return never_translate;
}

#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)

std::string TranslateUIDelegate::GetPageHost() const {}

const TranslateDriver* TranslateUIDelegate::GetTranslateDriver() const {}

}  // namespace translate