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