Bug 1142522 - Part 1 Share utils and crypto content modules with chrome as well. r=Standard8

This commit is contained in:
Mike de Boer 2015-04-13 11:54:25 +01:00
Родитель 925edeb002
Коммит c2e46fc06b
3 изменённых файлов: 40 добавлений и 23 удалений

Просмотреть файл

@ -5,10 +5,24 @@
/* global loop:true */
var loop = loop || {};
var inChrome = typeof Components != "undefined" && "utils" in Components;
loop.crypto = (function() {
(function(rootObject) {
"use strict";
var sharedUtils;
if (inChrome) {
this.EXPORTED_SYMBOLS = ["LoopCrypto"];
var Cu = Components.utils;
Cu.importGlobalProperties(["crypto"]);
rootObject = {
crypto: crypto
};
sharedUtils = Cu.import("resource:///modules/loop/utils.js", {}).utils;
} else {
sharedUtils = this.shared.utils;
}
var ALGORITHM = "AES-GCM";
var KEY_LENGTH = 128;
// We use JSON web key formats for the generated keys.
@ -19,14 +33,6 @@ loop.crypto = (function() {
var ENCRYPT_TAG_LENGTH = 128;
var INITIALIZATION_VECTOR_LENGTH = 12;
var sharedUtils = loop.shared.utils;
/**
* Root object, by default set to window.
* @type {DOMWindow|Object}
*/
var rootObject = window;
/**
* Sets a new root object. This is useful for testing crypto not supported as
* it allows us to fake crypto not being present.
@ -127,7 +133,7 @@ loop.crypto = (function() {
var joinedData = _mergeIVandCipherText(iv, new DataView(cipherText));
// Now convert to a string and base-64 encode.
var encryptedData = loop.shared.utils.btoa(joinedData);
var encryptedData = sharedUtils.btoa(joinedData);
resolve(encryptedData);
}).catch(function(error) {
@ -215,7 +221,7 @@ loop.crypto = (function() {
*/
function _splitIVandCipherText(encryptedData) {
// Convert into byte arrays.
var encryptedDataArray = loop.shared.utils.atob(encryptedData);
var encryptedDataArray = sharedUtils.atob(encryptedData);
// Now split out the initialization vector and the cipherText.
var iv = encryptedDataArray.slice(0, INITIALIZATION_VECTOR_LENGTH);
@ -228,11 +234,11 @@ loop.crypto = (function() {
};
}
return {
this[inChrome ? "LoopCrypto" : "crypto"] = {
decryptBytes: decryptBytes,
encryptBytes: encryptBytes,
generateKey: generateKey,
isSupported: isSupported,
setRootObject: setRootObject
};
})();
}).call(inChrome ? this : loop, this);

Просмотреть файл

@ -6,9 +6,21 @@
var loop = loop || {};
loop.shared = loop.shared || {};
loop.shared.utils = (function(mozL10n) {
var inChrome = typeof Components != "undefined" && "utils" in Components;
(function() {
"use strict";
var mozL10n;
if (inChrome) {
this.EXPORTED_SYMBOLS = ["utils"];
mozL10n = { get: function() {
throw new Error("mozL10n.get not availabled from chrome!");
}};
} else {
mozL10n = document.mozL10n || navigator.mozL10n
}
/**
* Call types used for determining if a call is audio/video or audio-only.
*/
@ -152,7 +164,7 @@ loop.shared.utils = (function(mozL10n) {
* `false`.
* @return {String} The platform we're currently running on, in lower-case.
*/
var getOS = _.memoize(function(platform, withVersion) {
var getOS = function(platform, withVersion) {
if (!platform) {
if ("oscpu" in window.navigator) {
// See https://developer.mozilla.org/en-US/docs/Web/API/Navigator/oscpu
@ -189,10 +201,7 @@ loop.shared.utils = (function(mozL10n) {
}
return platform.trim();
}, function(platform, withVersion) {
// Cache the return values with the following key.
return (platform + "") + (withVersion + "");
});
};
/**
* Helper to get the Operating System version.
@ -205,7 +214,7 @@ loop.shared.utils = (function(mozL10n) {
* @return {String} The current version of the platform we're currently running
* on.
*/
var getOSVersion = _.memoize(function(platform) {
var getOSVersion = function(platform) {
var os = getOS(platform, true);
var digitsRE = /\s([0-9.]+)/;
@ -249,7 +258,7 @@ loop.shared.utils = (function(mozL10n) {
}
return { major: Infinity, minor: 0 };
});
};
/**
* Helper to allow getting some of the location data in a way that's compatible
@ -517,7 +526,7 @@ loop.shared.utils = (function(mozL10n) {
return result;
}
return {
this.utils = {
CALL_TYPES: CALL_TYPES,
FAILURE_DETAILS: FAILURE_DETAILS,
REST_ERRNOS: REST_ERRNOS,
@ -541,4 +550,4 @@ loop.shared.utils = (function(mozL10n) {
strToUint8Array: strToUint8Array,
Uint8ArrayToStr: Uint8ArrayToStr
};
})(document.mozL10n || navigator.mozL10n);
}).call(inChrome ? this : loop.shared);

Просмотреть файл

@ -14,6 +14,8 @@ BROWSER_CHROME_MANIFESTS += [
EXTRA_JS_MODULES.loop += [
'CardDavImporter.jsm',
'content/shared/js/crypto.js',
'content/shared/js/utils.js',
'GoogleImporter.jsm',
'LoopCalls.jsm',
'LoopContacts.jsm',