Mojo JS Bindings: Add support for closing Proxy and Stub connections

BUG=
R=jamesr@chromium.org

Review URL: https://codereview.chromium.org/883953003
diff --git a/mojo/public/js/bindings.js b/mojo/public/js/bindings.js
index c6c98d5..44aa9f4 100644
--- a/mojo/public/js/bindings.js
+++ b/mojo/public/js/bindings.js
@@ -4,7 +4,8 @@
 
 define("mojo/public/js/bindings", [
   "mojo/public/js/router",
-], function(router) {
+  "mojo/public/js/core",
+], function(router, core) {
 
   var Router = router.Router;
 
@@ -30,12 +31,31 @@
       throw new Error("no stub object");
   }
 
+  function connectionHandle(connection) {
+    return connection &&
+        connection.router &&
+        connection.router.connector_ &&
+        connection.router.connector_.handle_;
+  }
+
+  ProxyProperties.prototype.close = function() {
+    var handle = connectionHandle(this.connection);
+    if (handle)
+      core.close(handle);
+  }
+
   // Public stub class properties that are managed at runtime by the JS
   // bindings. See StubBindings below.
   function StubProperties(delegate) {
     this.delegate = delegate;
   }
 
+  StubProperties.prototype.close = function() {
+    var handle = connectionHandle(this.connection);
+    if (handle)
+      core.close(handle);
+  }
+
   // The base class for generated proxy classes.
   function ProxyBase(receiver) {
     this[kProxyProperties] = new ProxyProperties(receiver);
diff --git a/mojo/public/js/connection.js b/mojo/public/js/connection.js
index 90f1f1e..223e711 100644
--- a/mojo/public/js/connection.js
+++ b/mojo/public/js/connection.js
@@ -168,8 +168,6 @@
     var router = new Router(handle);
     var connection = new BaseConnection(undefined, proxy, router);
     ProxyBindings(proxy).connection = connection;
-    // TODO(hansmuller): Provide a way to explicitly close a proxy.
-    proxy.handleStash = handle;
     return proxy;
   }
 
diff --git a/mojo/services/public/js/shell.js b/mojo/services/public/js/shell.js
index d80a2e9..e6c2dee 100644
--- a/mojo/services/public/js/shell.js
+++ b/mojo/services/public/js/shell.js
@@ -46,8 +46,7 @@
       this.applications_.forEach(function(application, url) {
         application.close();
       });
-      // TODO(hansmuller): Use a proper API on Proxy to close.
-      core.close(this.shellProxy.handleStash);
+      ProxyBindings(this.shellProxy).close();
       this.applications_.clear();
     }
   }