Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: chrome/browser/translate/translate_manager.cc

Issue 10914264: Send API keys with translate API URL requests. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 28 matching lines...) Expand all
39 #include "content/public/browser/navigation_controller.h" 39 #include "content/public/browser/navigation_controller.h"
40 #include "content/public/browser/navigation_details.h" 40 #include "content/public/browser/navigation_details.h"
41 #include "content/public/browser/navigation_entry.h" 41 #include "content/public/browser/navigation_entry.h"
42 #include "content/public/browser/notification_details.h" 42 #include "content/public/browser/notification_details.h"
43 #include "content/public/browser/notification_service.h" 43 #include "content/public/browser/notification_service.h"
44 #include "content/public/browser/notification_source.h" 44 #include "content/public/browser/notification_source.h"
45 #include "content/public/browser/notification_types.h" 45 #include "content/public/browser/notification_types.h"
46 #include "content/public/browser/render_process_host.h" 46 #include "content/public/browser/render_process_host.h"
47 #include "content/public/browser/render_view_host.h" 47 #include "content/public/browser/render_view_host.h"
48 #include "content/public/browser/web_contents.h" 48 #include "content/public/browser/web_contents.h"
49 #include "google_apis/google_api_keys.h"
49 #include "grit/browser_resources.h" 50 #include "grit/browser_resources.h"
50 #include "net/base/escape.h" 51 #include "net/base/escape.h"
51 #include "net/base/load_flags.h" 52 #include "net/base/load_flags.h"
52 #include "net/url_request/url_fetcher.h" 53 #include "net/url_request/url_fetcher.h"
53 #include "net/url_request/url_request_status.h" 54 #include "net/url_request/url_request_status.h"
54 #include "ui/base/layout.h" 55 #include "ui/base/layout.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"
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 "cb=cr.googleTranslate.onTranslateElementLoad&hl=%s"; 163 "cb=cr.googleTranslate.onTranslateElementLoad&hl=%s";
163 const char* const kTranslateScriptHeader = 164 const char* const kTranslateScriptHeader =
164 "Google-Translate-Element-Mode: library"; 165 "Google-Translate-Element-Mode: library";
165 const char* const kReportLanguageDetectionErrorURL = 166 const char* const kReportLanguageDetectionErrorURL =
166 "https://translate.google.com/translate_error"; 167 "https://translate.google.com/translate_error";
167 const char* const kLanguageListFetchURL = 168 const char* const kLanguageListFetchURL =
168 "https://translate.googleapis.com/translate_a/l?client=chrome&cb=sl&hl=%s"; 169 "https://translate.googleapis.com/translate_a/l?client=chrome&cb=sl&hl=%s";
169 const int kMaxRetryLanguageListFetch = 5; 170 const int kMaxRetryLanguageListFetch = 5;
170 const int kTranslateScriptExpirationDelayDays = 1; 171 const int kTranslateScriptExpirationDelayDays = 1;
171 172
173 void AddApiKeyToUrl(GURL* url) {
174 std::string api_key = google_apis::GetAPIKey();
175 if (!api_key.empty()) {
Jói 2012/09/13 16:25:02 There is no need for this check. If the key isn't
MAD 2012/09/13 17:23:21 OK... Note that I copied the behavior from here: h
176 std::string query(url->query());
177 if (!query.empty())
178 query += "&";
179 query += "key=" + net::EscapeQueryParamValue(api_key, true);
180 GURL::Replacements replacements;
181 replacements.SetQueryStr(query);
182 *url = url->ReplaceComponents(replacements);
183 }
184 }
185
172 } // namespace 186 } // namespace
173 187
174 // This must be kept in sync with the &cb= value in the kLanguageListFetchURL. 188 // This must be kept in sync with the &cb= value in the kLanguageListFetchURL.
175 const char* const TranslateManager::kLanguageListCallbackName = "sl("; 189 const char* const TranslateManager::kLanguageListCallbackName = "sl(";
176 const char* const TranslateManager::kTargetLanguagesKey = "tl"; 190 const char* const TranslateManager::kTargetLanguagesKey = "tl";
177 191
178 // static 192 // static
179 base::LazyInstance<std::set<std::string> > 193 base::LazyInstance<std::set<std::string> >
180 TranslateManager::supported_languages_ = LAZY_INSTANCE_INITIALIZER; 194 TranslateManager::supported_languages_ = LAZY_INSTANCE_INITIALIZER;
181 195
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID())); 670 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID()));
657 671
658 TranslateTabHelper* helper = 672 TranslateTabHelper* helper =
659 TabContents::FromWebContents(web_contents)->translate_tab_helper(); 673 TabContents::FromWebContents(web_contents)->translate_tab_helper();
660 helper->language_state().set_current_language( 674 helper->language_state().set_current_language(
661 helper->language_state().original_language()); 675 helper->language_state().original_language());
662 } 676 }
663 677
664 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { 678 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) {
665 UMA_HISTOGRAM_COUNTS("Translate.ReportLanguageDetectionError", 1); 679 UMA_HISTOGRAM_COUNTS("Translate.ReportLanguageDetectionError", 1);
666 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); 680 // We'll open the URL in a new tab so that the user can tell us more.
667 // Report option should be disabled for secure URLs.
668 DCHECK(!page_url.SchemeIsSecure());
669 std::string report_error_url(kReportLanguageDetectionErrorURL);
670 report_error_url += "?client=cr&action=langidc&u=";
671 report_error_url += net::EscapeUrlEncodedData(page_url.spec(), true);
672 report_error_url += "&sl=";
673
674 TranslateTabHelper* helper =
675 TabContents::FromWebContents(web_contents)->translate_tab_helper();
676 report_error_url += helper->language_state().original_language();
677 report_error_url += "&hl=";
678 report_error_url +=
679 GetLanguageCode(g_browser_process->GetApplicationLocale());
680 // Open that URL in a new tab so that the user can tell us more.
681 Browser* browser = browser::FindBrowserWithWebContents(web_contents); 681 Browser* browser = browser::FindBrowserWithWebContents(web_contents);
682 if (!browser) { 682 if (!browser) {
683 NOTREACHED(); 683 NOTREACHED();
684 return; 684 return;
685 } 685 }
686 chrome::AddSelectedTabWithURL(browser, GURL(report_error_url), 686
687 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL();
688 // Report option should be disabled for secure URLs.
689 DCHECK(!page_url.SchemeIsSecure());
690 std::string report_error_url_str(kReportLanguageDetectionErrorURL);
691 report_error_url_str += "?client=cr&action=langidc&u=";
692 report_error_url_str += net::EscapeUrlEncodedData(page_url.spec(), true);
693 report_error_url_str += "&sl=";
694
695 TranslateTabHelper* helper =
696 TabContents::FromWebContents(web_contents)->translate_tab_helper();
697 report_error_url_str += helper->language_state().original_language();
698 report_error_url_str += "&hl=";
699 report_error_url_str +=
700 GetLanguageCode(g_browser_process->GetApplicationLocale());
701
702 GURL report_error_url(report_error_url_str);
703 AddApiKeyToUrl(&report_error_url);
704 chrome::AddSelectedTabWithURL(browser, report_error_url,
687 content::PAGE_TRANSITION_AUTO_BOOKMARK); 705 content::PAGE_TRANSITION_AUTO_BOOKMARK);
688 } 706 }
689 707
690 void TranslateManager::DoTranslatePage(WebContents* web_contents, 708 void TranslateManager::DoTranslatePage(WebContents* web_contents,
691 const std::string& translate_script, 709 const std::string& translate_script,
692 const std::string& source_lang, 710 const std::string& source_lang,
693 const std::string& target_lang) { 711 const std::string& target_lang) {
694 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); 712 NavigationEntry* entry = web_contents->GetController().GetActiveEntry();
695 if (!entry) { 713 if (!entry) {
696 NOTREACHED(); 714 NOTREACHED();
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 return; 822 return;
805 823
806 // We don't want to do this when translate is disabled. 824 // We don't want to do this when translate is disabled.
807 DCHECK(prefs != NULL); 825 DCHECK(prefs != NULL);
808 if (CommandLine::ForCurrentProcess()->HasSwitch( 826 if (CommandLine::ForCurrentProcess()->HasSwitch(
809 switches::kDisableTranslate) || 827 switches::kDisableTranslate) ||
810 (prefs != NULL && !prefs->GetBoolean(prefs::kEnableTranslate))) { 828 (prefs != NULL && !prefs->GetBoolean(prefs::kEnableTranslate))) {
811 return; 829 return;
812 } 830 }
813 831
814 std::string language_list_fetch_url = base::StringPrintf( 832 GURL language_list_fetch_url = GURL(
815 kLanguageListFetchURL, 833 base::StringPrintf(
816 GetLanguageCode(g_browser_process->GetApplicationLocale()).c_str()); 834 kLanguageListFetchURL,
835 GetLanguageCode(g_browser_process->GetApplicationLocale()).c_str()));
836 AddApiKeyToUrl(&language_list_fetch_url);
817 language_list_request_pending_.reset(net::URLFetcher::Create( 837 language_list_request_pending_.reset(net::URLFetcher::Create(
818 1, GURL(language_list_fetch_url), net::URLFetcher::GET, this)); 838 1, language_list_fetch_url, net::URLFetcher::GET, this));
819 language_list_request_pending_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 839 language_list_request_pending_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
820 net::LOAD_DO_NOT_SAVE_COOKIES); 840 net::LOAD_DO_NOT_SAVE_COOKIES);
821 language_list_request_pending_->SetRequestContext( 841 language_list_request_pending_->SetRequestContext(
822 g_browser_process->system_request_context()); 842 g_browser_process->system_request_context());
823 language_list_request_pending_->SetMaxRetries(kMaxRetryLanguageListFetch); 843 language_list_request_pending_->SetMaxRetries(kMaxRetryLanguageListFetch);
824 language_list_request_pending_->Start(); 844 language_list_request_pending_->Start();
825 } 845 }
826 846
827 void TranslateManager::CleanupPendingUlrFetcher() { 847 void TranslateManager::CleanupPendingUlrFetcher() {
828 language_list_request_pending_.reset(); 848 language_list_request_pending_.reset();
829 translate_script_request_pending_.reset(); 849 translate_script_request_pending_.reset();
830 } 850 }
831 851
832 void TranslateManager::RequestTranslateScript() { 852 void TranslateManager::RequestTranslateScript() {
833 if (translate_script_request_pending_.get() != NULL) 853 if (translate_script_request_pending_.get() != NULL)
834 return; 854 return;
835 855
836 std::string translate_script_url = base::StringPrintf( 856 GURL translate_script_url = GURL(base::StringPrintf(
837 kTranslateScriptURL, 857 kTranslateScriptURL,
838 GetLanguageCode(g_browser_process->GetApplicationLocale()).c_str()); 858 GetLanguageCode(g_browser_process->GetApplicationLocale()).c_str()));
859 AddApiKeyToUrl(&translate_script_url);
839 translate_script_request_pending_.reset(net::URLFetcher::Create( 860 translate_script_request_pending_.reset(net::URLFetcher::Create(
840 0, GURL(translate_script_url), net::URLFetcher::GET, this)); 861 0, translate_script_url, net::URLFetcher::GET, this));
841 translate_script_request_pending_->SetLoadFlags( 862 translate_script_request_pending_->SetLoadFlags(
842 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); 863 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES);
843 translate_script_request_pending_->SetRequestContext( 864 translate_script_request_pending_->SetRequestContext(
844 g_browser_process->system_request_context()); 865 g_browser_process->system_request_context());
845 translate_script_request_pending_->SetExtraRequestHeaders( 866 translate_script_request_pending_->SetExtraRequestHeaders(
846 kTranslateScriptHeader); 867 kTranslateScriptHeader);
847 translate_script_request_pending_->Start(); 868 translate_script_request_pending_->Start();
848 } 869 }
849 870
850 void TranslateManager::ShowInfoBar(content::WebContents* web_contents, 871 void TranslateManager::ShowInfoBar(content::WebContents* web_contents,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper(); 920 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
900 921
901 for (size_t i = 0; i < infobar_helper->GetInfoBarCount(); ++i) { 922 for (size_t i = 0; i < infobar_helper->GetInfoBarCount(); ++i) {
902 TranslateInfoBarDelegate* delegate = 923 TranslateInfoBarDelegate* delegate =
903 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); 924 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate();
904 if (delegate) 925 if (delegate)
905 return delegate; 926 return delegate;
906 } 927 }
907 return NULL; 928 return NULL;
908 } 929 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698