Home | History | Annotate | Download | only in js
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 define("mojo/public/js/bindings", [
      6   "mojo/public/js/router",
      7   "mojo/public/js/core",
      8 ], function(router, core) {
      9 
     10   var Router = router.Router;
     11 
     12   var kProxyProperties = Symbol("proxyProperties");
     13   var kStubProperties = Symbol("stubProperties");
     14 
     15   // Public proxy class properties that are managed at runtime by the JS
     16   // bindings. See ProxyBindings below.
     17   function ProxyProperties(receiver) {
     18     this.receiver = receiver;
     19   }
     20 
     21   // TODO(hansmuller): remove then after 'Client=' has been removed from Mojom.
     22   ProxyProperties.prototype.getLocalDelegate = function() {
     23     return this.local && StubBindings(this.local).delegate;
     24   }
     25 
     26   // TODO(hansmuller): remove then after 'Client=' has been removed from Mojom.
     27   ProxyProperties.prototype.setLocalDelegate = function(impl) {
     28     if (this.local)
     29       StubBindings(this.local).delegate = impl;
     30     else
     31       throw new Error("no stub object");
     32   }
     33 
     34   ProxyProperties.prototype.close = function() {
     35     this.connection.close();
     36   }
     37 
     38   // Public stub class properties that are managed at runtime by the JS
     39   // bindings. See StubBindings below.
     40   function StubProperties(delegate) {
     41     this.delegate = delegate;
     42   }
     43 
     44   StubProperties.prototype.close = function() {
     45     this.connection.close();
     46   }
     47 
     48   // The base class for generated proxy classes.
     49   function ProxyBase(receiver) {
     50     this[kProxyProperties] = new ProxyProperties(receiver);
     51 
     52     // TODO(hansmuller): Temporary, for Chrome backwards compatibility.
     53     if (receiver instanceof Router)
     54       this.receiver_ = receiver;
     55   }
     56 
     57   // The base class for generated stub classes.
     58   function StubBase(delegate) {
     59     this[kStubProperties] = new StubProperties(delegate);
     60   }
     61 
     62   // TODO(hansmuller): remove everything except the connection property doc
     63   // after 'Client=' has been removed from Mojom.
     64 
     65   // Provides access to properties added to a proxy object without risking
     66   // Mojo interface name collisions. Unless otherwise specified, the initial
     67   // value of all properties is undefined.
     68   //
     69   // ProxyBindings(proxy).connection - The Connection object that links the
     70   //   proxy for a remote Mojo service to an optional local stub for a local
     71   //   service. The value of ProxyBindings(proxy).connection.remote == proxy.
     72   //
     73   // ProxyBindings(proxy).local  - The "local" stub object whose delegate
     74   //   implements the proxy's Mojo client interface.
     75   //
     76   // ProxyBindings(proxy).setLocalDelegate(impl) - Sets the implementation
     77   //   delegate of the proxy's client stub object. This is just shorthand
     78   //   for |StubBindings(ProxyBindings(proxy).local).delegate = impl|.
     79   //
     80   // ProxyBindings(proxy).getLocalDelegate() - Returns the implementation
     81   //   delegate of the proxy's client stub object. This is just shorthand
     82   //   for |StubBindings(ProxyBindings(proxy).local).delegate|.
     83 
     84   function ProxyBindings(proxy) {
     85     return (proxy instanceof ProxyBase) ? proxy[kProxyProperties] : proxy;
     86   }
     87 
     88   // TODO(hansmuller): remove the remote doc after 'Client=' has been
     89   // removed from Mojom.
     90 
     91   // Provides access to properties added to a stub object without risking
     92   // Mojo interface name collisions. Unless otherwise specified, the initial
     93   // value of all properties is undefined.
     94   //
     95   // StubBindings(stub).delegate - The optional implementation delegate for
     96   //  the Mojo interface stub.
     97   //
     98   // StubBindings(stub).connection - The Connection object that links an
     99   //   optional proxy for a remote service to this stub. The value of
    100   //   StubBindings(stub).connection.local == stub.
    101   //
    102   // StubBindings(stub).remote - A proxy for the the stub's Mojo client
    103   //   service.
    104 
    105   function StubBindings(stub) {
    106     return stub instanceof StubBase ?  stub[kStubProperties] : stub;
    107   }
    108 
    109   var exports = {};
    110   exports.EmptyProxy = ProxyBase;
    111   exports.EmptyStub = StubBase;
    112   exports.ProxyBase = ProxyBase;
    113   exports.ProxyBindings = ProxyBindings;
    114   exports.StubBase = StubBase;
    115   exports.StubBindings = StubBindings;
    116   return exports;
    117 });
    118