michael@0: const Ci = SpecialPowers.Ci; michael@0: const Cc = SpecialPowers.Cc; michael@0: ok(Ci != null, "Access Ci"); michael@0: ok(Cc != null, "Access Cc"); michael@0: michael@0: var didDialog; michael@0: michael@0: var isSelectDialog = false; michael@0: var isTabModal = false; michael@0: var usePromptService = true; michael@0: michael@0: var timer; // keep in outer scope so it's not GC'd before firing michael@0: function startCallbackTimer() { michael@0: didDialog = false; michael@0: michael@0: // Delay before the callback twiddles the prompt. michael@0: const dialogDelay = 10; michael@0: michael@0: // Use a timer to invoke a callback to twiddle the authentication dialog michael@0: timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: timer.init(observer, dialogDelay, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: } michael@0: michael@0: michael@0: var observer = { michael@0: QueryInterface : function (iid) { michael@0: const interfaces = [Ci.nsIObserver, michael@0: Ci.nsISupports, Ci.nsISupportsWeakReference]; michael@0: michael@0: if (!interfaces.some( function(v) { return iid.equals(v) } )) michael@0: throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE; michael@0: return this; michael@0: }, michael@0: michael@0: observe : SpecialPowers.wrapCallback(function (subject, topic, data) { michael@0: try { michael@0: if (isTabModal) { michael@0: var promptBox = getTabModalPromptBox(window); michael@0: ok(promptBox, "got tabmodal promptbox"); michael@0: var prompts = SpecialPowers.wrap(promptBox).listPrompts(); michael@0: if (prompts.length) michael@0: handleDialog(prompts[0].Dialog.ui, testNum); michael@0: else michael@0: startCallbackTimer(); // try again in a bit michael@0: } else { michael@0: var doc = getDialogDoc(); michael@0: if (isSelectDialog && doc) michael@0: handleDialog(doc, testNum); michael@0: else if (doc) michael@0: handleDialog(doc.defaultView.Dialog.ui, testNum); michael@0: else michael@0: startCallbackTimer(); // try again in a bit michael@0: } michael@0: } catch (e) { michael@0: ok(false, "Exception thrown in the timer callback: " + e + " at " + (e.fileName || e.filename) + ":" + (e.lineNumber || e.linenumber)); michael@0: } michael@0: }) michael@0: }; michael@0: michael@0: function getTabModalPromptBox(domWin) { michael@0: var promptBox = null; michael@0: michael@0: // Given a content DOM window, returns the chrome window it's in. michael@0: function getChromeWindow(aWindow) { michael@0: var chromeWin = SpecialPowers.wrap(aWindow).QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIWebNavigation) michael@0: .QueryInterface(Ci.nsIDocShell) michael@0: .chromeEventHandler.ownerDocument.defaultView; michael@0: return chromeWin; michael@0: } michael@0: michael@0: try { michael@0: // Get the topmost window, in case we're in a frame. michael@0: var promptWin = domWin.top; michael@0: michael@0: // Get the chrome window for the content window we're using. michael@0: var chromeWin = getChromeWindow(promptWin); michael@0: michael@0: if (chromeWin.getTabModalPromptBox) michael@0: promptBox = chromeWin.getTabModalPromptBox(promptWin); michael@0: } catch (e) { michael@0: // If any errors happen, just assume no tabmodal prompter. michael@0: } michael@0: michael@0: // Callers get confused by a wrapped promptBox here. michael@0: return SpecialPowers.unwrap(promptBox); michael@0: } michael@0: michael@0: function getDialogDoc() { michael@0: // Trudge through all the open windows, until we find the one michael@0: // that has either commonDialog.xul or selectDialog.xul loaded. michael@0: var wm = Cc["@mozilla.org/appshell/window-mediator;1"]. michael@0: getService(Ci.nsIWindowMediator); michael@0: //var enumerator = wm.getEnumerator("navigator:browser"); michael@0: var enumerator = wm.getXULWindowEnumerator(null); michael@0: michael@0: while (enumerator.hasMoreElements()) { michael@0: var win = enumerator.getNext(); michael@0: var windowDocShell = win.QueryInterface(Ci.nsIXULWindow).docShell; michael@0: michael@0: var containedDocShells = windowDocShell.getDocShellEnumerator( michael@0: Ci.nsIDocShellTreeItem.typeChrome, michael@0: Ci.nsIDocShell.ENUMERATE_FORWARDS); michael@0: while (containedDocShells.hasMoreElements()) { michael@0: // Get the corresponding document for this docshell michael@0: var childDocShell = containedDocShells.getNext(); michael@0: // We don't want it if it's not done loading. michael@0: if (childDocShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE) michael@0: continue; michael@0: var childDoc = childDocShell.QueryInterface(Ci.nsIDocShell) michael@0: .contentViewer michael@0: .DOMDocument; michael@0: michael@0: //ok(true, "Got window: " + childDoc.location.href); michael@0: if (childDoc.location.href == "chrome://global/content/commonDialog.xul") michael@0: return childDoc; michael@0: if (childDoc.location.href == "chrome://global/content/selectDialog.xul") michael@0: return childDoc; michael@0: } michael@0: } michael@0: michael@0: return null; michael@0: }