diff options
author | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-31 22:31:04 +0000 |
---|---|---|
committer | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-31 22:31:04 +0000 |
commit | 9ee73628f9d8ec2998b6060805dac0efd825af44 (patch) | |
tree | 39b1016beca729c2295d8cc937bfd654040f83de /chrome/browser | |
parent | 30f9ac3cd37f6a4833be4ce0aefa969dea9a1808 (diff) | |
download | chromium_src-9ee73628f9d8ec2998b6060805dac0efd825af44.zip chromium_src-9ee73628f9d8ec2998b6060805dac0efd825af44.tar.gz chromium_src-9ee73628f9d8ec2998b6060805dac0efd825af44.tar.bz2 |
Hooking the UI part to the translate feature.
Review URL: http://codereview.chromium.org/549217
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37674 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/browser_main.cc | 8 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_tabs_module.cc | 2 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_view_host.cc | 12 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_view_host.h | 3 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_view_host_delegate.h | 5 | ||||
-rw-r--r-- | chrome/browser/tab_contents/tab_contents.cc | 36 | ||||
-rw-r--r-- | chrome/browser/tab_contents/tab_contents.h | 7 | ||||
-rw-r--r-- | chrome/browser/translate/translate_infobars_delegates.cc | 5 | ||||
-rw-r--r-- | chrome/browser/translate/translate_manager.cc | 113 | ||||
-rw-r--r-- | chrome/browser/translate/translate_manager.h | 47 |
10 files changed, 219 insertions, 19 deletions
diff --git a/chrome/browser/browser_main.cc b/chrome/browser/browser_main.cc index 42d2c14..a0ba774 100644 --- a/chrome/browser/browser_main.cc +++ b/chrome/browser/browser_main.cc @@ -46,6 +46,7 @@ #include "chrome/browser/profile_manager.h" #include "chrome/browser/renderer_host/resource_dispatcher_host.h" #include "chrome/browser/shell_integration.h" +#include "chrome/browser/translate/translate_manager.h" #include "chrome/browser/user_data_manager.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_paths.h" @@ -803,6 +804,13 @@ int BrowserMain(const MainFunctionParams& parameters) { process_singleton.Create(); +#if defined(OS_WIN) + // Create the TranslateManager singleton. + // TODO(jcampan): enable on non Windows platforms when the info-bars are + // implemented. + Singleton<TranslateManager>::get(); +#endif + // Show the First Run UI if this is the first time Chrome has been run on // this computer, or we're being compelled to do so by a command line flag. // Note that this be done _after_ the PrefService is initialized and all diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc index 7b9aef0..f85f054 100644 --- a/chrome/browser/extensions/extension_tabs_module.cc +++ b/chrome/browser/extensions/extension_tabs_module.cc @@ -897,7 +897,7 @@ bool DetectTabLanguageFunction::RunImpl() { // The tab contents does not know its language yet. Let's wait until it // receives it, or until the tab is closed/navigates to some other page. registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, - Source<RenderViewHost>(contents->render_view_host())); + Source<TabContents>(contents)); registrar_.Add(this, NotificationType::TAB_CLOSING, Source<NavigationController>(&(contents->controller()))); registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, diff --git a/chrome/browser/renderer_host/render_view_host.cc b/chrome/browser/renderer_host/render_view_host.cc index cb0eddf..820b575 100644 --- a/chrome/browser/renderer_host/render_view_host.cc +++ b/chrome/browser/renderer_host/render_view_host.cc @@ -825,6 +825,7 @@ void RenderViewHost::OnMessageReceived(const IPC::Message& msg) { OnAccessibilityFocusChange) IPC_MESSAGE_HANDLER(ViewHostMsg_OnCSSInserted, OnCSSInserted) IPC_MESSAGE_HANDLER(ViewHostMsg_PageContents, OnPageContents) + IPC_MESSAGE_HANDLER(ViewHostMsg_PageTranslated, OnPageTranslated) // Have the super handle all other messages. IPC_MESSAGE_UNHANDLED(RenderWidgetHost::OnMessageReceived(msg)) IPC_END_MESSAGE_MAP_EX() @@ -1794,3 +1795,14 @@ void RenderViewHost::OnPageContents(const GURL& url, integration_delegate->OnPageContents(url, process()->id(), page_id, contents, language); } + +void RenderViewHost::OnPageTranslated(int32 page_id, + const std::string& original_lang, + const std::string& translated_lang) { + RenderViewHostDelegate::BrowserIntegration* integration_delegate = + delegate_->GetBrowserIntegrationDelegate(); + if (!integration_delegate) + return; + integration_delegate->OnPageTranslated(page_id, + original_lang, translated_lang); +} diff --git a/chrome/browser/renderer_host/render_view_host.h b/chrome/browser/renderer_host/render_view_host.h index 5890ae7..764be45 100644 --- a/chrome/browser/renderer_host/render_view_host.h +++ b/chrome/browser/renderer_host/render_view_host.h @@ -606,6 +606,9 @@ class RenderViewHost : public RenderWidgetHost { int32 page_id, const std::wstring& contents, const std::string& language); + void OnPageTranslated(int32 page_id, + const std::string& original_lang, + const std::string& translated_lang); private: friend class TestRenderViewHost; diff --git a/chrome/browser/renderer_host/render_view_host_delegate.h b/chrome/browser/renderer_host/render_view_host_delegate.h index 680ff0f..892aa86 100644 --- a/chrome/browser/renderer_host/render_view_host_delegate.h +++ b/chrome/browser/renderer_host/render_view_host_delegate.h @@ -223,6 +223,11 @@ class RenderViewHostDelegate { int32 page_id, const std::wstring& contents, const std::string& language) = 0; + + // Notification that the page has been translated. + virtual void OnPageTranslated(int32 page_id, + const std::string& original_lang, + const std::string& translated_lang) = 0; }; // Resource ------------------------------------------------------------------ diff --git a/chrome/browser/tab_contents/tab_contents.cc b/chrome/browser/tab_contents/tab_contents.cc index a575ae5..4641f3b 100644 --- a/chrome/browser/tab_contents/tab_contents.cc +++ b/chrome/browser/tab_contents/tab_contents.cc @@ -772,6 +772,16 @@ void TabContents::ShowPageInfo(const GURL& url, delegate_->ShowPageInfo(profile(), url, ssl, show_history); } +void TabContents::TranslatePage(const std::string& source_lang, + const std::string& target_lang) { + NavigationEntry* entry = controller_.GetActiveEntry(); + if (!entry) { + NOTREACHED(); + return; + } + render_view_host()->TranslatePage(entry->page_id(), source_lang, target_lang); +} + ConstrainedWindow* TabContents::CreateConstrainedDialog( ConstrainedWindowDelegate* delegate) { ConstrainedWindow* window = @@ -1813,26 +1823,24 @@ void TabContents::OnPageContents(const GURL& url, entry->set_language(language); } - if (CommandLine::ForCurrentProcess()->HasSwitch( - switches::kAutoPageTranslate) && - TranslationService::IsTranslationEnabled()) { - std::string locale = g_browser_process->GetApplicationLocale(); - if (!locale.empty() && locale != language) { - // Don't translate the NTP, download page, history... - if (entry && !entry->url().SchemeIs("chrome") && - TranslationService::ShouldTranslatePage(language, locale)) { - render_view_host()->TranslatePage(entry->page_id(), language, locale); - } - } - } - std::string lang = language; NotificationService::current()->Notify( NotificationType::TAB_LANGUAGE_DETERMINED, - Source<RenderViewHost>(render_view_host()), + Source<TabContents>(this), Details<std::string>(&lang)); } +void TabContents::OnPageTranslated(int32 page_id, + const std::string& original_lang, + const std::string& translated_lang) { + std::pair<std::string, std::string> lang_pair = + std::make_pair(original_lang, translated_lang); + NotificationService::current()->Notify( + NotificationType::PAGE_TRANSLATED, + Source<TabContents>(this), + Details<std::pair<std::string, std::string> >(&lang_pair)); +} + void TabContents::DidStartProvisionalLoadForFrame( RenderViewHost* render_view_host, bool is_main_frame, diff --git a/chrome/browser/tab_contents/tab_contents.h b/chrome/browser/tab_contents/tab_contents.h index 9586f88..e42c1fb 100644 --- a/chrome/browser/tab_contents/tab_contents.h +++ b/chrome/browser/tab_contents/tab_contents.h @@ -350,6 +350,10 @@ class TabContents : public PageNavigator, const NavigationEntry::SSLStatus& ssl, bool show_history); + // Translates the page contents from |source_lang| to |target_lang|. + void TranslatePage(const std::string& source_lang, + const std::string& target_lang); + // Window management --------------------------------------------------------- // Create a new window constrained to this TabContents' clip and visibility. @@ -827,6 +831,9 @@ class TabContents : public PageNavigator, int32 page_id, const std::wstring& contents, const std::string& language); + virtual void OnPageTranslated(int32 page_id, + const std::string& original_lang, + const std::string& translated_lang); // RenderViewHostDelegate::Resource implementation. virtual void DidStartProvisionalLoadForFrame(RenderViewHost* render_view_host, diff --git a/chrome/browser/translate/translate_infobars_delegates.cc b/chrome/browser/translate/translate_infobars_delegates.cc index ecf0a95..15d165b 100644 --- a/chrome/browser/translate/translate_infobars_delegates.cc +++ b/chrome/browser/translate/translate_infobars_delegates.cc @@ -93,10 +93,7 @@ void TranslateInfoBarDelegate::GetAvailableTargetLanguages( } void TranslateInfoBarDelegate::Translate() { - // TODO(kuan): Call actual Translate method. -/* - Translate(WideToUTF8(original_language()), WideToUTF8(target_language())); -*/ + tab_contents_->TranslatePage(original_language_, target_language_); } bool TranslateInfoBarDelegate::IsLanguageBlacklisted() { diff --git a/chrome/browser/translate/translate_manager.cc b/chrome/browser/translate/translate_manager.cc new file mode 100644 index 0000000..5a7938a --- /dev/null +++ b/chrome/browser/translate/translate_manager.cc @@ -0,0 +1,113 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/translate/translate_manager.h" + +#include "chrome/browser/browser_process.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/renderer_host/translation_service.h" +#include "chrome/browser/tab_contents/navigation_controller.h" +#include "chrome/browser/tab_contents/navigation_entry.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/browser/translate/translate_infobars_delegates.h" +#include "chrome/browser/translate/translate_prefs.h" +#include "chrome/common/notification_details.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_source.h" +#include "chrome/common/notification_type.h" +#include "chrome/common/pref_service.h" + +TranslateManager::~TranslateManager() { +} + +void TranslateManager::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + switch (type.value) { + case NotificationType::NAV_ENTRY_COMMITTED: { + // We have navigated to a new page. + NavigationController* controller = + Source<NavigationController>(source).ptr(); + NavigationEntry* entry = controller->GetActiveEntry(); + if (!entry->language().empty()) { + // The language for that page is known (it must be a back/forward + // navigation to a page we already visited). + InitiateTranslation(controller->tab_contents(), entry->language()); + } + break; + } + case NotificationType::TAB_LANGUAGE_DETERMINED: { + TabContents* tab = Source<TabContents>(source).ptr(); + std::string language = *(Details<std::string>(details).ptr()); + InitiateTranslation(tab, language); + break; + } + case NotificationType::PAGE_TRANSLATED: { + TabContents* tab = Source<TabContents>(source).ptr(); + std::pair<std::string, std::string>* language_pair = + (Details<std::pair<std::string, std::string> >(details).ptr()); + PrefService* prefs = GetPrefService(tab); + tab->AddInfoBar(new AfterTranslateInfoBarDelegate(tab, prefs, + language_pair->first, + language_pair->second)); + break; + } + default: + NOTREACHED(); + } +} + +TranslateManager::TranslateManager() { + if (!TranslationService::IsTranslationEnabled()) + return; + + notification_registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, + NotificationService::AllSources()); + notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, + NotificationService::AllSources()); + notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED, + NotificationService::AllSources()); +} + +void TranslateManager::InitiateTranslation(TabContents* tab, + const std::string& page_lang) { + PrefService* prefs = GetPrefService(tab); + NavigationEntry* entry = tab->controller().GetActiveEntry(); + if (!entry) { + NOTREACHED(); + return; + } + + std::string chrome_lang = g_browser_process->GetApplicationLocale(); + + // We don't want to translate: + // - any Chrome specific page (New Tab Page, Download, History... pages). + // - similar languages (ex: en-US to en). + // - any user black-listed URLs or user selected language combination. + if (entry->url().SchemeIs("chrome") || page_lang == chrome_lang || + !TranslationService::ShouldTranslatePage(page_lang, chrome_lang) || + !TranslatePrefs::CanTranslate(prefs, page_lang, entry->url())) { + return; + } + if (TranslatePrefs::ShouldAutoTranslate(prefs, page_lang, chrome_lang)) { + // The user has previously select "always translate" for this language. + tab->TranslatePage(page_lang, chrome_lang); + return; + } + + // Prompts the user if he/she wants the page translated. + tab->AddInfoBar(new BeforeTranslateInfoBarDelegate(tab, prefs, + entry->url(), + page_lang, chrome_lang)); +} + +PrefService* TranslateManager::GetPrefService(TabContents* tab) { + PrefService* prefs = NULL; + if (tab->profile()) + prefs = tab->profile()->GetPrefs(); + if (prefs) + return prefs; + + return g_browser_process->local_state(); +} diff --git a/chrome/browser/translate/translate_manager.h b/chrome/browser/translate/translate_manager.h new file mode 100644 index 0000000..b51f089 --- /dev/null +++ b/chrome/browser/translate/translate_manager.h @@ -0,0 +1,47 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ +#define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ + +#include <string> + +#include "base/singleton.h" +#include "chrome/common/notification_observer.h" +#include "chrome/common/notification_registrar.h" + +class PrefService; +class TabContents; + +// The TranslateManager class is responsible for showing an info-bar when a page +// in a language different than the user language is loaded. It triggers the +// page translation the user requests. +// It is a singleton. + +class TranslateManager : public NotificationObserver { + public: + virtual ~TranslateManager(); + + // NotificationObserver implementation: + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + private: + friend struct DefaultSingletonTraits<TranslateManager>; + + TranslateManager(); + + // Starts the translation process on |tab| containing the page in the + // |page_lang| language. + void InitiateTranslation(TabContents* tab, const std::string& page_lang); + + // Convenience method that retrieves the PrefService. + PrefService* GetPrefService(TabContents* tab); + + NotificationRegistrar notification_registrar_; + + DISALLOW_COPY_AND_ASSIGN(TranslateManager); +}; + +#endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |