Commit 51e8fd92 authored by toyoshim@chromium.org's avatar toyoshim@chromium.org

Translate: element API callback interface change

The progress callback interface will be changed as the third parameter
goes to number from boolean. This change make translate.js support
both boolean and number for API migration. Once this change goes to stable
channel, server side library will change to return number to specify error code.

BUG=211308
TEST=none

Review URL: https://chromiumcodereview.appspot.com/24024004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221992 0039d316-1c4b-4281-b951-d872f2087c98
parent e620d36e
......@@ -7,7 +7,7 @@
// language to another.
// It should be included in the page before the Translate Element script.
var cr = {};
var cr = cr || {};
/**
* An object to provide functions to interact with the Translate library.
......@@ -27,12 +27,36 @@ cr.googleTranslate = (function() {
var libReady = false;
/**
* A flag representing if the Translate Element library returns error while
* performing translation. Also it is set to true when the Translate Element
* library is not initialized in 600 msec from the library is loaded.
* @type {boolean}
* Error definitions for |errorCode|. See chrome/common/translate_errors.h
* to modify the definition.
* @const
*/
var ERROR = {
'NONE': 0,
'INITIALIZATION_ERROR': 2,
'UNSUPPORTED_LANGUAGE': 4,
'TRANSLATION_ERROR': 6,
'TRANSLATION_TIMEOUT': 7,
'UNEXPECTED_SCRIPT_ERROR': 8,
'BAD_ORIGIN': 9,
'SCRIPT_LOAD_ERROR': 10
};
/**
* Error code map from te.dom.DomTranslator.Error to |errorCode|.
* See also go/dom_translator.js in google3.
* @const
*/
var TRANSLATE_ERROR_TO_ERROR_CODE_MAP = {
0: ERROR['NONE'],
1: ERROR['TRANSLATION_ERROR'],
2: ERROR['UNSUPPORTED_LANGUAGE']
};
/**
* An error code happened in translate.js and the Translate Element library.
*/
var error = false;
var errorCode = ERROR['NONE'];
/**
* A flag representing if the Translate Element has finished a translation.
......@@ -85,7 +109,7 @@ cr.googleTranslate = (function() {
return;
}
if (checkReadyCount++ > 5) {
error = true;
errorCode = ERROR['TRANSLATION_TIMEOUT'];
return;
}
setTimeout(checkLibReady, 100);
......@@ -95,9 +119,13 @@ cr.googleTranslate = (function() {
finished = opt_finished;
// opt_error can be 'undefined'.
if (typeof opt_error == 'boolean' && opt_error) {
error = true;
// TODO(toyoshim): Remove boolean case once a server is updated.
errorCode = ERROR['TRANSLATION_ERROR'];
// We failed to translate, restore so the page is in a consistent state.
lib.restore();
} else if (typeof opt_error == 'number' && opt_error != 0) {
errorCode = TRANSLATE_ERROR_TO_ERROR_CODE_MAP[opt_error];
lib.restore();
}
if (finished)
endTime = performance.now();
......@@ -128,7 +156,15 @@ cr.googleTranslate = (function() {
* @type {boolean}
*/
get error() {
return error;
return errorCode != ERROR['NONE'];
},
/**
* Returns a number to represent error type.
* @type {number}
*/
get errorCode() {
return errorCode;
},
/**
......@@ -140,7 +176,7 @@ cr.googleTranslate = (function() {
* @type {boolean}
*/
get sourceLang() {
if (!libReady || !finished || error)
if (!libReady || !finished || errorCode != ERROR['NONE'])
return '';
if (!lib.getDetectedLanguage)
return 'und'; // defined as chrome::kUnknownLanguageCode in C++ world.
......@@ -181,8 +217,8 @@ cr.googleTranslate = (function() {
/**
* Translate the page contents. Note that the translation is asynchronous.
* You need to regularly check the state of |finished| and |error| to know
* if the translation finished or if there was an error.
* You need to regularly check the state of |finished| and |errorCode| to
* know if the translation finished or if there was an error.
* @param {string} originalLang The language the page is in.
* @param {string} targetLang The language the page should be translated to.
* @return {boolean} False if the translate library was not ready, in which
......@@ -190,7 +226,7 @@ cr.googleTranslate = (function() {
*/
translate: function(originalLang, targetLang) {
finished = false;
error = false;
errorCode = ERROR['NONE'];
if (!libReady)
return false;
startTime = performance.now();
......@@ -198,8 +234,7 @@ cr.googleTranslate = (function() {
lib.translatePage(originalLang, targetLang, onTranslateProgress);
} catch (err) {
console.error('Translate: ' + err);
// TODO(toyoshim): Check if it shows an error notification.
error = true;
errorCode = ERROR['UNEXPECTED_SCRIPT_ERROR'];
return false;
}
return true;
......@@ -227,7 +262,7 @@ cr.googleTranslate = (function() {
});
translateApiKey = undefined;
} catch (err) {
error = true;
errorCode = ERROR['INITIALIZATION_ERROR'];
translateApiKey = undefined;
return;
}
......@@ -258,16 +293,19 @@ cr.googleTranslate = (function() {
onLoadJavascript: function(url) {
// securityOrigin is predefined by translate_script.cc.
if (url.indexOf(securityOrigin) != 0) {
// TODO(toyoshim): Handle this error to show an error notification.
console.error('Translate: ' + url + ' is not allowed to load.');
error = true;
errorCode = ERROR['BAD_ORIGIN'];
return;
}
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (this.readyState != this.DONE || this.status != 200)
if (this.readyState != this.DONE)
return;
if (this.status != 200) {
errorCode = ERROR['SCRIPT_LOAD_ERROR'];
return;
}
eval(this.responseText);
}
xhr.send();
......
......@@ -115,6 +115,10 @@
4: 'Unsupported Language',
5: 'Identical Languages',
6: 'Translation Error',
7: 'Translation Timeout',
8: 'Unexpected Script Error',
9: 'Bad Origin',
10: 'Script Load Error',
};
if (error < 0 || errorStrs.length <= error) {
......
......@@ -7,6 +7,7 @@
// This file consolidates all the error types for translation of a page.
// Note: TranslateErrors is used for UMA and translate_internals.js.
// Assigned numbers should be changed because the number is binded to UMA value.
// enum TranslateError in histograms.xml and errorStrs in translate_internals.js
// should be updated when the type is updated.
......@@ -14,14 +15,18 @@ class TranslateErrors {
public:
enum Type {
NONE = 0,
NETWORK, // No connectivity.
INITIALIZATION_ERROR, // The translation script failed to initialize.
UNKNOWN_LANGUAGE, // The page's language could not be detected.
UNSUPPORTED_LANGUAGE, // The server detected a language that the browser
// does not know.
IDENTICAL_LANGUAGES, // The original and target languages are the same.
TRANSLATION_ERROR, // An error was reported by the translation script
// during translation.
NETWORK, // No connectivity.
INITIALIZATION_ERROR, // The translation script failed to initialize.
UNKNOWN_LANGUAGE, // The page's language could not be detected.
UNSUPPORTED_LANGUAGE, // The server detected a language that the browser
// does not know.
IDENTICAL_LANGUAGES, // The original and target languages are the same.
TRANSLATION_ERROR, // An error was reported by the translation script
// during translation.
TRANSLATION_TIMEOUT, // The library doesn't finish the translation.
UNEXPECTED_SCRIPT_ERROR, // The library raises an unexpected exception.
BAD_ORIGIN, // The library is blocked because of bad origin.
SCRIPT_LOAD_ERROR, // Loader fails to load a dependent JavaScript.
TRANSLATE_ERROR_MAX,
};
......
......@@ -391,6 +391,7 @@ void TranslateHelper::CheckTranslateStatus() {
// First check if there was an error.
if (HasTranslationFailed()) {
// TODO(toyoshim): Check |errorCode| of translate.js and notify it here.
NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR);
return; // There was an error.
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment