Commit 74eda5a3 authored by Anthony Cui's avatar Anthony Cui Committed by Commit Bot

Reland "Consolidate translation check and execution logic between...

Reland "Consolidate translation check and execution logic between TranslateManager and RenderViewContextMenu"

This is a reland of 24c91f76

Original change's description:
> Consolidate translation check and execution logic between TranslateManager and RenderViewContextMenu
>
> Consolidated logic by having RenderViewContextMenu::ExecTranslate call
> TranslateManager::InitiateManualTranslation, and RenderViewContextMenu::AppendPageItems call
> TranslateManager:CanManuallyTranslate.
>
> Bug: 898168
> Change-Id: If91cf98626f73d0c357522f8fdca840695185bc2
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2385636
> Reviewed-by: Avi Drissman <avi@chromium.org>
> Reviewed-by: Megan Jablonski <megjablon@chromium.org>
> Commit-Queue: Anthony Cui <cuianthony@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#806161}

Bug: 898168
Change-Id: Iddd188034e471d3b4e8e09650d9f1e88cb2bc268
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2410645Reviewed-by: default avatarMegan Jablonski <megjablon@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Commit-Queue: Anthony Cui <cuianthony@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807475}
parent 189eeaf5
...@@ -595,12 +595,11 @@ RenderViewContextMenu::RenderViewContextMenu( ...@@ -595,12 +595,11 @@ RenderViewContextMenu::RenderViewContextMenu(
SetContentCustomCommandIdRange(IDC_CONTENT_CONTEXT_CUSTOM_FIRST, SetContentCustomCommandIdRange(IDC_CONTENT_CONTEXT_CUSTOM_FIRST,
IDC_CONTENT_CONTEXT_CUSTOM_LAST); IDC_CONTENT_CONTEXT_CUSTOM_LAST);
} }
set_content_type(ContextMenuContentTypeFactory::Create( set_content_type(
source_web_contents_, params)); ContextMenuContentTypeFactory::Create(source_web_contents_, params));
} }
RenderViewContextMenu::~RenderViewContextMenu() { RenderViewContextMenu::~RenderViewContextMenu() = default;
}
// Menu construction functions ------------------------------------------------- // Menu construction functions -------------------------------------------------
...@@ -685,18 +684,17 @@ void RenderViewContextMenu::AppendAllExtensionItems() { ...@@ -685,18 +684,17 @@ void RenderViewContextMenu::AppendAllExtensionItems() {
// Get a list of extension id's that have context menu items, and sort by the // Get a list of extension id's that have context menu items, and sort by the
// top level context menu title of the extension. // top level context menu title of the extension.
std::set<MenuItem::ExtensionKey> ids = menu_manager->ExtensionIds();
std::vector<base::string16> sorted_menu_titles; std::vector<base::string16> sorted_menu_titles;
std::map<base::string16, std::vector<const Extension*>> std::map<base::string16, std::vector<const Extension*>>
title_to_extensions_map; title_to_extensions_map;
for (auto iter = ids.begin(); iter != ids.end(); ++iter) { for (const auto& id : menu_manager->ExtensionIds()) {
const Extension* extension = registry->GetExtensionById( const Extension* extension = registry->GetExtensionById(
iter->extension_id, extensions::ExtensionRegistry::ENABLED); id.extension_id, extensions::ExtensionRegistry::ENABLED);
// Platform apps have their context menus created directly in // Platform apps have their context menus created directly in
// AppendPlatformAppItems. // AppendPlatformAppItems.
if (extension && !extension->is_platform_app()) { if (extension && !extension->is_platform_app()) {
base::string16 menu_title = extension_items_.GetTopLevelContextMenuTitle( base::string16 menu_title = extension_items_.GetTopLevelContextMenuTitle(
*iter, printable_selection_text); id, printable_selection_text);
title_to_extensions_map[menu_title].push_back(extension); title_to_extensions_map[menu_title].push_back(extension);
sorted_menu_titles.push_back(menu_title); sorted_menu_titles.push_back(menu_title);
} }
...@@ -1179,14 +1177,14 @@ void RenderViewContextMenu::AppendLinkItems() { ...@@ -1179,14 +1177,14 @@ void RenderViewContextMenu::AppendLinkItems() {
// Find all regular profiles other than the current one which have at // Find all regular profiles other than the current one which have at
// least one open window. // least one open window.
std::vector<ProfileAttributesEntry*> entries = std::vector<ProfileAttributesEntry*> entries =
profile_manager->GetProfileAttributesStorage(). profile_manager->GetProfileAttributesStorage()
GetAllProfilesAttributesSortedByName(); .GetAllProfilesAttributesSortedByName();
std::vector<ProfileAttributesEntry*> target_profiles_entries; std::vector<ProfileAttributesEntry*> target_profiles_entries;
for (ProfileAttributesEntry* entry : entries) { for (ProfileAttributesEntry* entry : entries) {
base::FilePath profile_path = entry->GetPath(); base::FilePath profile_path = entry->GetPath();
Profile* profile = profile_manager->GetProfileByPath(profile_path); Profile* profile = profile_manager->GetProfileByPath(profile_path);
if (profile != GetProfile() && if (profile != GetProfile() && !entry->IsOmitted() &&
!entry->IsOmitted() && !entry->IsSigninRequired()) { !entry->IsSigninRequired()) {
target_profiles_entries.push_back(entry); target_profiles_entries.push_back(entry);
if (chrome::FindLastActiveWithProfile(profile)) if (chrome::FindLastActiveWithProfile(profile))
multiple_profiles_open_ = true; multiple_profiles_open_ = true;
...@@ -1278,11 +1276,10 @@ void RenderViewContextMenu::AppendLinkItems() { ...@@ -1278,11 +1276,10 @@ void RenderViewContextMenu::AppendLinkItems() {
IDS_CONTENT_CONTEXT_SAVELINKAS); IDS_CONTENT_CONTEXT_SAVELINKAS);
} }
menu_model_.AddItemWithStringId( menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYLINKLOCATION,
IDC_CONTENT_CONTEXT_COPYLINKLOCATION, params_.link_url.SchemeIs(url::kMailToScheme)
params_.link_url.SchemeIs(url::kMailToScheme) ? ? IDS_CONTENT_CONTEXT_COPYEMAILADDRESS
IDS_CONTENT_CONTEXT_COPYEMAILADDRESS : : IDS_CONTENT_CONTEXT_COPYLINKLOCATION);
IDS_CONTENT_CONTEXT_COPYLINKLOCATION);
if (params_.source_type == ui::MENU_SOURCE_TOUCH && if (params_.source_type == ui::MENU_SOURCE_TOUCH &&
params_.media_type != ContextMenuDataMediaType::kImage && params_.media_type != ContextMenuDataMediaType::kImage &&
...@@ -1527,14 +1524,16 @@ void RenderViewContextMenu::AppendPageItems() { ...@@ -1527,14 +1524,16 @@ void RenderViewContextMenu::AppendPageItems() {
menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
} }
if (TranslateService::IsTranslatableURL(params_.page_url)) { ChromeTranslateClient* chrome_translate_client =
std::unique_ptr<translate::TranslatePrefs> prefs( ChromeTranslateClient::FromWebContents(embedder_web_contents_);
ChromeTranslateClient::CreateTranslatePrefs( if (chrome_translate_client &&
GetPrefs(browser_context_))); chrome_translate_client->GetTranslateManager()->CanManuallyTranslate()) {
if (prefs->IsTranslateAllowedByPolicy()) {
language::LanguageModel* language_model = language::LanguageModel* language_model =
LanguageModelManagerFactory::GetForBrowserContext(browser_context_) LanguageModelManagerFactory::GetForBrowserContext(browser_context_)
->GetPrimaryModel(); ->GetPrimaryModel();
std::unique_ptr<translate::TranslatePrefs> prefs(
ChromeTranslateClient::CreateTranslatePrefs(
GetPrefs(browser_context_)));
std::string locale = translate::TranslateManager::GetTargetLanguage( std::string locale = translate::TranslateManager::GetTargetLanguage(
prefs.get(), language_model); prefs.get(), language_model);
base::string16 language = base::string16 language =
...@@ -1543,7 +1542,6 @@ void RenderViewContextMenu::AppendPageItems() { ...@@ -1543,7 +1542,6 @@ void RenderViewContextMenu::AppendPageItems() {
IDC_CONTENT_CONTEXT_TRANSLATE, IDC_CONTENT_CONTEXT_TRANSLATE,
l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_TRANSLATE, language)); l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_TRANSLATE, language));
} }
}
} }
void RenderViewContextMenu::AppendExitFullscreenItem() { void RenderViewContextMenu::AppendExitFullscreenItem() {
...@@ -2459,7 +2457,7 @@ void RenderViewContextMenu::RegisterMenuShownCallbackForTesting( ...@@ -2459,7 +2457,7 @@ void RenderViewContextMenu::RegisterMenuShownCallbackForTesting(
} }
ProtocolHandlerRegistry::ProtocolHandlerList ProtocolHandlerRegistry::ProtocolHandlerList
RenderViewContextMenu::GetHandlersForLinkUrl() { RenderViewContextMenu::GetHandlersForLinkUrl() {
ProtocolHandlerRegistry::ProtocolHandlerList handlers = ProtocolHandlerRegistry::ProtocolHandlerList handlers =
protocol_handler_registry_->GetHandlersFor(params_.link_url.scheme()); protocol_handler_registry_->GetHandlersFor(params_.link_url.scheme());
std::sort(handlers.begin(), handlers.end()); std::sort(handlers.begin(), handlers.end());
...@@ -2473,8 +2471,7 @@ void RenderViewContextMenu::NotifyMenuShown() { ...@@ -2473,8 +2471,7 @@ void RenderViewContextMenu::NotifyMenuShown() {
} }
base::string16 RenderViewContextMenu::PrintableSelectionText() { base::string16 RenderViewContextMenu::PrintableSelectionText() {
return gfx::TruncateString(params_.selection_text, return gfx::TruncateString(params_.selection_text, kMaxSelectionTextLength,
kMaxSelectionTextLength,
gfx::WORD_BREAK); gfx::WORD_BREAK);
} }
...@@ -2568,7 +2565,8 @@ bool RenderViewContextMenu::IsSaveLinkAsEnabled() const { ...@@ -2568,7 +2565,8 @@ bool RenderViewContextMenu::IsSaveLinkAsEnabled() const {
if (profile->IsChild()) { if (profile->IsChild()) {
SupervisedUserService* supervised_user_service = SupervisedUserService* supervised_user_service =
SupervisedUserServiceFactory::GetForProfile(profile); SupervisedUserServiceFactory::GetForProfile(profile);
SupervisedUserURLFilter* url_filter = supervised_user_service->GetURLFilter(); SupervisedUserURLFilter* url_filter =
supervised_user_service->GetURLFilter();
if (url_filter->GetFilteringBehaviorForURL(params_.link_url) != if (url_filter->GetFilteringBehaviorForURL(params_.link_url) !=
SupervisedUserURLFilter::FilteringBehavior::ALLOW) SupervisedUserURLFilter::FilteringBehavior::ALLOW)
return false; return false;
...@@ -2768,9 +2766,7 @@ void RenderViewContextMenu::ExecProtocolHandler(int event_flags, ...@@ -2768,9 +2766,7 @@ void RenderViewContextMenu::ExecProtocolHandler(int event_flags,
WindowOpenDisposition disposition = ui::DispositionFromEventFlags( WindowOpenDisposition disposition = ui::DispositionFromEventFlags(
event_flags, WindowOpenDisposition::NEW_FOREGROUND_TAB); event_flags, WindowOpenDisposition::NEW_FOREGROUND_TAB);
OpenURL(handlers[handler_index].TranslateUrl(params_.link_url), OpenURL(handlers[handler_index].TranslateUrl(params_.link_url),
GetDocumentURL(params_), GetDocumentURL(params_), disposition, ui::PAGE_TRANSITION_LINK);
disposition,
ui::PAGE_TRANSITION_LINK);
} }
void RenderViewContextMenu::ExecOpenLinkInProfile(int profile_index) { void RenderViewContextMenu::ExecOpenLinkInProfile(int profile_index) {
...@@ -2847,8 +2843,8 @@ void RenderViewContextMenu::ExecSaveLinkAs() { ...@@ -2847,8 +2843,8 @@ void RenderViewContextMenu::ExecSaveLinkAs() {
} }
void RenderViewContextMenu::ExecSaveAs() { void RenderViewContextMenu::ExecSaveAs() {
bool is_large_data_url = params_.has_image_contents && bool is_large_data_url =
params_.src_url.is_empty(); params_.has_image_contents && params_.src_url.is_empty();
if (params_.media_type == ContextMenuDataMediaType::kCanvas || if (params_.media_type == ContextMenuDataMediaType::kCanvas ||
(params_.media_type == ContextMenuDataMediaType::kImage && (params_.media_type == ContextMenuDataMediaType::kImage &&
is_large_data_url)) { is_large_data_url)) {
...@@ -3016,32 +3012,15 @@ void RenderViewContextMenu::ExecRouteMedia() { ...@@ -3016,32 +3012,15 @@ void RenderViewContextMenu::ExecRouteMedia() {
} }
void RenderViewContextMenu::ExecTranslate() { void RenderViewContextMenu::ExecTranslate() {
// A translation might have been triggered by the time the menu got
// selected, do nothing in that case.
ChromeTranslateClient* chrome_translate_client = ChromeTranslateClient* chrome_translate_client =
ChromeTranslateClient::FromWebContents(embedder_web_contents_); ChromeTranslateClient::FromWebContents(embedder_web_contents_);
if (!chrome_translate_client || if (!chrome_translate_client)
chrome_translate_client->GetLanguageState().IsPageTranslated() ||
chrome_translate_client->GetLanguageState().translation_pending()) {
return; return;
}
std::string original_lang =
chrome_translate_client->GetLanguageState().original_language();
std::string target_lang = GetTargetLanguage();
// Since the user decided to translate for that language and site, clears
// any preferences for not translating them.
std::unique_ptr<translate::TranslatePrefs> prefs(
ChromeTranslateClient::CreateTranslatePrefs(
GetPrefs(browser_context_)));
prefs->UnblockLanguage(original_lang);
prefs->RemoveSiteFromBlacklist(params_.page_url.HostNoBrackets());
translate::TranslateManager* manager = translate::TranslateManager* manager =
chrome_translate_client->GetTranslateManager(); chrome_translate_client->GetTranslateManager();
DCHECK(manager); DCHECK(manager);
manager->TranslatePage(original_lang, target_lang, true); manager->InitiateManualTranslation(true, true);
} }
void RenderViewContextMenu::ExecLanguageSettings(int event_flags) { void RenderViewContextMenu::ExecLanguageSettings(int event_flags) {
......
...@@ -200,6 +200,8 @@ bool TranslateManager::CanManuallyTranslate() { ...@@ -200,6 +200,8 @@ bool TranslateManager::CanManuallyTranslate() {
std::unique_ptr<TranslatePrefs> translate_prefs( std::unique_ptr<TranslatePrefs> translate_prefs(
translate_client_->GetTranslatePrefs()); translate_client_->GetTranslatePrefs());
if (!translate_prefs->IsTranslateAllowedByPolicy())
return false;
const std::string target_lang = GetManualTargetLanguage( const std::string target_lang = GetManualTargetLanguage(
TranslateDownloadManager::GetLanguageCode(source_language), TranslateDownloadManager::GetLanguageCode(source_language),
language_state_, translate_prefs.get(), language_model_); language_state_, translate_prefs.get(), language_model_);
...@@ -209,7 +211,13 @@ bool TranslateManager::CanManuallyTranslate() { ...@@ -209,7 +211,13 @@ bool TranslateManager::CanManuallyTranslate() {
return true; return true;
} }
void TranslateManager::InitiateManualTranslation(bool auto_translate) { void TranslateManager::InitiateManualTranslation(bool auto_translate,
bool triggered_from_menu) {
// If a translation has already been triggered, do nothing.
if (language_state_.IsPageTranslated() ||
language_state_.translation_pending())
return;
std::unique_ptr<TranslatePrefs> translate_prefs( std::unique_ptr<TranslatePrefs> translate_prefs(
translate_client_->GetTranslatePrefs()); translate_client_->GetTranslatePrefs());
const std::string source_code = TranslateDownloadManager::GetLanguageCode( const std::string source_code = TranslateDownloadManager::GetLanguageCode(
...@@ -221,8 +229,8 @@ void TranslateManager::InitiateManualTranslation(bool auto_translate) { ...@@ -221,8 +229,8 @@ void TranslateManager::InitiateManualTranslation(bool auto_translate) {
// Translate the page if it has not been translated and manual translate // Translate the page if it has not been translated and manual translate
// should trigger translation automatically. Otherwise, only show the infobar. // should trigger translation automatically. Otherwise, only show the infobar.
if (!language_state_.IsPageTranslated() && auto_translate) { if (auto_translate) {
TranslatePage(source_code, target_lang, false); TranslatePage(source_code, target_lang, triggered_from_menu);
return; return;
} }
......
...@@ -120,7 +120,8 @@ class TranslateManager { ...@@ -120,7 +120,8 @@ class TranslateManager {
// Initiate a manually triggered translation process for the current page. // Initiate a manually triggered translation process for the current page.
// Collect source and target languages, and show translation UI. If // Collect source and target languages, and show translation UI. If
// |auto_translate| is true the page gets translated to the target language. // |auto_translate| is true the page gets translated to the target language.
void InitiateManualTranslation(bool auto_translate = false); void InitiateManualTranslation(bool auto_translate = false,
bool triggered_from_menu = false);
// Returns true iff the current page could be manually translated. // Returns true iff the current page could be manually translated.
bool CanManuallyTranslate(); bool CanManuallyTranslate();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment