| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 define("mojo/public/js/bindings", [ |
| 6 var internal = mojoBindings.internal; | 6 "mojo/public/js/core", |
| 7 "mojo/public/js/lib/control_message_proxy", |
| 8 "mojo/public/js/interface_types", |
| 9 "mojo/public/js/router", |
| 10 ], function(core, controlMessageProxy, types, router) { |
| 11 |
| 7 // --------------------------------------------------------------------------- | 12 // --------------------------------------------------------------------------- |
| 8 | 13 |
| 9 function makeRequest(interfacePtr) { | 14 function makeRequest(interfacePtr) { |
| 10 var pipe = Mojo.createMessagePipe(); | 15 var pipe = core.createMessagePipe(); |
| 11 interfacePtr.ptr.bind(new mojoBindings.InterfacePtrInfo(pipe.handle0, 0)); | 16 interfacePtr.ptr.bind(new types.InterfacePtrInfo(pipe.handle0, 0)); |
| 12 return new mojoBindings.InterfaceRequest(pipe.handle1); | 17 return new types.InterfaceRequest(pipe.handle1); |
| 13 } | 18 } |
| 14 | 19 |
| 15 // --------------------------------------------------------------------------- | 20 // --------------------------------------------------------------------------- |
| 16 | 21 |
| 17 // Operations used to setup/configure an interface pointer. Exposed as the | 22 // Operations used to setup/configure an interface pointer. Exposed as the |
| 18 // |ptr| field of generated interface pointer classes. | 23 // |ptr| field of generated interface pointer classes. |
| 19 // |ptrInfoOrHandle| could be omitted and passed into bind() later. | 24 // |ptrInfoOrHandle| could be omitted and passed into bind() later. |
| 20 function InterfacePtrController(interfaceType, ptrInfoOrHandle) { | 25 function InterfacePtrController(interfaceType, ptrInfoOrHandle) { |
| 21 this.version = 0; | 26 this.version = 0; |
| 22 | 27 |
| 23 this.interfaceType_ = interfaceType; | 28 this.interfaceType_ = interfaceType; |
| 24 this.router_ = null; | 29 this.router_ = null; |
| 25 this.proxy_ = null; | 30 this.proxy_ = null; |
| 26 | 31 |
| 27 // |router_| is lazily initialized. |handle_| is valid between bind() and | 32 // |router_| is lazily initialized. |handle_| is valid between bind() and |
| 28 // the initialization of |router_|. | 33 // the initialization of |router_|. |
| 29 this.handle_ = null; | 34 this.handle_ = null; |
| 30 this.controlMessageProxy_ = null; | 35 this.controlMessageProxy_ = null; |
| 31 | 36 |
| 32 if (ptrInfoOrHandle) | 37 if (ptrInfoOrHandle) |
| 33 this.bind(ptrInfoOrHandle); | 38 this.bind(ptrInfoOrHandle); |
| 34 } | 39 } |
| 35 | 40 |
| 36 InterfacePtrController.prototype.bind = function(ptrInfoOrHandle) { | 41 InterfacePtrController.prototype.bind = function(ptrInfoOrHandle) { |
| 37 this.reset(); | 42 this.reset(); |
| 38 | 43 |
| 39 if (ptrInfoOrHandle instanceof mojoBindings.InterfacePtrInfo) { | 44 if (ptrInfoOrHandle instanceof types.InterfacePtrInfo) { |
| 40 this.version = ptrInfoOrHandle.version; | 45 this.version = ptrInfoOrHandle.version; |
| 41 this.handle_ = ptrInfoOrHandle.handle; | 46 this.handle_ = ptrInfoOrHandle.handle; |
| 42 } else { | 47 } else { |
| 43 this.handle_ = ptrInfoOrHandle; | 48 this.handle_ = ptrInfoOrHandle; |
| 44 } | 49 } |
| 45 }; | 50 }; |
| 46 | 51 |
| 47 InterfacePtrController.prototype.isBound = function() { | 52 InterfacePtrController.prototype.isBound = function() { |
| 48 return this.router_ !== null || this.handle_ !== null; | 53 return this.router_ !== null || this.handle_ !== null; |
| 49 }; | 54 }; |
| 50 | 55 |
| 51 // Although users could just discard the object, reset() closes the pipe | 56 // Although users could just discard the object, reset() closes the pipe |
| 52 // immediately. | 57 // immediately. |
| 53 InterfacePtrController.prototype.reset = function() { | 58 InterfacePtrController.prototype.reset = function() { |
| 54 this.version = 0; | 59 this.version = 0; |
| 55 if (this.router_) { | 60 if (this.router_) { |
| 56 this.router_.close(); | 61 this.router_.close(); |
| 57 this.router_ = null; | 62 this.router_ = null; |
| 58 | 63 |
| 59 this.proxy_ = null; | 64 this.proxy_ = null; |
| 60 } | 65 } |
| 61 if (this.handle_) { | 66 if (this.handle_) { |
| 62 this.handle_.close(); | 67 core.close(this.handle_); |
| 63 this.handle_ = null; | 68 this.handle_ = null; |
| 64 } | 69 } |
| 65 }; | 70 }; |
| 66 | 71 |
| 67 InterfacePtrController.prototype.setConnectionErrorHandler | 72 InterfacePtrController.prototype.setConnectionErrorHandler |
| 68 = function(callback) { | 73 = function(callback) { |
| 69 if (!this.isBound()) | 74 if (!this.isBound()) |
| 70 throw new Error("Cannot set connection error handler if not bound."); | 75 throw new Error("Cannot set connection error handler if not bound."); |
| 71 | 76 |
| 72 this.configureProxyIfNecessary_(); | 77 this.configureProxyIfNecessary_(); |
| 73 this.router_.setErrorHandler(callback); | 78 this.router_.setErrorHandler(callback); |
| 74 }; | 79 }; |
| 75 | 80 |
| 76 InterfacePtrController.prototype.passInterface = function() { | 81 InterfacePtrController.prototype.passInterface = function() { |
| 77 var result; | 82 var result; |
| 78 if (this.router_) { | 83 if (this.router_) { |
| 79 // TODO(yzshen): Fix Router interface to support extracting handle. | 84 // TODO(yzshen): Fix Router interface to support extracting handle. |
| 80 result = new mojoBindings.InterfacePtrInfo( | 85 result = new types.InterfacePtrInfo( |
| 81 this.router_.connector_.handle_, this.version); | 86 this.router_.connector_.handle_, this.version); |
| 82 this.router_.connector_.handle_ = null; | 87 this.router_.connector_.handle_ = null; |
| 83 } else { | 88 } else { |
| 84 // This also handles the case when this object is not bound. | 89 // This also handles the case when this object is not bound. |
| 85 result = new mojoBindings.InterfacePtrInfo(this.handle_, this.version); | 90 result = new types.InterfacePtrInfo(this.handle_, this.version); |
| 86 this.handle_ = null; | 91 this.handle_ = null; |
| 87 } | 92 } |
| 88 | 93 |
| 89 this.reset(); | 94 this.reset(); |
| 90 return result; | 95 return result; |
| 91 }; | 96 }; |
| 92 | 97 |
| 93 InterfacePtrController.prototype.getProxy = function() { | 98 InterfacePtrController.prototype.getProxy = function() { |
| 94 this.configureProxyIfNecessary_(); | 99 this.configureProxyIfNecessary_(); |
| 95 return this.proxy_; | 100 return this.proxy_; |
| 96 }; | 101 }; |
| 97 | 102 |
| 98 InterfacePtrController.prototype.enableTestingMode = function() { | 103 InterfacePtrController.prototype.enableTestingMode = function() { |
| 99 this.configureProxyIfNecessary_(); | 104 this.configureProxyIfNecessary_(); |
| 100 return this.router_.enableTestingMode(); | 105 return this.router_.enableTestingMode(); |
| 101 }; | 106 }; |
| 102 | 107 |
| 103 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { | 108 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { |
| 104 if (!this.handle_) | 109 if (!this.handle_) |
| 105 return; | 110 return; |
| 106 | 111 |
| 107 this.router_ = new internal.Router(this.handle_); | 112 this.router_ = new router.Router(this.handle_); |
| 108 this.handle_ = null; | 113 this.handle_ = null; |
| 109 this.router_ .setPayloadValidators([this.interfaceType_.validateResponse]); | 114 this.router_ .setPayloadValidators([this.interfaceType_.validateResponse]); |
| 110 | 115 |
| 111 this.controlMessageProxy_ = new internal.ControlMessageProxy(this.router_); | 116 this.controlMessageProxy_ = new |
| 117 controlMessageProxy.ControlMessageProxy(this.router_); |
| 112 | 118 |
| 113 this.proxy_ = new this.interfaceType_.proxyClass(this.router_); | 119 this.proxy_ = new this.interfaceType_.proxyClass(this.router_); |
| 114 }; | 120 }; |
| 115 | 121 |
| 116 InterfacePtrController.prototype.queryVersion = function() { | 122 InterfacePtrController.prototype.queryVersion = function() { |
| 117 function onQueryVersion(version) { | 123 function onQueryVersion(version) { |
| 118 this.version = version; | 124 this.version = version; |
| 119 return version; | 125 return version; |
| 120 } | 126 } |
| 121 | 127 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 Binding.prototype.createInterfacePtrAndBind = function() { | 172 Binding.prototype.createInterfacePtrAndBind = function() { |
| 167 var ptr = new this.interfaceType_.ptrClass(); | 173 var ptr = new this.interfaceType_.ptrClass(); |
| 168 // TODO(yzshen): Set the version of the interface pointer. | 174 // TODO(yzshen): Set the version of the interface pointer. |
| 169 this.bind(makeRequest(ptr)); | 175 this.bind(makeRequest(ptr)); |
| 170 return ptr; | 176 return ptr; |
| 171 } | 177 } |
| 172 | 178 |
| 173 Binding.prototype.bind = function(requestOrHandle) { | 179 Binding.prototype.bind = function(requestOrHandle) { |
| 174 this.close(); | 180 this.close(); |
| 175 | 181 |
| 176 var handle = requestOrHandle instanceof mojoBindings.InterfaceRequest ? | 182 var handle = requestOrHandle instanceof types.InterfaceRequest ? |
| 177 requestOrHandle.handle : requestOrHandle; | 183 requestOrHandle.handle : requestOrHandle; |
| 178 if (!(handle instanceof MojoHandle)) | 184 if (!core.isHandle(handle)) |
| 179 return; | 185 return; |
| 180 | 186 |
| 181 this.stub_ = new this.interfaceType_.stubClass(this.impl_); | 187 this.stub_ = new this.interfaceType_.stubClass(this.impl_); |
| 182 this.router_ = new internal.Router(handle, this.interfaceType_.kVersion); | 188 this.router_ = new router.Router(handle, this.interfaceType_.kVersion); |
| 183 this.router_.setIncomingReceiver(this.stub_); | 189 this.router_.setIncomingReceiver(this.stub_); |
| 184 this.router_ .setPayloadValidators([this.interfaceType_.validateRequest]); | 190 this.router_ .setPayloadValidators([this.interfaceType_.validateRequest]); |
| 185 }; | 191 }; |
| 186 | 192 |
| 187 Binding.prototype.close = function() { | 193 Binding.prototype.close = function() { |
| 188 if (!this.isBound()) | 194 if (!this.isBound()) |
| 189 return; | 195 return; |
| 190 | 196 |
| 191 this.router_.close(); | 197 this.router_.close(); |
| 192 this.router_ = null; | 198 this.router_ = null; |
| 193 this.stub_ = null; | 199 this.stub_ = null; |
| 194 }; | 200 }; |
| 195 | 201 |
| 196 Binding.prototype.setConnectionErrorHandler | 202 Binding.prototype.setConnectionErrorHandler |
| 197 = function(callback) { | 203 = function(callback) { |
| 198 if (!this.isBound()) | 204 if (!this.isBound()) |
| 199 throw new Error("Cannot set connection error handler if not bound."); | 205 throw new Error("Cannot set connection error handler if not bound."); |
| 200 this.router_.setErrorHandler(callback); | 206 this.router_.setErrorHandler(callback); |
| 201 }; | 207 }; |
| 202 | 208 |
| 203 Binding.prototype.unbind = function() { | 209 Binding.prototype.unbind = function() { |
| 204 if (!this.isBound()) | 210 if (!this.isBound()) |
| 205 return new mojoBindings.InterfaceRequest(null); | 211 return new types.InterfaceRequest(null); |
| 206 | 212 |
| 207 var result = new mojoBindings.InterfaceRequest( | 213 var result = new types.InterfaceRequest(this.router_.connector_.handle_); |
| 208 this.router_.connector_.handle_); | |
| 209 this.router_.connector_.handle_ = null; | 214 this.router_.connector_.handle_ = null; |
| 210 this.close(); | 215 this.close(); |
| 211 return result; | 216 return result; |
| 212 }; | 217 }; |
| 213 | 218 |
| 214 Binding.prototype.enableTestingMode = function() { | 219 Binding.prototype.enableTestingMode = function() { |
| 215 return this.router_.enableTestingMode(); | 220 return this.router_.enableTestingMode(); |
| 216 }; | 221 }; |
| 217 | 222 |
| 218 // --------------------------------------------------------------------------- | 223 // --------------------------------------------------------------------------- |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 this.errorHandler_ = callback; | 266 this.errorHandler_ = callback; |
| 262 }; | 267 }; |
| 263 | 268 |
| 264 BindingSet.prototype.onConnectionError = function(bindingId) { | 269 BindingSet.prototype.onConnectionError = function(bindingId) { |
| 265 this.bindings_.delete(bindingId); | 270 this.bindings_.delete(bindingId); |
| 266 | 271 |
| 267 if (this.errorHandler_) | 272 if (this.errorHandler_) |
| 268 this.errorHandler_(); | 273 this.errorHandler_(); |
| 269 }; | 274 }; |
| 270 | 275 |
| 276 var exports = {}; |
| 277 exports.InterfacePtrInfo = types.InterfacePtrInfo; |
| 278 exports.InterfaceRequest = types.InterfaceRequest; |
| 279 exports.makeRequest = makeRequest; |
| 280 exports.InterfacePtrController = InterfacePtrController; |
| 281 exports.Binding = Binding; |
| 282 exports.BindingSet = BindingSet; |
| 271 | 283 |
| 272 mojoBindings.makeRequest = makeRequest; | 284 return exports; |
| 273 mojoBindings.Binding = Binding; | 285 }); |
| 274 mojoBindings.BindingSet = BindingSet; | |
| 275 mojoBindings.InterfacePtrController = InterfacePtrController; | |
| 276 })(); | |
| OLD | NEW |