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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 #include "grit/browser_resources.h" | 49 #include "grit/browser_resources.h" |
50 #include "net/base/escape.h" | 50 #include "net/base/escape.h" |
51 #include "net/base/load_flags.h" | 51 #include "net/base/load_flags.h" |
52 #include "net/base/url_util.h" | 52 #include "net/base/url_util.h" |
53 #include "net/url_request/url_fetcher.h" | 53 #include "net/url_request/url_fetcher.h" |
54 #include "net/url_request/url_request_status.h" | 54 #include "net/url_request/url_request_status.h" |
55 #include "ui/base/resource/resource_bundle.h" | 55 #include "ui/base/resource/resource_bundle.h" |
56 | 56 |
57 #ifdef FILE_MANAGER_EXTENSION | 57 #ifdef FILE_MANAGER_EXTENSION |
58 #include "chrome/browser/chromeos/extensions/file_manager_util.h" | 58 #include "chrome/browser/chromeos/extensions/file_manager_util.h" |
| 59 #include "extensions/common/constants.h" |
59 #endif | 60 #endif |
60 | 61 |
61 using content::NavigationController; | 62 using content::NavigationController; |
62 using content::NavigationEntry; | 63 using content::NavigationEntry; |
63 using content::WebContents; | 64 using content::WebContents; |
64 | 65 |
65 namespace { | 66 namespace { |
66 | 67 |
67 // The default list of languages the Google translation server supports. | 68 // The default list of languages the Google translation server supports. |
68 // We use this list until we receive the list that the server exposes. | 69 // We use this list until we receive the list that the server exposes. |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 } | 178 } |
178 | 179 |
179 // static | 180 // static |
180 TranslateManager* TranslateManager::GetInstance() { | 181 TranslateManager* TranslateManager::GetInstance() { |
181 return Singleton<TranslateManager>::get(); | 182 return Singleton<TranslateManager>::get(); |
182 } | 183 } |
183 | 184 |
184 // static | 185 // static |
185 bool TranslateManager::IsTranslatableURL(const GURL& url) { | 186 bool TranslateManager::IsTranslatableURL(const GURL& url) { |
186 // A URLs is translatable unless it is one of the following: | 187 // A URLs is translatable unless it is one of the following: |
| 188 // - empty (can happen for popups created with window.open("")) |
187 // - an internal URL (chrome:// and others) | 189 // - an internal URL (chrome:// and others) |
188 // - the devtools (which is considered UI) | 190 // - the devtools (which is considered UI) |
| 191 // - Chrome OS file manager extension |
189 // - an FTP page (as FTP pages tend to have long lists of filenames that may | 192 // - an FTP page (as FTP pages tend to have long lists of filenames that may |
190 // confuse the CLD) | 193 // confuse the CLD) |
191 return !url.SchemeIs(chrome::kChromeUIScheme) && | 194 return !url.is_empty() && |
| 195 !url.SchemeIs(chrome::kChromeUIScheme) && |
192 !url.SchemeIs(chrome::kChromeDevToolsScheme) && | 196 !url.SchemeIs(chrome::kChromeDevToolsScheme) && |
| 197 #ifdef FILE_MANAGER_EXTENSION |
| 198 !(url.SchemeIs(extensions::kExtensionScheme) && |
| 199 url.DomainIs(kFileBrowserDomain)) && |
| 200 #endif |
193 !url.SchemeIs(chrome::kFtpScheme); | 201 !url.SchemeIs(chrome::kFtpScheme); |
194 } | 202 } |
195 | 203 |
196 // static | 204 // static |
197 void TranslateManager::SetSupportedLanguages(const std::string& language_list) { | 205 void TranslateManager::SetSupportedLanguages(const std::string& language_list) { |
198 // The format is: | 206 // The format is: |
199 // sl({'sl': {'XX': 'LanguageName', ...}, 'tl': {'XX': 'LanguageName', ...}}) | 207 // sl({'sl': {'XX': 'LanguageName', ...}, 'tl': {'XX': 'LanguageName', ...}}) |
200 // Where "sl(" is set in kLanguageListCallbackName | 208 // Where "sl(" is set in kLanguageListCallbackName |
201 // and 'tl' is kTargetLanguagesKey | 209 // and 'tl' is kTargetLanguagesKey |
202 if (!StartsWithASCII(language_list, kLanguageListCallbackName, false) || | 210 if (!StartsWithASCII(language_list, kLanguageListCallbackName, false) || |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 content::NotificationService::AllSources()); | 488 content::NotificationService::AllSources()); |
481 notification_registrar_.Add(this, | 489 notification_registrar_.Add(this, |
482 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 490 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
483 content::NotificationService::AllSources()); | 491 content::NotificationService::AllSources()); |
484 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, | 492 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, |
485 content::NotificationService::AllSources()); | 493 content::NotificationService::AllSources()); |
486 } | 494 } |
487 | 495 |
488 void TranslateManager::InitiateTranslation(WebContents* web_contents, | 496 void TranslateManager::InitiateTranslation(WebContents* web_contents, |
489 const std::string& page_lang) { | 497 const std::string& page_lang) { |
490 #ifdef FILE_MANAGER_EXTENSION | |
491 const GURL& page_url = web_contents->GetURL(); | |
492 if (page_url.SchemeIs("chrome-extension") && | |
493 page_url.DomainIs(kFileBrowserDomain)) | |
494 return; | |
495 #endif | |
496 | |
497 Profile* profile = | 498 Profile* profile = |
498 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 499 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
499 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); | 500 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); |
500 if (!prefs->GetBoolean(prefs::kEnableTranslate)) | 501 if (!prefs->GetBoolean(prefs::kEnableTranslate)) |
501 return; | 502 return; |
502 | 503 |
503 // Allow disabling of translate from the command line to assist with | 504 // Allow disabling of translate from the command line to assist with |
504 // automated browser testing. | 505 // automated browser testing. |
505 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) | 506 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) |
506 return; | 507 return; |
507 | 508 |
508 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | |
509 if (!entry) { | |
510 // This can happen for popups created with window.open(""). | |
511 return; | |
512 } | |
513 | |
514 std::string target_lang = GetTargetLanguage(prefs); | 509 std::string target_lang = GetTargetLanguage(prefs); |
515 std::string language_code = GetLanguageCode(page_lang); | 510 std::string language_code = GetLanguageCode(page_lang); |
516 // Nothing to do if either the language Chrome is in or the language of the | 511 // Nothing to do if either the language Chrome is in or the language of the |
517 // page is not supported by the translation server. | 512 // page is not supported by the translation server. |
518 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { | 513 if (target_lang.empty() || !IsSupportedLanguage(language_code)) { |
519 return; | 514 return; |
520 } | 515 } |
521 | 516 |
522 // We don't want to translate: | 517 // We don't want to translate: |
523 // - any Chrome specific page (New Tab Page, Download, History... pages). | 518 // - any Chrome specific page (New Tab Page, Download, History... pages). |
524 // - similar languages (ex: en-US to en). | 519 // - similar languages (ex: en-US to en). |
525 // - any user black-listed URLs or user selected language combination. | 520 // - any user black-listed URLs or user selected language combination. |
526 // - any language the user configured as accepted languages. | 521 // - any language the user configured as accepted languages. |
527 if (!IsTranslatableURL(entry->GetURL()) || language_code == target_lang || | 522 GURL page_url = web_contents->GetURL(); |
528 !TranslatePrefs::CanTranslate(prefs, language_code, entry->GetURL()) || | 523 if (!IsTranslatableURL(page_url) || |
| 524 language_code == target_lang || |
| 525 !TranslatePrefs::CanTranslate(prefs, language_code, page_url) || |
529 IsAcceptLanguage(web_contents, language_code)) { | 526 IsAcceptLanguage(web_contents, language_code)) { |
530 return; | 527 return; |
531 } | 528 } |
532 | 529 |
533 // If the user has previously selected "always translate" for this language we | 530 // If the user has previously selected "always translate" for this language we |
534 // automatically translate. Note that in incognito mode we disable that | 531 // automatically translate. Note that in incognito mode we disable that |
535 // feature; the user will get an infobar, so they can control whether the | 532 // feature; the user will get an infobar, so they can control whether the |
536 // page's text is sent to the translate server. | 533 // page's text is sent to the translate server. |
537 std::string auto_target_lang; | 534 std::string auto_target_lang; |
538 if (!web_contents->GetBrowserContext()->IsOffTheRecord() && | 535 if (!web_contents->GetBrowserContext()->IsOffTheRecord() && |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
872 // list or not at all if no such candidate exists | 869 // list or not at all if no such candidate exists |
873 std::vector<std::string>::iterator iter; | 870 std::vector<std::string>::iterator iter; |
874 for (iter = accept_langs_list.begin(); | 871 for (iter = accept_langs_list.begin(); |
875 iter != accept_langs_list.end(); ++iter) { | 872 iter != accept_langs_list.end(); ++iter) { |
876 std::string lang_code = GetLanguageCode(*iter); | 873 std::string lang_code = GetLanguageCode(*iter); |
877 if (IsSupportedLanguage(lang_code)) | 874 if (IsSupportedLanguage(lang_code)) |
878 return lang_code; | 875 return lang_code; |
879 } | 876 } |
880 return std::string(); | 877 return std::string(); |
881 } | 878 } |
OLD | NEW |