Commit 0added55 authored by droger@chromium.org's avatar droger@chromium.org

Remove dependency of TranslateInfobarDelegate on chrome/ and content/

BUG=371845
TBR=rohitrao

Review URL: https://codereview.chromium.org/292513002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271685 0039d316-1c4b-4281-b951-d872f2087c98
parent 6cd43fe5
...@@ -7,23 +7,28 @@ ...@@ -7,23 +7,28 @@
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "chrome/app/chrome_command_ids.h" #include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/translate/translate_infobar_delegate.h" #include "chrome/browser/translate/translate_infobar_delegate.h"
#include "chrome/common/url_constants.h" #include "components/translate/core/browser/translate_driver.h"
#include "content/public/browser/web_contents.h"
#include "grit/component_strings.h" #include "grit/component_strings.h"
#include "grit/locale_settings.h" #include "grit/locale_settings.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
using content::NavigationEntry; namespace {
using content::OpenURLParams;
using content::Referrer; const char kAboutGoogleTranslateURL[] =
using content::WebContents; #if defined(OS_CHROMEOS)
"https://support.google.com/chromeos/?p=ib_translation_bar";
#else
"https://support.google.com/chrome/?p=ib_translation_bar";
#endif
} // namespace
OptionsMenuModel::OptionsMenuModel( OptionsMenuModel::OptionsMenuModel(
TranslateInfoBarDelegate* translate_delegate) TranslateInfoBarDelegate* translate_delegate)
: ui::SimpleMenuModel(this), : ui::SimpleMenuModel(this),
translate_infobar_delegate_(translate_delegate) { translate_infobar_delegate_(translate_delegate) {
// |translate_delegate| must already be owned. // |translate_delegate| must already be owned.
DCHECK(translate_infobar_delegate_->GetWebContents()); DCHECK(translate_infobar_delegate_->GetTranslateDriver());
base::string16 original_language = translate_delegate->language_name_at( base::string16 original_language = translate_delegate->language_name_at(
translate_delegate->original_language_index()); translate_delegate->original_language_index());
...@@ -121,13 +126,10 @@ void OptionsMenuModel::ExecuteCommand(int command_id, int event_flags) { ...@@ -121,13 +126,10 @@ void OptionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
break; break;
case IDC_TRANSLATE_OPTIONS_ABOUT: { case IDC_TRANSLATE_OPTIONS_ABOUT: {
WebContents* web_contents = translate_infobar_delegate_->GetWebContents(); TranslateDriver* translate_driver =
if (web_contents) { translate_infobar_delegate_->GetTranslateDriver();
OpenURLParams params( if (translate_driver)
GURL(chrome::kAboutGoogleTranslateURL), Referrer(), translate_driver->OpenUrlInNewTab(GURL(kAboutGoogleTranslateURL));
NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false);
web_contents->OpenURL(params);
}
break; break;
} }
......
...@@ -8,9 +8,8 @@ ...@@ -8,9 +8,8 @@
#include "base/i18n/string_compare.h" #include "base/i18n/string_compare.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "components/infobars/core/infobar.h" #include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_manager.h"
#include "components/translate/core/browser/language_state.h" #include "components/translate/core/browser/language_state.h"
#include "components/translate/core/browser/translate_accept_languages.h" #include "components/translate/core/browser/translate_accept_languages.h"
#include "components/translate/core/browser/translate_client.h" #include "components/translate/core/browser/translate_client.h"
...@@ -18,7 +17,6 @@ ...@@ -18,7 +17,6 @@
#include "components/translate/core/browser/translate_driver.h" #include "components/translate/core/browser/translate_driver.h"
#include "components/translate/core/browser/translate_manager.h" #include "components/translate/core/browser/translate_manager.h"
#include "components/translate/core/common/translate_constants.h" #include "components/translate/core/common/translate_constants.h"
#include "content/public/browser/web_contents.h"
#include "grit/component_strings.h" #include "grit/component_strings.h"
#include "grit/theme_resources.h" #include "grit/theme_resources.h"
#include "third_party/icu/source/i18n/unicode/coll.h" #include "third_party/icu/source/i18n/unicode/coll.h"
...@@ -55,16 +53,15 @@ TranslateInfoBarDelegate::~TranslateInfoBarDelegate() { ...@@ -55,16 +53,15 @@ TranslateInfoBarDelegate::~TranslateInfoBarDelegate() {
void TranslateInfoBarDelegate::Create( void TranslateInfoBarDelegate::Create(
bool replace_existing_infobar, bool replace_existing_infobar,
const base::WeakPtr<TranslateManager>& translate_manager, const base::WeakPtr<TranslateManager>& translate_manager,
InfoBarService* infobar_service, infobars::InfoBarManager* infobar_manager,
bool is_off_the_record, bool is_off_the_record,
translate::TranslateStep step, translate::TranslateStep step,
const std::string& original_language, const std::string& original_language,
const std::string& target_language, const std::string& target_language,
TranslateErrors::Type error_type, TranslateErrors::Type error_type,
PrefService* prefs,
bool triggered_from_menu) { bool triggered_from_menu) {
DCHECK(translate_manager); DCHECK(translate_manager);
DCHECK(infobar_service); DCHECK(infobar_manager);
// Check preconditions. // Check preconditions.
if (step != translate::TRANSLATE_STEP_TRANSLATE_ERROR) { if (step != translate::TRANSLATE_STEP_TRANSLATE_ERROR) {
...@@ -90,8 +87,8 @@ void TranslateInfoBarDelegate::Create( ...@@ -90,8 +87,8 @@ void TranslateInfoBarDelegate::Create(
// Find any existing translate infobar delegate. // Find any existing translate infobar delegate.
infobars::InfoBar* old_infobar = NULL; infobars::InfoBar* old_infobar = NULL;
TranslateInfoBarDelegate* old_delegate = NULL; TranslateInfoBarDelegate* old_delegate = NULL;
for (size_t i = 0; i < infobar_service->infobar_count(); ++i) { for (size_t i = 0; i < infobar_manager->infobar_count(); ++i) {
old_infobar = infobar_service->infobar_at(i); old_infobar = infobar_manager->infobar_at(i);
old_delegate = old_infobar->delegate()->AsTranslateInfoBarDelegate(); old_delegate = old_infobar->delegate()->AsTranslateInfoBarDelegate();
if (old_delegate) { if (old_delegate) {
if (!replace_existing_infobar) if (!replace_existing_infobar)
...@@ -104,12 +101,12 @@ void TranslateInfoBarDelegate::Create( ...@@ -104,12 +101,12 @@ void TranslateInfoBarDelegate::Create(
scoped_ptr<infobars::InfoBar> infobar(CreateInfoBar( scoped_ptr<infobars::InfoBar> infobar(CreateInfoBar(
scoped_ptr<TranslateInfoBarDelegate>(new TranslateInfoBarDelegate( scoped_ptr<TranslateInfoBarDelegate>(new TranslateInfoBarDelegate(
translate_manager, is_off_the_record, step, old_delegate, translate_manager, is_off_the_record, step, old_delegate,
original_language, target_language, error_type, prefs, original_language, target_language, error_type,
triggered_from_menu)))); triggered_from_menu))));
if (old_delegate) if (old_delegate)
infobar_service->ReplaceInfoBar(old_infobar, infobar.Pass()); infobar_manager->ReplaceInfoBar(old_infobar, infobar.Pass());
else else
infobar_service->AddInfoBar(infobar.Pass()); infobar_manager->AddInfoBar(infobar.Pass());
} }
...@@ -272,10 +269,6 @@ bool TranslateInfoBarDelegate::ShouldShowAlwaysTranslateShortcut() { ...@@ -272,10 +269,6 @@ bool TranslateInfoBarDelegate::ShouldShowAlwaysTranslateShortcut() {
kAlwaysTranslateMinCount); kAlwaysTranslateMinCount);
} }
content::WebContents* TranslateInfoBarDelegate::GetWebContents() {
return InfoBarService::WebContentsFromInfoBar(infobar());
}
// static // static
void TranslateInfoBarDelegate::GetAfterTranslateStrings( void TranslateInfoBarDelegate::GetAfterTranslateStrings(
std::vector<base::string16>* strings, std::vector<base::string16>* strings,
...@@ -311,6 +304,13 @@ void TranslateInfoBarDelegate::GetAfterTranslateStrings( ...@@ -311,6 +304,13 @@ void TranslateInfoBarDelegate::GetAfterTranslateStrings(
strings->push_back(text.substr(offsets[1])); strings->push_back(text.substr(offsets[1]));
} }
TranslateDriver* TranslateInfoBarDelegate::GetTranslateDriver() {
if (!translate_manager_)
return NULL;
return translate_manager_->translate_client()->GetTranslateDriver();
}
TranslateInfoBarDelegate::TranslateInfoBarDelegate( TranslateInfoBarDelegate::TranslateInfoBarDelegate(
const base::WeakPtr<TranslateManager>& translate_manager, const base::WeakPtr<TranslateManager>& translate_manager,
bool is_off_the_record, bool is_off_the_record,
...@@ -319,7 +319,6 @@ TranslateInfoBarDelegate::TranslateInfoBarDelegate( ...@@ -319,7 +319,6 @@ TranslateInfoBarDelegate::TranslateInfoBarDelegate(
const std::string& original_language, const std::string& original_language,
const std::string& target_language, const std::string& target_language,
TranslateErrors::Type error_type, TranslateErrors::Type error_type,
PrefService* prefs,
bool triggered_from_menu) bool triggered_from_menu)
: infobars::InfoBarDelegate(), : infobars::InfoBarDelegate(),
is_off_the_record_(is_off_the_record), is_off_the_record_(is_off_the_record),
......
...@@ -19,13 +19,12 @@ ...@@ -19,13 +19,12 @@
#include "components/translate/core/common/translate_constants.h" #include "components/translate/core/common/translate_constants.h"
#include "components/translate/core/common/translate_errors.h" #include "components/translate/core/common/translate_errors.h"
class InfoBarService;
class PrefService;
class TranslateClient; class TranslateClient;
class TranslateDriver;
class TranslateManager; class TranslateManager;
namespace content { namespace infobars {
class WebContents; class InfoBarManager;
} }
class TranslateInfoBarDelegate : public infobars::InfoBarDelegate { class TranslateInfoBarDelegate : public infobars::InfoBarDelegate {
...@@ -50,18 +49,17 @@ class TranslateInfoBarDelegate : public infobars::InfoBarDelegate { ...@@ -50,18 +49,17 @@ class TranslateInfoBarDelegate : public infobars::InfoBarDelegate {
// |step| == TRANSLATING and |original_language| == kUnknownLanguageCode. // |step| == TRANSLATING and |original_language| == kUnknownLanguageCode.
// //
// If |replace_existing_infobar| is true, the infobar is created and added to // If |replace_existing_infobar| is true, the infobar is created and added to
// the infobar service for |web_contents|, replacing any other translate // the infobar manager, replacing any other translate infobar already present
// infobar already present there. Otherwise, the infobar will only be added // there. Otherwise, the infobar will only be added if there is no other
// if there is no other translate infobar already present. // translate infobar already present.
static void Create(bool replace_existing_infobar, static void Create(bool replace_existing_infobar,
const base::WeakPtr<TranslateManager>& translate_manager, const base::WeakPtr<TranslateManager>& translate_manager,
InfoBarService* infobar_service, infobars::InfoBarManager* infobar_manager,
bool is_off_the_record, bool is_off_the_record,
translate::TranslateStep step, translate::TranslateStep step,
const std::string& original_language, const std::string& original_language,
const std::string& target_language, const std::string& target_language,
TranslateErrors::Type error_type, TranslateErrors::Type error_type,
PrefService* prefs,
bool triggered_from_menu); bool triggered_from_menu);
// Returns the number of languages supported. // Returns the number of languages supported.
...@@ -156,9 +154,6 @@ class TranslateInfoBarDelegate : public infobars::InfoBarDelegate { ...@@ -156,9 +154,6 @@ class TranslateInfoBarDelegate : public infobars::InfoBarDelegate {
bool ShouldShowNeverTranslateShortcut(); bool ShouldShowNeverTranslateShortcut();
bool ShouldShowAlwaysTranslateShortcut(); bool ShouldShowAlwaysTranslateShortcut();
// Returns the WebContents associated with the TranslateInfoBarDelegate.
content::WebContents* GetWebContents();
// Adds the strings that should be displayed in the after translate infobar to // Adds the strings that should be displayed in the after translate infobar to
// |strings|. If |autodetermined_source_language| is false, the text in that // |strings|. If |autodetermined_source_language| is false, the text in that
// infobar is: // infobar is:
...@@ -175,6 +170,10 @@ class TranslateInfoBarDelegate : public infobars::InfoBarDelegate { ...@@ -175,6 +170,10 @@ class TranslateInfoBarDelegate : public infobars::InfoBarDelegate {
bool* swap_languages, bool* swap_languages,
bool autodetermined_source_language); bool autodetermined_source_language);
// Gets the TranslateDriver associated with this object.
// May return NULL if the driver has been destroyed.
TranslateDriver* GetTranslateDriver();
protected: protected:
TranslateInfoBarDelegate( TranslateInfoBarDelegate(
const base::WeakPtr<TranslateManager>& translate_manager, const base::WeakPtr<TranslateManager>& translate_manager,
...@@ -184,7 +183,6 @@ class TranslateInfoBarDelegate : public infobars::InfoBarDelegate { ...@@ -184,7 +183,6 @@ class TranslateInfoBarDelegate : public infobars::InfoBarDelegate {
const std::string& original_language, const std::string& original_language,
const std::string& target_language, const std::string& target_language,
TranslateErrors::Type error_type, TranslateErrors::Type error_type,
PrefService* prefs,
bool triggered_from_menu); bool triggered_from_menu);
private: private:
...@@ -195,6 +193,7 @@ class TranslateInfoBarDelegate : public infobars::InfoBarDelegate { ...@@ -195,6 +193,7 @@ class TranslateInfoBarDelegate : public infobars::InfoBarDelegate {
static scoped_ptr<infobars::InfoBar> CreateInfoBar( static scoped_ptr<infobars::InfoBar> CreateInfoBar(
scoped_ptr<TranslateInfoBarDelegate> delegate); scoped_ptr<TranslateInfoBarDelegate> delegate);
// Gets the TranslateClient associated with this object.
// May return NULL if the client has been destroyed. // May return NULL if the client has been destroyed.
TranslateClient* GetTranslateClient(); TranslateClient* GetTranslateClient();
......
...@@ -174,19 +174,15 @@ void TranslateTabHelper::ShowTranslateUI(translate::TranslateStep step, ...@@ -174,19 +174,15 @@ void TranslateTabHelper::ShowTranslateUI(translate::TranslateStep step,
} }
// Infobar UI. // Infobar UI.
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
Profile* original_profile = profile->GetOriginalProfile();
TranslateInfoBarDelegate::Create( TranslateInfoBarDelegate::Create(
step != translate::TRANSLATE_STEP_BEFORE_TRANSLATE, step != translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
translate_manager_->GetWeakPtr(), translate_manager_->GetWeakPtr(),
InfoBarService::FromWebContents(web_contents()), InfoBarService::FromWebContents(web_contents()),
profile->IsOffTheRecord(), web_contents()->GetBrowserContext()->IsOffTheRecord(),
step, step,
source_language, source_language,
target_language, target_language,
error_type, error_type,
original_profile->GetPrefs(),
triggered_from_menu); triggered_from_menu);
} }
......
...@@ -39,8 +39,7 @@ class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { ...@@ -39,8 +39,7 @@ class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate {
public: public:
MockTranslateInfoBarDelegate(content::WebContents* web_contents, MockTranslateInfoBarDelegate(content::WebContents* web_contents,
translate::TranslateStep step, translate::TranslateStep step,
TranslateErrors::Type error, TranslateErrors::Type error)
PrefService* prefs)
: TranslateInfoBarDelegate( : TranslateInfoBarDelegate(
TranslateTabHelper::GetManagerFromWebContents( TranslateTabHelper::GetManagerFromWebContents(
web_contents)->GetWeakPtr(), web_contents)->GetWeakPtr(),
...@@ -50,7 +49,6 @@ class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { ...@@ -50,7 +49,6 @@ class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate {
"en", "en",
"es", "es",
error, error,
prefs,
false) {} false) {}
MOCK_METHOD0(Translate, void()); MOCK_METHOD0(Translate, void());
...@@ -96,13 +94,10 @@ class TranslationInfoBarTest : public CocoaProfileTest { ...@@ -96,13 +94,10 @@ class TranslationInfoBarTest : public CocoaProfileTest {
TranslateErrors::Type error = TranslateErrors::NONE; TranslateErrors::Type error = TranslateErrors::NONE;
if (type == translate::TRANSLATE_STEP_TRANSLATE_ERROR) if (type == translate::TRANSLATE_STEP_TRANSLATE_ERROR)
error = TranslateErrors::NETWORK; error = TranslateErrors::NETWORK;
Profile* profile =
Profile::FromBrowserContext(web_contents_->GetBrowserContext());
[[infobar_controller_ view] removeFromSuperview]; [[infobar_controller_ view] removeFromSuperview];
scoped_ptr<TranslateInfoBarDelegate> delegate( scoped_ptr<TranslateInfoBarDelegate> delegate(
new MockTranslateInfoBarDelegate(web_contents_.get(), type, error, new MockTranslateInfoBarDelegate(web_contents_.get(), type, error));
profile->GetPrefs()));
scoped_ptr<infobars::InfoBar> infobar( scoped_ptr<infobars::InfoBar> infobar(
TranslateInfoBarDelegate::CreateInfoBar(delegate.Pass())); TranslateInfoBarDelegate::CreateInfoBar(delegate.Pass()));
if (infobar_) if (infobar_)
......
...@@ -425,13 +425,6 @@ const char kSettingsSearchHelpURL[] = ...@@ -425,13 +425,6 @@ const char kSettingsSearchHelpURL[] =
"https://support.google.com/chrome/?p=settings_search_help"; "https://support.google.com/chrome/?p=settings_search_help";
#endif #endif
const char kAboutGoogleTranslateURL[] =
#if defined(OS_CHROMEOS)
"https://support.google.com/chromeos/?p=ib_translation_bar";
#else
"https://support.google.com/chrome/?p=ib_translation_bar";
#endif
const char kOmniboxLearnMoreURL[] = const char kOmniboxLearnMoreURL[] =
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
"https://support.google.com/chromeos/?p=settings_omnibox"; "https://support.google.com/chromeos/?p=settings_omnibox";
......
...@@ -381,9 +381,6 @@ extern const char kSupervisedUserManagementDisplayURL[]; ...@@ -381,9 +381,6 @@ extern const char kSupervisedUserManagementDisplayURL[];
// Help URL for the settings page's search feature. // Help URL for the settings page's search feature.
extern const char kSettingsSearchHelpURL[]; extern const char kSettingsSearchHelpURL[];
// "About" URL for the translate bar's options menu.
extern const char kAboutGoogleTranslateURL[];
// Help URL for the Omnibox setting. // Help URL for the Omnibox setting.
extern const char kOmniboxLearnMoreURL[]; extern const char kOmniboxLearnMoreURL[];
......
...@@ -10,8 +10,10 @@ ...@@ -10,8 +10,10 @@
#include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_details.h" #include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h" #include "content/public/browser/navigation_entry.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/render_view_host.h" #include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/common/referrer.h"
#include "url/gurl.h" #include "url/gurl.h"
ContentTranslateDriver::ContentTranslateDriver( ContentTranslateDriver::ContentTranslateDriver(
...@@ -123,3 +125,12 @@ int ContentTranslateDriver::GetCurrentPageID() { ...@@ -123,3 +125,12 @@ int ContentTranslateDriver::GetCurrentPageID() {
content::NavigationEntry* entry = navigation_controller_->GetActiveEntry(); content::NavigationEntry* entry = navigation_controller_->GetActiveEntry();
return entry->GetPageID(); return entry->GetPageID();
} }
void ContentTranslateDriver::OpenUrlInNewTab(const GURL& url) {
content::OpenURLParams params(url,
content::Referrer(),
NEW_FOREGROUND_TAB,
content::PAGE_TRANSITION_LINK,
false);
navigation_controller_->GetWebContents()->OpenURL(params);
}
...@@ -57,6 +57,7 @@ class ContentTranslateDriver : public TranslateDriver { ...@@ -57,6 +57,7 @@ class ContentTranslateDriver : public TranslateDriver {
virtual const GURL& GetVisibleURL() OVERRIDE; virtual const GURL& GetVisibleURL() OVERRIDE;
virtual bool HasCurrentPage() OVERRIDE; virtual bool HasCurrentPage() OVERRIDE;
virtual int GetCurrentPageID() OVERRIDE; virtual int GetCurrentPageID() OVERRIDE;
virtual void OpenUrlInNewTab(const GURL& url) OVERRIDE;
private: private:
// The navigation controller of the tab we are associated with. // The navigation controller of the tab we are associated with.
......
...@@ -69,6 +69,8 @@ class MockTranslateDriver : public TranslateDriver { ...@@ -69,6 +69,8 @@ class MockTranslateDriver : public TranslateDriver {
virtual int GetCurrentPageID() OVERRIDE { return 0; } virtual int GetCurrentPageID() OVERRIDE { return 0; }
virtual void OpenUrlInNewTab(const GURL& url) OVERRIDE {}
bool on_is_page_translated_changed_called() const { bool on_is_page_translated_changed_called() const {
return on_is_page_translated_changed_called_; return on_is_page_translated_changed_called_;
} }
......
...@@ -57,6 +57,9 @@ class TranslateDriver { ...@@ -57,6 +57,9 @@ class TranslateDriver {
// Returns an int identifying the current page. Should only be called if // Returns an int identifying the current page. Should only be called if
// |HasCurrentPage()| is true. // |HasCurrentPage()| is true.
virtual int GetCurrentPageID() = 0; virtual int GetCurrentPageID() = 0;
// Opens |url| in a new tab.
virtual void OpenUrlInNewTab(const GURL& url) = 0;
}; };
#endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DRIVER_H_ #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DRIVER_H_
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