NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / translate / translate_tab_helper.cc
blob35a6b4fbf5c290b392df6245e3512cd24041dd37
1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/translate/translate_tab_helper.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/translate/translate_accept_languages_factory.h"
10 #include "chrome/browser/translate/translate_infobar_delegate.h"
11 #include "chrome/browser/translate/translate_service.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/browser/ui/translate/translate_bubble_factory.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/render_messages.h"
19 #include "components/translate/core/browser/page_translated_details.h"
20 #include "components/translate/core/browser/translate_accept_languages.h"
21 #include "components/translate/core/browser/translate_prefs.h"
22 #include "components/translate/core/common/language_detection_details.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/web_contents.h"
26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper);
28 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents)
29 : content::WebContentsObserver(web_contents),
30 translate_driver_(&web_contents->GetController()) {
33 TranslateTabHelper::~TranslateTabHelper() {
36 LanguageState& TranslateTabHelper::GetLanguageState() {
37 return translate_driver_.language_state();
40 // static
41 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs(
42 PrefService* prefs) {
43 #if defined(OS_CHROMEOS)
44 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages;
45 #else
46 const char* preferred_languages_prefs = NULL;
47 #endif
48 return scoped_ptr<TranslatePrefs>(new TranslatePrefs(
49 prefs, prefs::kAcceptLanguages, preferred_languages_prefs));
52 // static
53 TranslateAcceptLanguages* TranslateTabHelper::GetTranslateAcceptLanguages(
54 content::BrowserContext* browser_context) {
55 return TranslateAcceptLanguagesFactory::GetForBrowserContext(browser_context);
58 void TranslateTabHelper::ShowTranslateUI(TranslateTabHelper::TranslateStep step,
59 content::WebContents* web_contents,
60 const std::string source_language,
61 const std::string target_language,
62 TranslateErrors::Type error_type) {
63 if (error_type != TranslateErrors::NONE)
64 step = TranslateTabHelper::TRANSLATE_ERROR;
66 if (TranslateService::IsTranslateBubbleEnabled()) {
67 // Bubble UI.
68 if (step == BEFORE_TRANSLATE) {
69 // TODO: Move this logic out of UI code.
70 GetLanguageState().SetTranslateEnabled(true);
71 if (!GetLanguageState().HasLanguageChanged())
72 return;
74 ShowBubble(web_contents, step, error_type);
75 return;
78 // Infobar UI.
79 Profile* profile =
80 Profile::FromBrowserContext(web_contents->GetBrowserContext());
81 Profile* original_profile = profile->GetOriginalProfile();
82 TranslateInfoBarDelegate::Create(step != BEFORE_TRANSLATE,
83 web_contents,
84 step,
85 source_language,
86 target_language,
87 error_type,
88 original_profile->GetPrefs());
91 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) {
92 bool handled = true;
93 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message)
94 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined,
95 OnLanguageDetermined)
96 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated)
97 IPC_MESSAGE_UNHANDLED(handled = false)
98 IPC_END_MESSAGE_MAP()
100 return handled;
103 void TranslateTabHelper::DidNavigateAnyFrame(
104 const content::LoadCommittedDetails& details,
105 const content::FrameNavigateParams& params) {
106 // Let the LanguageState clear its state.
107 translate_driver_.DidNavigate(details);
110 void TranslateTabHelper::OnLanguageDetermined(
111 const LanguageDetectionDetails& details,
112 bool page_needs_translation) {
113 translate_driver_.language_state().LanguageDetermined(
114 details.adopted_language, page_needs_translation);
116 content::NotificationService::current()->Notify(
117 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
118 content::Source<content::WebContents>(web_contents()),
119 content::Details<const LanguageDetectionDetails>(&details));
122 void TranslateTabHelper::OnPageTranslated(int32 page_id,
123 const std::string& original_lang,
124 const std::string& translated_lang,
125 TranslateErrors::Type error_type) {
126 translate_driver_.language_state().SetCurrentLanguage(translated_lang);
127 translate_driver_.language_state().set_translation_pending(false);
128 PageTranslatedDetails details;
129 details.source_language = original_lang;
130 details.target_language = translated_lang;
131 details.error_type = error_type;
132 content::NotificationService::current()->Notify(
133 chrome::NOTIFICATION_PAGE_TRANSLATED,
134 content::Source<content::WebContents>(web_contents()),
135 content::Details<PageTranslatedDetails>(&details));
138 void TranslateTabHelper::ShowBubble(content::WebContents* web_contents,
139 TranslateTabHelper::TranslateStep step,
140 TranslateErrors::Type error_type) {
141 // The bubble is implemented only on the desktop platforms.
142 #if !defined(OS_ANDROID) && !defined(OS_IOS)
143 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
145 // |browser| might be NULL when testing. In this case, Show(...) should be
146 // called because the implementation for testing is used.
147 if (!browser) {
148 TranslateBubbleFactory::Show(NULL, web_contents, step, error_type);
149 return;
152 if (web_contents != browser->tab_strip_model()->GetActiveWebContents())
153 return;
155 // This ShowBubble function is also used for upating the existing bubble.
156 // However, with the bubble shown, any browser windows are NOT activated
157 // because the bubble takes the focus from the other widgets including the
158 // browser windows. So it is checked that |browser| is the last activated
159 // browser, not is now activated.
160 if (browser !=
161 chrome::FindLastActiveWithHostDesktopType(browser->host_desktop_type())) {
162 return;
165 // During auto-translating, the bubble should not be shown.
166 if (step == TranslateTabHelper::TRANSLATING ||
167 step == TranslateTabHelper::AFTER_TRANSLATE) {
168 if (GetLanguageState().InTranslateNavigation())
169 return;
172 TranslateBubbleFactory::Show(
173 browser->window(), web_contents, step, error_type);
174 #else
175 NOTREACHED();
176 #endif