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