| 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 define("mojo/public/js/bindings", [ | 5 define("mojo/public/js/bindings", [ |
| 6 "mojo/public/js/core", | 6 "mojo/public/js/core", |
| 7 "mojo/public/js/lib/control_message_proxy", |
| 7 "mojo/public/js/interface_types", | 8 "mojo/public/js/interface_types", |
| 8 "mojo/public/js/lib/interface_endpoint_client", | |
| 9 "mojo/public/js/router", | 9 "mojo/public/js/router", |
| 10 ], function(core, types, interfaceEndpointClient, router) { | 10 ], function(core, controlMessageProxy, types, router) { |
| 11 | |
| 12 var InterfaceEndpointClient = interfaceEndpointClient.InterfaceEndpointClient; | |
| 13 | 11 |
| 14 // --------------------------------------------------------------------------- | 12 // --------------------------------------------------------------------------- |
| 15 | 13 |
| 16 function makeRequest(interfacePtr) { | 14 function makeRequest(interfacePtr) { |
| 17 var pipe = core.createMessagePipe(); | 15 var pipe = core.createMessagePipe(); |
| 18 interfacePtr.ptr.bind(new types.InterfacePtrInfo(pipe.handle0, 0)); | 16 interfacePtr.ptr.bind(new types.InterfacePtrInfo(pipe.handle0, 0)); |
| 19 return new types.InterfaceRequest(pipe.handle1); | 17 return new types.InterfaceRequest(pipe.handle1); |
| 20 } | 18 } |
| 21 | 19 |
| 22 // --------------------------------------------------------------------------- | 20 // --------------------------------------------------------------------------- |
| 23 | 21 |
| 24 // Operations used to setup/configure an interface pointer. Exposed as the | 22 // Operations used to setup/configure an interface pointer. Exposed as the |
| 25 // |ptr| field of generated interface pointer classes. | 23 // |ptr| field of generated interface pointer classes. |
| 26 // |ptrInfoOrHandle| could be omitted and passed into bind() later. | 24 // |ptrInfoOrHandle| could be omitted and passed into bind() later. |
| 27 function InterfacePtrController(interfaceType, ptrInfoOrHandle) { | 25 function InterfacePtrController(interfaceType, ptrInfoOrHandle) { |
| 28 this.version = 0; | 26 this.version = 0; |
| 29 | 27 |
| 30 this.interfaceType_ = interfaceType; | 28 this.interfaceType_ = interfaceType; |
| 31 this.router_ = null; | 29 this.router_ = null; |
| 32 this.interfaceEndpointClient_ = null; | |
| 33 this.proxy_ = null; | 30 this.proxy_ = null; |
| 34 | 31 |
| 35 // |router_| and |interfaceEndpointClient_| are lazily initialized. | 32 // |router_| is lazily initialized. |handle_| is valid between bind() and |
| 36 // |handle_| is valid between bind() and | 33 // the initialization of |router_|. |
| 37 // the initialization of |router_| and |interfaceEndpointClient_|. | |
| 38 this.handle_ = null; | 34 this.handle_ = null; |
| 35 this.controlMessageProxy_ = null; |
| 39 | 36 |
| 40 if (ptrInfoOrHandle) | 37 if (ptrInfoOrHandle) |
| 41 this.bind(ptrInfoOrHandle); | 38 this.bind(ptrInfoOrHandle); |
| 42 } | 39 } |
| 43 | 40 |
| 44 InterfacePtrController.prototype.bind = function(ptrInfoOrHandle) { | 41 InterfacePtrController.prototype.bind = function(ptrInfoOrHandle) { |
| 45 this.reset(); | 42 this.reset(); |
| 46 | 43 |
| 47 if (ptrInfoOrHandle instanceof types.InterfacePtrInfo) { | 44 if (ptrInfoOrHandle instanceof types.InterfacePtrInfo) { |
| 48 this.version = ptrInfoOrHandle.version; | 45 this.version = ptrInfoOrHandle.version; |
| 49 this.handle_ = ptrInfoOrHandle.handle; | 46 this.handle_ = ptrInfoOrHandle.handle; |
| 50 } else { | 47 } else { |
| 51 this.handle_ = ptrInfoOrHandle; | 48 this.handle_ = ptrInfoOrHandle; |
| 52 } | 49 } |
| 53 }; | 50 }; |
| 54 | 51 |
| 55 InterfacePtrController.prototype.isBound = function() { | 52 InterfacePtrController.prototype.isBound = function() { |
| 56 return this.router_ !== null || this.handle_ !== null; | 53 return this.router_ !== null || this.handle_ !== null; |
| 57 }; | 54 }; |
| 58 | 55 |
| 59 // Although users could just discard the object, reset() closes the pipe | 56 // Although users could just discard the object, reset() closes the pipe |
| 60 // immediately. | 57 // immediately. |
| 61 InterfacePtrController.prototype.reset = function() { | 58 InterfacePtrController.prototype.reset = function() { |
| 62 this.version = 0; | 59 this.version = 0; |
| 63 if (this.interfaceEndpointClient_) { | |
| 64 this.interfaceEndpointClient_.close(); | |
| 65 this.interfaceEndpointClient_ = null; | |
| 66 } | |
| 67 if (this.router_) { | 60 if (this.router_) { |
| 68 this.router_.close(); | 61 this.router_.close(); |
| 69 this.router_ = null; | 62 this.router_ = null; |
| 70 | 63 |
| 71 this.proxy_ = null; | 64 this.proxy_ = null; |
| 72 } | 65 } |
| 73 if (this.handle_) { | 66 if (this.handle_) { |
| 74 core.close(this.handle_); | 67 core.close(this.handle_); |
| 75 this.handle_ = null; | 68 this.handle_ = null; |
| 76 } | 69 } |
| 77 }; | 70 }; |
| 78 | 71 |
| 79 InterfacePtrController.prototype.resetWithReason = function(reason) { | 72 InterfacePtrController.prototype.setConnectionErrorHandler |
| 80 this.configureProxyIfNecessary_(); | 73 = function(callback) { |
| 81 this.interfaceEndpointClient_.close(reason); | |
| 82 this.interfaceEndpointClient_ = null; | |
| 83 this.reset(); | |
| 84 }; | |
| 85 | |
| 86 InterfacePtrController.prototype.setConnectionErrorHandler = function( | |
| 87 callback) { | |
| 88 if (!this.isBound()) | 74 if (!this.isBound()) |
| 89 throw new Error("Cannot set connection error handler if not bound."); | 75 throw new Error("Cannot set connection error handler if not bound."); |
| 90 | 76 |
| 91 this.configureProxyIfNecessary_(); | 77 this.configureProxyIfNecessary_(); |
| 92 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); | 78 this.router_.setErrorHandler(callback); |
| 93 }; | 79 }; |
| 94 | 80 |
| 95 InterfacePtrController.prototype.passInterface = function() { | 81 InterfacePtrController.prototype.passInterface = function() { |
| 96 var result; | 82 var result; |
| 97 if (this.router_) { | 83 if (this.router_) { |
| 98 // TODO(yzshen): Fix Router interface to support extracting handle. | 84 // TODO(yzshen): Fix Router interface to support extracting handle. |
| 99 result = new types.InterfacePtrInfo( | 85 result = new types.InterfacePtrInfo( |
| 100 this.router_.connector_.handle_, this.version); | 86 this.router_.connector_.handle_, this.version); |
| 101 this.router_.connector_.handle_ = null; | 87 this.router_.connector_.handle_ = null; |
| 102 } else { | 88 } else { |
| 103 // This also handles the case when this object is not bound. | 89 // This also handles the case when this object is not bound. |
| 104 result = new types.InterfacePtrInfo(this.handle_, this.version); | 90 result = new types.InterfacePtrInfo(this.handle_, this.version); |
| 105 this.handle_ = null; | 91 this.handle_ = null; |
| 106 } | 92 } |
| 107 | 93 |
| 108 this.reset(); | 94 this.reset(); |
| 109 return result; | 95 return result; |
| 110 }; | 96 }; |
| 111 | 97 |
| 112 InterfacePtrController.prototype.getProxy = function() { | 98 InterfacePtrController.prototype.getProxy = function() { |
| 113 this.configureProxyIfNecessary_(); | 99 this.configureProxyIfNecessary_(); |
| 114 return this.proxy_; | 100 return this.proxy_; |
| 115 }; | 101 }; |
| 116 | 102 |
| 117 InterfacePtrController.prototype.waitForNextMessageForTesting = function() { | 103 InterfacePtrController.prototype.enableTestingMode = function() { |
| 118 this.configureProxyIfNecessary_(); | 104 this.configureProxyIfNecessary_(); |
| 119 this.router_.waitForNextMessageForTesting(); | 105 return this.router_.enableTestingMode(); |
| 120 }; | 106 }; |
| 121 | 107 |
| 122 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { | 108 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { |
| 123 if (!this.handle_) | 109 if (!this.handle_) |
| 124 return; | 110 return; |
| 125 | 111 |
| 126 this.router_ = new router.Router(this.handle_); | 112 this.router_ = new router.Router(this.handle_); |
| 127 this.handle_ = null; | 113 this.handle_ = null; |
| 114 this.router_ .setPayloadValidators([this.interfaceType_.validateResponse]); |
| 128 | 115 |
| 129 this.interfaceEndpointClient_ = new InterfaceEndpointClient( | 116 this.controlMessageProxy_ = new |
| 130 this.router_.createLocalEndpointHandle(types.kMasterInterfaceId), | 117 controlMessageProxy.ControlMessageProxy(this.router_); |
| 131 this.router_); | |
| 132 | 118 |
| 133 this.interfaceEndpointClient_ .setPayloadValidators([ | 119 this.proxy_ = new this.interfaceType_.proxyClass(this.router_); |
| 134 this.interfaceType_.validateResponse]); | |
| 135 this.proxy_ = new this.interfaceType_.proxyClass( | |
| 136 this.interfaceEndpointClient_); | |
| 137 }; | 120 }; |
| 138 | 121 |
| 139 InterfacePtrController.prototype.queryVersion = function() { | 122 InterfacePtrController.prototype.queryVersion = function() { |
| 140 function onQueryVersion(version) { | 123 function onQueryVersion(version) { |
| 141 this.version = version; | 124 this.version = version; |
| 142 return version; | 125 return version; |
| 143 } | 126 } |
| 144 | 127 |
| 145 this.configureProxyIfNecessary_(); | 128 this.configureProxyIfNecessary_(); |
| 146 return this.interfaceEndpointClient_.queryVersion().then( | 129 return this.controlMessageProxy_.queryVersion().then( |
| 147 onQueryVersion.bind(this)); | 130 onQueryVersion.bind(this)); |
| 148 }; | 131 }; |
| 149 | 132 |
| 150 InterfacePtrController.prototype.requireVersion = function(version) { | 133 InterfacePtrController.prototype.requireVersion = function(version) { |
| 151 this.configureProxyIfNecessary_(); | 134 this.configureProxyIfNecessary_(); |
| 152 | 135 |
| 153 if (this.version >= version) { | 136 if (this.version >= version) { |
| 154 return; | 137 return; |
| 155 } | 138 } |
| 156 this.version = version; | 139 this.version = version; |
| 157 this.interfaceEndpointClient_.requireVersion(version); | 140 this.controlMessageProxy_.requireVersion(version); |
| 158 }; | 141 }; |
| 159 | 142 |
| 160 // --------------------------------------------------------------------------- | 143 // --------------------------------------------------------------------------- |
| 161 | 144 |
| 162 // |request| could be omitted and passed into bind() later. | 145 // |request| could be omitted and passed into bind() later. |
| 163 // | 146 // |
| 164 // Example: | 147 // Example: |
| 165 // | 148 // |
| 166 // // FooImpl implements mojom.Foo. | 149 // // FooImpl implements mojom.Foo. |
| 167 // function FooImpl() { ... } | 150 // function FooImpl() { ... } |
| 168 // FooImpl.prototype.fooMethod1 = function() { ... } | 151 // FooImpl.prototype.fooMethod1 = function() { ... } |
| 169 // FooImpl.prototype.fooMethod2 = function() { ... } | 152 // FooImpl.prototype.fooMethod2 = function() { ... } |
| 170 // | 153 // |
| 171 // var fooPtr = new mojom.FooPtr(); | 154 // var fooPtr = new mojom.FooPtr(); |
| 172 // var request = makeRequest(fooPtr); | 155 // var request = makeRequest(fooPtr); |
| 173 // var binding = new Binding(mojom.Foo, new FooImpl(), request); | 156 // var binding = new Binding(mojom.Foo, new FooImpl(), request); |
| 174 // fooPtr.fooMethod1(); | 157 // fooPtr.fooMethod1(); |
| 175 function Binding(interfaceType, impl, requestOrHandle) { | 158 function Binding(interfaceType, impl, requestOrHandle) { |
| 176 this.interfaceType_ = interfaceType; | 159 this.interfaceType_ = interfaceType; |
| 177 this.impl_ = impl; | 160 this.impl_ = impl; |
| 178 this.router_ = null; | 161 this.router_ = null; |
| 179 this.interfaceEndpointClient_ = null; | |
| 180 this.stub_ = null; | 162 this.stub_ = null; |
| 181 | 163 |
| 182 if (requestOrHandle) | 164 if (requestOrHandle) |
| 183 this.bind(requestOrHandle); | 165 this.bind(requestOrHandle); |
| 184 } | 166 } |
| 185 | 167 |
| 186 Binding.prototype.isBound = function() { | 168 Binding.prototype.isBound = function() { |
| 187 return this.router_ !== null; | 169 return this.router_ !== null; |
| 188 }; | 170 }; |
| 189 | 171 |
| 190 Binding.prototype.createInterfacePtrAndBind = function() { | 172 Binding.prototype.createInterfacePtrAndBind = function() { |
| 191 var ptr = new this.interfaceType_.ptrClass(); | 173 var ptr = new this.interfaceType_.ptrClass(); |
| 192 // TODO(yzshen): Set the version of the interface pointer. | 174 // TODO(yzshen): Set the version of the interface pointer. |
| 193 this.bind(makeRequest(ptr)); | 175 this.bind(makeRequest(ptr)); |
| 194 return ptr; | 176 return ptr; |
| 195 }; | 177 } |
| 196 | 178 |
| 197 Binding.prototype.bind = function(requestOrHandle) { | 179 Binding.prototype.bind = function(requestOrHandle) { |
| 198 this.close(); | 180 this.close(); |
| 199 | 181 |
| 200 var handle = requestOrHandle instanceof types.InterfaceRequest ? | 182 var handle = requestOrHandle instanceof types.InterfaceRequest ? |
| 201 requestOrHandle.handle : requestOrHandle; | 183 requestOrHandle.handle : requestOrHandle; |
| 202 if (!core.isHandle(handle)) | 184 if (!core.isHandle(handle)) |
| 203 return; | 185 return; |
| 204 | 186 |
| 205 this.router_ = new router.Router(handle); | |
| 206 | |
| 207 this.stub_ = new this.interfaceType_.stubClass(this.impl_); | 187 this.stub_ = new this.interfaceType_.stubClass(this.impl_); |
| 208 this.interfaceEndpointClient_ = new InterfaceEndpointClient( | 188 this.router_ = new router.Router(handle, this.interfaceType_.kVersion); |
| 209 this.router_.createLocalEndpointHandle(types.kMasterInterfaceId), | 189 this.router_.setIncomingReceiver(this.stub_); |
| 210 this.router_, this.interfaceType_.kVersion); | 190 this.router_ .setPayloadValidators([this.interfaceType_.validateRequest]); |
| 211 this.interfaceEndpointClient_.setIncomingReceiver(this.stub_); | |
| 212 this.interfaceEndpointClient_ .setPayloadValidators([ | |
| 213 this.interfaceType_.validateRequest]); | |
| 214 }; | 191 }; |
| 215 | 192 |
| 216 Binding.prototype.close = function() { | 193 Binding.prototype.close = function() { |
| 217 if (!this.isBound()) | 194 if (!this.isBound()) |
| 218 return; | 195 return; |
| 219 | 196 |
| 220 if (this.interfaceEndpointClient_) { | |
| 221 this.interfaceEndpointClient_.close(); | |
| 222 this.interfaceEndpointClient_ = null; | |
| 223 } | |
| 224 | |
| 225 this.router_.close(); | 197 this.router_.close(); |
| 226 this.router_ = null; | 198 this.router_ = null; |
| 227 this.stub_ = null; | 199 this.stub_ = null; |
| 228 }; | 200 }; |
| 229 | 201 |
| 230 Binding.prototype.closeWithReason = function(reason) { | |
| 231 if (this.interfaceEndpointClient_) { | |
| 232 this.interfaceEndpointClient_.close(reason); | |
| 233 this.interfaceEndpointClient_ = null; | |
| 234 } | |
| 235 this.close(); | |
| 236 }; | |
| 237 | |
| 238 Binding.prototype.setConnectionErrorHandler | 202 Binding.prototype.setConnectionErrorHandler |
| 239 = function(callback) { | 203 = function(callback) { |
| 240 if (!this.isBound()) { | 204 if (!this.isBound()) |
| 241 throw new Error("Cannot set connection error handler if not bound."); | 205 throw new Error("Cannot set connection error handler if not bound."); |
| 242 } | 206 this.router_.setErrorHandler(callback); |
| 243 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); | |
| 244 }; | 207 }; |
| 245 | 208 |
| 246 Binding.prototype.unbind = function() { | 209 Binding.prototype.unbind = function() { |
| 247 if (!this.isBound()) | 210 if (!this.isBound()) |
| 248 return new types.InterfaceRequest(null); | 211 return new types.InterfaceRequest(null); |
| 249 | 212 |
| 250 var result = new types.InterfaceRequest(this.router_.connector_.handle_); | 213 var result = new types.InterfaceRequest(this.router_.connector_.handle_); |
| 251 this.router_.connector_.handle_ = null; | 214 this.router_.connector_.handle_ = null; |
| 252 this.close(); | 215 this.close(); |
| 253 return result; | 216 return result; |
| 254 }; | 217 }; |
| 255 | 218 |
| 256 Binding.prototype.waitForNextMessageForTesting = function() { | 219 Binding.prototype.enableTestingMode = function() { |
| 257 this.router_.waitForNextMessageForTesting(); | 220 return this.router_.enableTestingMode(); |
| 258 }; | 221 }; |
| 259 | 222 |
| 260 // --------------------------------------------------------------------------- | 223 // --------------------------------------------------------------------------- |
| 261 | 224 |
| 262 function BindingSetEntry(bindingSet, interfaceType, impl, requestOrHandle, | 225 function BindingSetEntry(bindingSet, interfaceType, impl, requestOrHandle, |
| 263 bindingId) { | 226 bindingId) { |
| 264 this.bindingSet_ = bindingSet; | 227 this.bindingSet_ = bindingSet; |
| 265 this.bindingId_ = bindingId; | 228 this.bindingId_ = bindingId; |
| 266 this.binding_ = new Binding(interfaceType, impl, requestOrHandle); | 229 this.binding_ = new Binding(interfaceType, impl, requestOrHandle); |
| 267 | 230 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 var exports = {}; | 276 var exports = {}; |
| 314 exports.InterfacePtrInfo = types.InterfacePtrInfo; | 277 exports.InterfacePtrInfo = types.InterfacePtrInfo; |
| 315 exports.InterfaceRequest = types.InterfaceRequest; | 278 exports.InterfaceRequest = types.InterfaceRequest; |
| 316 exports.makeRequest = makeRequest; | 279 exports.makeRequest = makeRequest; |
| 317 exports.InterfacePtrController = InterfacePtrController; | 280 exports.InterfacePtrController = InterfacePtrController; |
| 318 exports.Binding = Binding; | 281 exports.Binding = Binding; |
| 319 exports.BindingSet = BindingSet; | 282 exports.BindingSet = BindingSet; |
| 320 | 283 |
| 321 return exports; | 284 return exports; |
| 322 }); | 285 }); |
| OLD | NEW |