| 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 // --------------------------------------------------------------------------- |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 this.router_ = null; | 64 this.router_ = null; |
| 63 | 65 |
| 64 this.proxy_ = null; | 66 this.proxy_ = null; |
| 65 } | 67 } |
| 66 if (this.handle_) { | 68 if (this.handle_) { |
| 67 core.close(this.handle_); | 69 core.close(this.handle_); |
| 68 this.handle_ = null; | 70 this.handle_ = null; |
| 69 } | 71 } |
| 70 }; | 72 }; |
| 71 | 73 |
| 72 InterfacePtrController.prototype.setConnectionErrorHandler | 74 InterfacePtrController.prototype.resetWithReason = function(reason) { |
| 73 = function(callback) { | 75 this.configureProxyIfNecessary_(); |
| 76 this.interfaceEndpointClient_.closeWithReason(reason); |
| 77 this.reset(); |
| 78 }; |
| 79 |
| 80 InterfacePtrController.prototype.setConnectionErrorHandler = function( |
| 81 callback) { |
| 74 if (!this.isBound()) | 82 if (!this.isBound()) |
| 75 throw new Error("Cannot set connection error handler if not bound."); | 83 throw new Error("Cannot set connection error handler if not bound."); |
| 76 | 84 |
| 77 this.configureProxyIfNecessary_(); | 85 this.configureProxyIfNecessary_(); |
| 78 this.router_.setErrorHandler(callback); | 86 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); |
| 79 }; | 87 }; |
| 80 | 88 |
| 81 InterfacePtrController.prototype.passInterface = function() { | 89 InterfacePtrController.prototype.passInterface = function() { |
| 82 var result; | 90 var result; |
| 83 if (this.router_) { | 91 if (this.router_) { |
| 84 // TODO(yzshen): Fix Router interface to support extracting handle. | 92 // TODO(yzshen): Fix Router interface to support extracting handle. |
| 85 result = new types.InterfacePtrInfo( | 93 result = new types.InterfacePtrInfo( |
| 86 this.router_.connector_.handle_, this.version); | 94 this.router_.connector_.handle_, this.version); |
| 87 this.router_.connector_.handle_ = null; | 95 this.router_.connector_.handle_ = null; |
| 88 } else { | 96 } else { |
| 89 // This also handles the case when this object is not bound. | 97 // This also handles the case when this object is not bound. |
| 90 result = new types.InterfacePtrInfo(this.handle_, this.version); | 98 result = new types.InterfacePtrInfo(this.handle_, this.version); |
| 91 this.handle_ = null; | 99 this.handle_ = null; |
| 92 } | 100 } |
| 93 | 101 |
| 94 this.reset(); | 102 this.reset(); |
| 95 return result; | 103 return result; |
| 96 }; | 104 }; |
| 97 | 105 |
| 98 InterfacePtrController.prototype.getProxy = function() { | 106 InterfacePtrController.prototype.getProxy = function() { |
| 99 this.configureProxyIfNecessary_(); | 107 this.configureProxyIfNecessary_(); |
| 100 return this.proxy_; | 108 return this.proxy_; |
| 101 }; | 109 }; |
| 102 | 110 |
| 103 InterfacePtrController.prototype.enableTestingMode = function() { | 111 InterfacePtrController.prototype.waitForNextMessageForTesting = function() { |
| 104 this.configureProxyIfNecessary_(); | 112 this.configureProxyIfNecessary_(); |
| 105 return this.router_.enableTestingMode(); | 113 this.router_.waitForNextMessageForTesting(); |
| 106 }; | 114 }; |
| 107 | 115 |
| 108 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { | 116 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { |
| 109 if (!this.handle_) | 117 if (!this.handle_) |
| 110 return; | 118 return; |
| 111 | 119 |
| 112 this.router_ = new router.Router(this.handle_); | 120 this.router_ = new router.Router(this.handle_); |
| 113 this.handle_ = null; | 121 this.handle_ = null; |
| 114 this.router_ .setPayloadValidators([this.interfaceType_.validateResponse]); | |
| 115 | 122 |
| 116 this.controlMessageProxy_ = new | 123 this.interfaceEndpointClient_ = new InterfaceEndpointClient( |
| 117 controlMessageProxy.ControlMessageProxy(this.router_); | 124 this.router_.createLocalEndpointHandle(types.kMasterInterfaceId), |
| 125 this.router_); |
| 118 | 126 |
| 119 this.proxy_ = new this.interfaceType_.proxyClass(this.router_); | 127 this.interfaceEndpointClient_ .setPayloadValidators([ |
| 128 this.interfaceType_.validateResponse]); |
| 129 this.proxy_ = new this.interfaceType_.proxyClass( |
| 130 this.interfaceEndpointClient_); |
| 120 }; | 131 }; |
| 121 | 132 |
| 122 InterfacePtrController.prototype.queryVersion = function() { | 133 InterfacePtrController.prototype.queryVersion = function() { |
| 123 function onQueryVersion(version) { | 134 function onQueryVersion(version) { |
| 124 this.version = version; | 135 this.version = version; |
| 125 return version; | 136 return version; |
| 126 } | 137 } |
| 127 | 138 |
| 128 this.configureProxyIfNecessary_(); | 139 this.configureProxyIfNecessary_(); |
| 129 return this.controlMessageProxy_.queryVersion().then( | 140 return this.interfaceEndpointClient_.queryVersion().then( |
| 130 onQueryVersion.bind(this)); | 141 onQueryVersion.bind(this)); |
| 131 }; | 142 }; |
| 132 | 143 |
| 133 InterfacePtrController.prototype.requireVersion = function(version) { | 144 InterfacePtrController.prototype.requireVersion = function(version) { |
| 134 this.configureProxyIfNecessary_(); | 145 this.configureProxyIfNecessary_(); |
| 135 | 146 |
| 136 if (this.version >= version) { | 147 if (this.version >= version) { |
| 137 return; | 148 return; |
| 138 } | 149 } |
| 139 this.version = version; | 150 this.version = version; |
| 140 this.controlMessageProxy_.requireVersion(version); | 151 this.interfaceEndpointClient_.requireVersion(version); |
| 141 }; | 152 }; |
| 142 | 153 |
| 143 // --------------------------------------------------------------------------- | 154 // --------------------------------------------------------------------------- |
| 144 | 155 |
| 145 // |request| could be omitted and passed into bind() later. | 156 // |request| could be omitted and passed into bind() later. |
| 146 // | 157 // |
| 147 // Example: | 158 // Example: |
| 148 // | 159 // |
| 149 // // FooImpl implements mojom.Foo. | 160 // // FooImpl implements mojom.Foo. |
| 150 // function FooImpl() { ... } | 161 // function FooImpl() { ... } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 167 | 178 |
| 168 Binding.prototype.isBound = function() { | 179 Binding.prototype.isBound = function() { |
| 169 return this.router_ !== null; | 180 return this.router_ !== null; |
| 170 }; | 181 }; |
| 171 | 182 |
| 172 Binding.prototype.createInterfacePtrAndBind = function() { | 183 Binding.prototype.createInterfacePtrAndBind = function() { |
| 173 var ptr = new this.interfaceType_.ptrClass(); | 184 var ptr = new this.interfaceType_.ptrClass(); |
| 174 // TODO(yzshen): Set the version of the interface pointer. | 185 // TODO(yzshen): Set the version of the interface pointer. |
| 175 this.bind(makeRequest(ptr)); | 186 this.bind(makeRequest(ptr)); |
| 176 return ptr; | 187 return ptr; |
| 177 } | 188 }; |
| 178 | 189 |
| 179 Binding.prototype.bind = function(requestOrHandle) { | 190 Binding.prototype.bind = function(requestOrHandle) { |
| 180 this.close(); | 191 this.close(); |
| 181 | 192 |
| 182 var handle = requestOrHandle instanceof types.InterfaceRequest ? | 193 var handle = requestOrHandle instanceof types.InterfaceRequest ? |
| 183 requestOrHandle.handle : requestOrHandle; | 194 requestOrHandle.handle : requestOrHandle; |
| 184 if (!core.isHandle(handle)) | 195 if (!core.isHandle(handle)) |
| 185 return; | 196 return; |
| 186 | 197 |
| 198 this.router_ = new router.Router(handle); |
| 199 |
| 187 this.stub_ = new this.interfaceType_.stubClass(this.impl_); | 200 this.stub_ = new this.interfaceType_.stubClass(this.impl_); |
| 188 this.router_ = new router.Router(handle, this.interfaceType_.kVersion); | 201 this.interfaceEndpointClient_ = new InterfaceEndpointClient( |
| 189 this.router_.setIncomingReceiver(this.stub_); | 202 this.router_.createLocalEndpointHandle(types.kMasterInterfaceId), |
| 190 this.router_ .setPayloadValidators([this.interfaceType_.validateRequest]); | 203 this.router_, this.interfaceType_.kVersion); |
| 204 this.interfaceEndpointClient_.setIncomingReceiver(this.stub_); |
| 205 this.interfaceEndpointClient_ .setPayloadValidators([ |
| 206 this.interfaceType_.validateRequest]); |
| 191 }; | 207 }; |
| 192 | 208 |
| 193 Binding.prototype.close = function() { | 209 Binding.prototype.close = function() { |
| 194 if (!this.isBound()) | 210 if (!this.isBound()) |
| 195 return; | 211 return; |
| 196 | 212 |
| 197 this.router_.close(); | 213 this.router_.close(); |
| 198 this.router_ = null; | 214 this.router_ = null; |
| 199 this.stub_ = null; | 215 this.stub_ = null; |
| 200 }; | 216 }; |
| 201 | 217 |
| 218 Binding.prototype.closeWithReason = function(reason) { |
| 219 if (this.interfaceEndpointClient_) { |
| 220 this.interfaceEndpointClient_.closeWithReason(reason); |
| 221 } |
| 222 this.close(); |
| 223 }; |
| 224 |
| 202 Binding.prototype.setConnectionErrorHandler | 225 Binding.prototype.setConnectionErrorHandler |
| 203 = function(callback) { | 226 = function(callback) { |
| 204 if (!this.isBound()) | 227 if (!this.isBound()) { |
| 205 throw new Error("Cannot set connection error handler if not bound."); | 228 throw new Error("Cannot set connection error handler if not bound."); |
| 206 this.router_.setErrorHandler(callback); | 229 } |
| 230 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); |
| 207 }; | 231 }; |
| 208 | 232 |
| 209 Binding.prototype.unbind = function() { | 233 Binding.prototype.unbind = function() { |
| 210 if (!this.isBound()) | 234 if (!this.isBound()) |
| 211 return new types.InterfaceRequest(null); | 235 return new types.InterfaceRequest(null); |
| 212 | 236 |
| 213 var result = new types.InterfaceRequest(this.router_.connector_.handle_); | 237 var result = new types.InterfaceRequest(this.router_.connector_.handle_); |
| 214 this.router_.connector_.handle_ = null; | 238 this.router_.connector_.handle_ = null; |
| 215 this.close(); | 239 this.close(); |
| 216 return result; | 240 return result; |
| 217 }; | 241 }; |
| 218 | 242 |
| 219 Binding.prototype.enableTestingMode = function() { | 243 Binding.prototype.waitForNextMessageForTesting = function() { |
| 220 return this.router_.enableTestingMode(); | 244 this.router_.waitForNextMessageForTesting(); |
| 221 }; | 245 }; |
| 222 | 246 |
| 223 // --------------------------------------------------------------------------- | 247 // --------------------------------------------------------------------------- |
| 224 | 248 |
| 225 function BindingSetEntry(bindingSet, interfaceType, impl, requestOrHandle, | 249 function BindingSetEntry(bindingSet, interfaceType, impl, requestOrHandle, |
| 226 bindingId) { | 250 bindingId) { |
| 227 this.bindingSet_ = bindingSet; | 251 this.bindingSet_ = bindingSet; |
| 228 this.bindingId_ = bindingId; | 252 this.bindingId_ = bindingId; |
| 229 this.binding_ = new Binding(interfaceType, impl, requestOrHandle); | 253 this.binding_ = new Binding(interfaceType, impl, requestOrHandle); |
| 230 | 254 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 var exports = {}; | 300 var exports = {}; |
| 277 exports.InterfacePtrInfo = types.InterfacePtrInfo; | 301 exports.InterfacePtrInfo = types.InterfacePtrInfo; |
| 278 exports.InterfaceRequest = types.InterfaceRequest; | 302 exports.InterfaceRequest = types.InterfaceRequest; |
| 279 exports.makeRequest = makeRequest; | 303 exports.makeRequest = makeRequest; |
| 280 exports.InterfacePtrController = InterfacePtrController; | 304 exports.InterfacePtrController = InterfacePtrController; |
| 281 exports.Binding = Binding; | 305 exports.Binding = Binding; |
| 282 exports.BindingSet = BindingSet; | 306 exports.BindingSet = BindingSet; |
| 283 | 307 |
| 284 return exports; | 308 return exports; |
| 285 }); | 309 }); |
| OLD | NEW |