OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_manager.h" | 5 #include "chrome/browser/translate/translate_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 } | 171 } |
172 | 172 |
173 // static | 173 // static |
174 TranslateManager* TranslateManager::GetInstance() { | 174 TranslateManager* TranslateManager::GetInstance() { |
175 return Singleton<TranslateManager>::get(); | 175 return Singleton<TranslateManager>::get(); |
176 } | 176 } |
177 | 177 |
178 // static | 178 // static |
179 bool TranslateManager::IsTranslatableURL(const GURL& url) { | 179 bool TranslateManager::IsTranslatableURL(const GURL& url) { |
180 // A URLs is translatable unless it is one of the following: | 180 // A URLs is translatable unless it is one of the following: |
| 181 // - empty (can happen for popups created with window.open("")) |
181 // - an internal URL (chrome:// and others) | 182 // - an internal URL (chrome:// and others) |
182 // - the devtools (which is considered UI) | 183 // - the devtools (which is considered UI) |
| 184 // - Chrome OS file manager extension |
183 // - an FTP page (as FTP pages tend to have long lists of filenames that may | 185 // - an FTP page (as FTP pages tend to have long lists of filenames that may |
184 // confuse the CLD) | 186 // confuse the CLD) |
185 return !url.SchemeIs(chrome::kChromeUIScheme) && | 187 return !url.is_empty() && |
| 188 !url.SchemeIs(chrome::kChromeUIScheme) && |
186 !url.SchemeIs(chrome::kChromeDevToolsScheme) && | 189 !url.SchemeIs(chrome::kChromeDevToolsScheme) && |
| 190 #ifdef FILE_MANAGER_EXTENSION |
| 191 !(url.SchemeIs(chrome::kChromeExtension) && |
| 192 url.DomainIs(kFileBrowserDomain)) && |
| 193 #endif |
187 !url.SchemeIs(chrome::kFtpScheme); | 194 !url.SchemeIs(chrome::kFtpScheme); |
188 } | 195 } |
189 | 196 |
190 // static | 197 // static |
191 void TranslateManager::SetSupportedLanguages(const std::string& language_list) { | 198 void TranslateManager::SetSupportedLanguages(const std::string& language_list) { |
192 // The format is: | 199 // The format is: |
193 // sl({'sl': {'XX': 'LanguageName', ...}, 'tl': {'XX': 'LanguageName', ...}}) | 200 // sl({'sl': {'XX': 'LanguageName', ...}, 'tl': {'XX': 'LanguageName', ...}}) |
194 // Where "sl(" is set in kLanguageListCallbackName | 201 // Where "sl(" is set in kLanguageListCallbackName |
195 // and 'tl' is kTargetLanguagesKey | 202 // and 'tl' is kTargetLanguagesKey |
196 if (!StartsWithASCII(language_list, kLanguageListCallbackName, false) || | 203 if (!StartsWithASCII(language_list, kLanguageListCallbackName, false) || |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 content::NotificationService::AllSources()); | 481 content::NotificationService::AllSources()); |
475 notification_registrar_.Add(this, | 482 notification_registrar_.Add(this, |
476 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 483 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
477 content::NotificationService::AllSources()); | 484 content::NotificationService::AllSources()); |
478 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, | 485 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, |
479 content::NotificationService::AllSources()); | 486 content::NotificationService::AllSources()); |
480 } | 487 } |
481 | 488 |
482 void TranslateManager::InitiateTranslation(WebContents* web_contents, | 489 void TranslateManager::InitiateTranslation(WebContents* web_contents, |
483 const std::string& page_lang) { | 490 const std::string& page_lang) { |
484 #ifdef FILE_MANAGER_EXTENSION | |
485 const GURL& page_url = web_contents->GetURL(); | |
486 if (page_url.SchemeIs("chrome-extension") && | |
487 page_url.DomainIs(kFileBrowserDomain)) | |
488 return; | |
489 #endif | |
490 | |
491 Profile* profile = | 491 Profile* profile = |
492 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 492 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
493 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); | 493 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); |
494 if (!prefs->GetBoolean(prefs::kEnableTranslate)) | 494 if (!prefs->GetBoolean(prefs::kEnableTranslate)) |
495 return; | 495 return; |
496 | 496 |
497 // Allow disabling of translate from the command line to assist with | 497 // Allow disabling of translate from the command line to assist with |
498 // automated browser testing. | 498 // automated browser testing. |
499 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) | 499 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) |
500 return; | 500 return; |
501 | 501 |
502 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | |
503 if (!entry) { | |
504 // This can happen for popups created with window.open(""). | |
505 return; | |
506 } | |
507 | |
508 std::string target_lang = GetTargetLanguage(prefs); | 502 std::string target_lang = GetTargetLanguage(prefs); |
509 std::string language_code = GetLanguageCode(page_lang); | 503 std::string language_code = GetLanguageCode(page_lang); |
510 // Nothing to do if either the language Chrome is in or the language of the | 504 // Nothing to do if either the language Chrome is in or the language of the |
511 // page is not supported by the translation server. | 505 // page is not supported by the translation server. |
512 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { | 506 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { |
513 return; | 507 return; |
514 } | 508 } |
515 | 509 |
516 // We don't want to translate: | 510 // We don't want to translate: |
517 // - any Chrome specific page (New Tab Page, Download, History... pages). | 511 // - any Chrome specific page (New Tab Page, Download, History... pages). |
518 // - similar languages (ex: en-US to en). | 512 // - similar languages (ex: en-US to en). |
519 // - any user black-listed URLs or user selected language combination. | 513 // - any user black-listed URLs or user selected language combination. |
520 // - any language the user configured as accepted languages. | 514 // - any language the user configured as accepted languages. |
521 if (!IsTranslatableURL(entry->GetURL()) || language_code == target_lang || | 515 GURL page_url = web_contents->GetURL(); |
522 !TranslatePrefs::CanTranslate(prefs, language_code, entry->GetURL()) || | 516 if (!IsTranslatableURL(page_url) || |
| 517 language_code == target_lang || |
| 518 !TranslatePrefs::CanTranslate(prefs, language_code, page_url) || |
523 IsAcceptLanguage(web_contents, language_code)) { | 519 IsAcceptLanguage(web_contents, language_code)) { |
524 return; | 520 return; |
525 } | 521 } |
526 | 522 |
527 // If the user has previously selected "always translate" for this language we | 523 // If the user has previously selected "always translate" for this language we |
528 // automatically translate. Note that in incognito mode we disable that | 524 // automatically translate. Note that in incognito mode we disable that |
529 // feature; the user will get an infobar, so they can control whether the | 525 // feature; the user will get an infobar, so they can control whether the |
530 // page's text is sent to the translate server. | 526 // page's text is sent to the translate server. |
531 std::string auto_target_lang; | 527 std::string auto_target_lang; |
532 if (!web_contents->GetBrowserContext()->IsOffTheRecord() && | 528 if (!web_contents->GetBrowserContext()->IsOffTheRecord() && |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 // list or not at all if no such candidate exists | 850 // list or not at all if no such candidate exists |
855 std::vector<std::string>::iterator iter; | 851 std::vector<std::string>::iterator iter; |
856 for (iter = accept_langs_list.begin(); | 852 for (iter = accept_langs_list.begin(); |
857 iter != accept_langs_list.end(); ++iter) { | 853 iter != accept_langs_list.end(); ++iter) { |
858 std::string lang_code = GetLanguageCode(*iter); | 854 std::string lang_code = GetLanguageCode(*iter); |
859 if (IsSupportedLanguage(lang_code)) | 855 if (IsSupportedLanguage(lang_code)) |
860 return lang_code; | 856 return lang_code; |
861 } | 857 } |
862 return std::string(); | 858 return std::string(); |
863 } | 859 } |
OLD | NEW |