Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/associated_bindings", [ | 5 define("mojo/public/js/associated_bindings", [ |
| 6 "mojo/public/js/core", | 6 "mojo/public/js/core", |
| 7 "mojo/public/js/interface_types", | 7 "mojo/public/js/interface_types", |
| 8 "mojo/public/js/lib/interface_endpoint_client", | 8 "mojo/public/js/lib/interface_endpoint_client", |
| 9 "mojo/public/js/lib/interface_endpoint_handle", | 9 "mojo/public/js/lib/interface_endpoint_handle", |
| 10 ], function(core, types, interfaceEndpointClient, interfaceEndpointHandle) { | 10 ], function(core, types, interfaceEndpointClient, interfaceEndpointHandle) { |
| 11 | 11 |
| 12 var InterfaceEndpointClient = interfaceEndpointClient.InterfaceEndpointClient; | |
| 13 | |
| 14 // --------------------------------------------------------------------------- | |
| 15 | |
| 16 function makeRequest(associatedInterfacePtrInfo) { | |
| 17 var {handle0, handle1} = | |
|
yzshen1
2017/04/19 21:11:33
I am not quite sure whether destructuring assignme
wangjimmy
2017/04/20 15:36:43
Done. associated_bindings.js resource is loaded vi
| |
| 18 interfaceEndpointHandle.createPairPendingAssociation(); | |
| 19 | |
| 20 associatedInterfacePtrInfo.interfaceEndpointHandle = handle0; | |
| 21 associatedInterfacePtrInfo.version = 0; | |
| 22 | |
| 23 var request = new types.AssociatedInterfaceRequest(handle1); | |
| 24 return request; | |
| 25 } | |
| 26 | |
| 27 // --------------------------------------------------------------------------- | |
| 28 | |
| 29 // Operations used to setup/configure an associated interface pointer. | |
| 30 // Exposed as |ptr| field of generated associated interface pointer classes. | |
| 31 // |associatedPtrInfo| could be omitted and passed into bind() later. | |
| 32 // | |
| 33 // Example: | |
| 34 // // IntegerSenderImpl implements mojom.IntegerSender | |
| 35 // function IntegerSenderImpl() { ... } | |
| 36 // IntegerSenderImpl.prototype.echo = function() { ... } | |
| 37 // | |
| 38 // // IntegerSenderConnectionImpl implements mojom.IntegerSenderConnection | |
| 39 // function IntegerSenderConnectionImpl() { ... } | |
| 40 // IntegerSenderConnectionImpl.prototype.getSender = function( | |
| 41 // associatedRequest) { | |
| 42 // var binding = new AssociatedBinding(mojom.IntegerSender, | |
|
yzshen1
2017/04/19 21:11:33
This binding is garbage collected when it goes out
wangjimmy
2017/04/20 15:36:43
Done.
| |
| 43 // new IntegerSenderImpl(), | |
| 44 // associatedRequest); | |
| 45 // } | |
| 46 // | |
| 47 // var integerSenderConnection = new mojom.IntegerSenderConnectionPtr(); | |
| 48 // var integerSenderConnectionBinding = new Binding( | |
| 49 // mojom.IntegerSenderConnection, | |
| 50 // new IntegerSenderConnectionImpl(), | |
| 51 // bindings.makeRequest(integerSenderConnection)); | |
| 52 // | |
| 53 // // An associated handle must be sent/received over the master interface | |
|
yzshen1
2017/04/19 21:11:33
Maybe the following is more clear?
A locally-crea
wangjimmy
2017/04/20 15:36:43
Done.
| |
| 54 // // before you can use it to make calls on your associated interface. | |
| 55 // var associatedInterfacePtrInfo = new AssociatedInterfacePtrInfo(); | |
| 56 // var associatedRequest = makeRequest(interfacePtrInfo); | |
| 57 // | |
| 58 // integerSenderConnection.getSender(associatedRequest); | |
| 59 // | |
| 60 // // Create an associated interface and bind the associated handle. | |
| 61 // var integerSender = new mojom.AssociatedIntegerSenderPtr(); | |
| 62 // integerSender.ptr.bind(associatedInterfacePtrInfo); | |
| 63 // integerSender.echo(); | |
| 64 | |
| 65 function AssociatedInterfacePtrController(interfaceType, associatedPtrInfo) { | |
| 66 this.version = 0; | |
| 67 | |
| 68 this.interfaceType_ = interfaceType; | |
| 69 this.interfaceEndpointClient_ = null; | |
| 70 this.proxy_ = null; | |
| 71 | |
| 72 if (associatedPtrInfo) { | |
| 73 this.bind(associatedPtrInfo); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 AssociatedInterfacePtrController.prototype.bind = function( | |
| 78 associatedPtrInfo) { | |
| 79 this.reset(); | |
| 80 this.version = associatedPtrInfo.version; | |
| 81 | |
| 82 this.interfaceEndpointClient_ = new InterfaceEndpointClient( | |
| 83 associatedPtrInfo.interfaceEndpointHandle); | |
| 84 | |
| 85 this.interfaceEndpointClient_ .setPayloadValidators([ | |
| 86 this.interfaceType_.validateResponse]); | |
| 87 this.proxy_ = new this.interfaceType_.proxyClass( | |
| 88 this.interfaceEndpointClient_); | |
| 89 }; | |
| 90 | |
| 91 AssociatedInterfacePtrController.prototype.isBound = function() { | |
| 92 return this.interfaceEndpointClient_ !== null; | |
| 93 }; | |
| 94 | |
| 95 AssociatedInterfacePtrController.prototype.reset = function() { | |
| 96 this.version = 0; | |
| 97 if (this.interfaceEndpointClient_) { | |
| 98 this.interfaceEndpointClient_.close(); | |
| 99 this.interfaceEndpointClient_ = null; | |
| 100 } | |
| 101 if (this.proxy_) { | |
| 102 this.proxy_ = null; | |
| 103 } | |
| 104 }; | |
| 105 | |
| 106 AssociatedInterfacePtrController.prototype.resetWithReason = function( | |
| 107 reason) { | |
| 108 if (this.isBound()) { | |
| 109 this.interfaceEndpointClient_.close(reason); | |
| 110 this.interfaceEndpointClient_ = null; | |
| 111 } | |
| 112 this.reset(); | |
| 113 }; | |
| 114 | |
| 115 AssociatedInterfacePtrController.prototype.setConnectionErrorHandler = | |
| 116 function(callback) { | |
| 117 if (!this.isBound()) { | |
| 118 throw new Error("Cannot set connection error handler if not bound."); | |
| 119 } | |
| 120 | |
| 121 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); | |
| 122 }; | |
| 123 | |
| 124 AssociatedInterfacePtrController.prototype.passInterface = function() { | |
| 125 if (!this.isBound()) { | |
| 126 return new types.AssociatedInterfacePtrInfo(null); | |
| 127 } | |
| 128 | |
| 129 var result = new types.AssociatedInterfacePtrInfo( | |
| 130 this.interfaceEndpointClient_.passHandle(), this.version); | |
| 131 this.reset(); | |
| 132 return result; | |
| 133 }; | |
| 134 | |
| 135 AssociatedInterfacePtrController.prototype.getProxy = function() { | |
| 136 return this.proxy_; | |
| 137 }; | |
| 138 | |
| 139 AssociatedInterfacePtrController.prototype.queryVersion = function() { | |
| 140 function onQueryVersion(version) { | |
| 141 this.version = version; | |
| 142 return version; | |
| 143 } | |
| 144 | |
| 145 return this.interfaceEndpointClient_.queryVersion().then( | |
| 146 onQueryVersion.bind(this)); | |
| 147 }; | |
| 148 | |
| 149 AssociatedInterfacePtrController.prototype.requireVersion = function( | |
| 150 version) { | |
| 151 if (this.version >= version) { | |
| 152 return; | |
| 153 } | |
| 154 this.version = version; | |
| 155 this.interfaceEndpointClient_.requireVersion(version); | |
| 156 }; | |
| 157 | |
| 158 // --------------------------------------------------------------------------- | |
| 159 | |
| 160 // |associatedInterfaceRequest| could be omitted and passed into bind() | |
| 161 // later. | |
| 162 function AssociatedBinding(interfaceType, impl, associatedInterfaceRequest) { | |
| 163 this.interfaceType_ = interfaceType; | |
| 164 this.impl_ = impl; | |
| 165 this.interfaceEndpointClient_ = null; | |
| 166 this.stub_ = null; | |
| 167 | |
| 168 if (associatedInterfaceRequest) { | |
| 169 this.bind(associatedInterfaceRequest); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 AssociatedBinding.prototype.isBound = function() { | |
| 174 return this.interfaceEndpointClient_ !== null; | |
| 175 }; | |
| 176 | |
| 177 AssociatedBinding.prototype.bind = function(associatedInterfaceRequest) { | |
| 178 this.close(); | |
| 179 | |
| 180 this.stub_ = new this.interfaceType_.stubClass(this.impl_); | |
| 181 this.interfaceEndpointClient_ = new InterfaceEndpointClient( | |
| 182 associatedInterfaceRequest.interfaceEndpointHandle, this.stub_, | |
| 183 this.interfaceType_.kVersion); | |
| 184 | |
| 185 this.interfaceEndpointClient_ .setPayloadValidators([ | |
| 186 this.interfaceType_.validateRequest]); | |
| 187 }; | |
| 188 | |
| 189 | |
| 190 AssociatedBinding.prototype.close = function() { | |
| 191 if (!this.isBound()) { | |
| 192 return; | |
| 193 } | |
| 194 | |
| 195 if (this.interfaceEndpointClient_) { | |
| 196 this.interfaceEndpointClient_.close(); | |
| 197 this.interfaceEndpointClient_ = null; | |
| 198 } | |
| 199 | |
| 200 this.stub_ = null; | |
| 201 }; | |
| 202 | |
| 203 AssociatedBinding.prototype.closeWithReason = function(reason) { | |
| 204 if (this.interfaceEndpointClient_) { | |
| 205 this.interfaceEndpointClient_.close(reason); | |
| 206 this.interfaceEndpointClient_ = null; | |
| 207 } | |
| 208 this.close(); | |
| 209 }; | |
| 210 | |
| 211 AssociatedBinding.prototype.setConnectionErrorHandler = function(callback) { | |
| 212 if (!this.isBound()) { | |
| 213 throw new Error("Cannot set connection error handler if not bound."); | |
| 214 } | |
| 215 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); | |
| 216 }; | |
| 217 | |
| 218 AssociatedBinding.prototype.unbind = function() { | |
| 219 if (!this.isBound()) { | |
| 220 return new types.AssociatedInterfaceRequest(null); | |
| 221 } | |
| 222 | |
| 223 var result = new types.AssociatedInterfaceRequest( | |
| 224 this.interfaceEndpointClient_.passHandle()); | |
| 225 this.close(); | |
| 226 return result; | |
| 227 }; | |
| 228 | |
| 12 var exports = {}; | 229 var exports = {}; |
| 13 exports.AssociatedInterfacePtrInfo = types.AssociatedInterfacePtrInfo; | 230 exports.AssociatedInterfacePtrInfo = types.AssociatedInterfacePtrInfo; |
| 14 exports.AssociatedInterfaceRequest = types.AssociatedInterfaceRequest; | 231 exports.AssociatedInterfaceRequest = types.AssociatedInterfaceRequest; |
| 232 exports.makeRequest = makeRequest; | |
| 233 exports.AssociatedInterfacePtrController = AssociatedInterfacePtrController; | |
| 234 exports.AssociatedBinding = AssociatedBinding; | |
| 15 | 235 |
| 16 return exports; | 236 return exports; |
| 17 }); | 237 }); |
| OLD | NEW |