From 05c4d2f18b794ed2314baa0a6c2266d6ee266c85 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Fri, 8 Jul 2022 17:58:15 +0700 Subject: [PATCH] Android translate (#14047) * Enable translate for Android * Make kOfferTranslateEnabled unsyncable * Update chromium_src/components/translate/core/browser/translate_manager.cc --- browser/about_flags.cc | 2 +- browser/brave_profile_prefs.cc | 4 +--- .../translate/brave_translate_prefs_migration.cc | 3 +++ .../translate/core/browser/translate_manager.cc | 14 ++++++++++++++ .../translate/core/browser/translate_prefs.cc | 14 -------------- .../chrome-browser-ui-browser_ui_prefs.cc.patch | 13 +++++++++++++ 6 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 patches/chrome-browser-ui-browser_ui_prefs.cc.patch diff --git a/browser/about_flags.cc b/browser/about_flags.cc index 64438419e03a..420c94fbfeb8 100644 --- a/browser/about_flags.cc +++ b/browser/about_flags.cc @@ -438,7 +438,7 @@ constexpr char kRestrictWebSocketsPoolDescription[] = {"brave-translate-go", \ flag_descriptions::kBraveTranslateGoName, \ flag_descriptions::kBraveTranslateGoDescription, \ - kOsDesktop, \ + kOsDesktop | kOsAndroid, \ FEATURE_VALUE_TYPE(translate::features::kUseBraveTranslateGo)}, #else #define BRAVE_TRANSLATE_GO_FEATURE_ENTRIES diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index ad8f3b62cb16..bf29bd1621fb 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -270,9 +270,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { feed::RegisterProfilePrefs(registry); registry->RegisterBooleanPref(feed::prefs::kEnableSnippets, false); registry->RegisterBooleanPref(feed::prefs::kArticlesListVisible, false); - // Translate is not available on Android - registry->SetDefaultPrefValue(translate::prefs::kOfferTranslateEnabled, - base::Value(false)); + // Explicitly disable safe browsing extended reporting by default in case they // change it in upstream. registry->SetDefaultPrefValue(prefs::kSafeBrowsingScoutReportingEnabled, diff --git a/browser/translate/brave_translate_prefs_migration.cc b/browser/translate/brave_translate_prefs_migration.cc index 2f9e13680045..441afd8cd6d2 100644 --- a/browser/translate/brave_translate_prefs_migration.cc +++ b/browser/translate/brave_translate_prefs_migration.cc @@ -29,6 +29,9 @@ void MigrateBraveProfilePrefs(PrefService* prefs) { return; // Already migrated prefs->SetBoolean(prefs::kMigratedToInternalTranslation, true); + + // TODO(matuchin): make kOfferTranslateEnabled syncable again when the + // migration is finished. prefs->ClearPref(prefs::kOfferTranslateEnabled); } diff --git a/chromium_src/components/translate/core/browser/translate_manager.cc b/chromium_src/components/translate/core/browser/translate_manager.cc index 66a5725abbdd..ad5a546c2aa2 100644 --- a/chromium_src/components/translate/core/browser/translate_manager.cc +++ b/chromium_src/components/translate/core/browser/translate_manager.cc @@ -5,8 +5,10 @@ #include "components/translate/core/browser/translate_manager.h" +#include "brave/components/translate/core/common/brave_translate_features.h" #include "brave/components/translate/core/common/brave_translate_language_filter.h" #include "brave/components/translate/core/common/buildflags.h" +#include "build/build_config.h" #include "components/translate/core/browser/translate_download_manager.h" #include "components/translate/core/browser/translate_prefs.h" @@ -46,6 +48,18 @@ void TranslateManager::FilterIsTranslatePossible( ChromiumTranslateManager::FilterIsTranslatePossible( decision, translate_prefs, page_language_code, target_lang); #if BUILDFLAG(ENABLE_BRAVE_TRANSLATE_GO) +#if BUILDFLAG(IS_ANDROID) + // Disable translate completely if brave translate feature is disabled. + // The code is Android only because desktops use TranslateManager to show + // Google translate extension bubble. + if (!IsBraveTranslateGoAvailable()) { + decision->PreventAllTriggering(); + decision->initiation_statuses.push_back( + TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH); + GetActiveTranslateMetricsLogger()->LogTriggerDecision( + TriggerDecision::kDisabledTranslationFeatureDisabled); + } +#endif // The source language is not supported by Brave backend. Currently we allow a // user to trigger a manual translation to have a chance to change the // incorrectly recognized source language to the correct one. diff --git a/chromium_src/components/translate/core/browser/translate_prefs.cc b/chromium_src/components/translate/core/browser/translate_prefs.cc index f9cf4ae68bec..610448d11712 100644 --- a/chromium_src/components/translate/core/browser/translate_prefs.cc +++ b/chromium_src/components/translate/core/browser/translate_prefs.cc @@ -7,20 +7,6 @@ // chromium_src/chrome/browser/prefs/browser_prefs.cc #include "components/translate/core/browser/translate_prefs.h" -#include "build/build_config.h" - #define MigrateObsoleteProfilePrefs MigrateObsoleteProfilePrefs_ChromiumImpl #include "src/components/translate/core/browser/translate_prefs.cc" #undef MigrateObsoleteProfilePrefs - -#include "base/feature_override.h" - -namespace translate { - -OVERRIDE_FEATURE_DEFAULT_STATES({{ -#if BUILDFLAG(IS_ANDROID) - {kTranslate, base::FEATURE_DISABLED_BY_DEFAULT}, -#endif -}}); - -} // namespace translate diff --git a/patches/chrome-browser-ui-browser_ui_prefs.cc.patch b/patches/chrome-browser-ui-browser_ui_prefs.cc.patch new file mode 100644 index 000000000000..d9d7eda75e0d --- /dev/null +++ b/patches/chrome-browser-ui-browser_ui_prefs.cc.patch @@ -0,0 +1,13 @@ +diff --git a/chrome/browser/ui/browser_ui_prefs.cc b/chrome/browser/ui/browser_ui_prefs.cc +index d70f76650ce3d2c380e81751f0fa039ec2894ad9..a2f87acfa34cfa6b59c2150baeb7a85af1048021 100644 +--- a/chrome/browser/ui/browser_ui_prefs.cc ++++ b/chrome/browser/ui/browser_ui_prefs.cc +@@ -78,7 +78,7 @@ void RegisterBrowserUserPrefs(user_prefs::PrefRegistrySyncable* registry) { + registry->RegisterBooleanPref(prefs::kWebAppCreateInQuickLaunchBar, true); + registry->RegisterBooleanPref( + translate::prefs::kOfferTranslateEnabled, true, +- user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); ++ user_prefs::PrefRegistrySyncable::NO_REGISTRATION_FLAGS); + registry->RegisterStringPref(prefs::kCloudPrintEmail, std::string()); + registry->RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, true); + registry->RegisterBooleanPref(prefs::kCloudPrintSubmitEnabled, true);