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

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

Issue 13488014: Translate: Adding a flag to enable "alpha language" translation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/about_flags.cc ('k') | chrome/common/chrome_switches.h » ('j') | 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 #include "content/public/browser/notification_service.h" 42 #include "content/public/browser/notification_service.h"
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/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/resource/resource_bundle.h" 55 #include "ui/base/resource/resource_bundle.h"
55 56
56 #ifdef FILE_MANAGER_EXTENSION 57 #ifdef FILE_MANAGER_EXTENSION
57 #include "chrome/browser/chromeos/extensions/file_manager_util.h" 58 #include "chrome/browser/chromeos/extensions/file_manager_util.h"
58 #endif 59 #endif
59 60
60 using content::NavigationController; 61 using content::NavigationController;
61 using content::NavigationEntry; 62 using content::NavigationEntry;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 135
135 const char* const kTranslateScriptURL = 136 const char* const kTranslateScriptURL =
136 "https://translate.google.com/translate_a/element.js"; 137 "https://translate.google.com/translate_a/element.js";
137 const char* const kTranslateScriptQuery = 138 const char* const kTranslateScriptQuery =
138 "?cb=cr.googleTranslate.onTranslateElementLoad&hl=%s"; 139 "?cb=cr.googleTranslate.onTranslateElementLoad&hl=%s";
139 const char* const kTranslateScriptHeader = 140 const char* const kTranslateScriptHeader =
140 "Google-Translate-Element-Mode: library"; 141 "Google-Translate-Element-Mode: library";
141 const char* const kReportLanguageDetectionErrorURL = 142 const char* const kReportLanguageDetectionErrorURL =
142 "https://translate.google.com/translate_error"; 143 "https://translate.google.com/translate_error";
143 const char* const kLanguageListFetchURL = 144 const char* const kLanguageListFetchURL =
144 "https://translate.googleapis.com/translate_a/l?client=chrome&cb=sl&hl=%s"; 145 "https://translate.googleapis.com/translate_a/l?client=chrome&cb=sl";
146
147 const char* const kLanguageListFetchLocaleQueryName = "hl";
148 const char* const kLanguageListFetchAlphaLanguageQueryName = "alpha";
149 const char* const kLanguageListFetchAlphaLanguageQueryValue = "1";
150
145 const int kMaxRetryLanguageListFetch = 5; 151 const int kMaxRetryLanguageListFetch = 5;
146 const int kTranslateScriptExpirationDelayDays = 1; 152 const int kTranslateScriptExpirationDelayDays = 1;
147 153
148 void AddApiKeyToUrl(GURL* url) { 154 void AddApiKeyToUrl(GURL* url) {
149 std::string api_key = google_apis::GetAPIKey(); 155 std::string api_key = google_apis::GetAPIKey();
150 std::string query(url->query()); 156 std::string query(url->query());
151 if (!query.empty()) 157 if (!query.empty())
152 query += "&"; 158 query += "&";
153 query += "key=" + net::EscapeQueryParamValue(api_key, true); 159 query += "key=" + net::EscapeQueryParamValue(api_key, true);
154 GURL::Replacements replacements; 160 GURL::Replacements replacements;
(...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 return; 775 return;
770 776
771 // We don't want to do this when translate is disabled. 777 // We don't want to do this when translate is disabled.
772 DCHECK(prefs != NULL); 778 DCHECK(prefs != NULL);
773 if (CommandLine::ForCurrentProcess()->HasSwitch( 779 if (CommandLine::ForCurrentProcess()->HasSwitch(
774 switches::kDisableTranslate) || 780 switches::kDisableTranslate) ||
775 (prefs != NULL && !prefs->GetBoolean(prefs::kEnableTranslate))) { 781 (prefs != NULL && !prefs->GetBoolean(prefs::kEnableTranslate))) {
776 return; 782 return;
777 } 783 }
778 784
779 GURL language_list_fetch_url = GURL( 785 GURL language_list_fetch_url = GURL(kLanguageListFetchURL);
780 base::StringPrintf( 786 language_list_fetch_url = net::AppendQueryParameter(
781 kLanguageListFetchURL, 787 language_list_fetch_url,
782 GetLanguageCode(g_browser_process->GetApplicationLocale()).c_str())); 788 kLanguageListFetchLocaleQueryName,
789 GetLanguageCode(g_browser_process->GetApplicationLocale()).c_str());
790
791 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
792 if (command_line.HasSwitch(switches::kEnableTranslateAlphaLanguages)) {
793 language_list_fetch_url = net::AppendQueryParameter(
794 language_list_fetch_url,
795 kLanguageListFetchAlphaLanguageQueryName,
796 kLanguageListFetchAlphaLanguageQueryValue);
797 }
783 AddApiKeyToUrl(&language_list_fetch_url); 798 AddApiKeyToUrl(&language_list_fetch_url);
799 VLOG(9) << "Fetch supporting language list from: "
800 << language_list_fetch_url.spec().c_str();
801
784 language_list_request_pending_.reset(net::URLFetcher::Create( 802 language_list_request_pending_.reset(net::URLFetcher::Create(
785 1, language_list_fetch_url, net::URLFetcher::GET, this)); 803 1, language_list_fetch_url, net::URLFetcher::GET, this));
786 language_list_request_pending_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 804 language_list_request_pending_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
787 net::LOAD_DO_NOT_SAVE_COOKIES); 805 net::LOAD_DO_NOT_SAVE_COOKIES);
788 language_list_request_pending_->SetRequestContext( 806 language_list_request_pending_->SetRequestContext(
789 g_browser_process->system_request_context()); 807 g_browser_process->system_request_context());
790 language_list_request_pending_->SetMaxRetriesOn5xx( 808 language_list_request_pending_->SetMaxRetriesOn5xx(
791 kMaxRetryLanguageListFetch); 809 kMaxRetryLanguageListFetch);
792 language_list_request_pending_->Start(); 810 language_list_request_pending_->Start();
793 } 811 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
854 // list or not at all if no such candidate exists 872 // list or not at all if no such candidate exists
855 std::vector<std::string>::iterator iter; 873 std::vector<std::string>::iterator iter;
856 for (iter = accept_langs_list.begin(); 874 for (iter = accept_langs_list.begin();
857 iter != accept_langs_list.end(); ++iter) { 875 iter != accept_langs_list.end(); ++iter) {
858 std::string lang_code = GetLanguageCode(*iter); 876 std::string lang_code = GetLanguageCode(*iter);
859 if (IsSupportedLanguage(lang_code)) 877 if (IsSupportedLanguage(lang_code))
860 return lang_code; 878 return lang_code;
861 } 879 }
862 return std::string(); 880 return std::string();
863 } 881 }
OLDNEW
« no previous file with comments | « chrome/browser/about_flags.cc ('k') | chrome/common/chrome_switches.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698