OLD | NEW |
| (Empty) |
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 ], function(router) { | |
8 | |
9 var Router = router.Router; | |
10 | |
11 var kProxyProperties = Symbol("proxyProperties"); | |
12 var kStubProperties = Symbol("stubProperties"); | |
13 | |
14 // Public proxy class properties that are managed at runtime by the JS | |
15 // bindings. See ProxyBindings below. | |
16 function ProxyProperties(receiver) { | |
17 this.receiver = receiver; | |
18 } | |
19 | |
20 ProxyProperties.prototype.getLocalDelegate = function() { | |
21 return this.local && StubBindings(this.local).delegate; | |
22 } | |
23 | |
24 ProxyProperties.prototype.setLocalDelegate = function(impl) { | |
25 if (this.local) | |
26 StubBindings(this.local).delegate = impl; | |
27 else | |
28 throw new Error("no stub object"); | |
29 } | |
30 | |
31 // Public stub class properties that are managed at runtime by the JS | |
32 // bindings. See StubBindings below. | |
33 function StubProperties(delegate) { | |
34 this.delegate = delegate; | |
35 } | |
36 | |
37 // The base class for generated proxy classes. | |
38 function ProxyBase(receiver) { | |
39 this[kProxyProperties] = new ProxyProperties(receiver); | |
40 | |
41 // TODO(hansmuller): Temporary, for Chrome backwards compatibility. | |
42 if (receiver instanceof Router) | |
43 this.receiver_ = receiver; | |
44 } | |
45 | |
46 // The base class for generated stub classes. | |
47 function StubBase(delegate) { | |
48 this[kStubProperties] = new StubProperties(delegate); | |
49 } | |
50 | |
51 // Provides access to properties added to a proxy object without risking | |
52 // Mojo interface name collisions. Unless otherwise specified, the initial | |
53 // value of all properties is undefined. | |
54 // | |
55 // ProxyBindings(proxy).connection - The Connection object that links the | |
56 // proxy for a remote Mojo service to an optional local stub for a local | |
57 // service. The value of ProxyBindings(proxy).connection.remote == proxy. | |
58 // | |
59 // ProxyBindings(proxy).local - The "local" stub object whose delegate | |
60 // implements the proxy's Mojo client interface. | |
61 // | |
62 // ProxyBindings(proxy).setLocalDelegate(impl) - Sets the implementation | |
63 // delegate of the proxy's client stub object. This is just shorthand | |
64 // for |StubBindings(ProxyBindings(proxy).local).delegate = impl|. | |
65 // | |
66 // ProxyBindings(proxy).getLocalDelegate() - Returns the implementation | |
67 // delegate of the proxy's client stub object. This is just shorthand | |
68 // for |StubBindings(ProxyBindings(proxy).local).delegate|. | |
69 | |
70 function ProxyBindings(proxy) { | |
71 return (proxy instanceof ProxyBase) ? proxy[kProxyProperties] : proxy; | |
72 } | |
73 | |
74 // Provides access to properties added to a stub object without risking | |
75 // Mojo interface name collisions. Unless otherwise specified, the initial | |
76 // value of all properties is undefined. | |
77 // | |
78 // StubBindings(stub).delegate - The optional implementation delegate for | |
79 // the Mojo interface stub. | |
80 // | |
81 // StubBindings(stub).connection - The Connection object that links an | |
82 // optional proxy for a remote service to this stub. The value of | |
83 // StubBindings(stub).connection.local == stub. | |
84 // | |
85 // StubBindings(stub).remote - A proxy for the the stub's Mojo client | |
86 // service. | |
87 | |
88 function StubBindings(stub) { | |
89 return stub instanceof StubBase ? stub[kStubProperties] : stub; | |
90 } | |
91 | |
92 var exports = {}; | |
93 exports.EmptyProxy = ProxyBase; | |
94 exports.EmptyStub = StubBase; | |
95 exports.ProxyBase = ProxyBase; | |
96 exports.ProxyBindings = ProxyBindings; | |
97 exports.StubBase = StubBase; | |
98 exports.StubBindings = StubBindings; | |
99 return exports; | |
100 }); | |
OLD | NEW |