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" |
11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/stringprintf.h" |
14 #include "base/string_split.h" | 15 #include "base/string_split.h" |
15 #include "base/string_util.h" | 16 #include "base/string_util.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/infobars/infobar_tab_helper.h" | 19 #include "chrome/browser/infobars/infobar_tab_helper.h" |
19 #include "chrome/browser/prefs/pref_service.h" | 20 #include "chrome/browser/prefs/pref_service.h" |
20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/tab_contents/language_state.h" | 22 #include "chrome/browser/tab_contents/language_state.h" |
22 #include "chrome/browser/tab_contents/tab_util.h" | 23 #include "chrome/browser/tab_contents/tab_util.h" |
23 #include "chrome/browser/tabs/tab_strip_model.h" | 24 #include "chrome/browser/tabs/tab_strip_model.h" |
(...skipping 16 matching lines...) Expand all Loading... |
40 #include "content/public/browser/notification_details.h" | 41 #include "content/public/browser/notification_details.h" |
41 #include "content/public/browser/notification_service.h" | 42 #include "content/public/browser/notification_service.h" |
42 #include "content/public/browser/notification_source.h" | 43 #include "content/public/browser/notification_source.h" |
43 #include "content/public/browser/notification_types.h" | 44 #include "content/public/browser/notification_types.h" |
44 #include "content/public/browser/render_process_host.h" | 45 #include "content/public/browser/render_process_host.h" |
45 #include "content/public/browser/render_view_host.h" | 46 #include "content/public/browser/render_view_host.h" |
46 #include "content/public/browser/web_contents.h" | 47 #include "content/public/browser/web_contents.h" |
47 #include "content/public/common/url_fetcher.h" | 48 #include "content/public/common/url_fetcher.h" |
48 #include "grit/browser_resources.h" | 49 #include "grit/browser_resources.h" |
49 #include "net/base/escape.h" | 50 #include "net/base/escape.h" |
| 51 #include "net/base/load_flags.h" |
50 #include "net/url_request/url_request_status.h" | 52 #include "net/url_request/url_request_status.h" |
51 #include "ui/base/resource/resource_bundle.h" | 53 #include "ui/base/resource/resource_bundle.h" |
52 | 54 |
53 #ifdef FILE_MANAGER_EXTENSION | 55 #ifdef FILE_MANAGER_EXTENSION |
54 #include "chrome/browser/chromeos/extensions/file_manager_util.h" | 56 #include "chrome/browser/chromeos/extensions/file_manager_util.h" |
55 #endif | 57 #endif |
56 | 58 |
57 using content::NavigationController; | 59 using content::NavigationController; |
58 using content::NavigationEntry; | 60 using content::NavigationEntry; |
59 using content::WebContents; | 61 using content::WebContents; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 }; | 150 }; |
149 | 151 |
150 const LanguageCodeSynonym kLanguageCodeSynonyms[] = { | 152 const LanguageCodeSynonym kLanguageCodeSynonyms[] = { |
151 {"no", "nb"}, | 153 {"no", "nb"}, |
152 {"iw", "he"}, | 154 {"iw", "he"}, |
153 {"jw", "jv"}, | 155 {"jw", "jv"}, |
154 }; | 156 }; |
155 | 157 |
156 const char* const kTranslateScriptURL = | 158 const char* const kTranslateScriptURL = |
157 "https://translate.google.com/translate_a/element.js?" | 159 "https://translate.google.com/translate_a/element.js?" |
158 "cb=cr.googleTranslate.onTranslateElementLoad"; | 160 "cb=cr.googleTranslate.onTranslateElementLoad&hl=%s"; |
159 const char* const kTranslateScriptHeader = | 161 const char* const kTranslateScriptHeader = |
160 "Google-Translate-Element-Mode: library"; | 162 "Google-Translate-Element-Mode: library"; |
161 const char* const kReportLanguageDetectionErrorURL = | 163 const char* const kReportLanguageDetectionErrorURL = |
162 // TODO(palmer): bug 112236. Make this https://. | 164 // TODO(palmer): bug 112236. Make this https://. |
163 "http://translate.google.com/translate_error"; | 165 "http://translate.google.com/translate_error"; |
164 const char* const kLanguageListFetchURL = | 166 const char* const kLanguageListFetchURL = |
165 "https://translate.googleapis.com/translate_a/l?client=chrome&cb=sl"; | 167 "https://translate.googleapis.com/translate_a/l?client=chrome&cb=sl&hl=%s"; |
166 const int kMaxRetryLanguageListFetch = 5; | 168 const int kMaxRetryLanguageListFetch = 5; |
167 const int kTranslateScriptExpirationDelayDays = 1; | 169 const int kTranslateScriptExpirationDelayDays = 1; |
168 | 170 |
169 } // namespace | 171 } // namespace |
170 | 172 |
171 // This must be kept in sync with the &cb= value in the kLanguageListFetchURL. | 173 // This must be kept in sync with the &cb= value in the kLanguageListFetchURL. |
172 const char* const TranslateManager::kLanguageListCallbackName = "sl("; | 174 const char* const TranslateManager::kLanguageListCallbackName = "sl("; |
173 const char* const TranslateManager::kTargetLanguagesKey = "tl"; | 175 const char* const TranslateManager::kTargetLanguagesKey = "tl"; |
174 | 176 |
175 // static | 177 // static |
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
800 return; | 802 return; |
801 | 803 |
802 // We don't want to do this when translate is disabled. | 804 // We don't want to do this when translate is disabled. |
803 DCHECK(prefs != NULL); | 805 DCHECK(prefs != NULL); |
804 if (CommandLine::ForCurrentProcess()->HasSwitch( | 806 if (CommandLine::ForCurrentProcess()->HasSwitch( |
805 switches::kDisableTranslate) || | 807 switches::kDisableTranslate) || |
806 (prefs != NULL && !prefs->GetBoolean(prefs::kEnableTranslate))) { | 808 (prefs != NULL && !prefs->GetBoolean(prefs::kEnableTranslate))) { |
807 return; | 809 return; |
808 } | 810 } |
809 | 811 |
| 812 std::string language_list_fetch_url = base::StringPrintf( |
| 813 kLanguageListFetchURL, |
| 814 GetLanguageCode(g_browser_process->GetApplicationLocale()).c_str()); |
810 language_list_request_pending_.reset(content::URLFetcher::Create( | 815 language_list_request_pending_.reset(content::URLFetcher::Create( |
811 1, GURL(kLanguageListFetchURL), content::URLFetcher::GET, this)); | 816 1, GURL(language_list_fetch_url), content::URLFetcher::GET, this)); |
| 817 language_list_request_pending_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 818 net::LOAD_DO_NOT_SAVE_COOKIES); |
812 language_list_request_pending_->SetRequestContext( | 819 language_list_request_pending_->SetRequestContext( |
813 g_browser_process->system_request_context()); | 820 g_browser_process->system_request_context()); |
814 language_list_request_pending_->SetMaxRetries(kMaxRetryLanguageListFetch); | 821 language_list_request_pending_->SetMaxRetries(kMaxRetryLanguageListFetch); |
815 language_list_request_pending_->Start(); | 822 language_list_request_pending_->Start(); |
816 } | 823 } |
817 | 824 |
818 void TranslateManager::CleanupPendingUlrFetcher() { | 825 void TranslateManager::CleanupPendingUlrFetcher() { |
819 language_list_request_pending_.reset(); | 826 language_list_request_pending_.reset(); |
820 translate_script_request_pending_.reset(); | 827 translate_script_request_pending_.reset(); |
821 } | 828 } |
822 | 829 |
823 void TranslateManager::RequestTranslateScript() { | 830 void TranslateManager::RequestTranslateScript() { |
824 if (translate_script_request_pending_.get() != NULL) | 831 if (translate_script_request_pending_.get() != NULL) |
825 return; | 832 return; |
826 | 833 |
| 834 std::string translate_script_url = base::StringPrintf( |
| 835 kTranslateScriptURL, |
| 836 GetLanguageCode(g_browser_process->GetApplicationLocale()).c_str()); |
827 translate_script_request_pending_.reset(content::URLFetcher::Create( | 837 translate_script_request_pending_.reset(content::URLFetcher::Create( |
828 0, GURL(kTranslateScriptURL), content::URLFetcher::GET, this)); | 838 0, GURL(translate_script_url), content::URLFetcher::GET, this)); |
| 839 translate_script_request_pending_->SetLoadFlags( |
| 840 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); |
829 translate_script_request_pending_->SetRequestContext( | 841 translate_script_request_pending_->SetRequestContext( |
830 g_browser_process->system_request_context()); | 842 g_browser_process->system_request_context()); |
831 translate_script_request_pending_->SetExtraRequestHeaders( | 843 translate_script_request_pending_->SetExtraRequestHeaders( |
832 kTranslateScriptHeader); | 844 kTranslateScriptHeader); |
833 translate_script_request_pending_->Start(); | 845 translate_script_request_pending_->Start(); |
834 } | 846 } |
835 | 847 |
836 void TranslateManager::ShowInfoBar(content::WebContents* tab, | 848 void TranslateManager::ShowInfoBar(content::WebContents* tab, |
837 TranslateInfoBarDelegate* infobar) { | 849 TranslateInfoBarDelegate* infobar) { |
838 DCHECK(infobar != NULL); | 850 DCHECK(infobar != NULL); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
886 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); | 898 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
887 | 899 |
888 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { | 900 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { |
889 TranslateInfoBarDelegate* delegate = | 901 TranslateInfoBarDelegate* delegate = |
890 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 902 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
891 if (delegate) | 903 if (delegate) |
892 return delegate; | 904 return delegate; |
893 } | 905 } |
894 return NULL; | 906 return NULL; |
895 } | 907 } |
OLD | NEW |