Skip to content

Commit

Permalink
Move language detection to a component
Browse files Browse the repository at this point in the history
Language detection is used from the renderer on most platform, but from the
browser on iOS. This CL moves it from chrome/common/ to a new "translate"
component, which allows to track and address more cleanly dependencies issues.

BUG=297777

Review URL: https://codereview.chromium.org/25531002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@227015 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
droger@chromium.org committed Oct 4, 2013
1 parent b38806a commit 499e7c5
Show file tree
Hide file tree
Showing 48 changed files with 640 additions and 532 deletions.
4 changes: 2 additions & 2 deletions build/all.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
'some.gyp:*',
'../base/base.gyp:*',
'../chrome/chrome.gyp:*',
'../components/components.gyp:*',
'../content/content.gyp:*',
'../crypto/crypto.gyp:*',
'../mojo/mojo.gyp:*',
Expand All @@ -34,6 +33,7 @@
['OS!="ios"', {
'dependencies': [
'../cc/cc_tests.gyp:*',
'../components/components.gyp:*',
'../device/bluetooth/bluetooth.gyp:*',
'../device/device_tests.gyp:*',
'../device/usb/usb.gyp:*',
Expand Down Expand Up @@ -212,7 +212,6 @@
'dependencies': [
'../base/base.gyp:base_unittests',
'../chrome/chrome.gyp:unit_tests',
'../components/components.gyp:components_unittests',
'../crypto/crypto.gyp:crypto_unittests',
'../net/net.gyp:net_unittests',
'../sql/sql.gyp:sql_unittests',
Expand All @@ -232,6 +231,7 @@
'../chrome/chrome.gyp:interactive_ui_tests',
'../chrome/chrome.gyp:sync_integration_tests',
'../cloud_print/cloud_print.gyp:cloud_print_unittests',
'../components/components.gyp:components_unittests',
'../content/content.gyp:content_browsertests',
'../content/content.gyp:content_shell',
'../content/content.gyp:content_unittests',
Expand Down
3 changes: 3 additions & 0 deletions chrome/browser/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ include_rules = [
"+components/navigation_interception",
"+components/policy",
"+components/startup_metric_utils",
"+components/translate/common",
# TODO(droger): Remove dependency on language detection, see crbug.com/297777.
"+components/translate/language_detection",
"+components/user_prefs",
"+components/visitedlink/browser",
"+components/webdata",
Expand Down
3 changes: 2 additions & 1 deletion chrome/browser/chrome_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#include "chrome/installer/util/google_update_settings.h"
#include "chromeos/chromeos_constants.h"
#include "components/nacl/common/nacl_process_type.h"
#include "components/translate/common/translate_switches.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/browser_child_process_host.h"
#include "content/public/browser/browser_main_parts.h"
Expand Down Expand Up @@ -1456,8 +1457,8 @@ void ChromeContentBrowserClient::AppendExtraCommandLineSwitches(
switches::kRecordMode,
switches::kSilentDumpOnDCHECK,
switches::kSpdyProxyAuthOrigin,
switches::kTranslateSecurityOrigin,
switches::kWhitelistedExtensionID,
translate::switches::kTranslateSecurityOrigin,
};

command_line->CopySwitchesFrom(browser_command_line, kSwitchNames,
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/resources/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ cr.googleTranslate = (function() {
if (!libReady || !finished || errorCode != ERROR['NONE'])
return '';
if (!lib.getDetectedLanguage)
return 'und'; // defined as chrome::kUnknownLanguageCode in C++ world.
return 'und'; // Defined as translate::kUnknownLanguageCode in C++.
return lib.getDetectedLanguage();
},

Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/translate/translate_accept_languages.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/translate/translate_manager.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/translate/translate_util.h"
#include "components/translate/common/translate_util.h"
#include "content/public/browser/notification_source.h"
#include "net/url_request/url_fetcher.h"
#include "ui/base/l10n/l10n_util.h"
Expand All @@ -29,7 +29,7 @@ TranslateAcceptLanguages::~TranslateAcceptLanguages() {
bool TranslateAcceptLanguages::CanBeAcceptLanguage(
const std::string& language) {
std::string accept_language = language;
TranslateUtil::ToChromeLanguageSynonym(&accept_language);
translate::ToChromeLanguageSynonym(&accept_language);

const std::string locale = g_browser_process->GetApplicationLocale();
std::vector<std::string> accept_language_codes;
Expand Down Expand Up @@ -73,7 +73,7 @@ bool TranslateAcceptLanguages::IsAcceptLanguage(Profile* profile,
}

std::string accept_language = language;
TranslateUtil::ToChromeLanguageSynonym(&accept_language);
translate::ToChromeLanguageSynonym(&accept_language);
return iter->second.count(accept_language) != 0;
}

Expand Down
7 changes: 4 additions & 3 deletions chrome/browser/translate/translate_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include "chrome/browser/translate/translate_script.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/test_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/translate/common/translate_switches.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/browser_test_utils.h"
#include "net/http/http_status_code.h"
Expand Down Expand Up @@ -56,8 +56,9 @@ class TranslateBrowserTest : public InProcessBrowserTest {
// Setup alternate security origin for testing in order to allow XHR against
// local test server. Note that this flag shows a confirm infobar in tests.
GURL base_url = GetSecureURL("");
command_line->AppendSwitchASCII(switches::kTranslateSecurityOrigin,
base_url.GetOrigin().spec());
command_line->AppendSwitchASCII(
translate::switches::kTranslateSecurityOrigin,
base_url.GetOrigin().spec());
}

protected:
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/translate/translate_infobar_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void TranslateInfoBarDelegate::Create(
// infobar, which is the case when the user started a translation from the
// context menu.
DCHECK(infobar_type == TRANSLATING || infobar_type == AFTER_TRANSLATE);
DCHECK_EQ(chrome::kUnknownLanguageCode, original_language);
DCHECK_EQ(translate::kUnknownLanguageCode, original_language);
}
}

Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/translate/translate_infobar_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "base/logging.h"
#include "chrome/browser/infobars/infobar_delegate.h"
#include "chrome/browser/translate/translate_prefs.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/translate/translate_errors.h"
#include "components/translate/common/translate_constants.h"

class PrefService;

Expand Down Expand Up @@ -103,7 +103,7 @@ class TranslateInfoBarDelegate : public InfoBarDelegate {
// Convenience methods.
std::string original_language_code() const {
return (original_language_index() == kNoIndex) ?
chrome::kUnknownLanguageCode :
translate::kUnknownLanguageCode :
language_code_at(original_language_index());
}
std::string target_language_code() const {
Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/translate/translate_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/translate/language_detection_details.h"
#include "chrome/common/url_constants.h"
#include "components/translate/common/translate_constants.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
Expand Down Expand Up @@ -466,7 +466,7 @@ void TranslateManager::TranslatePage(WebContents* web_contents,
// server side auto language detection.
std::string source_lang(original_source_lang);
if (!IsSupportedLanguage(source_lang))
source_lang = std::string(chrome::kUnknownLanguageCode);
source_lang = std::string(translate::kUnknownLanguageCode);

Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
Expand Down Expand Up @@ -580,7 +580,7 @@ void TranslateManager::DoTranslatePage(WebContents* web_contents,
void TranslateManager::PageTranslated(WebContents* web_contents,
PageTranslatedDetails* details) {
if ((details->error_type == TranslateErrors::NONE) &&
details->source_language != chrome::kUnknownLanguageCode &&
details->source_language != translate::kUnknownLanguageCode &&
!IsSupportedLanguage(details->source_language)) {
details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE;
}
Expand Down
8 changes: 4 additions & 4 deletions chrome/browser/translate/translate_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "chrome/browser/translate/translate_accept_languages.h"
#include "chrome/browser/translate/translate_manager.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/translate/translate_util.h"
#include "components/translate/common/translate_util.h"
#include "components/user_prefs/pref_registry_syncable.h"

const char TranslatePrefs::kPrefTranslateLanguageBlacklist[] =
Expand Down Expand Up @@ -63,7 +63,7 @@ std::string ConvertLangCodeForTranslation(const std::string &lang) {
if (main_part == "zh")
return lang;

TranslateUtil::ToTranslateLanguageSynonym(&main_part);
translate::ToTranslateLanguageSynonym(&main_part);
return main_part;
}

Expand Down Expand Up @@ -116,7 +116,7 @@ void TranslatePrefs::BlockLanguage(

// Add the language to the language list at chrome://settings/languages.
std::string language = original_language;
TranslateUtil::ToChromeLanguageSynonym(&language);
translate::ToChromeLanguageSynonym(&language);

std::vector<std::string> languages;
GetLanguageList(&languages);
Expand Down Expand Up @@ -421,7 +421,7 @@ void TranslatePrefs::MigrateUserPrefs(PrefService* user_prefs) {
blocked_languages.begin();
it != blocked_languages.end(); ++it) {
std::string lang = *it;
TranslateUtil::ToChromeLanguageSynonym(&lang);
translate::ToChromeLanguageSynonym(&lang);
bool not_found =
std::find(accept_languages.begin(), accept_languages.end(), lang) ==
accept_languages.end();
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/translate/translate_script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "chrome/browser/translate/translate_url_fetcher.h"
#include "chrome/browser/translate/translate_url_util.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/translate/translate_util.h"
#include "components/translate/common/translate_util.h"
#include "google_apis/google_api_keys.h"
#include "grit/browser_resources.h"
#include "net/base/escape.h"
Expand Down Expand Up @@ -126,7 +126,7 @@ void TranslateScript::OnScriptFetchComplete(
data_ = base::StringPrintf("var translateApiKey = '%s';\n",
google_apis::GetAPIKey().c_str());

GURL security_origin = TranslateUtil::GetTranslateSecurityOrigin();
GURL security_origin = translate::GetTranslateSecurityOrigin();
base::StringAppendF(
&data_, "var securityOrigin = '%s';", security_origin.spec().c_str());

Expand Down
3 changes: 2 additions & 1 deletion chrome/browser/ui/startup/bad_flags_prompt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "components/translate/common/translate_switches.h"
#include "extensions/common/switches.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
Expand All @@ -36,7 +37,7 @@ void ShowBadFlagsPrompt(Browser* browser) {
extensions::switches::kExtensionsOnChromeURLs,
// This parameter should be used only for server side developments.
switches::kTranslateScriptURL,
switches::kTranslateSecurityOrigin,
translate::switches::kTranslateSecurityOrigin,
NULL
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/translate_internals/translate_internals_handler.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/translate/language_detection_util.h"
#include "chrome/common/url_constants.h"
#include "components/translate/language_detection/language_detection_util.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
Expand Down Expand Up @@ -61,7 +61,9 @@ content::WebUIDataSource* CreateTranslateInternalsHTMLSource() {
source->AddString(key, value);
}

std::string cld_version = LanguageDetectionUtil::GetCLDVersion();
// TODO(droger): Do not call GetCLDVersion() from here. Use a constant string
// instead, see http://crbug.com/297777.
std::string cld_version = translate::GetCLDVersion();
source->AddString("cld-version", cld_version);

return source;
Expand Down
3 changes: 3 additions & 0 deletions chrome/chrome_browser.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
'../components/components.gyp:encryptor',
'../components/components.gyp:sessions',
'../components/components.gyp:startup_metric_utils',
'../components/components.gyp:translate_common',
# TODO(droger): Remove this dependency, see http://crbug.com/297777.
'../components/components.gyp:translate_language_detection',
'../components/components.gyp:user_prefs',
# This depends directly on the variations target, rather than just
# transitively via the common target because the proto sources need to
Expand Down
16 changes: 0 additions & 16 deletions chrome/chrome_common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,7 @@
'common/thumbnail_score.h',
'common/translate/language_detection_details.cc',
'common/translate/language_detection_details.h',
'common/translate/language_detection_util.cc',
'common/translate/language_detection_util.h',
'common/translate/translate_common_metrics.cc',
'common/translate/translate_common_metrics.h',
'common/translate/translate_errors.h',
'common/translate/translate_util.cc',
'common/translate/translate_util.h',
'common/tts_messages.h',
'common/tts_utterance_request.cc',
'common/tts_utterance_request.h',
Expand Down Expand Up @@ -615,16 +609,6 @@
'common/print_messages.h',
]
}],
['cld_version==0 or cld_version==1', {
'dependencies': [
'<(DEPTH)/third_party/cld/cld.gyp:cld',
],
}],
['cld_version==0 or cld_version==2', {
'dependencies': [
'<(DEPTH)/third_party/cld_2/cld_2.gyp:cld_2',
],
}],
],
'target_conditions': [
['OS == "ios"', {
Expand Down
6 changes: 4 additions & 2 deletions chrome/chrome_renderer.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
'chrome_resources.gyp:chrome_strings',
'../components/components.gyp:autofill_content_renderer',
'../components/components.gyp:startup_metric_utils',
'../components/components.gyp:translate_common',
'../components/components.gyp:translate_language_detection',
'../components/components.gyp:visitedlink_renderer',
'../content/content.gyp:content_renderer',
'../net/net.gyp:net',
Expand Down Expand Up @@ -404,11 +406,11 @@
['exclude', '^renderer/automation/']
]
}],
['enable_printing==0', {
['enable_printing==0', {
'sources/': [
['exclude', '^renderer/printing/']
]
}],
}],
['OS=="android"', {
'sources!': [
'renderer/prerender/prerender_media_load_deferrer.cc',
Expand Down
3 changes: 0 additions & 3 deletions chrome/chrome_tests_unit.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -1815,9 +1815,6 @@
'common/service_process_util_unittest.cc',
'common/switch_utils_unittest.cc',
'common/thumbnail_score_unittest.cc',
'common/translate/language_detection_util_unittest.cc',
'common/translate/translate_common_metrics_unittest.cc',
'common/translate/translate_util_unittest.cc',
'common/worker_thread_ticker_unittest.cc',
'renderer/chrome_content_renderer_client_unittest.cc',
'renderer/content_settings_observer_unittest.cc',
Expand Down
3 changes: 0 additions & 3 deletions chrome/common/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ include_rules = [
# Other libraries.
"+chrome/third_party/xdg_user_dirs",
"+third_party/bzip2",
"+third_party/cld",
"+third_party/cld/encodings/compact_lang_det/win",
"+third_party/cld_2/src",
"+third_party/mt19937ar",
"+third_party/npapi",
"+third_party/re2",
Expand Down
2 changes: 0 additions & 2 deletions chrome/common/chrome_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ const wchar_t kUserDataDirname[] = L"User Data";

const bool kRecordModeEnabled = true;

const char* const kUnknownLanguageCode = "und";

const int kJavaScriptMessageExpectedDelay = 1000;

#if defined(OS_ANDROID)
Expand Down
4 changes: 0 additions & 4 deletions chrome/common/chrome_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ extern const wchar_t kUserDataDirname[];

extern const bool kRecordModeEnabled;

// The language code used when the language of a page could not be detected.
// (Matches what the CLD -Compact Language Detection- library reports.)
extern const char* const kUnknownLanguageCode;

// If a WebContents is impolite and displays a second JavaScript alert within
// kJavaScriptMessageExpectedDelay of a previous JavaScript alert being
// dismissed, display an option to suppress future alerts from this WebContents.
Expand Down
3 changes: 0 additions & 3 deletions chrome/common/chrome_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1448,9 +1448,6 @@ const char kTrackActiveVisitTime[] = "track-active-visit-time";
// Overrides the default server used for Google Translate.
const char kTranslateScriptURL[] = "translate-script-url";

// Overrides security-origin with which Translate runs in an isolated world.
const char kTranslateSecurityOrigin[] = "translate-security-origin";

// Disables same-origin check on HTTP resources pushed via a SPDY proxy.
// The value is the host:port of the trusted proxy.
const char kTrustedSpdyProxy[] = "trusted-spdy-proxy";
Expand Down
1 change: 0 additions & 1 deletion chrome/common/chrome_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ extern const char kTestType[];
extern const char kTestingChannelID[];
extern const char kTrackActiveVisitTime[];
extern const char kTranslateScriptURL[];
extern const char kTranslateSecurityOrigin[];
extern const char kTrustedSpdyProxy[];
extern const char kTryChromeAgain[];
extern const char kUninstallExtension[];
Expand Down
Loading

0 comments on commit 499e7c5

Please sign in to comment.