Bug 849009 - Uplift Add-on SDK changeset 645b0ca71ccd41bb1fc69d97d22c456b03452e89

This commit is contained in:
Wes Kocher
2013-03-07 15:09:34 -08:00
parent 0fe59ae286
commit cba4c2a7c8
75 changed files with 1154 additions and 687 deletions

View File

@@ -9,12 +9,13 @@ module.metadata = {
const { Cc, Ci } = require('chrome');
const array = require('../util/array');
const observers = require('../deprecated/observer-service');
const { defer } = require('sdk/core/promise');
const windowWatcher = Cc['@mozilla.org/embedcomp/window-watcher;1'].
getService(Ci.nsIWindowWatcher);
const appShellService = Cc['@mozilla.org/appshell/appShellService;1'].
getService(Ci.nsIAppShellService);
const observers = require('../deprecated/observer-service');
const WM = Cc['@mozilla.org/appshell/window-mediator;1'].
getService(Ci.nsIWindowMediator);
@@ -23,11 +24,41 @@ const BROWSER = 'navigator:browser',
NAME = '_blank',
FEATURES = 'chrome,all,dialog=no';
function isWindowPrivate(win) {
if (!win)
return false;
// if the pbService is undefined, the PrivateBrowsingUtils.jsm is available,
// and the app is Firefox, then assume per-window private browsing is
// enabled.
try {
return win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsILoadContext)
.usePrivateBrowsing;
}
catch(e) {}
// Sometimes the input is not a nsIDOMWindow.. but it is still a winodw.
try {
return !!win.docShell.QueryInterface(Ci.nsILoadContext).usePrivateBrowsing;
}
catch (e) {}
return false;
}
exports.isWindowPrivate = isWindowPrivate;
function getMostRecentBrowserWindow() {
return WM.getMostRecentWindow(BROWSER);
return getMostRecentWindow(BROWSER);
}
exports.getMostRecentBrowserWindow = getMostRecentBrowserWindow;
function getMostRecentWindow(type) {
return WM.getMostRecentWindow(type);
}
exports.getMostRecentWindow = getMostRecentWindow;
/**
* Returns the ID of the window's current inner window.
*/
@@ -107,6 +138,11 @@ exports.backgroundify = backgroundify;
function serializeFeatures(options) {
return Object.keys(options).reduce(function(result, name) {
let value = options[name];
// the chrome and private features are special
if ((name == 'private' || name == 'chrome'))
return result + ((value === true) ? ',' + name : '');
return result + ',' + name + '=' +
(value === true ? 'yes' : value === false ? 'no' : value);
}, '').substr(1);
@@ -121,19 +157,55 @@ function serializeFeatures(options) {
* @params {String} options.name
* Optional name that is assigned to the window.
* @params {Object} options.features
* Map of key, values like: `{ width: 10, height: 15, chrome: true }`.
* Map of key, values like: `{ width: 10, height: 15, chrome: true, private: true }`.
*/
function open(uri, options) {
options = options || {};
return windowWatcher.
let newWindow = windowWatcher.
openWindow(options.parent || null,
uri,
options.name || null,
serializeFeatures(options.features || {}),
options.args || null);
return newWindow;
}
exports.open = open;
function onFocus(window) {
let deferred = defer();
if (isFocused(window)) {
deferred.resolve(window);
}
else {
window.addEventListener("focus", function focusListener() {
window.removeEventListener("focus", focusListener, true);
deferred.resolve(window);
}, true);
}
return deferred.promise;
}
exports.onFocus = onFocus;
function isFocused(window) {
const FM = Cc["@mozilla.org/focus-manager;1"].
getService(Ci.nsIFocusManager);
let childTargetWindow = {};
FM.getFocusedElementForWindow(window, true, childTargetWindow);
childTargetWindow = childTargetWindow.value;
let focusedChildWindow = {};
if (FM.activeWindow) {
FM.getFocusedElementForWindow(FM.activeWindow, true, focusedChildWindow);
focusedChildWindow = focusedChildWindow.value;
}
return (focusedChildWindow === childTargetWindow);
}
/**
* Opens a top level window and returns it's `nsIDOMWindow` representation.
* Same as `open` but with more features
@@ -149,8 +221,13 @@ function openDialog(options) {
features = features.split(',').concat('private').join(',');
}
let browser = WM.getMostRecentWindow(BROWSER);
return browser.openDialog.apply(
let browser = getMostRecentBrowserWindow();
// if there is no browser then do nothing
if (!browser)
return undefined;
let newWindow = browser.openDialog.apply(
browser,
array.flatten([
options.url || URI_BROWSER,
@@ -159,6 +236,8 @@ function openDialog(options) {
options.args || null
])
);
return newWindow;
}
exports.openDialog = openDialog;
@@ -166,12 +245,17 @@ exports.openDialog = openDialog;
* Returns an array of all currently opened windows.
* Note that these windows may still be loading.
*/
function windows() {
function windows(type, options) {
options = options || {};
let list = [];
let winEnum = windowWatcher.getWindowEnumerator();
let winEnum = WM.getEnumerator(type);
while (winEnum.hasMoreElements()) {
let window = winEnum.getNext().QueryInterface(Ci.nsIDOMWindow);
list.push(window);
// Only add non-private windows when pb permission isn't set,
// unless an option forces the addition of them.
if (options.includePrivate || !isWindowPrivate(window)) {
list.push(window);
}
}
return list;
}
@@ -189,7 +273,11 @@ function isDocumentLoaded(window) {
exports.isDocumentLoaded = isDocumentLoaded;
function isBrowser(window) {
return window.document.documentElement.getAttribute("windowtype") === BROWSER;
try {
return window.document.documentElement.getAttribute("windowtype") === BROWSER;
}
catch (e) {}
return false;
};
exports.isBrowser = isBrowser;