Hans Muller | 8628fc3 | 2015-01-05 12:46:09 -0800 | [diff] [blame] | 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", |
Hans Muller | b771939 | 2015-01-28 09:27:11 -0800 | [diff] [blame] | 7 | "mojo/public/js/core", |
| 8 | ], function(router, core) { |
Hans Muller | 8628fc3 | 2015-01-05 12:46:09 -0800 | [diff] [blame] | 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 | |
Hans Muller | d3553d2 | 2015-01-23 11:10:32 -0800 | [diff] [blame] | 21 | // TODO(hansmuller): remove then after 'Client=' has been removed from Mojom. |
Hans Muller | 8628fc3 | 2015-01-05 12:46:09 -0800 | [diff] [blame] | 22 | ProxyProperties.prototype.getLocalDelegate = function() { |
| 23 | return this.local && StubBindings(this.local).delegate; |
| 24 | } |
| 25 | |
Hans Muller | d3553d2 | 2015-01-23 11:10:32 -0800 | [diff] [blame] | 26 | // TODO(hansmuller): remove then after 'Client=' has been removed from Mojom. |
Hans Muller | 8628fc3 | 2015-01-05 12:46:09 -0800 | [diff] [blame] | 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 | |
Hans Muller | b771939 | 2015-01-28 09:27:11 -0800 | [diff] [blame] | 34 | function connectionHandle(connection) { |
| 35 | return connection && |
| 36 | connection.router && |
| 37 | connection.router.connector_ && |
| 38 | connection.router.connector_.handle_; |
| 39 | } |
| 40 | |
| 41 | ProxyProperties.prototype.close = function() { |
| 42 | var handle = connectionHandle(this.connection); |
| 43 | if (handle) |
| 44 | core.close(handle); |
| 45 | } |
| 46 | |
Hans Muller | 8628fc3 | 2015-01-05 12:46:09 -0800 | [diff] [blame] | 47 | // Public stub class properties that are managed at runtime by the JS |
| 48 | // bindings. See StubBindings below. |
| 49 | function StubProperties(delegate) { |
| 50 | this.delegate = delegate; |
| 51 | } |
| 52 | |
Hans Muller | b771939 | 2015-01-28 09:27:11 -0800 | [diff] [blame] | 53 | StubProperties.prototype.close = function() { |
| 54 | var handle = connectionHandle(this.connection); |
| 55 | if (handle) |
| 56 | core.close(handle); |
| 57 | } |
| 58 | |
Hans Muller | 8628fc3 | 2015-01-05 12:46:09 -0800 | [diff] [blame] | 59 | // The base class for generated proxy classes. |
| 60 | function ProxyBase(receiver) { |
| 61 | this[kProxyProperties] = new ProxyProperties(receiver); |
| 62 | |
| 63 | // TODO(hansmuller): Temporary, for Chrome backwards compatibility. |
| 64 | if (receiver instanceof Router) |
| 65 | this.receiver_ = receiver; |
| 66 | } |
| 67 | |
| 68 | // The base class for generated stub classes. |
| 69 | function StubBase(delegate) { |
| 70 | this[kStubProperties] = new StubProperties(delegate); |
| 71 | } |
| 72 | |
Hans Muller | d3553d2 | 2015-01-23 11:10:32 -0800 | [diff] [blame] | 73 | // TODO(hansmuller): remove everything except the connection property doc |
| 74 | // after 'Client=' has been removed from Mojom. |
| 75 | |
Hans Muller | 8628fc3 | 2015-01-05 12:46:09 -0800 | [diff] [blame] | 76 | // Provides access to properties added to a proxy object without risking |
| 77 | // Mojo interface name collisions. Unless otherwise specified, the initial |
| 78 | // value of all properties is undefined. |
| 79 | // |
| 80 | // ProxyBindings(proxy).connection - The Connection object that links the |
| 81 | // proxy for a remote Mojo service to an optional local stub for a local |
| 82 | // service. The value of ProxyBindings(proxy).connection.remote == proxy. |
| 83 | // |
| 84 | // ProxyBindings(proxy).local - The "local" stub object whose delegate |
| 85 | // implements the proxy's Mojo client interface. |
| 86 | // |
| 87 | // ProxyBindings(proxy).setLocalDelegate(impl) - Sets the implementation |
| 88 | // delegate of the proxy's client stub object. This is just shorthand |
| 89 | // for |StubBindings(ProxyBindings(proxy).local).delegate = impl|. |
| 90 | // |
| 91 | // ProxyBindings(proxy).getLocalDelegate() - Returns the implementation |
| 92 | // delegate of the proxy's client stub object. This is just shorthand |
| 93 | // for |StubBindings(ProxyBindings(proxy).local).delegate|. |
| 94 | |
| 95 | function ProxyBindings(proxy) { |
| 96 | return (proxy instanceof ProxyBase) ? proxy[kProxyProperties] : proxy; |
| 97 | } |
| 98 | |
Hans Muller | d3553d2 | 2015-01-23 11:10:32 -0800 | [diff] [blame] | 99 | // TODO(hansmuller): remove the remote doc after 'Client=' has been |
| 100 | // removed from Mojom. |
| 101 | |
Hans Muller | 8628fc3 | 2015-01-05 12:46:09 -0800 | [diff] [blame] | 102 | // Provides access to properties added to a stub object without risking |
| 103 | // Mojo interface name collisions. Unless otherwise specified, the initial |
| 104 | // value of all properties is undefined. |
| 105 | // |
| 106 | // StubBindings(stub).delegate - The optional implementation delegate for |
| 107 | // the Mojo interface stub. |
| 108 | // |
| 109 | // StubBindings(stub).connection - The Connection object that links an |
| 110 | // optional proxy for a remote service to this stub. The value of |
| 111 | // StubBindings(stub).connection.local == stub. |
| 112 | // |
| 113 | // StubBindings(stub).remote - A proxy for the the stub's Mojo client |
| 114 | // service. |
| 115 | |
| 116 | function StubBindings(stub) { |
| 117 | return stub instanceof StubBase ? stub[kStubProperties] : stub; |
| 118 | } |
| 119 | |
| 120 | var exports = {}; |
| 121 | exports.EmptyProxy = ProxyBase; |
| 122 | exports.EmptyStub = StubBase; |
| 123 | exports.ProxyBase = ProxyBase; |
| 124 | exports.ProxyBindings = ProxyBindings; |
| 125 | exports.StubBase = StubBase; |
| 126 | exports.StubBindings = StubBindings; |
| 127 | return exports; |
| 128 | }); |