Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Unified Diff: mojo/public/js/bindings.js

Issue 2549683002: Mojo JS bindings: introduce concepts that are more similar to C++ bindings: (Closed)
Patch Set: put interface control methods in the |ptr| property Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/edk/js/tests/sample_service_tests.js ('k') | mojo/public/js/connection.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/js/bindings.js
diff --git a/mojo/public/js/bindings.js b/mojo/public/js/bindings.js
index 2fdcae39f30d17114602f3602299670eed5c1884..652a4b40bfe35b3d7300389923eef087930366fa 100644
--- a/mojo/public/js/bindings.js
+++ b/mojo/public/js/bindings.js
@@ -3,115 +3,140 @@
// found in the LICENSE file.
define("mojo/public/js/bindings", [
- "mojo/public/js/router",
+ "mojo/public/js/connection",
"mojo/public/js/core",
-], function(router, core) {
+], function(connection, core) {
- var Router = router.Router;
+ // ---------------------------------------------------------------------------
- var kProxyProperties = Symbol("proxyProperties");
- var kStubProperties = Symbol("stubProperties");
-
- // Public proxy class properties that are managed at runtime by the JS
- // bindings. See ProxyBindings below.
- function ProxyProperties(receiver) {
- this.receiver = receiver;
+ function InterfacePtrInfo(handle, version) {
+ this.handle = handle;
+ this.version = version;
}
- // TODO(hansmuller): remove then after 'Client=' has been removed from Mojom.
- ProxyProperties.prototype.getLocalDelegate = function() {
- return this.local && StubBindings(this.local).delegate;
+ InterfacePtrInfo.prototype.isValid = function() {
+ return core.isHandle(this.handle);
}
- // TODO(hansmuller): remove then after 'Client=' has been removed from Mojom.
- ProxyProperties.prototype.setLocalDelegate = function(impl) {
- if (this.local)
- StubBindings(this.local).delegate = impl;
- else
- throw new Error("no stub object");
- }
+ // ---------------------------------------------------------------------------
- ProxyProperties.prototype.close = function() {
- this.connection.close();
+ function InterfaceRequest(handle) {
+ this.handle = handle;
}
- // Public stub class properties that are managed at runtime by the JS
- // bindings. See StubBindings below.
- function StubProperties(delegate) {
- this.delegate = delegate;
+ InterfaceRequest.prototype.isValid = function() {
+ return core.isHandle(this.handle);
}
- StubProperties.prototype.close = function() {
- this.connection.close();
+ // ---------------------------------------------------------------------------
+
+ function makeRequest(interfacePtr) {
+ var pipe = core.createMessagePipe();
+ interfacePtr.ptr.bind(new InterfacePtrInfo(pipe.handle0, 0));
+ return new InterfaceRequest(pipe.handle1);
}
- // The base class for generated proxy classes.
- function ProxyBase(receiver) {
- this[kProxyProperties] = new ProxyProperties(receiver);
+ // ---------------------------------------------------------------------------
- // TODO(hansmuller): Temporary, for Chrome backwards compatibility.
- if (receiver instanceof Router)
- this.receiver_ = receiver;
+ // Operations used to setup/configure an interface pointer. Exposed as the
+ // |ptr| field of generated interface pointer classes.
+ function InterfacePtrController(interfaceType) {
+ this.version = 0;
+ this.connection = null;
+
+ this.interfaceType_ = interfaceType;
}
- // The base class for generated stub classes.
- function StubBase(delegate) {
- this[kStubProperties] = new StubProperties(delegate);
+ InterfacePtrController.prototype.bind = function(interfacePtrInfo) {
+ this.reset();
+ this.version = interfacePtrInfo.version;
+ this.connection = new connection.Connection(
+ interfacePtrInfo.handle, undefined, this.interfaceType_.proxyClass);
}
- // TODO(hansmuller): remove everything except the connection property doc
- // after 'Client=' has been removed from Mojom.
+ InterfacePtrController.prototype.isBound = function() {
+ return this.connection !== null;
+ }
- // Provides access to properties added to a proxy object without risking
- // Mojo interface name collisions. Unless otherwise specified, the initial
- // value of all properties is undefined.
- //
- // ProxyBindings(proxy).connection - The Connection object that links the
- // proxy for a remote Mojo service to an optional local stub for a local
- // service. The value of ProxyBindings(proxy).connection.remote == proxy.
- //
- // ProxyBindings(proxy).local - The "local" stub object whose delegate
- // implements the proxy's Mojo client interface.
- //
- // ProxyBindings(proxy).setLocalDelegate(impl) - Sets the implementation
- // delegate of the proxy's client stub object. This is just shorthand
- // for |StubBindings(ProxyBindings(proxy).local).delegate = impl|.
- //
- // ProxyBindings(proxy).getLocalDelegate() - Returns the implementation
- // delegate of the proxy's client stub object. This is just shorthand
- // for |StubBindings(ProxyBindings(proxy).local).delegate|.
+ // Although users could just discard the object, reset() closes the pipe
+ // immediately.
+ InterfacePtrController.prototype.reset = function() {
+ if (!this.isBound())
+ return;
- function ProxyBindings(proxy) {
- return (proxy instanceof ProxyBase) ? proxy[kProxyProperties] : proxy;
+ this.version = 0;
+ this.connection.close();
+ this.connection = null;
}
- // TODO(hansmuller): remove the remote doc after 'Client=' has been
- // removed from Mojom.
+ // TODO(yzshen): Implement the following methods.
+ // InterfacePtrController.prototype.setConnectionErrorHandler
+ // InterfacePtrController.prototype.passInterface
+ // InterfacePtrController.prototype.queryVersion
+ // InterfacePtrController.prototype.requireVersion
+
+ // ---------------------------------------------------------------------------
- // Provides access to properties added to a stub object without risking
- // Mojo interface name collisions. Unless otherwise specified, the initial
- // value of all properties is undefined.
+ // |request| could be omitted and passed into bind() later.
+ // NOTE: |impl| shouldn't hold a reference to this object, because that
+ // results in circular references.
//
- // StubBindings(stub).delegate - The optional implementation delegate for
- // the Mojo interface stub.
+ // Example:
//
- // StubBindings(stub).connection - The Connection object that links an
- // optional proxy for a remote service to this stub. The value of
- // StubBindings(stub).connection.local == stub.
+ // // FooImpl implements mojom.Foo.
+ // function FooImpl() { ... }
+ // FooImpl.prototype.fooMethod1 = function() { ... }
+ // FooImpl.prototype.fooMethod2 = function() { ... }
//
- // StubBindings(stub).remote - A proxy for the the stub's Mojo client
- // service.
+ // var fooPtr = new mojom.FooPtr();
+ // var request = makeRequest(fooPtr);
+ // var binding = new Binding(mojom.Foo, new FooImpl(), request);
+ // fooPtr.fooMethod1();
+ function Binding(interfaceType, impl, request) {
+ this.interfaceType_ = interfaceType;
+ this.impl_ = impl;
+ this.stub_ = null;
+
+ if (request)
+ this.bind(request);
+ }
+
+ Binding.prototype.isBound = function() {
+ return this.stub_ !== null;
+ }
+
+ Binding.prototype.bind = function(request) {
+ this.close();
+ this.stub_ = connection.bindHandleToStub(request.handle,
+ this.interfaceType_);
+ connection.StubBindings(this.stub_).delegate = this.impl_;
+ }
- function StubBindings(stub) {
- return stub instanceof StubBase ? stub[kStubProperties] : stub;
+ Binding.prototype.close = function() {
+ if (!this.isBound())
+ return;
+ connection.StubBindings(this.stub_).close();
+ this.stub_ = null;
}
+ // TODO(yzshen): Implement the following methods.
+ // Binding.prototype.setConnectionErrorHandler
+ // Binding.prototype.unbind
+
var exports = {};
- exports.EmptyProxy = ProxyBase;
- exports.EmptyStub = StubBase;
- exports.ProxyBase = ProxyBase;
- exports.ProxyBindings = ProxyBindings;
- exports.StubBase = StubBase;
- exports.StubBindings = StubBindings;
+ exports.InterfacePtrInfo = InterfacePtrInfo;
+ exports.InterfaceRequest = InterfaceRequest;
+ exports.makeRequest = makeRequest;
+ exports.InterfacePtrController = InterfacePtrController;
+ exports.Binding = Binding;
+
+ // TODO(yzshen): Remove the following exports.
+ exports.EmptyProxy = connection.EmptyProxy;
+ exports.EmptyStub = connection.EmptyStub;
+ exports.ProxyBase = connection.ProxyBase;
+ exports.ProxyBindings = connection.ProxyBindings;
+ exports.StubBase = connection.StubBase;
+ exports.StubBindings = connection.StubBindings;
+
return exports;
});
« no previous file with comments | « mojo/edk/js/tests/sample_service_tests.js ('k') | mojo/public/js/connection.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698