nassh: fix opening a new window from the omnibox

Since we open a new window before querying the active tab/window, Chrome
might have marked the new window as the active one which we then end up
closing right away.  Query the active tab before we open the window to
make sure that doesn't happen.

Change-Id: I37e68f1dd347b90e5e650cb8a77447727c056fbc
Reviewed-on: https://chromium-review.googlesource.com/657568
Reviewed-by: Brandon Gilmore <varz@google.com>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/nassh/js/nassh_app.js b/nassh/js/nassh_app.js
index 53bcc95..59f993c 100644
--- a/nassh/js/nassh_app.js
+++ b/nassh/js/nassh_app.js
@@ -145,14 +145,17 @@
       chrome.tabs.create({url: url, active: false});
       break;
     case 'newForegroundTab':
+      // Close the active tab.  We need to do this before opening a new window
+      // in case Chrome selects that as the new active tab.  It won't kill us
+      // right away though as the JS execution model guarantees we'll finish
+      // running this func before the callback runs.
+      chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
+        chrome.tabs.remove(tabs[0].id);
+      });
       // We'll abuse this to open a window instead of a tab.
       window.open(url, '',
                   'chrome=no,close=yes,resize=yes,minimizable=yes,' +
                   'scrollbars=yes,width=900,height=600,noopener');
-      // Close the active tab.
-      chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
-        chrome.tabs.remove(tabs[0].id);
-      });
       break;
   }
 };