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