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