| 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} = |
| 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 // this.senderBinding_ = null; |
| 41 // } |
| 42 // IntegerSenderConnectionImpl.prototype.getSender = function( |
| 43 // associatedRequest) { |
| 44 // this.senderBinding_ = new AssociatedBinding(mojom.IntegerSender, |
| 45 // new IntegerSenderImpl(), |
| 46 // associatedRequest); |
| 47 // } |
| 48 // |
| 49 // var integerSenderConnection = new mojom.IntegerSenderConnectionPtr(); |
| 50 // var integerSenderConnectionBinding = new Binding( |
| 51 // mojom.IntegerSenderConnection, |
| 52 // new IntegerSenderConnectionImpl(), |
| 53 // bindings.makeRequest(integerSenderConnection)); |
| 54 // |
| 55 // // A locally-created associated interface pointer can only be used to |
| 56 // // make calls when the corresponding associated request is sent over |
| 57 // // another interface (either the master interface or another |
| 58 // // associated interface). |
| 59 // var associatedInterfacePtrInfo = new AssociatedInterfacePtrInfo(); |
| 60 // var associatedRequest = makeRequest(interfacePtrInfo); |
| 61 // |
| 62 // integerSenderConnection.getSender(associatedRequest); |
| 63 // |
| 64 // // Create an associated interface and bind the associated handle. |
| 65 // var integerSender = new mojom.AssociatedIntegerSenderPtr(); |
| 66 // integerSender.ptr.bind(associatedInterfacePtrInfo); |
| 67 // integerSender.echo(); |
| 68 |
| 69 function AssociatedInterfacePtrController(interfaceType, associatedPtrInfo) { |
| 70 this.version = 0; |
| 71 |
| 72 this.interfaceType_ = interfaceType; |
| 73 this.interfaceEndpointClient_ = null; |
| 74 this.proxy_ = null; |
| 75 |
| 76 if (associatedPtrInfo) { |
| 77 this.bind(associatedPtrInfo); |
| 78 } |
| 79 } |
| 80 |
| 81 AssociatedInterfacePtrController.prototype.bind = function( |
| 82 associatedPtrInfo) { |
| 83 this.reset(); |
| 84 this.version = associatedPtrInfo.version; |
| 85 |
| 86 this.interfaceEndpointClient_ = new InterfaceEndpointClient( |
| 87 associatedPtrInfo.interfaceEndpointHandle); |
| 88 |
| 89 this.interfaceEndpointClient_ .setPayloadValidators([ |
| 90 this.interfaceType_.validateResponse]); |
| 91 this.proxy_ = new this.interfaceType_.proxyClass( |
| 92 this.interfaceEndpointClient_); |
| 93 }; |
| 94 |
| 95 AssociatedInterfacePtrController.prototype.isBound = function() { |
| 96 return this.interfaceEndpointClient_ !== null; |
| 97 }; |
| 98 |
| 99 AssociatedInterfacePtrController.prototype.reset = function() { |
| 100 this.version = 0; |
| 101 if (this.interfaceEndpointClient_) { |
| 102 this.interfaceEndpointClient_.close(); |
| 103 this.interfaceEndpointClient_ = null; |
| 104 } |
| 105 if (this.proxy_) { |
| 106 this.proxy_ = null; |
| 107 } |
| 108 }; |
| 109 |
| 110 AssociatedInterfacePtrController.prototype.resetWithReason = function( |
| 111 reason) { |
| 112 if (this.isBound()) { |
| 113 this.interfaceEndpointClient_.close(reason); |
| 114 this.interfaceEndpointClient_ = null; |
| 115 } |
| 116 this.reset(); |
| 117 }; |
| 118 |
| 119 AssociatedInterfacePtrController.prototype.setConnectionErrorHandler = |
| 120 function(callback) { |
| 121 if (!this.isBound()) { |
| 122 throw new Error("Cannot set connection error handler if not bound."); |
| 123 } |
| 124 |
| 125 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); |
| 126 }; |
| 127 |
| 128 AssociatedInterfacePtrController.prototype.passInterface = function() { |
| 129 if (!this.isBound()) { |
| 130 return new types.AssociatedInterfacePtrInfo(null); |
| 131 } |
| 132 |
| 133 var result = new types.AssociatedInterfacePtrInfo( |
| 134 this.interfaceEndpointClient_.passHandle(), this.version); |
| 135 this.reset(); |
| 136 return result; |
| 137 }; |
| 138 |
| 139 AssociatedInterfacePtrController.prototype.getProxy = function() { |
| 140 return this.proxy_; |
| 141 }; |
| 142 |
| 143 AssociatedInterfacePtrController.prototype.queryVersion = function() { |
| 144 function onQueryVersion(version) { |
| 145 this.version = version; |
| 146 return version; |
| 147 } |
| 148 |
| 149 return this.interfaceEndpointClient_.queryVersion().then( |
| 150 onQueryVersion.bind(this)); |
| 151 }; |
| 152 |
| 153 AssociatedInterfacePtrController.prototype.requireVersion = function( |
| 154 version) { |
| 155 if (this.version >= version) { |
| 156 return; |
| 157 } |
| 158 this.version = version; |
| 159 this.interfaceEndpointClient_.requireVersion(version); |
| 160 }; |
| 161 |
| 162 // --------------------------------------------------------------------------- |
| 163 |
| 164 // |associatedInterfaceRequest| could be omitted and passed into bind() |
| 165 // later. |
| 166 function AssociatedBinding(interfaceType, impl, associatedInterfaceRequest) { |
| 167 this.interfaceType_ = interfaceType; |
| 168 this.impl_ = impl; |
| 169 this.interfaceEndpointClient_ = null; |
| 170 this.stub_ = null; |
| 171 |
| 172 if (associatedInterfaceRequest) { |
| 173 this.bind(associatedInterfaceRequest); |
| 174 } |
| 175 } |
| 176 |
| 177 AssociatedBinding.prototype.isBound = function() { |
| 178 return this.interfaceEndpointClient_ !== null; |
| 179 }; |
| 180 |
| 181 AssociatedBinding.prototype.bind = function(associatedInterfaceRequest) { |
| 182 this.close(); |
| 183 |
| 184 this.stub_ = new this.interfaceType_.stubClass(this.impl_); |
| 185 this.interfaceEndpointClient_ = new InterfaceEndpointClient( |
| 186 associatedInterfaceRequest.interfaceEndpointHandle, this.stub_, |
| 187 this.interfaceType_.kVersion); |
| 188 |
| 189 this.interfaceEndpointClient_ .setPayloadValidators([ |
| 190 this.interfaceType_.validateRequest]); |
| 191 }; |
| 192 |
| 193 |
| 194 AssociatedBinding.prototype.close = function() { |
| 195 if (!this.isBound()) { |
| 196 return; |
| 197 } |
| 198 |
| 199 if (this.interfaceEndpointClient_) { |
| 200 this.interfaceEndpointClient_.close(); |
| 201 this.interfaceEndpointClient_ = null; |
| 202 } |
| 203 |
| 204 this.stub_ = null; |
| 205 }; |
| 206 |
| 207 AssociatedBinding.prototype.closeWithReason = function(reason) { |
| 208 if (this.interfaceEndpointClient_) { |
| 209 this.interfaceEndpointClient_.close(reason); |
| 210 this.interfaceEndpointClient_ = null; |
| 211 } |
| 212 this.close(); |
| 213 }; |
| 214 |
| 215 AssociatedBinding.prototype.setConnectionErrorHandler = function(callback) { |
| 216 if (!this.isBound()) { |
| 217 throw new Error("Cannot set connection error handler if not bound."); |
| 218 } |
| 219 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); |
| 220 }; |
| 221 |
| 222 AssociatedBinding.prototype.unbind = function() { |
| 223 if (!this.isBound()) { |
| 224 return new types.AssociatedInterfaceRequest(null); |
| 225 } |
| 226 |
| 227 var result = new types.AssociatedInterfaceRequest( |
| 228 this.interfaceEndpointClient_.passHandle()); |
| 229 this.close(); |
| 230 return result; |
| 231 }; |
| 232 |
| 12 var exports = {}; | 233 var exports = {}; |
| 13 exports.AssociatedInterfacePtrInfo = types.AssociatedInterfacePtrInfo; | 234 exports.AssociatedInterfacePtrInfo = types.AssociatedInterfacePtrInfo; |
| 14 exports.AssociatedInterfaceRequest = types.AssociatedInterfaceRequest; | 235 exports.AssociatedInterfaceRequest = types.AssociatedInterfaceRequest; |
| 236 exports.makeRequest = makeRequest; |
| 237 exports.AssociatedInterfacePtrController = AssociatedInterfacePtrController; |
| 238 exports.AssociatedBinding = AssociatedBinding; |
| 15 | 239 |
| 16 return exports; | 240 return exports; |
| 17 }); | 241 }); |
| OLD | NEW |