| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 define("mojo/public/js/bindings", [ | 5 define("mojo/public/js/bindings", [ |
| 6 "mojo/public/js/router", | 6 "mojo/public/js/connection", |
| 7 "mojo/public/js/core", | 7 "mojo/public/js/core", |
| 8 ], function(router, core) { | 8 ], function(connection, core) { |
| 9 | 9 |
| 10 var Router = router.Router; | 10 // --------------------------------------------------------------------------- |
| 11 | 11 |
| 12 var kProxyProperties = Symbol("proxyProperties"); | 12 function InterfacePtrInfo(handle, version) { |
| 13 var kStubProperties = Symbol("stubProperties"); | 13 this.handle = handle; |
| 14 | 14 this.version = version; |
| 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 } | 15 } |
| 20 | 16 |
| 21 // TODO(hansmuller): remove then after 'Client=' has been removed from Mojom. | 17 InterfacePtrInfo.prototype.isValid = function() { |
| 22 ProxyProperties.prototype.getLocalDelegate = function() { | 18 return core.isHandle(this.handle); |
| 23 return this.local && StubBindings(this.local).delegate; | |
| 24 } | 19 } |
| 25 | 20 |
| 26 // TODO(hansmuller): remove then after 'Client=' has been removed from Mojom. | 21 // --------------------------------------------------------------------------- |
| 27 ProxyProperties.prototype.setLocalDelegate = function(impl) { | 22 |
| 28 if (this.local) | 23 function InterfaceRequest(handle) { |
| 29 StubBindings(this.local).delegate = impl; | 24 this.handle = handle; |
| 30 else | |
| 31 throw new Error("no stub object"); | |
| 32 } | 25 } |
| 33 | 26 |
| 34 ProxyProperties.prototype.close = function() { | 27 InterfaceRequest.prototype.isValid = function() { |
| 35 this.connection.close(); | 28 return core.isHandle(this.handle); |
| 36 } | 29 } |
| 37 | 30 |
| 38 // Public stub class properties that are managed at runtime by the JS | 31 // --------------------------------------------------------------------------- |
| 39 // bindings. See StubBindings below. | 32 |
| 40 function StubProperties(delegate) { | 33 function makeRequest(interfacePtr) { |
| 41 this.delegate = delegate; | 34 var pipe = core.createMessagePipe(); |
| 35 interfacePtr.bind(new InterfacePtrInfo(pipe.handle0, 0)); |
| 36 return new InterfaceRequest(pipe.handle1); |
| 42 } | 37 } |
| 43 | 38 |
| 44 StubProperties.prototype.close = function() { | 39 // --------------------------------------------------------------------------- |
| 45 this.connection.close(); | 40 |
| 41 // Usually you don't need to use this class directly. Instead, you should use |
| 42 // a generated subclass. |
| 43 function InterfacePtrBase(interfaceType) { |
| 44 this.version = 0; |
| 45 this.connection = null; |
| 46 |
| 47 this.interfaceType_ = interfaceType; |
| 46 } | 48 } |
| 47 | 49 |
| 48 // The base class for generated proxy classes. | 50 InterfacePtrBase.prototype.bind = function(interfacePtrInfo) { |
| 49 function ProxyBase(receiver) { | 51 this.reset(); |
| 50 this[kProxyProperties] = new ProxyProperties(receiver); | 52 this.version = interfacePtrInfo.version; |
| 51 | 53 this.connection = new connection.Connection( |
| 52 // TODO(hansmuller): Temporary, for Chrome backwards compatibility. | 54 interfacePtrInfo.handle, undefined, this.interfaceType_.proxyClass); |
| 53 if (receiver instanceof Router) | |
| 54 this.receiver_ = receiver; | |
| 55 } | 55 } |
| 56 | 56 |
| 57 // The base class for generated stub classes. | 57 InterfacePtrBase.prototype.isBound = function() { |
| 58 function StubBase(delegate) { | 58 return this.connection !== null; |
| 59 this[kStubProperties] = new StubProperties(delegate); | |
| 60 } | 59 } |
| 61 | 60 |
| 62 // TODO(hansmuller): remove everything except the connection property doc | 61 // Although users could just discard the object, reset() closes the pipe |
| 63 // after 'Client=' has been removed from Mojom. | 62 // immediately. |
| 63 InterfacePtrBase.prototype.reset = function() { |
| 64 if (!this.isBound()) |
| 65 return; |
| 64 | 66 |
| 65 // Provides access to properties added to a proxy object without risking | 67 this.version = 0; |
| 66 // Mojo interface name collisions. Unless otherwise specified, the initial | 68 this.connection.close(); |
| 67 // value of all properties is undefined. | 69 this.connection = null; |
| 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 } | 70 } |
| 87 | 71 |
| 88 // TODO(hansmuller): remove the remote doc after 'Client=' has been | 72 // TODO(yzshen): Implement the following methods. |
| 89 // removed from Mojom. | 73 // InterfacePtrBase.prototype.setConnectionErrorHandler |
| 74 // InterfacePtrBase.prototype.passInterface |
| 75 // InterfacePtrBase.prototype.queryVersion |
| 76 // InterfacePtrBase.prototype.requireVersion |
| 90 | 77 |
| 91 // Provides access to properties added to a stub object without risking | 78 // --------------------------------------------------------------------------- |
| 92 // Mojo interface name collisions. Unless otherwise specified, the initial | 79 |
| 93 // value of all properties is undefined. | 80 // |request| could be omitted and passed into bind() later. |
| 81 // NOTE: |impl| shouldn't hold a reference to this object, because that |
| 82 // results in circular references. |
| 94 // | 83 // |
| 95 // StubBindings(stub).delegate - The optional implementation delegate for | 84 // Example: |
| 96 // the Mojo interface stub. | |
| 97 // | 85 // |
| 98 // StubBindings(stub).connection - The Connection object that links an | 86 // // FooImpl implements mojom.Foo. |
| 99 // optional proxy for a remote service to this stub. The value of | 87 // function FooImpl() { ... } |
| 100 // StubBindings(stub).connection.local == stub. | 88 // FooImpl.prototype.fooMethod1 = function() { ... } |
| 89 // FooImpl.prototype.fooMethod2 = function() { ... } |
| 101 // | 90 // |
| 102 // StubBindings(stub).remote - A proxy for the the stub's Mojo client | 91 // var fooPtr = new mojom.FooPtr(); |
| 103 // service. | 92 // var request = makeRequest(fooPtr); |
| 93 // var binding = new Binding(mojom.Foo, new FooImpl(), request); |
| 94 // fooPtr.fooMethod1(); |
| 95 function Binding(interfaceType, impl, request) { |
| 96 this.interfaceType_ = interfaceType; |
| 97 this.impl_ = impl; |
| 98 this.stub_ = null; |
| 104 | 99 |
| 105 function StubBindings(stub) { | 100 if (request) |
| 106 return stub instanceof StubBase ? stub[kStubProperties] : stub; | 101 this.bind(request); |
| 107 } | 102 } |
| 108 | 103 |
| 104 Binding.prototype.isBound = function() { |
| 105 return this.stub_ !== null; |
| 106 } |
| 107 |
| 108 Binding.prototype.bind = function(request) { |
| 109 this.close(); |
| 110 this.stub_ = connection.bindHandleToStub(request.handle, |
| 111 this.interfaceType_); |
| 112 connection.StubBindings(this.stub_).delegate = this.impl_; |
| 113 } |
| 114 |
| 115 Binding.prototype.close = function() { |
| 116 if (!this.isBound()) |
| 117 return; |
| 118 connection.StubBindings(this.stub_).close(); |
| 119 this.stub_ = null; |
| 120 } |
| 121 |
| 122 // TODO(yzshen): Implement the following methods. |
| 123 // Binding.prototype.setConnectionErrorHandler |
| 124 // Binding.prototype.unbind |
| 125 |
| 109 var exports = {}; | 126 var exports = {}; |
| 110 exports.EmptyProxy = ProxyBase; | 127 exports.InterfacePtrInfo = InterfacePtrInfo; |
| 111 exports.EmptyStub = StubBase; | 128 exports.InterfaceRequest = InterfaceRequest; |
| 112 exports.ProxyBase = ProxyBase; | 129 exports.makeRequest = makeRequest; |
| 113 exports.ProxyBindings = ProxyBindings; | 130 exports.InterfacePtrBase = InterfacePtrBase; |
| 114 exports.StubBase = StubBase; | 131 exports.Binding = Binding; |
| 115 exports.StubBindings = StubBindings; | 132 |
| 133 // TODO(yzshen): Remove the following exports. |
| 134 exports.EmptyProxy = connection.EmptyProxy; |
| 135 exports.EmptyStub = connection.EmptyStub; |
| 136 exports.ProxyBase = connection.ProxyBase; |
| 137 exports.ProxyBindings = connection.ProxyBindings; |
| 138 exports.StubBase = connection.StubBase; |
| 139 exports.StubBindings = connection.StubBindings; |
| 140 |
| 116 return exports; | 141 return exports; |
| 117 }); | 142 }); |
| OLD | NEW |