toolkit/components/passwordmgr/test/prompt_common.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial