gecko/browser/components/loop/MozLoopAPI.jsm

91 lines
2.7 KiB
JavaScript
Raw Normal View History

/* 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");
Cu.import("resource:///modules/loop/MozLoopService.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "hookWindowCloseForPanelClose",
"resource://gre/modules/MozSocialAPI.jsm");
this.EXPORTED_SYMBOLS = ["injectLoopAPI"];
/**
* 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) {
let api = {
/**
* Returns the uri for the Loop server from preferences.
*
* @return {String} The Loop server uri
*/
loopServer: {
enumerable: true,
configurable: true,
writable: false,
value: function() {
return Services.prefs.getCharPref("loop.server");
}
},
/**
* Returns the current locale of the browser.
*
* @returns {String} The locale string
*/
getLocale: {
enumerable: true,
configurable: true,
writable: false,
value: function() {
return MozLoopService.locale;
}
},
/**
* 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,
configurable: true,
writable: false,
value: function(key) {
return MozLoopService.getStrings(key);
}
}
};
let contentObj = Cu.createObjectIn(targetWindow);
Object.defineProperties(contentObj, api);
Cu.makeObjectPropsNormal(contentObj);
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);
}