| OLD | NEW |
| 1 // Copyright 2014 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 (function() { | 5 (function() { |
| 6 var internal = mojo.internal; | 6 var internal = mojo.internal; |
| 7 |
| 7 // --------------------------------------------------------------------------- | 8 // --------------------------------------------------------------------------- |
| 8 | 9 |
| 9 function makeRequest(interfacePtr) { | 10 // |output| could be an interface pointer, InterfacePtrInfo or |
| 11 // AssociatedInterfacePtrInfo. |
| 12 function makeRequest(output) { |
| 13 if (output instanceof mojo.AssociatedInterfacePtrInfo) { |
| 14 var {handle0, handle1} = internal.createPairPendingAssociation(); |
| 15 output.interfaceEndpointHandle = handle0; |
| 16 output.version = 0; |
| 17 |
| 18 return new mojo.AssociatedInterfaceRequest(handle1); |
| 19 } |
| 20 |
| 21 if (output instanceof mojo.InterfacePtrInfo) { |
| 22 var pipe = Mojo.createMessagePipe(); |
| 23 output.handle = pipe.handle0; |
| 24 output.version = 0; |
| 25 |
| 26 return new mojo.InterfaceRequest(pipe.handle1); |
| 27 } |
| 28 |
| 10 var pipe = Mojo.createMessagePipe(); | 29 var pipe = Mojo.createMessagePipe(); |
| 11 interfacePtr.ptr.bind(new mojo.InterfacePtrInfo(pipe.handle0, 0)); | 30 output.ptr.bind(new mojo.InterfacePtrInfo(pipe.handle0, 0)); |
| 12 return new mojo.InterfaceRequest(pipe.handle1); | 31 return new mojo.InterfaceRequest(pipe.handle1); |
| 13 } | 32 } |
| 14 | 33 |
| 15 // --------------------------------------------------------------------------- | 34 // --------------------------------------------------------------------------- |
| 16 | 35 |
| 17 // Operations used to setup/configure an interface pointer. Exposed as the | 36 // Operations used to setup/configure an interface pointer. Exposed as the |
| 18 // |ptr| field of generated interface pointer classes. | 37 // |ptr| field of generated interface pointer classes. |
| 19 // |ptrInfoOrHandle| could be omitted and passed into bind() later. | 38 // |ptrInfoOrHandle| could be omitted and passed into bind() later. |
| 20 function InterfacePtrController(interfaceType, ptrInfoOrHandle) { | 39 function InterfacePtrController(interfaceType, ptrInfoOrHandle) { |
| 21 this.version = 0; | 40 this.version = 0; |
| 22 | 41 |
| 23 this.interfaceType_ = interfaceType; | 42 this.interfaceType_ = interfaceType; |
| 24 this.router_ = null; | 43 this.router_ = null; |
| 44 this.interfaceEndpointClient_ = null; |
| 25 this.proxy_ = null; | 45 this.proxy_ = null; |
| 26 | 46 |
| 27 // |router_| is lazily initialized. |handle_| is valid between bind() and | 47 // |router_| and |interfaceEndpointClient_| are lazily initialized. |
| 28 // the initialization of |router_|. | 48 // |handle_| is valid between bind() and |
| 49 // the initialization of |router_| and |interfaceEndpointClient_|. |
| 29 this.handle_ = null; | 50 this.handle_ = null; |
| 30 this.controlMessageProxy_ = null; | |
| 31 | 51 |
| 32 if (ptrInfoOrHandle) | 52 if (ptrInfoOrHandle) |
| 33 this.bind(ptrInfoOrHandle); | 53 this.bind(ptrInfoOrHandle); |
| 34 } | 54 } |
| 35 | 55 |
| 36 InterfacePtrController.prototype.bind = function(ptrInfoOrHandle) { | 56 InterfacePtrController.prototype.bind = function(ptrInfoOrHandle) { |
| 37 this.reset(); | 57 this.reset(); |
| 38 | 58 |
| 39 if (ptrInfoOrHandle instanceof mojo.InterfacePtrInfo) { | 59 if (ptrInfoOrHandle instanceof mojo.InterfacePtrInfo) { |
| 40 this.version = ptrInfoOrHandle.version; | 60 this.version = ptrInfoOrHandle.version; |
| 41 this.handle_ = ptrInfoOrHandle.handle; | 61 this.handle_ = ptrInfoOrHandle.handle; |
| 42 } else { | 62 } else { |
| 43 this.handle_ = ptrInfoOrHandle; | 63 this.handle_ = ptrInfoOrHandle; |
| 44 } | 64 } |
| 45 }; | 65 }; |
| 46 | 66 |
| 47 InterfacePtrController.prototype.isBound = function() { | 67 InterfacePtrController.prototype.isBound = function() { |
| 48 return this.router_ !== null || this.handle_ !== null; | 68 return this.interfaceEndpointClient_ !== null || this.handle_ !== null; |
| 49 }; | 69 }; |
| 50 | 70 |
| 51 // Although users could just discard the object, reset() closes the pipe | 71 // Although users could just discard the object, reset() closes the pipe |
| 52 // immediately. | 72 // immediately. |
| 53 InterfacePtrController.prototype.reset = function() { | 73 InterfacePtrController.prototype.reset = function() { |
| 54 this.version = 0; | 74 this.version = 0; |
| 75 if (this.interfaceEndpointClient_) { |
| 76 this.interfaceEndpointClient_.close(); |
| 77 this.interfaceEndpointClient_ = null; |
| 78 } |
| 55 if (this.router_) { | 79 if (this.router_) { |
| 56 this.router_.close(); | 80 this.router_.close(); |
| 57 this.router_ = null; | 81 this.router_ = null; |
| 58 | 82 |
| 59 this.proxy_ = null; | 83 this.proxy_ = null; |
| 60 } | 84 } |
| 61 if (this.handle_) { | 85 if (this.handle_) { |
| 62 this.handle_.close(); | 86 this.handle_.close(); |
| 63 this.handle_ = null; | 87 this.handle_ = null; |
| 64 } | 88 } |
| 65 }; | 89 }; |
| 66 | 90 |
| 67 InterfacePtrController.prototype.setConnectionErrorHandler | 91 InterfacePtrController.prototype.resetWithReason = function(reason) { |
| 68 = function(callback) { | 92 if (this.isBound()) { |
| 93 this.configureProxyIfNecessary_(); |
| 94 this.interfaceEndpointClient_.close(reason); |
| 95 this.interfaceEndpointClient_ = null; |
| 96 } |
| 97 this.reset(); |
| 98 }; |
| 99 |
| 100 InterfacePtrController.prototype.setConnectionErrorHandler = function( |
| 101 callback) { |
| 69 if (!this.isBound()) | 102 if (!this.isBound()) |
| 70 throw new Error("Cannot set connection error handler if not bound."); | 103 throw new Error("Cannot set connection error handler if not bound."); |
| 71 | 104 |
| 72 this.configureProxyIfNecessary_(); | 105 this.configureProxyIfNecessary_(); |
| 73 this.router_.setErrorHandler(callback); | 106 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); |
| 74 }; | 107 }; |
| 75 | 108 |
| 76 InterfacePtrController.prototype.passInterface = function() { | 109 InterfacePtrController.prototype.passInterface = function() { |
| 77 var result; | 110 var result; |
| 78 if (this.router_) { | 111 if (this.router_) { |
| 79 // TODO(yzshen): Fix Router interface to support extracting handle. | 112 // TODO(yzshen): Fix Router interface to support extracting handle. |
| 80 result = new mojo.InterfacePtrInfo( | 113 result = new mojo.InterfacePtrInfo( |
| 81 this.router_.connector_.handle_, this.version); | 114 this.router_.connector_.handle_, this.version); |
| 82 this.router_.connector_.handle_ = null; | 115 this.router_.connector_.handle_ = null; |
| 83 } else { | 116 } else { |
| 84 // This also handles the case when this object is not bound. | 117 // This also handles the case when this object is not bound. |
| 85 result = new mojo.InterfacePtrInfo(this.handle_, this.version); | 118 result = new mojo.InterfacePtrInfo(this.handle_, this.version); |
| 86 this.handle_ = null; | 119 this.handle_ = null; |
| 87 } | 120 } |
| 88 | 121 |
| 89 this.reset(); | 122 this.reset(); |
| 90 return result; | 123 return result; |
| 91 }; | 124 }; |
| 92 | 125 |
| 93 InterfacePtrController.prototype.getProxy = function() { | 126 InterfacePtrController.prototype.getProxy = function() { |
| 94 this.configureProxyIfNecessary_(); | 127 this.configureProxyIfNecessary_(); |
| 95 return this.proxy_; | 128 return this.proxy_; |
| 96 }; | 129 }; |
| 97 | 130 |
| 98 InterfacePtrController.prototype.enableTestingMode = function() { | 131 InterfacePtrController.prototype.waitForNextMessageForTesting = function() { |
| 99 this.configureProxyIfNecessary_(); | 132 this.configureProxyIfNecessary_(); |
| 100 return this.router_.enableTestingMode(); | 133 this.router_.waitForNextMessageForTesting(); |
| 101 }; | 134 }; |
| 102 | 135 |
| 103 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { | 136 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { |
| 104 if (!this.handle_) | 137 if (!this.handle_) |
| 105 return; | 138 return; |
| 106 | 139 |
| 107 this.router_ = new internal.Router(this.handle_); | 140 this.router_ = new internal.Router(this.handle_, true); |
| 108 this.handle_ = null; | 141 this.handle_ = null; |
| 109 this.router_ .setPayloadValidators([this.interfaceType_.validateResponse]); | |
| 110 | 142 |
| 111 this.controlMessageProxy_ = new internal.ControlMessageProxy(this.router_); | 143 this.interfaceEndpointClient_ = new internal.InterfaceEndpointClient( |
| 144 this.router_.createLocalEndpointHandle(internal.kMasterInterfaceId)); |
| 112 | 145 |
| 113 this.proxy_ = new this.interfaceType_.proxyClass(this.router_); | 146 this.interfaceEndpointClient_ .setPayloadValidators([ |
| 147 this.interfaceType_.validateResponse]); |
| 148 this.proxy_ = new this.interfaceType_.proxyClass( |
| 149 this.interfaceEndpointClient_); |
| 114 }; | 150 }; |
| 115 | 151 |
| 116 InterfacePtrController.prototype.queryVersion = function() { | 152 InterfacePtrController.prototype.queryVersion = function() { |
| 117 function onQueryVersion(version) { | 153 function onQueryVersion(version) { |
| 118 this.version = version; | 154 this.version = version; |
| 119 return version; | 155 return version; |
| 120 } | 156 } |
| 121 | 157 |
| 122 this.configureProxyIfNecessary_(); | 158 this.configureProxyIfNecessary_(); |
| 123 return this.controlMessageProxy_.queryVersion().then( | 159 return this.interfaceEndpointClient_.queryVersion().then( |
| 124 onQueryVersion.bind(this)); | 160 onQueryVersion.bind(this)); |
| 125 }; | 161 }; |
| 126 | 162 |
| 127 InterfacePtrController.prototype.requireVersion = function(version) { | 163 InterfacePtrController.prototype.requireVersion = function(version) { |
| 128 this.configureProxyIfNecessary_(); | 164 this.configureProxyIfNecessary_(); |
| 129 | 165 |
| 130 if (this.version >= version) { | 166 if (this.version >= version) { |
| 131 return; | 167 return; |
| 132 } | 168 } |
| 133 this.version = version; | 169 this.version = version; |
| 134 this.controlMessageProxy_.requireVersion(version); | 170 this.interfaceEndpointClient_.requireVersion(version); |
| 135 }; | 171 }; |
| 136 | 172 |
| 137 // --------------------------------------------------------------------------- | 173 // --------------------------------------------------------------------------- |
| 138 | 174 |
| 139 // |request| could be omitted and passed into bind() later. | 175 // |request| could be omitted and passed into bind() later. |
| 140 // | 176 // |
| 141 // Example: | 177 // Example: |
| 142 // | 178 // |
| 143 // // FooImpl implements mojom.Foo. | 179 // // FooImpl implements mojom.Foo. |
| 144 // function FooImpl() { ... } | 180 // function FooImpl() { ... } |
| 145 // FooImpl.prototype.fooMethod1 = function() { ... } | 181 // FooImpl.prototype.fooMethod1 = function() { ... } |
| 146 // FooImpl.prototype.fooMethod2 = function() { ... } | 182 // FooImpl.prototype.fooMethod2 = function() { ... } |
| 147 // | 183 // |
| 148 // var fooPtr = new mojom.FooPtr(); | 184 // var fooPtr = new mojom.FooPtr(); |
| 149 // var request = makeRequest(fooPtr); | 185 // var request = makeRequest(fooPtr); |
| 150 // var binding = new Binding(mojom.Foo, new FooImpl(), request); | 186 // var binding = new Binding(mojom.Foo, new FooImpl(), request); |
| 151 // fooPtr.fooMethod1(); | 187 // fooPtr.fooMethod1(); |
| 152 function Binding(interfaceType, impl, requestOrHandle) { | 188 function Binding(interfaceType, impl, requestOrHandle) { |
| 153 this.interfaceType_ = interfaceType; | 189 this.interfaceType_ = interfaceType; |
| 154 this.impl_ = impl; | 190 this.impl_ = impl; |
| 155 this.router_ = null; | 191 this.router_ = null; |
| 192 this.interfaceEndpointClient_ = null; |
| 156 this.stub_ = null; | 193 this.stub_ = null; |
| 157 | 194 |
| 158 if (requestOrHandle) | 195 if (requestOrHandle) |
| 159 this.bind(requestOrHandle); | 196 this.bind(requestOrHandle); |
| 160 } | 197 } |
| 161 | 198 |
| 162 Binding.prototype.isBound = function() { | 199 Binding.prototype.isBound = function() { |
| 163 return this.router_ !== null; | 200 return this.router_ !== null; |
| 164 }; | 201 }; |
| 165 | 202 |
| 166 Binding.prototype.createInterfacePtrAndBind = function() { | 203 Binding.prototype.createInterfacePtrAndBind = function() { |
| 167 var ptr = new this.interfaceType_.ptrClass(); | 204 var ptr = new this.interfaceType_.ptrClass(); |
| 168 // TODO(yzshen): Set the version of the interface pointer. | 205 // TODO(yzshen): Set the version of the interface pointer. |
| 169 this.bind(makeRequest(ptr)); | 206 this.bind(makeRequest(ptr)); |
| 170 return ptr; | 207 return ptr; |
| 171 } | 208 }; |
| 172 | 209 |
| 173 Binding.prototype.bind = function(requestOrHandle) { | 210 Binding.prototype.bind = function(requestOrHandle) { |
| 174 this.close(); | 211 this.close(); |
| 175 | 212 |
| 176 var handle = requestOrHandle instanceof mojo.InterfaceRequest ? | 213 var handle = requestOrHandle instanceof mojo.InterfaceRequest ? |
| 177 requestOrHandle.handle : requestOrHandle; | 214 requestOrHandle.handle : requestOrHandle; |
| 178 if (!(handle instanceof MojoHandle)) | 215 if (!(handle instanceof MojoHandle)) |
| 179 return; | 216 return; |
| 180 | 217 |
| 218 this.router_ = new internal.Router(handle); |
| 219 |
| 181 this.stub_ = new this.interfaceType_.stubClass(this.impl_); | 220 this.stub_ = new this.interfaceType_.stubClass(this.impl_); |
| 182 this.router_ = new internal.Router(handle, this.interfaceType_.kVersion); | 221 this.interfaceEndpointClient_ = new internal.InterfaceEndpointClient( |
| 183 this.router_.setIncomingReceiver(this.stub_); | 222 this.router_.createLocalEndpointHandle(internal.kMasterInterfaceId), |
| 184 this.router_ .setPayloadValidators([this.interfaceType_.validateRequest]); | 223 this.stub_, this.interfaceType_.kVersion); |
| 224 |
| 225 this.interfaceEndpointClient_ .setPayloadValidators([ |
| 226 this.interfaceType_.validateRequest]); |
| 185 }; | 227 }; |
| 186 | 228 |
| 187 Binding.prototype.close = function() { | 229 Binding.prototype.close = function() { |
| 188 if (!this.isBound()) | 230 if (!this.isBound()) |
| 189 return; | 231 return; |
| 190 | 232 |
| 233 if (this.interfaceEndpointClient_) { |
| 234 this.interfaceEndpointClient_.close(); |
| 235 this.interfaceEndpointClient_ = null; |
| 236 } |
| 237 |
| 191 this.router_.close(); | 238 this.router_.close(); |
| 192 this.router_ = null; | 239 this.router_ = null; |
| 193 this.stub_ = null; | 240 this.stub_ = null; |
| 194 }; | 241 }; |
| 195 | 242 |
| 196 Binding.prototype.setConnectionErrorHandler | 243 Binding.prototype.closeWithReason = function(reason) { |
| 197 = function(callback) { | 244 if (this.interfaceEndpointClient_) { |
| 198 if (!this.isBound()) | 245 this.interfaceEndpointClient_.close(reason); |
| 246 this.interfaceEndpointClient_ = null; |
| 247 } |
| 248 this.close(); |
| 249 }; |
| 250 |
| 251 Binding.prototype.setConnectionErrorHandler = function(callback) { |
| 252 if (!this.isBound()) { |
| 199 throw new Error("Cannot set connection error handler if not bound."); | 253 throw new Error("Cannot set connection error handler if not bound."); |
| 200 this.router_.setErrorHandler(callback); | 254 } |
| 255 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); |
| 201 }; | 256 }; |
| 202 | 257 |
| 203 Binding.prototype.unbind = function() { | 258 Binding.prototype.unbind = function() { |
| 204 if (!this.isBound()) | 259 if (!this.isBound()) |
| 205 return new mojo.InterfaceRequest(null); | 260 return new mojo.InterfaceRequest(null); |
| 206 | 261 |
| 207 var result = new mojo.InterfaceRequest(this.router_.connector_.handle_); | 262 var result = new mojo.InterfaceRequest(this.router_.connector_.handle_); |
| 208 this.router_.connector_.handle_ = null; | 263 this.router_.connector_.handle_ = null; |
| 209 this.close(); | 264 this.close(); |
| 210 return result; | 265 return result; |
| 211 }; | 266 }; |
| 212 | 267 |
| 213 Binding.prototype.enableTestingMode = function() { | 268 Binding.prototype.waitForNextMessageForTesting = function() { |
| 214 return this.router_.enableTestingMode(); | 269 this.router_.waitForNextMessageForTesting(); |
| 215 }; | 270 }; |
| 216 | 271 |
| 217 // --------------------------------------------------------------------------- | 272 // --------------------------------------------------------------------------- |
| 218 | 273 |
| 219 function BindingSetEntry(bindingSet, interfaceType, impl, requestOrHandle, | 274 function BindingSetEntry(bindingSet, interfaceType, bindingType, impl, |
| 220 bindingId) { | 275 requestOrHandle, bindingId) { |
| 221 this.bindingSet_ = bindingSet; | 276 this.bindingSet_ = bindingSet; |
| 222 this.bindingId_ = bindingId; | 277 this.bindingId_ = bindingId; |
| 223 this.binding_ = new Binding(interfaceType, impl, requestOrHandle); | 278 this.binding_ = new bindingType(interfaceType, impl, |
| 279 requestOrHandle); |
| 224 | 280 |
| 225 this.binding_.setConnectionErrorHandler(function() { | 281 this.binding_.setConnectionErrorHandler(function(reason) { |
| 226 this.bindingSet_.onConnectionError(bindingId); | 282 this.bindingSet_.onConnectionError(bindingId, reason); |
| 227 }.bind(this)); | 283 }.bind(this)); |
| 228 } | 284 } |
| 229 | 285 |
| 230 BindingSetEntry.prototype.close = function() { | 286 BindingSetEntry.prototype.close = function() { |
| 231 this.binding_.close(); | 287 this.binding_.close(); |
| 232 }; | 288 }; |
| 233 | 289 |
| 234 function BindingSet(interfaceType) { | 290 function BindingSet(interfaceType) { |
| 235 this.interfaceType_ = interfaceType; | 291 this.interfaceType_ = interfaceType; |
| 236 this.nextBindingId_ = 0; | 292 this.nextBindingId_ = 0; |
| 237 this.bindings_ = new Map(); | 293 this.bindings_ = new Map(); |
| 238 this.errorHandler_ = null; | 294 this.errorHandler_ = null; |
| 295 this.bindingType_ = Binding; |
| 239 } | 296 } |
| 240 | 297 |
| 241 BindingSet.prototype.isEmpty = function() { | 298 BindingSet.prototype.isEmpty = function() { |
| 242 return this.bindings_.size == 0; | 299 return this.bindings_.size == 0; |
| 243 }; | 300 }; |
| 244 | 301 |
| 245 BindingSet.prototype.addBinding = function(impl, requestOrHandle) { | 302 BindingSet.prototype.addBinding = function(impl, requestOrHandle) { |
| 246 this.bindings_.set( | 303 this.bindings_.set( |
| 247 this.nextBindingId_, | 304 this.nextBindingId_, |
| 248 new BindingSetEntry(this, this.interfaceType_, impl, requestOrHandle, | 305 new BindingSetEntry(this, this.interfaceType_, this.bindingType_, impl, |
| 249 this.nextBindingId_)); | 306 requestOrHandle, this.nextBindingId_)); |
| 250 ++this.nextBindingId_; | 307 ++this.nextBindingId_; |
| 251 }; | 308 }; |
| 252 | 309 |
| 253 BindingSet.prototype.closeAllBindings = function() { | 310 BindingSet.prototype.closeAllBindings = function() { |
| 254 for (var entry of this.bindings_.values()) | 311 for (var entry of this.bindings_.values()) |
| 255 entry.close(); | 312 entry.close(); |
| 256 this.bindings_.clear(); | 313 this.bindings_.clear(); |
| 257 }; | 314 }; |
| 258 | 315 |
| 259 BindingSet.prototype.setConnectionErrorHandler = function(callback) { | 316 BindingSet.prototype.setConnectionErrorHandler = function(callback) { |
| 260 this.errorHandler_ = callback; | 317 this.errorHandler_ = callback; |
| 261 }; | 318 }; |
| 262 | 319 |
| 263 BindingSet.prototype.onConnectionError = function(bindingId) { | 320 BindingSet.prototype.onConnectionError = function(bindingId, reason) { |
| 264 this.bindings_.delete(bindingId); | 321 this.bindings_.delete(bindingId); |
| 265 | 322 |
| 266 if (this.errorHandler_) | 323 if (this.errorHandler_) |
| 267 this.errorHandler_(); | 324 this.errorHandler_(reason); |
| 268 }; | 325 }; |
| 269 | 326 |
| 327 // --------------------------------------------------------------------------- |
| 328 |
| 329 // Operations used to setup/configure an associated interface pointer. |
| 330 // Exposed as |ptr| field of generated associated interface pointer classes. |
| 331 // |associatedPtrInfo| could be omitted and passed into bind() later. |
| 332 // |
| 333 // Example: |
| 334 // // IntegerSenderImpl implements mojom.IntegerSender |
| 335 // function IntegerSenderImpl() { ... } |
| 336 // IntegerSenderImpl.prototype.echo = function() { ... } |
| 337 // |
| 338 // // IntegerSenderConnectionImpl implements mojom.IntegerSenderConnection |
| 339 // function IntegerSenderConnectionImpl() { |
| 340 // this.senderBinding_ = null; |
| 341 // } |
| 342 // IntegerSenderConnectionImpl.prototype.getSender = function( |
| 343 // associatedRequest) { |
| 344 // this.senderBinding_ = new AssociatedBinding(mojom.IntegerSender, |
| 345 // new IntegerSenderImpl(), |
| 346 // associatedRequest); |
| 347 // } |
| 348 // |
| 349 // var integerSenderConnection = new mojom.IntegerSenderConnectionPtr(); |
| 350 // var integerSenderConnectionBinding = new Binding( |
| 351 // mojom.IntegerSenderConnection, |
| 352 // new IntegerSenderConnectionImpl(), |
| 353 // mojo.makeRequest(integerSenderConnection)); |
| 354 // |
| 355 // // A locally-created associated interface pointer can only be used to |
| 356 // // make calls when the corresponding associated request is sent over |
| 357 // // another interface (either the master interface or another |
| 358 // // associated interface). |
| 359 // var associatedInterfacePtrInfo = new AssociatedInterfacePtrInfo(); |
| 360 // var associatedRequest = makeRequest(interfacePtrInfo); |
| 361 // |
| 362 // integerSenderConnection.getSender(associatedRequest); |
| 363 // |
| 364 // // Create an associated interface and bind the associated handle. |
| 365 // var integerSender = new mojom.AssociatedIntegerSenderPtr(); |
| 366 // integerSender.ptr.bind(associatedInterfacePtrInfo); |
| 367 // integerSender.echo(); |
| 368 |
| 369 function AssociatedInterfacePtrController(interfaceType, associatedPtrInfo) { |
| 370 this.version = 0; |
| 371 |
| 372 this.interfaceType_ = interfaceType; |
| 373 this.interfaceEndpointClient_ = null; |
| 374 this.proxy_ = null; |
| 375 |
| 376 if (associatedPtrInfo) { |
| 377 this.bind(associatedPtrInfo); |
| 378 } |
| 379 } |
| 380 |
| 381 AssociatedInterfacePtrController.prototype.bind = function( |
| 382 associatedPtrInfo) { |
| 383 this.reset(); |
| 384 this.version = associatedPtrInfo.version; |
| 385 |
| 386 this.interfaceEndpointClient_ = new internal.InterfaceEndpointClient( |
| 387 associatedPtrInfo.interfaceEndpointHandle); |
| 388 |
| 389 this.interfaceEndpointClient_ .setPayloadValidators([ |
| 390 this.interfaceType_.validateResponse]); |
| 391 this.proxy_ = new this.interfaceType_.proxyClass( |
| 392 this.interfaceEndpointClient_); |
| 393 }; |
| 394 |
| 395 AssociatedInterfacePtrController.prototype.isBound = function() { |
| 396 return this.interfaceEndpointClient_ !== null; |
| 397 }; |
| 398 |
| 399 AssociatedInterfacePtrController.prototype.reset = function() { |
| 400 this.version = 0; |
| 401 if (this.interfaceEndpointClient_) { |
| 402 this.interfaceEndpointClient_.close(); |
| 403 this.interfaceEndpointClient_ = null; |
| 404 } |
| 405 if (this.proxy_) { |
| 406 this.proxy_ = null; |
| 407 } |
| 408 }; |
| 409 |
| 410 AssociatedInterfacePtrController.prototype.resetWithReason = function( |
| 411 reason) { |
| 412 if (this.isBound()) { |
| 413 this.interfaceEndpointClient_.close(reason); |
| 414 this.interfaceEndpointClient_ = null; |
| 415 } |
| 416 this.reset(); |
| 417 }; |
| 418 |
| 419 // Indicates whether an error has been encountered. If true, method calls |
| 420 // on this interface will be dropped (and may already have been dropped). |
| 421 AssociatedInterfacePtrController.prototype.getEncounteredError = function() { |
| 422 return this.interfaceEndpointClient_ ? |
| 423 this.interfaceEndpointClient_.getEncounteredError() : false; |
| 424 }; |
| 425 |
| 426 AssociatedInterfacePtrController.prototype.setConnectionErrorHandler = |
| 427 function(callback) { |
| 428 if (!this.isBound()) { |
| 429 throw new Error("Cannot set connection error handler if not bound."); |
| 430 } |
| 431 |
| 432 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); |
| 433 }; |
| 434 |
| 435 AssociatedInterfacePtrController.prototype.passInterface = function() { |
| 436 if (!this.isBound()) { |
| 437 return new mojo.AssociatedInterfacePtrInfo(null); |
| 438 } |
| 439 |
| 440 var result = new mojo.AssociatedInterfacePtrInfo( |
| 441 this.interfaceEndpointClient_.passHandle(), this.version); |
| 442 this.reset(); |
| 443 return result; |
| 444 }; |
| 445 |
| 446 AssociatedInterfacePtrController.prototype.getProxy = function() { |
| 447 return this.proxy_; |
| 448 }; |
| 449 |
| 450 AssociatedInterfacePtrController.prototype.queryVersion = function() { |
| 451 function onQueryVersion(version) { |
| 452 this.version = version; |
| 453 return version; |
| 454 } |
| 455 |
| 456 return this.interfaceEndpointClient_.queryVersion().then( |
| 457 onQueryVersion.bind(this)); |
| 458 }; |
| 459 |
| 460 AssociatedInterfacePtrController.prototype.requireVersion = function( |
| 461 version) { |
| 462 if (this.version >= version) { |
| 463 return; |
| 464 } |
| 465 this.version = version; |
| 466 this.interfaceEndpointClient_.requireVersion(version); |
| 467 }; |
| 468 |
| 469 // --------------------------------------------------------------------------- |
| 470 |
| 471 // |associatedInterfaceRequest| could be omitted and passed into bind() |
| 472 // later. |
| 473 function AssociatedBinding(interfaceType, impl, associatedInterfaceRequest) { |
| 474 this.interfaceType_ = interfaceType; |
| 475 this.impl_ = impl; |
| 476 this.interfaceEndpointClient_ = null; |
| 477 this.stub_ = null; |
| 478 |
| 479 if (associatedInterfaceRequest) { |
| 480 this.bind(associatedInterfaceRequest); |
| 481 } |
| 482 } |
| 483 |
| 484 AssociatedBinding.prototype.isBound = function() { |
| 485 return this.interfaceEndpointClient_ !== null; |
| 486 }; |
| 487 |
| 488 AssociatedBinding.prototype.bind = function(associatedInterfaceRequest) { |
| 489 this.close(); |
| 490 |
| 491 this.stub_ = new this.interfaceType_.stubClass(this.impl_); |
| 492 this.interfaceEndpointClient_ = new internal.InterfaceEndpointClient( |
| 493 associatedInterfaceRequest.interfaceEndpointHandle, this.stub_, |
| 494 this.interfaceType_.kVersion); |
| 495 |
| 496 this.interfaceEndpointClient_ .setPayloadValidators([ |
| 497 this.interfaceType_.validateRequest]); |
| 498 }; |
| 499 |
| 500 |
| 501 AssociatedBinding.prototype.close = function() { |
| 502 if (!this.isBound()) { |
| 503 return; |
| 504 } |
| 505 |
| 506 if (this.interfaceEndpointClient_) { |
| 507 this.interfaceEndpointClient_.close(); |
| 508 this.interfaceEndpointClient_ = null; |
| 509 } |
| 510 |
| 511 this.stub_ = null; |
| 512 }; |
| 513 |
| 514 AssociatedBinding.prototype.closeWithReason = function(reason) { |
| 515 if (this.interfaceEndpointClient_) { |
| 516 this.interfaceEndpointClient_.close(reason); |
| 517 this.interfaceEndpointClient_ = null; |
| 518 } |
| 519 this.close(); |
| 520 }; |
| 521 |
| 522 AssociatedBinding.prototype.setConnectionErrorHandler = function(callback) { |
| 523 if (!this.isBound()) { |
| 524 throw new Error("Cannot set connection error handler if not bound."); |
| 525 } |
| 526 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); |
| 527 }; |
| 528 |
| 529 AssociatedBinding.prototype.unbind = function() { |
| 530 if (!this.isBound()) { |
| 531 return new mojo.AssociatedInterfaceRequest(null); |
| 532 } |
| 533 |
| 534 var result = new mojo.AssociatedInterfaceRequest( |
| 535 this.interfaceEndpointClient_.passHandle()); |
| 536 this.close(); |
| 537 return result; |
| 538 }; |
| 539 |
| 540 // --------------------------------------------------------------------------- |
| 541 |
| 542 function AssociatedBindingSet(interfaceType) { |
| 543 mojo.BindingSet.call(this, interfaceType); |
| 544 this.bindingType_ = AssociatedBinding; |
| 545 } |
| 546 |
| 547 AssociatedBindingSet.prototype = Object.create(BindingSet.prototype); |
| 548 AssociatedBindingSet.prototype.constructor = AssociatedBindingSet; |
| 270 | 549 |
| 271 mojo.makeRequest = makeRequest; | 550 mojo.makeRequest = makeRequest; |
| 551 mojo.AssociatedInterfacePtrController = AssociatedInterfacePtrController; |
| 552 mojo.AssociatedBinding = AssociatedBinding; |
| 553 mojo.AssociatedBindingSet = AssociatedBindingSet; |
| 272 mojo.Binding = Binding; | 554 mojo.Binding = Binding; |
| 273 mojo.BindingSet = BindingSet; | 555 mojo.BindingSet = BindingSet; |
| 274 mojo.InterfacePtrController = InterfacePtrController; | 556 mojo.InterfacePtrController = InterfacePtrController; |
| 275 })(); | 557 })(); |
| OLD | NEW |