Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: chrome/browser/translate/translate_tab_helper.cc

Issue 166963002: TranslateManager is no longer a singleton (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@temp
Patch Set: rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/translate/translate_tab_helper.h" 5 #include "chrome/browser/translate/translate_tab_helper.h"
6 6
7 #include "chrome/browser/chrome_notification_types.h" 7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/translate/translate_accept_languages_factory.h" 9 #include "chrome/browser/translate/translate_accept_languages_factory.h"
10 #include "chrome/browser/translate/translate_infobar_delegate.h" 10 #include "chrome/browser/translate/translate_infobar_delegate.h"
11 #include "chrome/browser/translate/translate_service.h" 11 #include "chrome/browser/translate/translate_service.h"
12 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h" 13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_window.h" 14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" 15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/browser/ui/translate/translate_bubble_factory.h" 16 #include "chrome/browser/ui/translate/translate_bubble_factory.h"
17 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/render_messages.h" 18 #include "chrome/common/render_messages.h"
19 #include "components/translate/core/browser/page_translated_details.h" 19 #include "components/translate/core/browser/page_translated_details.h"
20 #include "components/translate/core/browser/translate_accept_languages.h" 20 #include "components/translate/core/browser/translate_accept_languages.h"
21 #include "components/translate/core/browser/translate_prefs.h" 21 #include "components/translate/core/browser/translate_prefs.h"
22 #include "components/translate/core/common/language_detection_details.h" 22 #include "components/translate/core/common/language_detection_details.h"
23 #include "content/public/browser/notification_service.h" 23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/web_contents.h" 24 #include "content/public/browser/web_contents.h"
25 25
26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper); 26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper);
27 27
28 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents) 28 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents)
29 : content::WebContentsObserver(web_contents), 29 : content::WebContentsObserver(web_contents),
30 translate_driver_(&web_contents->GetController()) { 30 translate_driver_(&web_contents->GetController()),
31 } 31 translate_manager_(this) {}
32 32
33 TranslateTabHelper::~TranslateTabHelper() { 33 TranslateTabHelper::~TranslateTabHelper() {
34 } 34 }
35 35
36 LanguageState& TranslateTabHelper::GetLanguageState() { 36 LanguageState& TranslateTabHelper::GetLanguageState() {
37 return translate_driver_.language_state(); 37 return translate_driver_.language_state();
38 } 38 }
39 39
40 // static 40 // static
41 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs( 41 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs(
42 PrefService* prefs) { 42 PrefService* prefs) {
43 #if defined(OS_CHROMEOS) 43 #if defined(OS_CHROMEOS)
44 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages; 44 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages;
45 #else 45 #else
46 const char* preferred_languages_prefs = NULL; 46 const char* preferred_languages_prefs = NULL;
47 #endif 47 #endif
48 return scoped_ptr<TranslatePrefs>(new TranslatePrefs( 48 return scoped_ptr<TranslatePrefs>(new TranslatePrefs(
49 prefs, prefs::kAcceptLanguages, preferred_languages_prefs)); 49 prefs, prefs::kAcceptLanguages, preferred_languages_prefs));
50 } 50 }
51 51
52 // static 52 // static
53 TranslateAcceptLanguages* TranslateTabHelper::GetTranslateAcceptLanguages( 53 TranslateAcceptLanguages* TranslateTabHelper::GetTranslateAcceptLanguages(
54 content::BrowserContext* browser_context) { 54 content::BrowserContext* browser_context) {
55 return TranslateAcceptLanguagesFactory::GetForBrowserContext(browser_context); 55 return TranslateAcceptLanguagesFactory::GetForBrowserContext(browser_context);
56 } 56 }
57 57
58 // static
59 TranslateManager* TranslateTabHelper::GetManagerFromWebContents(
60 content::WebContents* web_contents) {
61 return FromWebContents(web_contents)->GetTranslateManager();
blundell 2014/02/17 15:26:28 This should short-circuit out if the TranslateTabH
droger 2014/02/17 16:52:48 Done.
62 }
63
64 TranslateManager* TranslateTabHelper::GetTranslateManager() {
65 return &translate_manager_;
66 }
67
68 content::WebContents* TranslateTabHelper::GetWebContents() {
69 return content::WebContentsObserver::web_contents();
blundell 2014/02/17 15:26:28 Can't this body just be web_contents()?
droger 2014/02/17 16:52:48 Done.
70 }
71
58 void TranslateTabHelper::ShowTranslateUI(TranslateTabHelper::TranslateStep step, 72 void TranslateTabHelper::ShowTranslateUI(TranslateTabHelper::TranslateStep step,
59 content::WebContents* web_contents,
60 const std::string source_language, 73 const std::string source_language,
61 const std::string target_language, 74 const std::string target_language,
62 TranslateErrors::Type error_type) { 75 TranslateErrors::Type error_type) {
63 if (error_type != TranslateErrors::NONE) 76 if (error_type != TranslateErrors::NONE)
64 step = TranslateTabHelper::TRANSLATE_ERROR; 77 step = TranslateTabHelper::TRANSLATE_ERROR;
65 78
66 if (TranslateService::IsTranslateBubbleEnabled()) { 79 if (TranslateService::IsTranslateBubbleEnabled()) {
67 // Bubble UI. 80 // Bubble UI.
68 if (step == BEFORE_TRANSLATE) { 81 if (step == BEFORE_TRANSLATE) {
69 // TODO: Move this logic out of UI code. 82 // TODO: Move this logic out of UI code.
70 GetLanguageState().SetTranslateEnabled(true); 83 GetLanguageState().SetTranslateEnabled(true);
71 if (!GetLanguageState().HasLanguageChanged()) 84 if (!GetLanguageState().HasLanguageChanged())
72 return; 85 return;
73 } 86 }
74 ShowBubble(web_contents, step, error_type); 87 ShowBubble(step, error_type);
75 return; 88 return;
76 } 89 }
77 90
78 // Infobar UI. 91 // Infobar UI.
79 Profile* profile = 92 Profile* profile =
80 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 93 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
81 Profile* original_profile = profile->GetOriginalProfile(); 94 Profile* original_profile = profile->GetOriginalProfile();
82 TranslateInfoBarDelegate::Create(step != BEFORE_TRANSLATE, 95 TranslateInfoBarDelegate::Create(step != BEFORE_TRANSLATE,
83 web_contents, 96 web_contents(),
84 step, 97 step,
85 source_language, 98 source_language,
86 target_language, 99 target_language,
87 error_type, 100 error_type,
88 original_profile->GetPrefs()); 101 original_profile->GetPrefs());
89 } 102 }
90 103
91 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) { 104 bool TranslateTabHelper::OnMessageReceived(const IPC::Message& message) {
92 bool handled = true; 105 bool handled = true;
93 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message) 106 IPC_BEGIN_MESSAGE_MAP(TranslateTabHelper, message)
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 PageTranslatedDetails details; 141 PageTranslatedDetails details;
129 details.source_language = original_lang; 142 details.source_language = original_lang;
130 details.target_language = translated_lang; 143 details.target_language = translated_lang;
131 details.error_type = error_type; 144 details.error_type = error_type;
132 content::NotificationService::current()->Notify( 145 content::NotificationService::current()->Notify(
133 chrome::NOTIFICATION_PAGE_TRANSLATED, 146 chrome::NOTIFICATION_PAGE_TRANSLATED,
134 content::Source<content::WebContents>(web_contents()), 147 content::Source<content::WebContents>(web_contents()),
135 content::Details<PageTranslatedDetails>(&details)); 148 content::Details<PageTranslatedDetails>(&details));
136 } 149 }
137 150
138 void TranslateTabHelper::ShowBubble(content::WebContents* web_contents, 151 void TranslateTabHelper::ShowBubble(TranslateTabHelper::TranslateStep step,
139 TranslateTabHelper::TranslateStep step,
140 TranslateErrors::Type error_type) { 152 TranslateErrors::Type error_type) {
141 // The bubble is implemented only on the desktop platforms. 153 // The bubble is implemented only on the desktop platforms.
142 #if !defined(OS_ANDROID) && !defined(OS_IOS) 154 #if !defined(OS_ANDROID) && !defined(OS_IOS)
143 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); 155 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
144 156
145 // |browser| might be NULL when testing. In this case, Show(...) should be 157 // |browser| might be NULL when testing. In this case, Show(...) should be
146 // called because the implementation for testing is used. 158 // called because the implementation for testing is used.
147 if (!browser) { 159 if (!browser) {
148 TranslateBubbleFactory::Show(NULL, web_contents, step, error_type); 160 TranslateBubbleFactory::Show(NULL, web_contents(), step, error_type);
149 return; 161 return;
150 } 162 }
151 163
152 if (web_contents != browser->tab_strip_model()->GetActiveWebContents()) 164 if (web_contents() != browser->tab_strip_model()->GetActiveWebContents())
153 return; 165 return;
154 166
155 // This ShowBubble function is also used for upating the existing bubble. 167 // This ShowBubble function is also used for upating the existing bubble.
156 // However, with the bubble shown, any browser windows are NOT activated 168 // However, with the bubble shown, any browser windows are NOT activated
157 // because the bubble takes the focus from the other widgets including the 169 // 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 170 // browser windows. So it is checked that |browser| is the last activated
159 // browser, not is now activated. 171 // browser, not is now activated.
160 if (browser != 172 if (browser !=
161 chrome::FindLastActiveWithHostDesktopType(browser->host_desktop_type())) { 173 chrome::FindLastActiveWithHostDesktopType(browser->host_desktop_type())) {
162 return; 174 return;
163 } 175 }
164 176
165 // During auto-translating, the bubble should not be shown. 177 // During auto-translating, the bubble should not be shown.
166 if (step == TranslateTabHelper::TRANSLATING || 178 if (step == TranslateTabHelper::TRANSLATING ||
167 step == TranslateTabHelper::AFTER_TRANSLATE) { 179 step == TranslateTabHelper::AFTER_TRANSLATE) {
168 if (GetLanguageState().InTranslateNavigation()) 180 if (GetLanguageState().InTranslateNavigation())
169 return; 181 return;
170 } 182 }
171 183
172 TranslateBubbleFactory::Show( 184 TranslateBubbleFactory::Show(
173 browser->window(), web_contents, step, error_type); 185 browser->window(), web_contents(), step, error_type);
174 #else 186 #else
175 NOTREACHED(); 187 NOTREACHED();
176 #endif 188 #endif
177 } 189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698