Bug 1022725: Rename BingTranslation to BingTranslator and allow overriding URLs. r=florian, a=gavin
--- a/browser/components/translation/BingTranslator.jsm
+++ b/browser/components/translation/BingTranslator.jsm
@@ -1,17 +1,17 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
-this.EXPORTED_SYMBOLS = [ "BingTranslation" ];
+this.EXPORTED_SYMBOLS = [ "BingTranslator" ];
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Log.jsm");
Cu.import("resource://gre/modules/Promise.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://services-common/utils.js");
Cu.import("resource://services-common/rest.js");
@@ -35,27 +35,27 @@ const MAX_REQUESTS = 15;
* @param translationDocument The TranslationDocument object that represents
* the webpage to be translated
* @param sourceLanguage The source language of the document
* @param targetLanguage The target language for the translation
*
* @returns {Promise} A promise that will resolve when the translation
* task is finished.
*/
-this.BingTranslation = function(translationDocument, sourceLanguage, targetLanguage) {
+this.BingTranslator = function(translationDocument, sourceLanguage, targetLanguage) {
this.translationDocument = translationDocument;
this.sourceLanguage = sourceLanguage;
this.targetLanguage = targetLanguage;
this._pendingRequests = 0;
this._partialSuccess = false;
this._serviceUnavailable = false;
this._translatedCharacterCount = 0;
};
-this.BingTranslation.prototype = {
+this.BingTranslator.prototype = {
/**
* Performs the translation, splitting the document into several chunks
* respecting the data limits of the API.
*
* @returns {Promise} A promise that will resolve when the translation
* task is finished.
*/
translate: function() {
@@ -277,17 +277,20 @@ function BingRequest(translationData, so
BingRequest.prototype = {
/**
* Initiates the request
*/
fireRequest: function() {
return Task.spawn(function *(){
let token = yield BingTokenManager.getToken();
let auth = "Bearer " + token;
- let request = new RESTRequest("https://api.microsofttranslator.com/v2/Http.svc/TranslateArray");
+ let url = getUrlParam("https://api.microsofttranslator.com/v2/Http.svc/TranslateArray",
+ "browser.translation.bing.translateArrayURL",
+ false);
+ let request = new RESTRequest(url);
request.setHeader("Content-type", "text/xml");
request.setHeader("Authorization", auth);
let requestString =
'<TranslateArrayRequest>' +
'<AppId/>' +
'<From>' + this.sourceLanguage + '</From>' +
'<Options>' +
@@ -353,25 +356,28 @@ let BingTokenManager = {
/**
* Generates a new token from the server.
*
* @returns {Promise} A promise that resolves with the token
* string once it is obtained.
*/
_getNewToken: function() {
- let request = new RESTRequest("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13");
+ let url = getUrlParam("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13",
+ "browser.translation.bing.authURL",
+ false);
+ let request = new RESTRequest(url);
request.setHeader("Content-type", "application/x-www-form-urlencoded");
let params = [
"grant_type=client_credentials",
"scope=" + encodeURIComponent("http://api.microsofttranslator.com"),
"client_id=" +
- getAuthTokenParam("%BING_API_CLIENTID%", "browser.translation.bing.clientIdOverride"),
+ getUrlParam("%BING_API_CLIENTID%", "browser.translation.bing.clientIdOverride"),
"client_secret=" +
- getAuthTokenParam("%BING_API_KEY%", "browser.translation.bing.apiKeyOverride")
+ getUrlParam("%BING_API_KEY%", "browser.translation.bing.apiKeyOverride")
];
let deferred = Promise.defer();
this._pendingRequest = deferred.promise;
request.post(params.join("&"), function(err) {
BingTokenManager._pendingRequest = null;
if (err) {
@@ -411,16 +417,15 @@ function escapeXML(aStr) {
.replace("<", "<", "g")
.replace(">", ">", "g");
}
/**
* Fetch an auth token (clientID or client secret), which may be overridden by
* a pref if it's set.
*/
-function getAuthTokenParam(key, prefName) {
- let val;
- try {
- val = Services.prefs.getCharPref(prefName);
- } catch(ex) {}
+function getUrlParam(paramValue, prefName, encode = true) {
+ if (Services.prefs.getPrefType(prefName))
+ paramValue = Services.prefs.getCharPref(prefName);
+ paramValue = Services.urlFormatter.formatURL(paramValue);
- return encodeURIComponent(Services.urlFormatter.formatURL(val || key));
+ return encode ? encodeURIComponent(paramValue) : paramValue;
}
--- a/browser/components/translation/TranslationContentHandler.jsm
+++ b/browser/components/translation/TranslationContentHandler.jsm
@@ -116,26 +116,26 @@ TranslationContentHandler.prototype = {
Cu.import("resource:///modules/translation/BingTranslator.jsm");
// If a TranslationDocument already exists for this document, it should
// be used instead of creating a new one so that we can use the original
// content of the page for the new translation instead of the newly
// translated text.
let translationDocument = this.global.content.translationDocument ||
new TranslationDocument(this.global.content.document);
- let bingTranslation = new BingTranslation(translationDocument,
- msg.data.from,
- msg.data.to);
+ let bingTranslator = new BingTranslator(translationDocument,
+ msg.data.from,
+ msg.data.to);
this.global.content.translationDocument = translationDocument;
translationDocument.translatedFrom = msg.data.from;
translationDocument.translatedTo = msg.data.to;
translationDocument.translationError = false;
- bingTranslation.translate().then(
+ bingTranslator.translate().then(
result => {
this.global.sendAsyncMessage("Translation:Finished", {
characterCount: result.characterCount,
from: msg.data.from,
to: msg.data.to,
success: true
});
translationDocument.showTranslation();