OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/chrome_translate_client.h" | 5 #include "chrome/browser/translate/chrome_translate_client.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 #endif | 77 #endif |
78 | 78 |
79 } // namespace | 79 } // namespace |
80 | 80 |
81 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); | 81 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); |
82 | 82 |
83 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) | 83 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) |
84 : content::WebContentsObserver(web_contents), | 84 : content::WebContentsObserver(web_contents), |
85 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), | 85 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), |
86 translate_driver_(&web_contents->GetController()), | 86 translate_driver_(&web_contents->GetController()), |
87 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)), | 87 translate_manager_( |
88 cld_data_provider_(translate::CreateBrowserCldDataProviderFor( | 88 new translate::TranslateManager(this, prefs::kAcceptLanguages)), |
89 web_contents)), | 89 cld_data_provider_( |
| 90 translate::CreateBrowserCldDataProviderFor(web_contents)), |
90 weak_pointer_factory_(this) { | 91 weak_pointer_factory_(this) { |
91 #if defined(CLD_DATA_FROM_STANDALONE) | 92 #if defined(CLD_DATA_FROM_STANDALONE) |
92 if (!g_cld_file_path_initialized_) | 93 if (!g_cld_file_path_initialized_) |
93 InitCldFilePath(); | 94 InitCldFilePath(); |
94 #endif | 95 #endif |
95 } | 96 } |
96 | 97 |
97 ChromeTranslateClient::~ChromeTranslateClient() { | 98 ChromeTranslateClient::~ChromeTranslateClient() { |
98 } | 99 } |
99 | 100 |
100 LanguageState& ChromeTranslateClient::GetLanguageState() { | 101 translate::LanguageState& ChromeTranslateClient::GetLanguageState() { |
101 return translate_manager_->GetLanguageState(); | 102 return translate_manager_->GetLanguageState(); |
102 } | 103 } |
103 | 104 |
104 // static | 105 // static |
105 scoped_ptr<TranslatePrefs> ChromeTranslateClient::CreateTranslatePrefs( | 106 scoped_ptr<translate::TranslatePrefs> |
106 PrefService* prefs) { | 107 ChromeTranslateClient::CreateTranslatePrefs(PrefService* prefs) { |
107 #if defined(OS_CHROMEOS) | 108 #if defined(OS_CHROMEOS) |
108 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages; | 109 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages; |
109 #else | 110 #else |
110 const char* preferred_languages_prefs = NULL; | 111 const char* preferred_languages_prefs = NULL; |
111 #endif | 112 #endif |
112 return scoped_ptr<TranslatePrefs>(new TranslatePrefs( | 113 return scoped_ptr<translate::TranslatePrefs>(new translate::TranslatePrefs( |
113 prefs, prefs::kAcceptLanguages, preferred_languages_prefs)); | 114 prefs, prefs::kAcceptLanguages, preferred_languages_prefs)); |
114 } | 115 } |
115 | 116 |
116 // static | 117 // static |
117 TranslateAcceptLanguages* ChromeTranslateClient::GetTranslateAcceptLanguages( | 118 translate::TranslateAcceptLanguages* |
| 119 ChromeTranslateClient::GetTranslateAcceptLanguages( |
118 content::BrowserContext* browser_context) { | 120 content::BrowserContext* browser_context) { |
119 return TranslateAcceptLanguagesFactory::GetForBrowserContext(browser_context); | 121 return TranslateAcceptLanguagesFactory::GetForBrowserContext(browser_context); |
120 } | 122 } |
121 | 123 |
122 // static | 124 // static |
123 TranslateManager* ChromeTranslateClient::GetManagerFromWebContents( | 125 translate::TranslateManager* ChromeTranslateClient::GetManagerFromWebContents( |
124 content::WebContents* web_contents) { | 126 content::WebContents* web_contents) { |
125 ChromeTranslateClient* chrome_translate_client = | 127 ChromeTranslateClient* chrome_translate_client = |
126 FromWebContents(web_contents); | 128 FromWebContents(web_contents); |
127 if (!chrome_translate_client) | 129 if (!chrome_translate_client) |
128 return NULL; | 130 return NULL; |
129 return chrome_translate_client->GetTranslateManager(); | 131 return chrome_translate_client->GetTranslateManager(); |
130 } | 132 } |
131 | 133 |
132 // static | 134 // static |
133 void ChromeTranslateClient::GetTranslateLanguages( | 135 void ChromeTranslateClient::GetTranslateLanguages( |
134 content::WebContents* web_contents, | 136 content::WebContents* web_contents, |
135 std::string* source, | 137 std::string* source, |
136 std::string* target) { | 138 std::string* target) { |
137 DCHECK(source != NULL); | 139 DCHECK(source != NULL); |
138 DCHECK(target != NULL); | 140 DCHECK(target != NULL); |
139 | 141 |
140 ChromeTranslateClient* chrome_translate_client = | 142 ChromeTranslateClient* chrome_translate_client = |
141 FromWebContents(web_contents); | 143 FromWebContents(web_contents); |
142 if (!chrome_translate_client) | 144 if (!chrome_translate_client) |
143 return; | 145 return; |
144 | 146 |
145 *source = TranslateDownloadManager::GetLanguageCode( | 147 *source = translate::TranslateDownloadManager::GetLanguageCode( |
146 chrome_translate_client->GetLanguageState().original_language()); | 148 chrome_translate_client->GetLanguageState().original_language()); |
147 | 149 |
148 Profile* profile = | 150 Profile* profile = |
149 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 151 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
150 Profile* original_profile = profile->GetOriginalProfile(); | 152 Profile* original_profile = profile->GetOriginalProfile(); |
151 PrefService* prefs = original_profile->GetPrefs(); | 153 PrefService* prefs = original_profile->GetPrefs(); |
152 scoped_ptr<TranslatePrefs> translate_prefs = CreateTranslatePrefs(prefs); | 154 scoped_ptr<translate::TranslatePrefs> translate_prefs = |
| 155 CreateTranslatePrefs(prefs); |
153 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { | 156 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { |
154 std::string auto_translate_language = | 157 std::string auto_translate_language = |
155 TranslateManager::GetAutoTargetLanguage(*source, translate_prefs.get()); | 158 translate::TranslateManager::GetAutoTargetLanguage( |
| 159 *source, translate_prefs.get()); |
156 if (!auto_translate_language.empty()) { | 160 if (!auto_translate_language.empty()) { |
157 *target = auto_translate_language; | 161 *target = auto_translate_language; |
158 return; | 162 return; |
159 } | 163 } |
160 } | 164 } |
161 | 165 |
162 std::string accept_languages_str = prefs->GetString(prefs::kAcceptLanguages); | 166 std::string accept_languages_str = prefs->GetString(prefs::kAcceptLanguages); |
163 std::vector<std::string> accept_languages_list; | 167 std::vector<std::string> accept_languages_list; |
164 base::SplitString(accept_languages_str, ',', &accept_languages_list); | 168 base::SplitString(accept_languages_str, ',', &accept_languages_list); |
165 *target = TranslateManager::GetTargetLanguage(accept_languages_list); | 169 *target = |
| 170 translate::TranslateManager::GetTargetLanguage(accept_languages_list); |
166 } | 171 } |
167 | 172 |
168 TranslateManager* ChromeTranslateClient::GetTranslateManager() { | 173 translate::TranslateManager* ChromeTranslateClient::GetTranslateManager() { |
169 return translate_manager_.get(); | 174 return translate_manager_.get(); |
170 } | 175 } |
171 | 176 |
172 content::WebContents* ChromeTranslateClient::GetWebContents() { | 177 content::WebContents* ChromeTranslateClient::GetWebContents() { |
173 return web_contents(); | 178 return web_contents(); |
174 } | 179 } |
175 | 180 |
176 void ChromeTranslateClient::ShowTranslateUI(translate::TranslateStep step, | 181 void ChromeTranslateClient::ShowTranslateUI( |
177 const std::string source_language, | 182 translate::TranslateStep step, |
178 const std::string target_language, | 183 const std::string source_language, |
179 TranslateErrors::Type error_type, | 184 const std::string target_language, |
180 bool triggered_from_menu) { | 185 translate::TranslateErrors::Type error_type, |
| 186 bool triggered_from_menu) { |
181 DCHECK(web_contents()); | 187 DCHECK(web_contents()); |
182 if (error_type != TranslateErrors::NONE) | 188 if (error_type != translate::TranslateErrors::NONE) |
183 step = translate::TRANSLATE_STEP_TRANSLATE_ERROR; | 189 step = translate::TRANSLATE_STEP_TRANSLATE_ERROR; |
184 | 190 |
185 if (TranslateService::IsTranslateBubbleEnabled()) { | 191 if (TranslateService::IsTranslateBubbleEnabled()) { |
186 // Bubble UI. | 192 // Bubble UI. |
187 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) { | 193 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) { |
188 // TODO(droger): Move this logic out of UI code. | 194 // TODO(droger): Move this logic out of UI code. |
189 GetLanguageState().SetTranslateEnabled(true); | 195 GetLanguageState().SetTranslateEnabled(true); |
190 if (!GetLanguageState().HasLanguageChanged()) | 196 if (!GetLanguageState().HasLanguageChanged()) |
191 return; | 197 return; |
192 | 198 |
193 if (!triggered_from_menu) { | 199 if (!triggered_from_menu) { |
194 if (web_contents()->GetBrowserContext()->IsOffTheRecord()) | 200 if (web_contents()->GetBrowserContext()->IsOffTheRecord()) |
195 return; | 201 return; |
196 if (GetTranslatePrefs()->IsTooOftenDenied()) | 202 if (GetTranslatePrefs()->IsTooOftenDenied()) |
197 return; | 203 return; |
198 } | 204 } |
199 } | 205 } |
200 ShowBubble(step, error_type); | 206 ShowBubble(step, error_type); |
201 return; | 207 return; |
202 } | 208 } |
203 | 209 |
204 // Infobar UI. | 210 // Infobar UI. |
205 TranslateInfoBarDelegate::Create( | 211 translate::TranslateInfoBarDelegate::Create( |
206 step != translate::TRANSLATE_STEP_BEFORE_TRANSLATE, | 212 step != translate::TRANSLATE_STEP_BEFORE_TRANSLATE, |
207 translate_manager_->GetWeakPtr(), | 213 translate_manager_->GetWeakPtr(), |
208 InfoBarService::FromWebContents(web_contents()), | 214 InfoBarService::FromWebContents(web_contents()), |
209 web_contents()->GetBrowserContext()->IsOffTheRecord(), | 215 web_contents()->GetBrowserContext()->IsOffTheRecord(), |
210 step, | 216 step, |
211 source_language, | 217 source_language, |
212 target_language, | 218 target_language, |
213 error_type, | 219 error_type, |
214 triggered_from_menu); | 220 triggered_from_menu); |
215 } | 221 } |
216 | 222 |
217 TranslateDriver* ChromeTranslateClient::GetTranslateDriver() { | 223 translate::TranslateDriver* ChromeTranslateClient::GetTranslateDriver() { |
218 return &translate_driver_; | 224 return &translate_driver_; |
219 } | 225 } |
220 | 226 |
221 PrefService* ChromeTranslateClient::GetPrefs() { | 227 PrefService* ChromeTranslateClient::GetPrefs() { |
222 DCHECK(web_contents()); | 228 DCHECK(web_contents()); |
223 Profile* profile = | 229 Profile* profile = |
224 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 230 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
225 return profile->GetOriginalProfile()->GetPrefs(); | 231 return profile->GetOriginalProfile()->GetPrefs(); |
226 } | 232 } |
227 | 233 |
228 scoped_ptr<TranslatePrefs> ChromeTranslateClient::GetTranslatePrefs() { | 234 scoped_ptr<translate::TranslatePrefs> |
| 235 ChromeTranslateClient::GetTranslatePrefs() { |
229 DCHECK(web_contents()); | 236 DCHECK(web_contents()); |
230 Profile* profile = | 237 Profile* profile = |
231 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 238 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
232 return CreateTranslatePrefs(profile->GetPrefs()); | 239 return CreateTranslatePrefs(profile->GetPrefs()); |
233 } | 240 } |
234 | 241 |
235 TranslateAcceptLanguages* ChromeTranslateClient::GetTranslateAcceptLanguages() { | 242 translate::TranslateAcceptLanguages* |
| 243 ChromeTranslateClient::GetTranslateAcceptLanguages() { |
236 DCHECK(web_contents()); | 244 DCHECK(web_contents()); |
237 return GetTranslateAcceptLanguages(web_contents()->GetBrowserContext()); | 245 return GetTranslateAcceptLanguages(web_contents()->GetBrowserContext()); |
238 } | 246 } |
239 | 247 |
240 int ChromeTranslateClient::GetInfobarIconID() const { | 248 int ChromeTranslateClient::GetInfobarIconID() const { |
241 return IDR_INFOBAR_TRANSLATE; | 249 return IDR_INFOBAR_TRANSLATE; |
242 } | 250 } |
243 | 251 |
244 // ChromeTranslateClient::CreateInfoBar() is implemented in platform-specific | 252 // ChromeTranslateClient::CreateInfoBar() is implemented in platform-specific |
245 // files, except the TOOLKIT_VIEWS implementation, which has been removed. | 253 // files, except the TOOLKIT_VIEWS implementation, which has been removed. |
246 #if defined(TOOLKIT_VIEWS) | 254 #if defined(TOOLKIT_VIEWS) |
247 scoped_ptr<infobars::InfoBar> ChromeTranslateClient::CreateInfoBar( | 255 scoped_ptr<infobars::InfoBar> ChromeTranslateClient::CreateInfoBar( |
248 scoped_ptr<TranslateInfoBarDelegate> delegate) const { | 256 scoped_ptr<translate::TranslateInfoBarDelegate> delegate) const { |
249 return scoped_ptr<infobars::InfoBar>(); | 257 return scoped_ptr<infobars::InfoBar>(); |
250 } | 258 } |
251 #endif | 259 #endif |
252 | 260 |
253 bool ChromeTranslateClient::IsTranslatableURL(const GURL& url) { | 261 bool ChromeTranslateClient::IsTranslatableURL(const GURL& url) { |
254 return TranslateService::IsTranslatableURL(url); | 262 return TranslateService::IsTranslatableURL(url); |
255 } | 263 } |
256 | 264 |
257 void ChromeTranslateClient::ShowReportLanguageDetectionErrorUI( | 265 void ChromeTranslateClient::ShowReportLanguageDetectionErrorUI( |
258 const GURL& report_url) { | 266 const GURL& report_url) { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 FROM_HERE, | 379 FROM_HERE, |
372 base::Bind(&ChromeTranslateClient::InitiateTranslation, | 380 base::Bind(&ChromeTranslateClient::InitiateTranslation, |
373 weak_pointer_factory_.GetWeakPtr(), | 381 weak_pointer_factory_.GetWeakPtr(), |
374 page_lang, | 382 page_lang, |
375 attempt + 1), | 383 attempt + 1), |
376 base::TimeDelta::FromMilliseconds(backoff)); | 384 base::TimeDelta::FromMilliseconds(backoff)); |
377 return; | 385 return; |
378 } | 386 } |
379 | 387 |
380 translate_manager_->InitiateTranslation( | 388 translate_manager_->InitiateTranslation( |
381 TranslateDownloadManager::GetLanguageCode(page_lang)); | 389 translate::TranslateDownloadManager::GetLanguageCode(page_lang)); |
382 } | 390 } |
383 | 391 |
384 void ChromeTranslateClient::OnTranslateAssignedSequenceNumber(int page_seq_no) { | 392 void ChromeTranslateClient::OnTranslateAssignedSequenceNumber(int page_seq_no) { |
385 translate_manager_->set_current_seq_no(page_seq_no); | 393 translate_manager_->set_current_seq_no(page_seq_no); |
386 } | 394 } |
387 | 395 |
388 void ChromeTranslateClient::OnLanguageDetermined( | 396 void ChromeTranslateClient::OnLanguageDetermined( |
389 const LanguageDetectionDetails& details, | 397 const translate::LanguageDetectionDetails& details, |
390 bool page_needs_translation) { | 398 bool page_needs_translation) { |
391 GetLanguageState().LanguageDetermined(details.adopted_language, | 399 GetLanguageState().LanguageDetermined(details.adopted_language, |
392 page_needs_translation); | 400 page_needs_translation); |
393 | 401 |
394 if (web_contents()) | 402 if (web_contents()) |
395 translate_manager_->InitiateTranslation(details.adopted_language); | 403 translate_manager_->InitiateTranslation(details.adopted_language); |
396 | 404 |
397 content::NotificationService::current()->Notify( | 405 content::NotificationService::current()->Notify( |
398 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 406 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
399 content::Source<content::WebContents>(web_contents()), | 407 content::Source<content::WebContents>(web_contents()), |
400 content::Details<const LanguageDetectionDetails>(&details)); | 408 content::Details<const translate::LanguageDetectionDetails>(&details)); |
401 } | 409 } |
402 | 410 |
403 void ChromeTranslateClient::OnPageTranslated(const std::string& original_lang, | 411 void ChromeTranslateClient::OnPageTranslated( |
404 const std::string& translated_lang, | 412 const std::string& original_lang, |
405 TranslateErrors::Type error_type) { | 413 const std::string& translated_lang, |
| 414 translate::TranslateErrors::Type error_type) { |
406 DCHECK(web_contents()); | 415 DCHECK(web_contents()); |
407 translate_manager_->PageTranslated( | 416 translate_manager_->PageTranslated( |
408 original_lang, translated_lang, error_type); | 417 original_lang, translated_lang, error_type); |
409 | 418 |
410 PageTranslatedDetails details; | 419 translate::PageTranslatedDetails details; |
411 details.source_language = original_lang; | 420 details.source_language = original_lang; |
412 details.target_language = translated_lang; | 421 details.target_language = translated_lang; |
413 details.error_type = error_type; | 422 details.error_type = error_type; |
414 content::NotificationService::current()->Notify( | 423 content::NotificationService::current()->Notify( |
415 chrome::NOTIFICATION_PAGE_TRANSLATED, | 424 chrome::NOTIFICATION_PAGE_TRANSLATED, |
416 content::Source<content::WebContents>(web_contents()), | 425 content::Source<content::WebContents>(web_contents()), |
417 content::Details<PageTranslatedDetails>(&details)); | 426 content::Details<translate::PageTranslatedDetails>(&details)); |
418 } | 427 } |
419 | 428 |
420 void ChromeTranslateClient::ShowBubble(translate::TranslateStep step, | 429 void ChromeTranslateClient::ShowBubble( |
421 TranslateErrors::Type error_type) { | 430 translate::TranslateStep step, |
| 431 translate::TranslateErrors::Type error_type) { |
422 // The bubble is implemented only on the desktop platforms. | 432 // The bubble is implemented only on the desktop platforms. |
423 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 433 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
424 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 434 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
425 | 435 |
426 // |browser| might be NULL when testing. In this case, Show(...) should be | 436 // |browser| might be NULL when testing. In this case, Show(...) should be |
427 // called because the implementation for testing is used. | 437 // called because the implementation for testing is used. |
428 if (!browser) { | 438 if (!browser) { |
429 TranslateBubbleFactory::Show(NULL, web_contents(), step, error_type); | 439 TranslateBubbleFactory::Show(NULL, web_contents(), step, error_type); |
430 return; | 440 return; |
431 } | 441 } |
(...skipping 17 matching lines...) Expand all Loading... |
449 if (GetLanguageState().InTranslateNavigation()) | 459 if (GetLanguageState().InTranslateNavigation()) |
450 return; | 460 return; |
451 } | 461 } |
452 | 462 |
453 TranslateBubbleFactory::Show( | 463 TranslateBubbleFactory::Show( |
454 browser->window(), web_contents(), step, error_type); | 464 browser->window(), web_contents(), step, error_type); |
455 #else | 465 #else |
456 NOTREACHED(); | 466 NOTREACHED(); |
457 #endif | 467 #endif |
458 } | 468 } |
OLD | NEW |