Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | var Ci = SpecialPowers.Ci; |
michael@0 | 2 | ok(Ci != null, "Access Ci"); |
michael@0 | 3 | var Cc = SpecialPowers.Cc; |
michael@0 | 4 | ok(Cc != null, "Access Cc"); |
michael@0 | 5 | |
michael@0 | 6 | var didDialog; |
michael@0 | 7 | |
michael@0 | 8 | var timer; // keep in outer scope so it's not GC'd before firing |
michael@0 | 9 | function startCallbackTimer() { |
michael@0 | 10 | didDialog = false; |
michael@0 | 11 | |
michael@0 | 12 | // Delay before the callback twiddles the prompt. |
michael@0 | 13 | const dialogDelay = 10; |
michael@0 | 14 | |
michael@0 | 15 | // Use a timer to invoke a callback to twiddle the authentication dialog |
michael@0 | 16 | timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
michael@0 | 17 | timer.init(observer, dialogDelay, Ci.nsITimer.TYPE_ONE_SHOT); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | |
michael@0 | 21 | var observer = SpecialPowers.wrapCallbackObject({ |
michael@0 | 22 | QueryInterface : function (iid) { |
michael@0 | 23 | const interfaces = [Ci.nsIObserver, |
michael@0 | 24 | Ci.nsISupports, Ci.nsISupportsWeakReference]; |
michael@0 | 25 | |
michael@0 | 26 | if (!interfaces.some( function(v) { return iid.equals(v) } )) |
michael@0 | 27 | throw SpecialPowers.Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 28 | return this; |
michael@0 | 29 | }, |
michael@0 | 30 | |
michael@0 | 31 | observe : function (subject, topic, data) { |
michael@0 | 32 | var doc = getDialogDoc(); |
michael@0 | 33 | if (doc) |
michael@0 | 34 | handleDialog(doc, testNum); |
michael@0 | 35 | else |
michael@0 | 36 | startCallbackTimer(); // try again in a bit |
michael@0 | 37 | } |
michael@0 | 38 | }); |
michael@0 | 39 | |
michael@0 | 40 | function getDialogDoc() { |
michael@0 | 41 | // Find the <browser> which contains notifyWindow, by looking |
michael@0 | 42 | // through all the open windows and all the <browsers> in each. |
michael@0 | 43 | var wm = Cc["@mozilla.org/appshell/window-mediator;1"]. |
michael@0 | 44 | getService(Ci.nsIWindowMediator); |
michael@0 | 45 | //var enumerator = wm.getEnumerator("navigator:browser"); |
michael@0 | 46 | var enumerator = wm.getXULWindowEnumerator(null); |
michael@0 | 47 | |
michael@0 | 48 | while (enumerator.hasMoreElements()) { |
michael@0 | 49 | var win = enumerator.getNext(); |
michael@0 | 50 | var windowDocShell = win.QueryInterface(Ci.nsIXULWindow).docShell; |
michael@0 | 51 | |
michael@0 | 52 | var containedDocShells = windowDocShell.getDocShellEnumerator( |
michael@0 | 53 | Ci.nsIDocShellTreeItem.typeChrome, |
michael@0 | 54 | Ci.nsIDocShell.ENUMERATE_FORWARDS); |
michael@0 | 55 | while (containedDocShells.hasMoreElements()) { |
michael@0 | 56 | // Get the corresponding document for this docshell |
michael@0 | 57 | var childDocShell = containedDocShells.getNext(); |
michael@0 | 58 | // We don't want it if it's not done loading. |
michael@0 | 59 | if (childDocShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE) |
michael@0 | 60 | continue; |
michael@0 | 61 | var childDoc = childDocShell.QueryInterface(Ci.nsIDocShell) |
michael@0 | 62 | .contentViewer |
michael@0 | 63 | .DOMDocument; |
michael@0 | 64 | |
michael@0 | 65 | //ok(true, "Got window: " + childDoc.location.href); |
michael@0 | 66 | if (childDoc.location.href == "chrome://global/content/commonDialog.xul") |
michael@0 | 67 | return childDoc; |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | return null; |
michael@0 | 72 | } |