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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 #include "content/public/browser/notification_source.h" | 43 #include "content/public/browser/notification_source.h" |
44 #include "content/public/browser/notification_types.h" | 44 #include "content/public/browser/notification_types.h" |
45 #include "content/public/browser/render_process_host.h" | 45 #include "content/public/browser/render_process_host.h" |
46 #include "content/public/browser/render_view_host.h" | 46 #include "content/public/browser/render_view_host.h" |
47 #include "content/public/browser/web_contents.h" | 47 #include "content/public/browser/web_contents.h" |
48 #include "google_apis/google_api_keys.h" | 48 #include "google_apis/google_api_keys.h" |
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/http/http_status_code.h" | |
53 #include "net/url_request/url_fetcher.h" | 54 #include "net/url_request/url_fetcher.h" |
54 #include "net/url_request/url_request_status.h" | 55 #include "net/url_request/url_request_status.h" |
55 #include "ui/base/resource/resource_bundle.h" | 56 #include "ui/base/resource/resource_bundle.h" |
56 | 57 |
57 #ifdef FILE_MANAGER_EXTENSION | 58 #ifdef FILE_MANAGER_EXTENSION |
58 #include "chrome/browser/chromeos/extensions/file_manager_util.h" | 59 #include "chrome/browser/chromeos/extensions/file_manager_util.h" |
59 #endif | 60 #endif |
60 | 61 |
61 using content::NavigationController; | 62 using content::NavigationController; |
62 using content::NavigationEntry; | 63 using content::NavigationEntry; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 "th", // Thai | 129 "th", // Thai |
129 "tr", // Turkish | 130 "tr", // Turkish |
130 "uk", // Ukrainian | 131 "uk", // Ukrainian |
131 "vi", // Vietnamese | 132 "vi", // Vietnamese |
132 "cy", // Welsh | 133 "cy", // Welsh |
133 "yi", // Yiddish | 134 "yi", // Yiddish |
134 }; | 135 }; |
135 | 136 |
136 const char* const kTranslateScriptURL = | 137 const char* const kTranslateScriptURL = |
137 "https://translate.google.com/translate_a/element.js"; | 138 "https://translate.google.com/translate_a/element.js"; |
138 const char* const kTranslateScriptQuery = | |
139 "?cb=cr.googleTranslate.onTranslateElementLoad&hl=%s"; | |
140 const char* const kTranslateScriptHeader = | 139 const char* const kTranslateScriptHeader = |
141 "Google-Translate-Element-Mode: library"; | 140 "Google-Translate-Element-Mode: library"; |
142 const char* const kReportLanguageDetectionErrorURL = | 141 const char* const kReportLanguageDetectionErrorURL = |
143 "https://translate.google.com/translate_error"; | 142 "https://translate.google.com/translate_error?client=cr&action=langidc"; |
144 const char* const kLanguageListFetchURL = | 143 const char* const kLanguageListFetchURL = |
145 "https://translate.googleapis.com/translate_a/l?client=chrome&cb=sl"; | 144 "https://translate.googleapis.com/translate_a/l?client=chrome&cb=sl"; |
146 | 145 |
147 const char* const kLanguageListFetchLocaleQueryName = "hl"; | 146 // Used in kTranslateScriptURL to request supporting languages list including |
148 const char* const kLanguageListFetchAlphaLanguageQueryName = "alpha"; | 147 // "alpha languages". |
149 const char* const kLanguageListFetchAlphaLanguageQueryValue = "1"; | 148 const char* const kAlphaLanguageQueryName = "alpha"; |
149 const char* const kAlphaLanguageQueryValue = "1"; | |
150 | |
151 // Used in all translate URLs to specify API Key. | |
152 const char* const kApiKeyName = "key"; | |
153 | |
154 // Used in kTranslateScriptURL to specify a callback function name. | |
155 const char* const kCallbackQueryName = "cb"; | |
156 const char* const kCallbackQueryValue = | |
157 "cr.googleTranslate.onTranslateElementLoad"; | |
158 | |
159 // Used in kTranslateScriptURL and kLanguageListFetchURL to specify the | |
160 // application locale. | |
161 const char* const kHostLocaleQueryName = "hl"; | |
162 | |
163 // Used in kReportLanguageDetectionErrorURL to specify the original page | |
164 // language. | |
165 const char* const kSourceLanguageQueryName = "sl"; | |
166 | |
167 // Used in kReportLanguageDetectionErrorURL to specify the page URL. | |
168 const char* const kUrlQueryName = "u"; | |
150 | 169 |
151 const int kMaxRetryLanguageListFetch = 5; | 170 const int kMaxRetryLanguageListFetch = 5; |
152 const int kTranslateScriptExpirationDelayDays = 1; | 171 const int kTranslateScriptExpirationDelayDays = 1; |
153 | 172 |
154 void AddApiKeyToUrl(GURL* url) { | 173 GURL AddApiKeyToUrl(const GURL& url) { |
155 std::string api_key = google_apis::GetAPIKey(); | 174 return net::AppendQueryParameter(url, kApiKeyName, google_apis::GetAPIKey()); |
156 std::string query(url->query()); | 175 } |
157 if (!query.empty()) | 176 |
158 query += "&"; | 177 GURL AddHostLocaleToUrl(const GURL& url) { |
159 query += "key=" + net::EscapeQueryParamValue(api_key, true); | 178 return net::AppendQueryParameter( |
160 GURL::Replacements replacements; | 179 url, |
161 replacements.SetQueryStr(query); | 180 kHostLocaleQueryName, |
162 *url = url->ReplaceComponents(replacements); | 181 TranslateManager::GetLanguageCode( |
182 g_browser_process->GetApplicationLocale())); | |
163 } | 183 } |
164 | 184 |
165 } // namespace | 185 } // namespace |
166 | 186 |
167 // This must be kept in sync with the &cb= value in the kLanguageListFetchURL. | 187 // This must be kept in sync with the &cb= value in the kLanguageListFetchURL. |
168 const char* const TranslateManager::kLanguageListCallbackName = "sl("; | 188 const char* const TranslateManager::kLanguageListCallbackName = "sl("; |
169 const char* const TranslateManager::kTargetLanguagesKey = "tl"; | 189 const char* const TranslateManager::kTargetLanguagesKey = "tl"; |
170 | 190 |
171 // static | 191 // static |
172 base::LazyInstance<std::set<std::string> > | 192 base::LazyInstance<std::set<std::string> > |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 } | 317 } |
298 | 318 |
299 TranslateTabHelper* translate_tab_helper = | 319 TranslateTabHelper* translate_tab_helper = |
300 TranslateTabHelper::FromWebContents(controller->GetWebContents()); | 320 TranslateTabHelper::FromWebContents(controller->GetWebContents()); |
301 if (!translate_tab_helper) | 321 if (!translate_tab_helper) |
302 return; | 322 return; |
303 | 323 |
304 // If the navigation happened while offline don't show the translate | 324 // If the navigation happened while offline don't show the translate |
305 // bar since there will be nothing to translate. | 325 // bar since there will be nothing to translate. |
306 if (load_details->http_status_code == 0 || | 326 if (load_details->http_status_code == 0 || |
307 load_details->http_status_code == 500) { | 327 load_details->http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) { |
308 return; | 328 return; |
309 } | 329 } |
310 | 330 |
311 if (!load_details->is_main_frame && | 331 if (!load_details->is_main_frame && |
312 translate_tab_helper->language_state().translation_declined()) { | 332 translate_tab_helper->language_state().translation_declined()) { |
313 // Some sites (such as Google map) may trigger sub-frame navigations | 333 // Some sites (such as Google map) may trigger sub-frame navigations |
314 // when the user interacts with the page. We don't want to show a new | 334 // when the user interacts with the page. We don't want to show a new |
315 // infobar if the user already dismissed one in that case. | 335 // infobar if the user already dismissed one in that case. |
316 return; | 336 return; |
317 } | 337 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
392 if (translate_script_request_pending_.get() != source && | 412 if (translate_script_request_pending_.get() != source && |
393 language_list_request_pending_.get() != source) { | 413 language_list_request_pending_.get() != source) { |
394 // Looks like crash on Mac is possibly caused with callback entering here | 414 // Looks like crash on Mac is possibly caused with callback entering here |
395 // with unknown fetcher when network is refreshed. | 415 // with unknown fetcher when network is refreshed. |
396 scoped_ptr<const net::URLFetcher> delete_ptr(source); | 416 scoped_ptr<const net::URLFetcher> delete_ptr(source); |
397 return; | 417 return; |
398 } | 418 } |
399 | 419 |
400 bool error = | 420 bool error = |
401 (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || | 421 (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || |
402 source->GetResponseCode() != 200); | 422 source->GetResponseCode() != net::HTTP_OK); |
403 if (translate_script_request_pending_.get() == source) { | 423 if (translate_script_request_pending_.get() == source) { |
404 scoped_ptr<const net::URLFetcher> delete_ptr( | 424 scoped_ptr<const net::URLFetcher> delete_ptr( |
405 translate_script_request_pending_.release()); | 425 translate_script_request_pending_.release()); |
406 if (!error) { | 426 if (!error) { |
407 base::StringPiece str = ResourceBundle::GetSharedInstance(). | 427 base::StringPiece str = ResourceBundle::GetSharedInstance(). |
408 GetRawDataResource(IDR_TRANSLATE_JS); | 428 GetRawDataResource(IDR_TRANSLATE_JS); |
409 DCHECK(translate_script_.empty()); | 429 DCHECK(translate_script_.empty()); |
410 str.CopyToString(&translate_script_); | 430 str.CopyToString(&translate_script_); |
411 std::string argument = "('"; | 431 std::string argument = "('"; |
412 std::string api_key = google_apis::GetAPIKey(); | 432 std::string api_key = google_apis::GetAPIKey(); |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
634 | 654 |
635 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { | 655 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { |
636 UMA_HISTOGRAM_COUNTS("Translate.ReportLanguageDetectionError", 1); | 656 UMA_HISTOGRAM_COUNTS("Translate.ReportLanguageDetectionError", 1); |
637 // We'll open the URL in a new tab so that the user can tell us more. | 657 // We'll open the URL in a new tab so that the user can tell us more. |
638 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 658 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
639 if (!browser) { | 659 if (!browser) { |
640 NOTREACHED(); | 660 NOTREACHED(); |
641 return; | 661 return; |
642 } | 662 } |
643 | 663 |
664 GURL report_error_url = GURL(kReportLanguageDetectionErrorURL); | |
665 | |
644 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); | 666 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); |
645 // Report option should be disabled for secure URLs. | 667 report_error_url = net::AppendQueryParameter( |
646 DCHECK(!page_url.SchemeIsSecure()); | 668 report_error_url, |
MAD
2013/04/08 17:00:10
Did you feel this DCHECK is not useful anymore, or
Takashi Toyoshima
2013/04/08 17:07:02
Actually, I enabled error report on secure URLs an
| |
647 std::string report_error_url_str(kReportLanguageDetectionErrorURL); | 669 kUrlQueryName, |
648 report_error_url_str += "?client=cr&action=langidc&u="; | 670 page_url.spec()); |
649 report_error_url_str += net::EscapeUrlEncodedData(page_url.spec(), true); | |
650 report_error_url_str += "&sl="; | |
651 | 671 |
652 TranslateTabHelper* translate_tab_helper = | 672 TranslateTabHelper* translate_tab_helper = |
653 TranslateTabHelper::FromWebContents(web_contents); | 673 TranslateTabHelper::FromWebContents(web_contents); |
654 report_error_url_str += | 674 report_error_url = net::AppendQueryParameter( |
655 translate_tab_helper->language_state().original_language(); | 675 report_error_url, |
656 report_error_url_str += "&hl="; | 676 kSourceLanguageQueryName, |
657 report_error_url_str += | 677 translate_tab_helper->language_state().original_language()); |
658 GetLanguageCode(g_browser_process->GetApplicationLocale()); | |
659 | 678 |
660 GURL report_error_url(report_error_url_str); | 679 report_error_url = AddHostLocaleToUrl(report_error_url); |
661 AddApiKeyToUrl(&report_error_url); | 680 report_error_url = AddApiKeyToUrl(report_error_url); |
681 | |
662 chrome::AddSelectedTabWithURL(browser, report_error_url, | 682 chrome::AddSelectedTabWithURL(browser, report_error_url, |
663 content::PAGE_TRANSITION_AUTO_BOOKMARK); | 683 content::PAGE_TRANSITION_AUTO_BOOKMARK); |
664 } | 684 } |
665 | 685 |
666 void TranslateManager::DoTranslatePage(WebContents* web_contents, | 686 void TranslateManager::DoTranslatePage(WebContents* web_contents, |
667 const std::string& translate_script, | 687 const std::string& translate_script, |
668 const std::string& source_lang, | 688 const std::string& source_lang, |
669 const std::string& target_lang) { | 689 const std::string& target_lang) { |
670 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 690 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
671 if (!entry) { | 691 if (!entry) { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
776 | 796 |
777 // We don't want to do this when translate is disabled. | 797 // We don't want to do this when translate is disabled. |
778 DCHECK(prefs != NULL); | 798 DCHECK(prefs != NULL); |
779 if (CommandLine::ForCurrentProcess()->HasSwitch( | 799 if (CommandLine::ForCurrentProcess()->HasSwitch( |
780 switches::kDisableTranslate) || | 800 switches::kDisableTranslate) || |
781 (prefs != NULL && !prefs->GetBoolean(prefs::kEnableTranslate))) { | 801 (prefs != NULL && !prefs->GetBoolean(prefs::kEnableTranslate))) { |
782 return; | 802 return; |
783 } | 803 } |
784 | 804 |
785 GURL language_list_fetch_url = GURL(kLanguageListFetchURL); | 805 GURL language_list_fetch_url = GURL(kLanguageListFetchURL); |
786 language_list_fetch_url = net::AppendQueryParameter( | 806 language_list_fetch_url = AddHostLocaleToUrl(language_list_fetch_url); |
787 language_list_fetch_url, | 807 language_list_fetch_url = AddApiKeyToUrl(language_list_fetch_url); |
788 kLanguageListFetchLocaleQueryName, | |
789 GetLanguageCode(g_browser_process->GetApplicationLocale()).c_str()); | |
790 | 808 |
791 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 809 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
792 if (command_line.HasSwitch(switches::kEnableTranslateAlphaLanguages)) { | 810 if (command_line.HasSwitch(switches::kEnableTranslateAlphaLanguages)) { |
793 language_list_fetch_url = net::AppendQueryParameter( | 811 language_list_fetch_url = net::AppendQueryParameter( |
794 language_list_fetch_url, | 812 language_list_fetch_url, |
795 kLanguageListFetchAlphaLanguageQueryName, | 813 kAlphaLanguageQueryName, |
796 kLanguageListFetchAlphaLanguageQueryValue); | 814 kAlphaLanguageQueryValue); |
797 } | 815 } |
798 AddApiKeyToUrl(&language_list_fetch_url); | 816 |
799 VLOG(9) << "Fetch supporting language list from: " | 817 VLOG(9) << "Fetch supporting language list from: " |
800 << language_list_fetch_url.spec().c_str(); | 818 << language_list_fetch_url.spec().c_str(); |
801 | 819 |
802 language_list_request_pending_.reset(net::URLFetcher::Create( | 820 language_list_request_pending_.reset(net::URLFetcher::Create( |
803 1, language_list_fetch_url, net::URLFetcher::GET, this)); | 821 1, language_list_fetch_url, net::URLFetcher::GET, this)); |
804 language_list_request_pending_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 822 language_list_request_pending_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
805 net::LOAD_DO_NOT_SAVE_COOKIES); | 823 net::LOAD_DO_NOT_SAVE_COOKIES); |
806 language_list_request_pending_->SetRequestContext( | 824 language_list_request_pending_->SetRequestContext( |
807 g_browser_process->system_request_context()); | 825 g_browser_process->system_request_context()); |
808 language_list_request_pending_->SetMaxRetriesOn5xx( | 826 language_list_request_pending_->SetMaxRetriesOn5xx( |
809 kMaxRetryLanguageListFetch); | 827 kMaxRetryLanguageListFetch); |
810 language_list_request_pending_->Start(); | 828 language_list_request_pending_->Start(); |
811 } | 829 } |
812 | 830 |
813 void TranslateManager::CleanupPendingUlrFetcher() { | 831 void TranslateManager::CleanupPendingUlrFetcher() { |
814 language_list_request_pending_.reset(); | 832 language_list_request_pending_.reset(); |
815 translate_script_request_pending_.reset(); | 833 translate_script_request_pending_.reset(); |
816 } | 834 } |
817 | 835 |
818 void TranslateManager::RequestTranslateScript() { | 836 void TranslateManager::RequestTranslateScript() { |
819 if (translate_script_request_pending_.get() != NULL) | 837 if (translate_script_request_pending_.get() != NULL) |
820 return; | 838 return; |
821 | 839 |
822 GURL translate_script_url; | 840 GURL translate_script_url; |
823 std::string translate_script; | |
824 // Check if command-line contains an alternative URL for translate service. | 841 // Check if command-line contains an alternative URL for translate service. |
825 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 842 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
826 if (command_line.HasSwitch(switches::kTranslateScriptURL)) { | 843 if (command_line.HasSwitch(switches::kTranslateScriptURL)) { |
827 translate_script = std::string( | 844 translate_script_url = GURL( |
828 command_line.GetSwitchValueASCII(switches::kTranslateScriptURL)); | 845 command_line.GetSwitchValueASCII(switches::kTranslateScriptURL)); |
829 translate_script_url = GURL(translate_script.c_str()); | |
830 if (!translate_script_url.is_valid() || | 846 if (!translate_script_url.is_valid() || |
831 !translate_script_url.query().empty()) { | 847 !translate_script_url.query().empty()) { |
832 LOG(WARNING) << "The following translate URL specified at the " | 848 LOG(WARNING) << "The following translate URL specified at the " |
833 << "command-line is invalid: " << translate_script; | 849 << "command-line is invalid: " |
834 translate_script.clear(); | 850 << translate_script_url.spec(); |
851 translate_script_url = GURL(); | |
835 } | 852 } |
836 } | 853 } |
837 // Use default URL when command-line argument is not specified, or specified | 854 // Use default URL when command-line argument is not specified, or specified |
838 // URL is invalid. | 855 // URL is invalid. |
839 if (translate_script.empty()) | 856 if (translate_script_url.is_empty()) |
840 translate_script = std::string(kTranslateScriptURL); | 857 translate_script_url = GURL(kTranslateScriptURL); |
841 | 858 |
842 translate_script += std::string(kTranslateScriptQuery); | 859 translate_script_url = net::AppendQueryParameter( |
843 translate_script_url = GURL(base::StringPrintf( | 860 translate_script_url, |
844 translate_script.c_str(), | 861 kCallbackQueryName, |
845 GetLanguageCode(g_browser_process->GetApplicationLocale()).c_str())); | 862 kCallbackQueryValue); |
846 AddApiKeyToUrl(&translate_script_url); | 863 translate_script_url = AddHostLocaleToUrl(translate_script_url); |
864 translate_script_url = AddApiKeyToUrl(translate_script_url); | |
865 | |
847 translate_script_request_pending_.reset(net::URLFetcher::Create( | 866 translate_script_request_pending_.reset(net::URLFetcher::Create( |
848 0, translate_script_url, net::URLFetcher::GET, this)); | 867 0, translate_script_url, net::URLFetcher::GET, this)); |
849 translate_script_request_pending_->SetLoadFlags( | 868 translate_script_request_pending_->SetLoadFlags( |
850 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); | 869 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); |
851 translate_script_request_pending_->SetRequestContext( | 870 translate_script_request_pending_->SetRequestContext( |
852 g_browser_process->system_request_context()); | 871 g_browser_process->system_request_context()); |
853 translate_script_request_pending_->SetExtraRequestHeaders( | 872 translate_script_request_pending_->SetExtraRequestHeaders( |
854 kTranslateScriptHeader); | 873 kTranslateScriptHeader); |
855 translate_script_request_pending_->Start(); | 874 translate_script_request_pending_->Start(); |
856 } | 875 } |
(...skipping 15 matching lines...) Expand all Loading... | |
872 // list or not at all if no such candidate exists | 891 // list or not at all if no such candidate exists |
873 std::vector<std::string>::iterator iter; | 892 std::vector<std::string>::iterator iter; |
874 for (iter = accept_langs_list.begin(); | 893 for (iter = accept_langs_list.begin(); |
875 iter != accept_langs_list.end(); ++iter) { | 894 iter != accept_langs_list.end(); ++iter) { |
876 std::string lang_code = GetLanguageCode(*iter); | 895 std::string lang_code = GetLanguageCode(*iter); |
877 if (IsSupportedLanguage(lang_code)) | 896 if (IsSupportedLanguage(lang_code)) |
878 return lang_code; | 897 return lang_code; |
879 } | 898 } |
880 return std::string(); | 899 return std::string(); |
881 } | 900 } |
OLD | NEW |