Bug 1022725: Rename BingTranslation to BingTranslator and allow overriding URLs. r=florian, a=gavin
authorMike de Boer <mdeboer@mozilla.com>
Thu, 19 Jun 2014 16:56:00 +0200
changeset 208427 a4b0020e343c7ea6ac3d688e7b5f5c6979405b36
parent 208426 3c799bb41e14c9f97869ec442b62b00ef4367300
child 208428 13c4804465d6b6a9cbbd1b19a804c0d7c71d144d
push id494
push userraliiev@mozilla.com
push dateMon, 25 Aug 2014 18:42:16 +0000
treeherdermozilla-release@a3cc3e46b571 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersflorian, gavin
bugs1022725
milestone32.0a2
Bug 1022725: Rename BingTranslation to BingTranslator and allow overriding URLs. r=florian, a=gavin
browser/components/translation/BingTranslator.jsm
browser/components/translation/TranslationContentHandler.jsm
--- 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("<", "&lt;", "g")
              .replace(">", "&gt;", "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();