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

Unified Diff: chrome/browser/resources/translate.js

Issue 24024004: Translate: element API callback interface change (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rename hash table Created 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/translate_internals/translate_internals.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/translate.js
diff --git a/chrome/browser/resources/translate.js b/chrome/browser/resources/translate.js
index 90e5a5454b4d9bf13a3a86d4044f2a704c0a3edc..4ce17c4c050cd0f553728513bffb56587d89d6d9 100644
--- a/chrome/browser/resources/translate.js
+++ b/chrome/browser/resources/translate.js
@@ -27,12 +27,34 @@ 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.
xiyuan 2013/09/06 16:16:41 nit: @const
Takashi Toyoshima 2013/09/09 04:02:29 Done.
+ */
+ var error = {
xiyuan 2013/09/06 16:16:41 nit: error -> ERROR for const var name.
Takashi Toyoshima 2013/09/09 04:02:29 Done.
+ '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.
xiyuan 2013/09/06 16:16:41 nit: @const
Takashi Toyoshima 2013/09/09 04:02:29 Done.
+ */
+ var mapTranslateErrorToErrorCode = {
xiyuan 2013/09/06 16:16:41 nit: mapTranslateErrorToErrorCode -> TRANSLATE_ERR
Takashi Toyoshima 2013/09/09 04:02:29 Done.
+ 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 +107,7 @@ cr.googleTranslate = (function() {
return;
}
if (checkReadyCount++ > 5) {
- error = true;
+ errorCode = error['TRANSLATION_TIMEOUT'];
return;
}
setTimeout(checkLibReady, 100);
@@ -95,9 +117,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 = mapTranslateErrorToErrorCode[opt_error];
+ lib.restore();
}
if (finished)
endTime = performance.now();
@@ -128,7 +154,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 +174,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 +215,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 +224,7 @@ cr.googleTranslate = (function() {
*/
translate: function(originalLang, targetLang) {
finished = false;
- error = false;
+ errorCode = error['NONE'];
if (!libReady)
return false;
startTime = performance.now();
@@ -198,8 +232,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 +260,7 @@ cr.googleTranslate = (function() {
});
translateApiKey = undefined;
} catch (err) {
- error = true;
+ errorCode = error['INITIALIZATION_ERROR'];
translateApiKey = undefined;
return;
}
@@ -258,16 +291,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();
« no previous file with comments | « no previous file | chrome/browser/resources/translate_internals/translate_internals.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698