2014-05-29 13:13:46 -07: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;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2014-06-04 06:42:41 -07:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2014-05-29 13:13:46 -07:00
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["MozLoopService"];
|
|
|
|
|
2014-06-09 15:14:14 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "console",
|
|
|
|
"resource://gre/modules/devtools/Console.jsm");
|
|
|
|
|
2014-05-29 13:13:46 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "injectLoopAPI",
|
|
|
|
"resource:///modules/loop/MozLoopAPI.jsm");
|
|
|
|
|
2014-06-09 15:14:14 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "convertToRTCStatsReport",
|
|
|
|
"resource://gre/modules/media/RTCStatsReport.jsm");
|
|
|
|
|
2014-05-29 13:13:46 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Chat", "resource:///modules/Chat.jsm");
|
|
|
|
|
2014-06-05 02:29:54 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CommonUtils",
|
|
|
|
"resource://services-common/utils.js");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CryptoUtils",
|
|
|
|
"resource://services-crypto/utils.js");
|
|
|
|
|
2014-06-18 02:42:15 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "HawkClient",
|
|
|
|
"resource://services-common/hawkclient.js");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "deriveHawkCredentials",
|
2014-06-05 02:29:54 -07:00
|
|
|
"resource://services-common/hawkrequest.js");
|
|
|
|
|
2014-06-16 00:41:17 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "MozLoopPushHandler",
|
|
|
|
"resource:///modules/loop/MozLoopPushHandler.jsm");
|
2014-05-29 13:13:46 -07:00
|
|
|
|
2014-06-09 15:14:14 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
|
|
|
|
"@mozilla.org/uuid-generator;1",
|
|
|
|
"nsIUUIDGenerator");
|
|
|
|
|
2014-05-29 13:13:46 -07:00
|
|
|
/**
|
|
|
|
* Internal helper methods and state
|
2014-05-29 13:13:47 -07:00
|
|
|
*
|
|
|
|
* The registration is a two-part process. First we need to connect to
|
|
|
|
* and register with the push server. Then we need to take the result of that
|
|
|
|
* and register with the Loop server.
|
2014-05-29 13:13:46 -07:00
|
|
|
*/
|
|
|
|
let MozLoopServiceInternal = {
|
|
|
|
// The uri of the Loop server.
|
|
|
|
loopServerUri: Services.prefs.getCharPref("loop.server"),
|
2014-06-09 15:14:14 -07:00
|
|
|
telemetryUri: Services.prefs.getCharPref("loop.telemetryURL"),
|
2014-05-29 13:13:46 -07:00
|
|
|
|
2014-06-04 06:42:41 -07:00
|
|
|
// The current deferred for the registration process. This is set if in progress
|
|
|
|
// or the registration was successful. This is null if a registration attempt was
|
|
|
|
// unsuccessful.
|
|
|
|
_registeredDeferred: null,
|
2014-05-29 13:13:48 -07:00
|
|
|
|
2014-05-29 13:13:46 -07:00
|
|
|
/**
|
2014-05-29 13:13:48 -07:00
|
|
|
* The initial delay for push registration. This ensures we don't start
|
|
|
|
* kicking off straight after browser startup, just a few seconds later.
|
|
|
|
*/
|
|
|
|
get initialRegistrationDelayMilliseconds() {
|
|
|
|
try {
|
|
|
|
// Let a pref override this for developer & testing use.
|
2014-06-04 06:42:41 -07:00
|
|
|
return Services.prefs.getIntPref("loop.initialDelay");
|
2014-05-29 13:13:48 -07:00
|
|
|
} catch (x) {
|
2014-06-04 06:42:41 -07:00
|
|
|
// Default to 5 seconds
|
|
|
|
return 5000;
|
2014-05-29 13:13:48 -07:00
|
|
|
}
|
|
|
|
return initialDelay;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current latest expiry time for urls.
|
2014-05-29 13:13:46 -07:00
|
|
|
*
|
2014-05-29 13:13:48 -07:00
|
|
|
* In seconds since epoch.
|
2014-05-29 13:13:46 -07:00
|
|
|
*/
|
2014-05-29 13:13:48 -07:00
|
|
|
get expiryTimeSeconds() {
|
|
|
|
try {
|
2014-06-04 06:42:41 -07:00
|
|
|
return Services.prefs.getIntPref("loop.urlsExpiryTimeSeconds");
|
2014-05-29 13:13:48 -07:00
|
|
|
} catch (x) {
|
|
|
|
// It is ok for the pref not to exist.
|
2014-06-04 06:42:41 -07:00
|
|
|
return 0;
|
2014-05-29 13:13:48 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the expiry time to either the specified time, or keeps it the same
|
|
|
|
* depending on which is latest.
|
|
|
|
*/
|
|
|
|
set expiryTimeSeconds(time) {
|
|
|
|
if (time > this.expiryTimeSeconds) {
|
|
|
|
Services.prefs.setIntPref("loop.urlsExpiryTimeSeconds", time);
|
|
|
|
}
|
|
|
|
},
|
2014-05-29 13:13:46 -07:00
|
|
|
|
2014-06-05 02:29:54 -07:00
|
|
|
/**
|
|
|
|
* Returns true if the expiry time is in the future.
|
|
|
|
*/
|
|
|
|
urlExpiryTimeIsInFuture: function() {
|
|
|
|
return this.expiryTimeSeconds * 1000 > Date.now();
|
|
|
|
},
|
|
|
|
|
2014-06-02 12:30:02 -07:00
|
|
|
/**
|
|
|
|
* Retrieves MozLoopService "do not disturb" pref value.
|
|
|
|
*
|
|
|
|
* @return {Boolean} aFlag
|
|
|
|
*/
|
|
|
|
get doNotDisturb() {
|
|
|
|
return Services.prefs.getBoolPref("loop.do_not_disturb");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets MozLoopService "do not disturb" pref value.
|
|
|
|
*
|
|
|
|
* @param {Boolean} aFlag
|
|
|
|
*/
|
|
|
|
set doNotDisturb(aFlag) {
|
|
|
|
Services.prefs.setBoolPref("loop.do_not_disturb", Boolean(aFlag));
|
|
|
|
},
|
|
|
|
|
2014-05-29 13:13:46 -07:00
|
|
|
/**
|
2014-05-29 13:13:47 -07:00
|
|
|
* Starts registration of Loop with the push server, and then will register
|
|
|
|
* with the Loop server. It will return early if already registered.
|
|
|
|
*
|
2014-06-16 00:41:17 -07:00
|
|
|
* @param {Object} mockPushHandler Optional, test-only mock push handler. Used
|
|
|
|
* to allow mocking of the MozLoopPushHandler.
|
2014-06-04 06:42:41 -07:00
|
|
|
* @returns {Promise} a promise that is resolved with no params on completion, or
|
|
|
|
* rejected with an error code or string.
|
2014-05-29 13:13:46 -07:00
|
|
|
*/
|
2014-06-16 00:41:17 -07:00
|
|
|
promiseRegisteredWithServers: function(mockPushHandler) {
|
2014-06-04 06:42:41 -07:00
|
|
|
if (this._registeredDeferred) {
|
|
|
|
return this._registeredDeferred.promise;
|
2014-05-29 13:13:47 -07:00
|
|
|
}
|
|
|
|
|
2014-06-04 06:42:41 -07:00
|
|
|
this._registeredDeferred = Promise.defer();
|
|
|
|
// We grab the promise early in case .initialize or its results sets
|
|
|
|
// it back to null on error.
|
|
|
|
let result = this._registeredDeferred.promise;
|
2014-05-29 13:13:47 -07:00
|
|
|
|
2014-06-16 00:41:17 -07:00
|
|
|
this._pushHandler = mockPushHandler || MozLoopPushHandler;
|
|
|
|
|
|
|
|
this._pushHandler.initialize(this.onPushRegistered.bind(this),
|
|
|
|
this.onHandleNotification.bind(this));
|
2014-05-29 13:13:46 -07:00
|
|
|
|
2014-06-04 06:42:41 -07:00
|
|
|
return result;
|
2014-05-29 13:13:47 -07:00
|
|
|
},
|
|
|
|
|
2014-06-05 02:29:54 -07:00
|
|
|
/**
|
2014-06-18 02:42:15 -07:00
|
|
|
* Performs a hawk based request to the loop server.
|
|
|
|
*
|
|
|
|
* @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.
|
2014-07-03 01:23:38 -07:00
|
|
|
* @returns {Promise}
|
|
|
|
* Returns a promise that resolves to the response of the API call,
|
|
|
|
* or is rejected with an error. If the server response can be parsed
|
|
|
|
* as JSON and contains an 'error' property, the promise will be
|
|
|
|
* rejected with this JSON-parsed response.
|
2014-06-18 02:42:15 -07:00
|
|
|
*/
|
|
|
|
hawkRequest: function(path, method, payloadObj) {
|
|
|
|
if (!this._hawkClient) {
|
|
|
|
this._hawkClient = new HawkClient(this.loopServerUri);
|
|
|
|
}
|
|
|
|
|
|
|
|
let sessionToken;
|
|
|
|
try {
|
|
|
|
sessionToken = Services.prefs.getCharPref("loop.hawk-session-token");
|
|
|
|
} catch (x) {
|
|
|
|
// It is ok for this not to exist, we'll default to sending no-creds
|
|
|
|
}
|
|
|
|
|
|
|
|
let credentials;
|
|
|
|
if (sessionToken) {
|
2014-07-03 01:23:40 -07:00
|
|
|
// true = use a hex key, as required by the server (see bug 1032738).
|
|
|
|
credentials = deriveHawkCredentials(sessionToken, "sessionToken",
|
|
|
|
2 * 32, true);
|
2014-06-18 02:42:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._hawkClient.request(path, method, credentials, payloadObj);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to store a session token from a request if it exists in the headers.
|
2014-06-05 02:29:54 -07:00
|
|
|
*
|
2014-06-18 02:42:15 -07:00
|
|
|
* @param {Object} headers The request headers, which may include a
|
|
|
|
* "hawk-session-token" to be saved.
|
|
|
|
* @return true on success or no token, false on failure.
|
2014-06-05 02:29:54 -07:00
|
|
|
*/
|
2014-06-18 02:42:15 -07:00
|
|
|
storeSessionToken: function(headers) {
|
|
|
|
let sessionToken = headers["hawk-session-token"];
|
|
|
|
if (sessionToken) {
|
|
|
|
// XXX should do more validation here
|
|
|
|
if (sessionToken.length === 64) {
|
|
|
|
Services.prefs.setCharPref("loop.hawk-session-token", sessionToken);
|
|
|
|
} else {
|
|
|
|
// XXX Bubble the precise details up to the UI somehow (bug 1013248).
|
|
|
|
console.warn("Loop server sent an invalid session token");
|
|
|
|
this._registeredDeferred.reject("session-token-wrong-size");
|
|
|
|
this._registeredDeferred = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2014-06-05 02:29:54 -07:00
|
|
|
},
|
|
|
|
|
2014-05-29 13:13:46 -07:00
|
|
|
/**
|
2014-06-16 00:41:17 -07:00
|
|
|
* Callback from MozLoopPushHandler - The push server has been registered
|
2014-05-29 13:13:46 -07:00
|
|
|
* and has given us a push url.
|
|
|
|
*
|
|
|
|
* @param {String} pushUrl The push url given by the push server.
|
|
|
|
*/
|
2014-05-29 13:13:47 -07:00
|
|
|
onPushRegistered: function(err, pushUrl) {
|
|
|
|
if (err) {
|
2014-06-04 06:42:41 -07:00
|
|
|
this._registeredDeferred.reject(err);
|
|
|
|
this._registeredDeferred = null;
|
2014-05-29 13:13:47 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-05 02:29:54 -07:00
|
|
|
this.registerWithLoopServer(pushUrl);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers with the Loop server.
|
|
|
|
*
|
|
|
|
* @param {String} pushUrl The push url given by the push server.
|
|
|
|
* @param {Boolean} noRetry Optional, don't retry if authentication fails.
|
|
|
|
*/
|
|
|
|
registerWithLoopServer: function(pushUrl, noRetry) {
|
2014-06-18 02:42:15 -07:00
|
|
|
this.hawkRequest("/registration", "POST", { simple_push_url: pushUrl})
|
|
|
|
.then((response) => {
|
|
|
|
// If this failed we got an invalid token. storeSessionToken rejects
|
|
|
|
// the _registeredDeferred promise for us, so here we just need to
|
|
|
|
// early return.
|
|
|
|
if (!this.storeSessionToken(response.headers))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.registeredLoopServer = true;
|
|
|
|
this._registeredDeferred.resolve();
|
|
|
|
// No need to clear the promise here, everything was good, so we don't need
|
|
|
|
// to re-register.
|
|
|
|
}, (error) => {
|
|
|
|
if (error.errno == 401) {
|
|
|
|
if (this.urlExpiryTimeIsInFuture()) {
|
|
|
|
// XXX Should this be reported to the user is a visible manner?
|
|
|
|
Cu.reportError("Loop session token is invalid, all previously "
|
|
|
|
+ "generated urls will no longer work.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authorization failed, invalid token, we need to try again with a new token.
|
|
|
|
Services.prefs.clearUserPref("loop.hawk-session-token");
|
|
|
|
this.registerWithLoopServer(pushUrl, true);
|
|
|
|
return;
|
2014-06-05 02:29:54 -07:00
|
|
|
}
|
2014-05-29 13:13:46 -07:00
|
|
|
|
2014-06-18 02:42:15 -07:00
|
|
|
// XXX Bubble the precise details up to the UI somehow (bug 1013248).
|
|
|
|
Cu.reportError("Failed to register with the loop server. error: " + error);
|
|
|
|
this._registeredDeferred.reject(error.errno);
|
|
|
|
this._registeredDeferred = null;
|
2014-06-05 02:29:54 -07:00
|
|
|
}
|
2014-06-18 02:42:15 -07:00
|
|
|
);
|
2014-05-29 13:13:46 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-06-16 00:41:17 -07:00
|
|
|
* Callback from MozLoopPushHandler - A push notification has been received from
|
2014-05-29 13:13:46 -07:00
|
|
|
* the server.
|
|
|
|
*
|
|
|
|
* @param {String} version The version information from the server.
|
|
|
|
*/
|
|
|
|
onHandleNotification: function(version) {
|
2014-06-02 12:30:02 -07:00
|
|
|
if (this.doNotDisturb) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-05 10:57:42 -07:00
|
|
|
this.openChatWindow(null, "LooP", "about:loopconversation#incoming/" + version);
|
2014-05-29 13:13:46 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A getter to obtain and store the strings for loop. This is structured
|
|
|
|
* for use by l10n.js.
|
|
|
|
*
|
|
|
|
* @returns {Object} a map of element ids with attributes to set.
|
|
|
|
*/
|
|
|
|
get localizedStrings() {
|
|
|
|
if (this._localizedStrings)
|
|
|
|
return this._localizedStrings;
|
|
|
|
|
|
|
|
var stringBundle =
|
|
|
|
Services.strings.createBundle('chrome://browser/locale/loop/loop.properties');
|
|
|
|
|
|
|
|
var map = {};
|
|
|
|
var enumerator = stringBundle.getSimpleEnumeration();
|
|
|
|
while (enumerator.hasMoreElements()) {
|
|
|
|
var string = enumerator.getNext().QueryInterface(Ci.nsIPropertyElement);
|
|
|
|
|
|
|
|
// 'textContent' is the default attribute to set if none are specified.
|
|
|
|
var key = string.key, property = 'textContent';
|
|
|
|
var i = key.lastIndexOf('.');
|
|
|
|
if (i >= 0) {
|
|
|
|
property = key.substring(i + 1);
|
|
|
|
key = key.substring(0, i);
|
|
|
|
}
|
|
|
|
if (!(key in map))
|
|
|
|
map[key] = {};
|
|
|
|
map[key][property] = string.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._localizedStrings = map;
|
|
|
|
},
|
|
|
|
|
2014-06-09 15:14:14 -07:00
|
|
|
/**
|
|
|
|
* Uploads telemetry logs to telemetryServer
|
|
|
|
*
|
|
|
|
* @param {Object} pc The peerConnection in question.
|
|
|
|
*/
|
|
|
|
uploadTelemetry: function(window, pc) {
|
|
|
|
if (!this.telemetryUri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
window.WebrtcGlobalInformation.getAllStats(allStats => {
|
|
|
|
let internalFormat = allStats.reports[0]; // filtered on pc.id
|
|
|
|
window.WebrtcGlobalInformation.getLogging('', logs => {
|
|
|
|
|
|
|
|
// We have stats and logs.
|
|
|
|
// Prepare payload from https://wiki.mozilla.org/Loop/Telemetry
|
|
|
|
|
|
|
|
let ai = Services.appinfo;
|
|
|
|
let report = convertToRTCStatsReport(internalFormat);
|
|
|
|
|
|
|
|
let payload = {
|
|
|
|
ver: 1,
|
|
|
|
info: {
|
|
|
|
appUpdateChannel: ai.defaultUpdateChannel,
|
|
|
|
appBuildID: ai.appBuildID,
|
|
|
|
appName: ai.name,
|
|
|
|
appVersion: ai.version,
|
|
|
|
reason: "loop",
|
|
|
|
OS: ai.OS,
|
|
|
|
version: Services.sysinfo.getProperty("version")
|
|
|
|
},
|
|
|
|
report: "ice failure",
|
|
|
|
connectionstate: pc.iceConnectionState,
|
|
|
|
stats: report,
|
|
|
|
localSdp: internalFormat.localSdp,
|
|
|
|
remoteSdp: internalFormat.remoteSdp,
|
|
|
|
log: ""
|
|
|
|
};
|
|
|
|
logs.forEach(s => { payload.log += s + "\n"; });
|
|
|
|
|
|
|
|
let uuid = uuidgen.generateUUID().toString();
|
|
|
|
uuid = uuid.substr(1,uuid.length-2); // remove uuid curly braces
|
|
|
|
|
|
|
|
let url = this.telemetryUri;
|
|
|
|
url += ((url.substr(-1) == "/")? "":"/") + uuid + "/loop/" +
|
|
|
|
ai.OS + "/" + ai.version + "/" + ai.defaultUpdateChannel + "/" +
|
|
|
|
ai.appBuildID;
|
|
|
|
|
|
|
|
// Send payload.
|
|
|
|
//TODO: gzip!
|
|
|
|
|
|
|
|
let xhr = new window.XMLHttpRequest();
|
|
|
|
xhr.open("POST", url, true);
|
|
|
|
xhr.setRequestHeader("Content-Type", 'application/json');
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState == 4 && xhr.status == 200) {
|
|
|
|
console.log("Failed to upload telemetry logs: " + xhr.responseText);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send(JSON.stringify(payload));
|
|
|
|
console.log("Uploading telemetry logs to " + url);
|
|
|
|
});
|
|
|
|
}, pc.id);
|
|
|
|
},
|
|
|
|
|
2014-05-29 13:13:46 -07:00
|
|
|
/**
|
|
|
|
* Opens the chat window
|
|
|
|
*
|
|
|
|
* @param {Object} contentWindow The window to open the chat window in, may
|
|
|
|
* be null.
|
|
|
|
* @param {String} title The title of the chat window.
|
|
|
|
* @param {String} url The page to load in the chat window.
|
|
|
|
*/
|
2014-06-09 15:14:14 -07:00
|
|
|
openChatWindow: function(contentWindow, title, url) {
|
2014-05-29 13:13:46 -07:00
|
|
|
// So I guess the origin is the loop server!?
|
|
|
|
let origin = this.loopServerUri;
|
|
|
|
url = url.spec || url;
|
|
|
|
|
|
|
|
let callback = chatbox => {
|
|
|
|
// We need to use DOMContentLoaded as otherwise the injection will happen
|
|
|
|
// in about:blank and then get lost.
|
|
|
|
// Sadly we can't use chatbox.promiseChatLoaded() as promise chaining
|
|
|
|
// involves event loop spins, which means it might be too late.
|
|
|
|
// Have we already done it?
|
|
|
|
if (chatbox.contentWindow.navigator.mozLoop) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chatbox.addEventListener("DOMContentLoaded", function loaded(event) {
|
|
|
|
if (event.target != chatbox.contentDocument) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chatbox.removeEventListener("DOMContentLoaded", loaded, true);
|
2014-06-09 15:14:14 -07:00
|
|
|
|
|
|
|
let window = chatbox.contentWindow;
|
|
|
|
injectLoopAPI(window);
|
|
|
|
|
|
|
|
let ourID = window.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
|
|
|
|
|
|
|
|
let onPCLifecycleChange = (pc, winID, type) => {
|
|
|
|
if (winID != ourID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type == "iceconnectionstatechange") {
|
|
|
|
switch(pc.iceConnectionState) {
|
|
|
|
case "failed":
|
|
|
|
case "disconnected":
|
|
|
|
this.uploadTelemetry(window, pc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let pc_static = new window.mozRTCPeerConnectionStatic();
|
|
|
|
pc_static.registerPeerConnectionLifecycleCallback(onPCLifecycleChange);
|
|
|
|
}.bind(this), true);
|
2014-05-29 13:13:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Chat.open(contentWindow, origin, title, url, undefined, undefined, callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public API
|
|
|
|
*/
|
|
|
|
this.MozLoopService = {
|
|
|
|
/**
|
|
|
|
* Initialized the loop service, and starts registration with the
|
|
|
|
* push and loop servers.
|
|
|
|
*/
|
2014-06-04 06:42:41 -07:00
|
|
|
initialize: function() {
|
|
|
|
// If expiresTime is in the future then kick-off registration.
|
2014-06-05 02:29:54 -07:00
|
|
|
if (MozLoopServiceInternal.urlExpiryTimeIsInFuture()) {
|
2014-06-04 06:42:41 -07:00
|
|
|
this._startInitializeTimer();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function, exposed for testing purposes only. Used to start the
|
|
|
|
* initialize timer.
|
|
|
|
*/
|
|
|
|
_startInitializeTimer: function() {
|
|
|
|
// Kick off the push notification service into registering after a timeout
|
|
|
|
// this ensures we're not doing too much straight after the browser's finished
|
|
|
|
// starting up.
|
|
|
|
this._initializeTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
this._initializeTimer.initWithCallback(function() {
|
|
|
|
this.register();
|
|
|
|
this._initializeTimer = null;
|
|
|
|
}.bind(this),
|
|
|
|
MozLoopServiceInternal.initialRegistrationDelayMilliseconds, Ci.nsITimer.TYPE_ONE_SHOT);
|
2014-05-29 13:13:46 -07:00
|
|
|
},
|
|
|
|
|
2014-05-29 13:13:47 -07:00
|
|
|
/**
|
|
|
|
* Starts registration of Loop with the push server, and then will register
|
|
|
|
* with the Loop server. It will return early if already registered.
|
|
|
|
*
|
2014-06-16 00:41:17 -07:00
|
|
|
* @param {Object} mockPushHandler Optional, test-only mock push handler. Used
|
|
|
|
* to allow mocking of the MozLoopPushHandler.
|
2014-06-04 06:42:41 -07:00
|
|
|
* @returns {Promise} a promise that is resolved with no params on completion, or
|
|
|
|
* rejected with an error code or string.
|
2014-05-29 13:13:47 -07:00
|
|
|
*/
|
2014-06-16 00:41:17 -07:00
|
|
|
register: function(mockPushHandler) {
|
|
|
|
return MozLoopServiceInternal.promiseRegisteredWithServers(mockPushHandler);
|
2014-05-29 13:13:47 -07:00
|
|
|
},
|
|
|
|
|
2014-05-29 13:13:48 -07: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.
|
|
|
|
*
|
|
|
|
* This is used to deterimine whether or not we should be registering with the
|
|
|
|
* push server on start.
|
|
|
|
*
|
|
|
|
* @param {Integer} expiryTimeSeconds The seconds since epoch of the expiry time
|
|
|
|
* of the url.
|
|
|
|
*/
|
|
|
|
noteCallUrlExpiry: function(expiryTimeSeconds) {
|
|
|
|
MozLoopServiceInternal.expiryTimeSeconds = expiryTimeSeconds;
|
|
|
|
},
|
|
|
|
|
2014-05-29 13:13:46 -07:00
|
|
|
/**
|
|
|
|
* Returns the strings for the specified element. Designed for use
|
|
|
|
* with l10n.js.
|
|
|
|
*
|
|
|
|
* @param {key} The element id to get strings for.
|
|
|
|
* @return {String} A JSON string containing the localized
|
|
|
|
* attribute/value pairs for the element.
|
|
|
|
*/
|
|
|
|
getStrings: function(key) {
|
|
|
|
var stringData = MozLoopServiceInternal.localizedStrings;
|
|
|
|
if (!(key in stringData)) {
|
|
|
|
Cu.reportError('No string for key: ' + key + 'found');
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(stringData[key]);
|
|
|
|
},
|
|
|
|
|
2014-06-02 12:30:02 -07:00
|
|
|
/**
|
|
|
|
* Retrieves MozLoopService "do not disturb" value.
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
get doNotDisturb() {
|
|
|
|
return MozLoopServiceInternal.doNotDisturb;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets MozLoopService "do not disturb" value.
|
|
|
|
*
|
|
|
|
* @param {Boolean} aFlag
|
|
|
|
*/
|
|
|
|
set doNotDisturb(aFlag) {
|
|
|
|
MozLoopServiceInternal.doNotDisturb = aFlag;
|
|
|
|
},
|
|
|
|
|
2014-05-29 13:13:46 -07:00
|
|
|
/**
|
|
|
|
* Returns the current locale
|
|
|
|
*
|
|
|
|
* @return {String} The code of the current locale.
|
|
|
|
*/
|
|
|
|
get locale() {
|
|
|
|
try {
|
|
|
|
return Services.prefs.getComplexValue("general.useragent.locale",
|
|
|
|
Ci.nsISupportsString).data;
|
|
|
|
} catch (ex) {
|
|
|
|
return "en-US";
|
|
|
|
}
|
2014-05-29 13:13:48 -07:00
|
|
|
},
|
|
|
|
|
2014-07-01 09:12:54 -07:00
|
|
|
/**
|
|
|
|
* Set any character preference under "loop.".
|
|
|
|
*
|
|
|
|
* @param {String} prefName The name of the pref without the preceding "loop."
|
|
|
|
* @param {String} value The value to set.
|
|
|
|
*
|
|
|
|
* Any errors thrown by the Mozilla pref API are logged to the console.
|
|
|
|
*/
|
|
|
|
setLoopCharPref: function(prefName, value) {
|
|
|
|
try {
|
|
|
|
Services.prefs.setCharPref("loop." + prefName, value);
|
|
|
|
} catch (ex) {
|
|
|
|
console.log("setLoopCharPref had trouble setting " + prefName +
|
|
|
|
"; exception: " + ex);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-05-29 13:13:48 -07: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: function(prefName) {
|
|
|
|
try {
|
|
|
|
return Services.prefs.getCharPref("loop." + prefName);
|
|
|
|
} catch (ex) {
|
|
|
|
console.log("getLoopCharPref had trouble getting " + prefName +
|
|
|
|
"; exception: " + ex);
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-03 01:23:38 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a hawk based request to the loop server.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* @returns {Promise}
|
|
|
|
* Returns a promise that resolves to the response of the API call,
|
|
|
|
* or is rejected with an error. If the server response can be parsed
|
|
|
|
* as JSON and contains an 'error' property, the promise will be
|
|
|
|
* rejected with this JSON-parsed response.
|
|
|
|
*/
|
|
|
|
hawkRequest: function(path, method, payloadObj) {
|
|
|
|
return MozLoopServiceInternal.hawkRequest(path, method, payloadObj);
|
|
|
|
},
|
2014-05-29 13:13:46 -07:00
|
|
|
};
|