2014-05-29 20:13:46 +00:00
|
|
|
/* 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;
|
|
|
|
|
2014-09-18 08:52:05 +00:00
|
|
|
Cu.import("resource://services-common/utils.js");
|
2014-05-29 20:13:46 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource:///modules/loop/MozLoopService.jsm");
|
2014-08-12 10:24:07 +00:00
|
|
|
Cu.import("resource:///modules/loop/LoopContacts.jsm");
|
2014-05-29 20:13:46 +00:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "hookWindowCloseForPanelClose",
|
2014-08-07 14:45:08 +00:00
|
|
|
"resource://gre/modules/MozSocialAPI.jsm");
|
2014-08-29 03:51:34 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
|
|
|
|
"resource://gre/modules/PluralForm.jsm");
|
2014-08-08 17:10:54 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "appInfo", function() {
|
|
|
|
return Cc["@mozilla.org/xre/app-info;1"]
|
|
|
|
.getService(Ci.nsIXULAppInfo)
|
|
|
|
.QueryInterface(Ci.nsIXULRuntime);
|
|
|
|
});
|
2014-08-07 14:45:08 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "clipboardHelper",
|
|
|
|
"@mozilla.org/widget/clipboardhelper;1",
|
|
|
|
"nsIClipboardHelper");
|
2014-09-18 08:52:05 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "extProtocolSvc",
|
|
|
|
"@mozilla.org/uriloader/external-protocol-service;1",
|
|
|
|
"nsIExternalProtocolService");
|
2014-05-29 20:13:46 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["injectLoopAPI"];
|
|
|
|
|
2014-08-12 10:24:07 +00:00
|
|
|
/**
|
|
|
|
* Trying to clone an Error object into a different container will yield an error.
|
|
|
|
* We can work around this by copying the properties we care about onto a regular
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* @param {Error} error Error object to copy
|
|
|
|
* @param {nsIDOMWindow} targetWindow The content window to attach the API
|
|
|
|
*/
|
|
|
|
const cloneErrorObject = function(error, targetWindow) {
|
|
|
|
let obj = new targetWindow.Error();
|
|
|
|
for (let prop of Object.getOwnPropertyNames(error)) {
|
|
|
|
obj[prop] = String(error[prop]);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2014-09-12 10:10:10 +00:00
|
|
|
/**
|
|
|
|
* Makes an object or value available to an unprivileged target window.
|
|
|
|
*
|
|
|
|
* Primitives are returned as they are, while objects are cloned into the
|
|
|
|
* specified target. Error objects are also handled correctly.
|
|
|
|
*
|
|
|
|
* @param {any} value Value or object to copy
|
|
|
|
* @param {nsIDOMWindow} targetWindow The content window to copy to
|
|
|
|
*/
|
|
|
|
const cloneValueInto = function(value, targetWindow) {
|
|
|
|
if (!value || typeof value != "object") {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inspect for an error this way, because the Error object is special.
|
|
|
|
if (value.constructor.name == "Error") {
|
|
|
|
return cloneErrorObject(value, targetWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Cu.cloneInto(value, targetWindow);
|
|
|
|
};
|
|
|
|
|
2014-08-12 10:24:07 +00:00
|
|
|
/**
|
|
|
|
* Inject any API containing _only_ function properties into the given window.
|
|
|
|
*
|
|
|
|
* @param {Object} api Object containing functions that need to
|
|
|
|
* be exposed to content
|
|
|
|
* @param {nsIDOMWindow} targetWindow The content window to attach the API
|
|
|
|
*/
|
|
|
|
const injectObjectAPI = function(api, targetWindow) {
|
|
|
|
let injectedAPI = {};
|
|
|
|
// Wrap all the methods in `api` to help results passed to callbacks get
|
|
|
|
// through the priv => unpriv barrier with `Cu.cloneInto()`.
|
|
|
|
Object.keys(api).forEach(func => {
|
|
|
|
injectedAPI[func] = function(...params) {
|
|
|
|
let callback = params.pop();
|
|
|
|
api[func](...params, function(...results) {
|
2014-09-12 10:10:10 +00:00
|
|
|
callback(...[cloneValueInto(r, targetWindow) for (r of results)]);
|
2014-08-12 10:24:07 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
let contentObj = Cu.cloneInto(injectedAPI, targetWindow, {cloneFunctions: true});
|
|
|
|
// Since we deny preventExtensions on XrayWrappers, because Xray semantics make
|
|
|
|
// it difficult to act like an object has actually been frozen, we try to seal
|
|
|
|
// the `contentObj` without Xrays.
|
|
|
|
try {
|
|
|
|
Object.seal(Cu.waiveXrays(contentObj));
|
|
|
|
} catch (ex) {}
|
|
|
|
return contentObj;
|
|
|
|
};
|
|
|
|
|
2014-09-19 15:02:00 +00:00
|
|
|
/**
|
|
|
|
* Get the two-digit hexadecimal code for a byte
|
|
|
|
*
|
|
|
|
* @param {byte} charCode
|
|
|
|
*/
|
|
|
|
const toHexString = function(charCode) {
|
|
|
|
return ("0" + charCode.toString(16)).slice(-2);
|
|
|
|
};
|
|
|
|
|
2014-05-29 20:13:46 +00:00
|
|
|
/**
|
|
|
|
* Inject the loop API into the given window. The caller must be sure the
|
|
|
|
* window is a loop content window (eg, a panel, chatwindow, or similar).
|
|
|
|
*
|
|
|
|
* See the documentation on the individual functions for details of the API.
|
|
|
|
*
|
|
|
|
* @param {nsIDOMWindow} targetWindow The content window to attach the API.
|
|
|
|
*/
|
|
|
|
function injectLoopAPI(targetWindow) {
|
2014-06-26 23:55:03 +00:00
|
|
|
let ringer;
|
|
|
|
let ringerStopper;
|
2014-08-08 17:10:54 +00:00
|
|
|
let appVersionInfo;
|
2014-08-12 10:24:07 +00:00
|
|
|
let contactsAPI;
|
2014-06-26 23:55:03 +00:00
|
|
|
|
2014-05-29 20:13:46 +00:00
|
|
|
let api = {
|
2014-09-11 19:36:14 +00:00
|
|
|
/**
|
|
|
|
* Gets an object with data that represents the currently
|
|
|
|
* authenticated user's identity.
|
2014-09-18 17:40:35 +00:00
|
|
|
*
|
|
|
|
* @return null if user not logged in; profile object otherwise
|
2014-09-11 19:36:14 +00:00
|
|
|
*/
|
|
|
|
userProfile: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
if (!MozLoopService.userProfile)
|
|
|
|
return null;
|
|
|
|
let userProfile = Cu.cloneInto({
|
|
|
|
email: MozLoopService.userProfile.email,
|
|
|
|
uid: MozLoopService.userProfile.uid
|
|
|
|
}, targetWindow);
|
|
|
|
return userProfile;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-02 19:30:02 +00:00
|
|
|
/**
|
|
|
|
* Sets and gets the "do not disturb" mode activation flag.
|
|
|
|
*/
|
|
|
|
doNotDisturb: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return MozLoopService.doNotDisturb;
|
|
|
|
},
|
|
|
|
set: function(aFlag) {
|
|
|
|
MozLoopService.doNotDisturb = aFlag;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-05-29 20:13:46 +00:00
|
|
|
/**
|
|
|
|
* Returns the current locale of the browser.
|
|
|
|
*
|
|
|
|
* @returns {String} The locale string
|
|
|
|
*/
|
2014-05-29 20:13:46 +00:00
|
|
|
locale: {
|
2014-05-29 20:13:46 +00:00
|
|
|
enumerable: true,
|
2014-05-29 20:13:46 +00:00
|
|
|
get: function() {
|
2014-05-29 20:13:46 +00:00
|
|
|
return MozLoopService.locale;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-09-10 14:22:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the callData for a specific callDataId
|
|
|
|
*
|
|
|
|
* The data was retrieved from the LoopServer via a GET/calls/<version> request
|
|
|
|
* triggered by an incoming message from the LoopPushServer.
|
|
|
|
*
|
|
|
|
* @param {int} loopCallId
|
|
|
|
* @returns {callData} The callData or undefined if error.
|
|
|
|
*/
|
|
|
|
getCallData: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(loopCallId) {
|
|
|
|
return Cu.cloneInto(MozLoopService.getCallData(loopCallId), targetWindow);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-08-12 10:24:07 +00:00
|
|
|
/**
|
|
|
|
* Returns the contacts API.
|
|
|
|
*
|
|
|
|
* @returns {Object} The contacts API object
|
|
|
|
*/
|
|
|
|
contacts: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
if (contactsAPI) {
|
|
|
|
return contactsAPI;
|
|
|
|
}
|
|
|
|
return contactsAPI = injectObjectAPI(LoopContacts, targetWindow);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-05-29 20:13:46 +00:00
|
|
|
/**
|
|
|
|
* Returns translated strings associated with an element. Designed
|
|
|
|
* for use with l10n.js
|
|
|
|
*
|
|
|
|
* @param {String} key The element id
|
|
|
|
* @returns {Object} A JSON string containing the localized
|
|
|
|
* attribute/value pairs for the element.
|
|
|
|
*/
|
|
|
|
getStrings: {
|
|
|
|
enumerable: true,
|
2014-05-29 20:13:46 +00:00
|
|
|
writable: true,
|
2014-05-29 20:13:46 +00:00
|
|
|
value: function(key) {
|
|
|
|
return MozLoopService.getStrings(key);
|
|
|
|
}
|
2014-05-29 20:13:47 +00:00
|
|
|
},
|
2014-05-29 20:13:46 +00:00
|
|
|
|
2014-08-29 03:51:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the correct form of a semi-colon separated string
|
|
|
|
* based on the value of the `num` argument and the current locale.
|
|
|
|
*
|
|
|
|
* @param {Integer} num The number used to find the plural form.
|
|
|
|
* @param {String} str The semi-colon separated string of word forms.
|
|
|
|
* @returns {String} The correct word form based on the value of the number
|
|
|
|
* and the current locale.
|
|
|
|
*/
|
|
|
|
getPluralForm: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(num, str) {
|
|
|
|
return PluralForm.get(num, str);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-05-29 20:13:47 +00:00
|
|
|
/**
|
|
|
|
* Call to ensure that any necessary registrations for the Loop Service
|
|
|
|
* have taken place.
|
|
|
|
*
|
|
|
|
* Callback parameters:
|
|
|
|
* - err null on successful registration, non-null otherwise.
|
|
|
|
*
|
|
|
|
* @param {Function} callback Will be called once registration is complete,
|
|
|
|
* or straight away if registration has already
|
|
|
|
* happened.
|
|
|
|
*/
|
|
|
|
ensureRegistered: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(callback) {
|
2014-06-04 13:42:41 +00:00
|
|
|
// We translate from a promise to a callback, as we can't pass promises from
|
|
|
|
// Promise.jsm across the priv versus unpriv boundary.
|
2014-09-12 10:10:10 +00:00
|
|
|
MozLoopService.register().then(() => {
|
2014-06-04 13:42:41 +00:00
|
|
|
callback(null);
|
|
|
|
}, err => {
|
2014-09-12 10:10:10 +00:00
|
|
|
callback(cloneValueInto(err, targetWindow));
|
|
|
|
}).catch(Cu.reportError);
|
2014-05-29 20:13:47 +00:00
|
|
|
}
|
2014-05-29 20:13:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to note a call url expiry time. If the time is later than the current
|
|
|
|
* latest expiry time, then the stored expiry time is increased. For times
|
|
|
|
* sooner, this function is a no-op; this ensures we always have the latest
|
|
|
|
* expiry time for a url.
|
|
|
|
*
|
2014-05-29 20:13:48 +00:00
|
|
|
* This is used to determine whether or not we should be registering with the
|
2014-05-29 20:13:48 +00:00
|
|
|
* push server on start.
|
|
|
|
*
|
|
|
|
* @param {Integer} expiryTimeSeconds The seconds since epoch of the expiry time
|
|
|
|
* of the url.
|
|
|
|
*/
|
|
|
|
noteCallUrlExpiry: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(expiryTimeSeconds) {
|
|
|
|
MozLoopService.noteCallUrlExpiry(expiryTimeSeconds);
|
|
|
|
}
|
2014-05-29 20:13:48 +00:00
|
|
|
},
|
|
|
|
|
2014-07-01 16:12:54 +00:00
|
|
|
/**
|
|
|
|
* Set any character preference under "loop."
|
|
|
|
*
|
|
|
|
* @param {String} prefName The name of the pref without the preceding "loop."
|
|
|
|
* @param {String} stringValue The value to set.
|
|
|
|
*
|
|
|
|
* Any errors thrown by the Mozilla pref API are logged to the console
|
|
|
|
* and cause false to be returned.
|
|
|
|
*/
|
|
|
|
setLoopCharPref: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(prefName, value) {
|
|
|
|
MozLoopService.setLoopCharPref(prefName, value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-05-29 20:13:48 +00:00
|
|
|
/**
|
|
|
|
* Return any preference under "loop." that's coercible to a character
|
|
|
|
* preference.
|
|
|
|
*
|
|
|
|
* @param {String} prefName The name of the pref without the preceding
|
|
|
|
* "loop."
|
|
|
|
*
|
|
|
|
* Any errors thrown by the Mozilla pref API are logged to the console
|
|
|
|
* and cause null to be returned. This includes the case of the preference
|
|
|
|
* not being found.
|
|
|
|
*
|
|
|
|
* @return {String} on success, null on error
|
|
|
|
*/
|
|
|
|
getLoopCharPref: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(prefName) {
|
|
|
|
return MozLoopService.getLoopCharPref(prefName);
|
|
|
|
}
|
2014-06-26 23:55:03 +00:00
|
|
|
},
|
|
|
|
|
2014-08-15 12:33:51 +00:00
|
|
|
/**
|
|
|
|
* Return any preference under "loop." that's coercible to a boolean
|
|
|
|
* preference.
|
|
|
|
*
|
|
|
|
* @param {String} prefName The name of the pref without the preceding
|
|
|
|
* "loop."
|
|
|
|
*
|
|
|
|
* Any errors thrown by the Mozilla pref API are logged to the console
|
|
|
|
* and cause null to be returned. This includes the case of the preference
|
|
|
|
* not being found.
|
|
|
|
*
|
|
|
|
* @return {String} on success, null on error
|
|
|
|
*/
|
|
|
|
getLoopBoolPref: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(prefName) {
|
|
|
|
return MozLoopService.getLoopBoolPref(prefName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-26 23:55:03 +00:00
|
|
|
/**
|
|
|
|
* Starts alerting the user about an incoming call
|
|
|
|
*/
|
|
|
|
startAlerting: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function() {
|
|
|
|
let chromeWindow = getChromeWindow(targetWindow);
|
|
|
|
chromeWindow.getAttention();
|
|
|
|
ringer = new chromeWindow.Audio();
|
|
|
|
ringer.src = Services.prefs.getCharPref("loop.ringtone");
|
|
|
|
ringer.loop = true;
|
|
|
|
ringer.load();
|
|
|
|
ringer.play();
|
|
|
|
targetWindow.document.addEventListener("visibilitychange",
|
|
|
|
ringerStopper = function(event) {
|
|
|
|
if (event.currentTarget.hidden) {
|
|
|
|
api.stopAlerting.value();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops alerting the user about an incoming call
|
|
|
|
*/
|
|
|
|
stopAlerting: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function() {
|
|
|
|
if (ringerStopper) {
|
|
|
|
targetWindow.document.removeEventListener("visibilitychange",
|
|
|
|
ringerStopper);
|
|
|
|
ringerStopper = null;
|
|
|
|
}
|
|
|
|
if (ringer) {
|
|
|
|
ringer.pause();
|
|
|
|
ringer = null;
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 08:23:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a hawk based request to the loop server.
|
|
|
|
*
|
|
|
|
* Callback parameters:
|
|
|
|
* - {Object|null} null if success. Otherwise an object:
|
|
|
|
* {
|
|
|
|
* code: 401,
|
|
|
|
* errno: 401,
|
|
|
|
* error: "Request failed",
|
|
|
|
* message: "invalid token"
|
|
|
|
* }
|
|
|
|
* - {String} The body of the response.
|
|
|
|
*
|
2014-09-18 17:40:35 +00:00
|
|
|
* @param {LOOP_SESSION_TYPE} sessionType The type of session to use for
|
|
|
|
* the request. This is one of the
|
|
|
|
* LOOP_SESSION_TYPE members
|
2014-07-03 08:23:38 +00:00
|
|
|
* @param {String} path The path to make the request to.
|
|
|
|
* @param {String} method The request method, e.g. 'POST', 'GET'.
|
|
|
|
* @param {Object} payloadObj An object which is converted to JSON and
|
|
|
|
* transmitted with the request.
|
|
|
|
* @param {Function} callback Called when the request completes.
|
|
|
|
*/
|
|
|
|
hawkRequest: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
2014-09-18 17:40:35 +00:00
|
|
|
value: function(sessionType, path, method, payloadObj, callback) {
|
2014-07-03 08:23:38 +00:00
|
|
|
// XXX Should really return a DOM promise here.
|
2014-09-18 17:40:35 +00:00
|
|
|
MozLoopService.hawkRequest(sessionType, path, method, payloadObj).then((response) => {
|
2014-07-03 08:23:38 +00:00
|
|
|
callback(null, response.body);
|
2014-09-12 10:10:10 +00:00
|
|
|
}, hawkError => {
|
|
|
|
// The hawkError.error property, while usually a string representing
|
|
|
|
// an HTTP response status message, may also incorrectly be a native
|
|
|
|
// error object that will cause the cloning function to fail.
|
|
|
|
callback(Cu.cloneInto({
|
|
|
|
error: (hawkError.error && typeof hawkError.error == "string")
|
|
|
|
? hawkError.error : "Unexpected exception",
|
|
|
|
message: hawkError.message,
|
|
|
|
code: hawkError.code,
|
|
|
|
errno: hawkError.errno,
|
|
|
|
}, targetWindow));
|
|
|
|
}).catch(Cu.reportError);
|
2014-07-03 08:23:38 +00:00
|
|
|
}
|
|
|
|
},
|
2014-08-07 14:45:08 +00:00
|
|
|
|
2014-09-10 08:48:54 +00:00
|
|
|
LOOP_SESSION_TYPE: {
|
|
|
|
enumerable: true,
|
2014-09-18 17:40:35 +00:00
|
|
|
get: function() {
|
|
|
|
return Cu.cloneInto(LOOP_SESSION_TYPE, targetWindow);
|
|
|
|
}
|
2014-09-10 08:48:54 +00:00
|
|
|
},
|
|
|
|
|
2014-08-29 13:11:54 +00:00
|
|
|
logInToFxA: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function() {
|
|
|
|
return MozLoopService.logInToFxA();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-09-18 20:53:44 +00:00
|
|
|
logOutFromFxA: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function() {
|
|
|
|
return MozLoopService.logOutFromFxA();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-08-07 14:45:08 +00:00
|
|
|
/**
|
|
|
|
* Copies passed string onto the system clipboard.
|
|
|
|
*
|
|
|
|
* @param {String} str The string to copy
|
|
|
|
*/
|
|
|
|
copyString: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(str) {
|
|
|
|
clipboardHelper.copyString(str);
|
|
|
|
}
|
2014-08-08 17:10:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the app version information for use during feedback.
|
|
|
|
*
|
|
|
|
* @return {Object} An object containing:
|
|
|
|
* - channel: The update channel the application is on
|
|
|
|
* - version: The application version
|
|
|
|
* - OS: The operating system the application is running on
|
|
|
|
*/
|
|
|
|
appVersionInfo: {
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
if (!appVersionInfo) {
|
|
|
|
let defaults = Services.prefs.getDefaultBranch(null);
|
|
|
|
|
2014-09-18 17:40:35 +00:00
|
|
|
// If the lazy getter explodes, we're probably loaded in xpcshell,
|
|
|
|
// which doesn't have what we need, so log an error.
|
|
|
|
try {
|
|
|
|
appVersionInfo = Cu.cloneInto({
|
|
|
|
channel: defaults.getCharPref("app.update.channel"),
|
|
|
|
version: appInfo.version,
|
|
|
|
OS: appInfo.OS
|
|
|
|
}, targetWindow);
|
|
|
|
} catch (ex) {
|
|
|
|
// only log outside of xpcshell to avoid extra message noise
|
|
|
|
if (typeof window !== 'undefined' && "console" in window) {
|
|
|
|
console.log("Failed to construct appVersionInfo; if this isn't " +
|
|
|
|
"an xpcshell unit test, something is wrong", ex);
|
|
|
|
}
|
|
|
|
}
|
2014-08-08 17:10:54 +00:00
|
|
|
}
|
|
|
|
return appVersionInfo;
|
|
|
|
}
|
|
|
|
},
|
2014-09-18 08:52:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Composes an email via the external protocol service.
|
|
|
|
*
|
|
|
|
* @param {String} subject Subject of the email to send
|
|
|
|
* @param {String} body Body message of the email to send
|
|
|
|
*/
|
|
|
|
composeEmail: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(subject, body) {
|
|
|
|
let mailtoURL = "mailto:?subject=" + encodeURIComponent(subject) + "&" +
|
|
|
|
"body=" + encodeURIComponent(body);
|
|
|
|
extProtocolSvc.loadURI(CommonUtils.makeURI(mailtoURL));
|
|
|
|
}
|
2014-09-18 12:05:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a value to a telemetry histogram.
|
|
|
|
*
|
|
|
|
* @param {string} histogramId Name of the telemetry histogram to update.
|
|
|
|
* @param {integer} value Value to add to the histogram.
|
|
|
|
*/
|
|
|
|
telemetryAdd: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(histogramId, value) {
|
|
|
|
Services.telemetry.getHistogramById(histogramId).add(value);
|
|
|
|
}
|
|
|
|
},
|
2014-09-19 15:02:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compose a URL pointing to the location of an avatar by email address.
|
|
|
|
* At the moment we use the Gravatar service to match email addresses with
|
|
|
|
* avatars. This might change in the future as avatars might come from another
|
|
|
|
* source.
|
|
|
|
*
|
|
|
|
* @param {String} emailAddress Users' email address
|
|
|
|
* @param {Number} size Size of the avatar image to return in pixels.
|
|
|
|
* Optional. Default value: 40.
|
|
|
|
* @return the URL pointing to an avatar matching the provided email address.
|
|
|
|
*/
|
|
|
|
getUserAvatar: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
value: function(emailAddress, size = 40) {
|
|
|
|
if (!emailAddress) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the MD5 dance.
|
|
|
|
let hasher = Cc["@mozilla.org/security/hash;1"]
|
|
|
|
.createInstance(Ci.nsICryptoHash);
|
|
|
|
hasher.init(Ci.nsICryptoHash.MD5);
|
|
|
|
let stringStream = Cc["@mozilla.org/io/string-input-stream;1"]
|
|
|
|
.createInstance(Ci.nsIStringInputStream);
|
|
|
|
stringStream.data = emailAddress.trim().toLowerCase();
|
|
|
|
hasher.updateFromStream(stringStream, -1);
|
|
|
|
let hash = hasher.finish(false);
|
|
|
|
// Convert the binary hash data to a hex string.
|
|
|
|
let md5Email = [toHexString(hash.charCodeAt(i)) for (i in hash)].join("");
|
|
|
|
|
|
|
|
// Compose the Gravatar URL.
|
|
|
|
return "http://www.gravatar.com/avatar/" + md5Email + ".jpg?default=blank&s=" + size;
|
|
|
|
}
|
|
|
|
},
|
2014-05-29 20:13:46 +00:00
|
|
|
};
|
|
|
|
|
2014-09-11 19:36:14 +00:00
|
|
|
function onStatusChanged(aSubject, aTopic, aData) {
|
|
|
|
let event = new targetWindow.CustomEvent("LoopStatusChanged");
|
|
|
|
targetWindow.dispatchEvent(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
function onDOMWindowDestroyed(aSubject, aTopic, aData) {
|
|
|
|
if (targetWindow && aSubject != targetWindow)
|
|
|
|
return;
|
|
|
|
Services.obs.removeObserver(onDOMWindowDestroyed, "dom-window-destroyed");
|
|
|
|
Services.obs.removeObserver(onStatusChanged, "loop-status-changed");
|
|
|
|
};
|
|
|
|
|
2014-05-29 20:13:46 +00:00
|
|
|
let contentObj = Cu.createObjectIn(targetWindow);
|
|
|
|
Object.defineProperties(contentObj, api);
|
2014-07-23 17:08:21 +00:00
|
|
|
Object.seal(contentObj);
|
2014-05-29 20:13:46 +00:00
|
|
|
Cu.makeObjectPropsNormal(contentObj);
|
2014-09-11 19:36:14 +00:00
|
|
|
Services.obs.addObserver(onStatusChanged, "loop-status-changed", false);
|
|
|
|
Services.obs.addObserver(onDOMWindowDestroyed, "dom-window-destroyed", false);
|
2014-05-29 20:13:46 +00:00
|
|
|
|
2014-09-18 17:40:35 +00:00
|
|
|
if ("navigator" in targetWindow) {
|
|
|
|
targetWindow.navigator.wrappedJSObject.__defineGetter__("mozLoop", function () {
|
|
|
|
// We do this in a getter, so that we create these objects
|
|
|
|
// only on demand (this is a potential concern, since
|
|
|
|
// otherwise we might add one per iframe, and keep them
|
|
|
|
// alive for as long as the window is alive).
|
|
|
|
delete targetWindow.navigator.wrappedJSObject.mozLoop;
|
|
|
|
return targetWindow.navigator.wrappedJSObject.mozLoop = contentObj;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle window.close correctly on the panel and chatbox.
|
|
|
|
hookWindowCloseForPanelClose(targetWindow);
|
|
|
|
} else {
|
|
|
|
// This isn't a window; but it should be a JS scope; used for testing
|
|
|
|
return targetWindow.mozLoop = contentObj;
|
|
|
|
}
|
2014-05-29 20:13:46 +00:00
|
|
|
|
|
|
|
}
|
2014-06-26 23:55:03 +00:00
|
|
|
|
|
|
|
function getChromeWindow(contentWin) {
|
|
|
|
return contentWin.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShellTreeItem)
|
|
|
|
.rootTreeItem
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindow);
|
|
|
|
}
|