Bug 1124604 - Move code for getting the outer window id into its own method. draft
authorHenrik Skupin <mail@hskupin.info>
Fri, 27 Jan 2017 09:09:32 +0100
changeset 468437 6a687c7fc9c67853bcf6f9512eea75fd89490f48
parent 468005 f7e1982a2582b14c5885d787b530f879da3a040e
child 468438 88970b42cca53703ce9b2910efab15ae41ee155c
child 468508 d4c3258a092f7f73a3f421d6481ad378b99fdf1e
push id43476
push userbmo:hskupin@gmail.com
push dateTue, 31 Jan 2017 15:35:41 +0000
bugs1124604
milestone54.0a1
Bug 1124604 - Move code for getting the outer window id into its own method. There are several places which make use of DOMWindowUtils to determine the outer window id. Lets centralize it to a single method to avoid duplication. MozReview-Commit-ID: 1IRKMpBPFH
testing/marionette/driver.js
--- a/testing/marionette/driver.js
+++ b/testing/marionette/driver.js
@@ -196,39 +196,32 @@ Object.defineProperty(GeckoDriver.protot
       if (tabBrowser) {
         tabBrowser.tabs.forEach(tab => {
           let winId = this.getIdForBrowser(browser.getBrowserForTab(tab));
           if (winId !== null) {
             hs.push(winId);
           }
         });
       } else {
-        // For other chrome windows beside the browser window, only count the window itself.
-        let winId = win.QueryInterface(Ci.nsIInterfaceRequestor)
-            .getInterface(Ci.nsIDOMWindowUtils)
-            .outerWindowID;
-        hs.push(winId.toString());
+        // For other chrome windows beside the browser window, only add the window itself.
+        hs.push(getOuterWindowId(win));
       }
     }
 
     return hs;
   },
 });
 
 Object.defineProperty(GeckoDriver.prototype, "chromeWindowHandles", {
   get : function () {
     let hs = [];
     let winEn = Services.wm.getEnumerator(null);
 
     while (winEn.hasMoreElements()) {
-      let foundWin = winEn.getNext();
-      let winId = foundWin.QueryInterface(Ci.nsIInterfaceRequestor)
-          .getInterface(Ci.nsIDOMWindowUtils)
-          .outerWindowID;
-      hs.push(winId.toString());
+      hs.push(getOuterWindowId(winEn.getNext()));
     }
 
     return hs;
   },
 });
 
 GeckoDriver.prototype.QueryInterface = XPCOMUtils.generateQI([
   Ci.nsIMessageListener,
@@ -354,19 +347,18 @@ GeckoDriver.prototype.addFrameCloseListe
  * @param {nsIDOMWindow} win
  *     Window for which we will create a browsing context.
  *
  * @return {string}
  *     Returns the unique server-assigned ID of the window.
  */
 GeckoDriver.prototype.addBrowser = function (win) {
   let bc = new browser.Context(win, this);
-  let winId = win.QueryInterface(Ci.nsIInterfaceRequestor)
-      .getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
-  winId = winId + ((this.appName == "B2G") ? "-b2g" : "");
+  let winId = getOuterWindowId(win);
+
   this.browsers[winId] = bc;
   this.curBrowser = this.browsers[winId];
   if (!this.wins.has(winId)) {
     // add this to seenItems so we can guarantee
     // the user will get winId as this window's id
     this.wins.set(winId, win);
   }
 };
@@ -1199,23 +1191,16 @@ GeckoDriver.prototype.setWindowPosition 
  *
  * @param {string} name
  *     Target name or ID of the window to switch to.
  */
 GeckoDriver.prototype.switchToWindow = function* (cmd, resp) {
   let switchTo = cmd.parameters.name;
   let found;
 
-  let getOuterWindowId = function (win) {
-    let rv = win.QueryInterface(Ci.nsIInterfaceRequestor)
-        .getInterface(Ci.nsIDOMWindowUtils)
-        .outerWindowID;
-    return rv;
-  };
-
   let byNameOrId = function (name, outerId, contentWindowId) {
     return switchTo == name ||
         switchTo == contentWindowId ||
         switchTo == outerId;
   };
 
   let winEn = Services.wm.getEnumerator(null);
   while (winEn.hasMoreElements()) {
@@ -2851,8 +2836,25 @@ GeckoDriver.prototype.commands = {
 function copy (obj) {
   if (Array.isArray(obj)) {
     return obj.slice();
   } else if (typeof obj == "object") {
     return Object.assign({}, obj);
   }
   return obj;
 }
+
+/**
+ * Get the outer window ID for the specified window.
+ *
+ * @param {nsIDOMWindow} win
+ *     Window whose browser we need to access.
+ *
+ * @return {string}
+ *     Returns the unique window ID.
+ */
+function getOuterWindowId(win) {
+  let id = win.QueryInterface(Ci.nsIInterfaceRequestor)
+      .getInterface(Ci.nsIDOMWindowUtils)
+      .outerWindowID;
+
+  return id.toString();
+}