| 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/connection", | 6 "mojo/public/js/connection", |
| 7 "mojo/public/js/core", | 7 "mojo/public/js/core", |
| 8 ], function(connection, core) { | 8 "mojo/public/js/interface_types", |
| 9 | 9 ], function(connection, core, types) { |
| 10 // --------------------------------------------------------------------------- | |
| 11 | |
| 12 function InterfacePtrInfo(handle, version) { | |
| 13 this.handle = handle; | |
| 14 this.version = version; | |
| 15 } | |
| 16 | |
| 17 InterfacePtrInfo.prototype.isValid = function() { | |
| 18 return core.isHandle(this.handle); | |
| 19 }; | |
| 20 | |
| 21 // --------------------------------------------------------------------------- | |
| 22 | |
| 23 function InterfaceRequest(handle) { | |
| 24 this.handle = handle; | |
| 25 } | |
| 26 | |
| 27 InterfaceRequest.prototype.isValid = function() { | |
| 28 return core.isHandle(this.handle); | |
| 29 }; | |
| 30 | 10 |
| 31 // --------------------------------------------------------------------------- | 11 // --------------------------------------------------------------------------- |
| 32 | 12 |
| 33 function makeRequest(interfacePtr) { | 13 function makeRequest(interfacePtr) { |
| 34 var pipe = core.createMessagePipe(); | 14 var pipe = core.createMessagePipe(); |
| 35 interfacePtr.ptr.bind(new InterfacePtrInfo(pipe.handle0, 0)); | 15 interfacePtr.ptr.bind(new types.InterfacePtrInfo(pipe.handle0, 0)); |
| 36 return new InterfaceRequest(pipe.handle1); | 16 return new types.InterfaceRequest(pipe.handle1); |
| 37 } | 17 } |
| 38 | 18 |
| 39 // --------------------------------------------------------------------------- | 19 // --------------------------------------------------------------------------- |
| 40 | 20 |
| 41 // Operations used to setup/configure an interface pointer. Exposed as the | 21 // Operations used to setup/configure an interface pointer. Exposed as the |
| 42 // |ptr| field of generated interface pointer classes. | 22 // |ptr| field of generated interface pointer classes. |
| 43 function InterfacePtrController(interfaceType) { | 23 function InterfacePtrController(interfaceType) { |
| 44 this.version = 0; | 24 this.version = 0; |
| 45 | 25 |
| 46 this.interfaceType_ = interfaceType; | 26 this.interfaceType_ = interfaceType; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 if (!this.isBound()) | 60 if (!this.isBound()) |
| 81 throw new Error("Cannot set connection error handler if not bound."); | 61 throw new Error("Cannot set connection error handler if not bound."); |
| 82 | 62 |
| 83 this.configureProxyIfNecessary_(); | 63 this.configureProxyIfNecessary_(); |
| 84 this.connection_.router_.setErrorHandler(callback); | 64 this.connection_.router_.setErrorHandler(callback); |
| 85 }; | 65 }; |
| 86 | 66 |
| 87 InterfacePtrController.prototype.passInterface = function() { | 67 InterfacePtrController.prototype.passInterface = function() { |
| 88 var result; | 68 var result; |
| 89 if (this.connection_) { | 69 if (this.connection_) { |
| 90 result = new InterfacePtrInfo( | 70 result = new types.InterfacePtrInfo( |
| 91 this.connection_.router_.connector_.handle_, this.version); | 71 this.connection_.router_.connector_.handle_, this.version); |
| 92 this.connection_.router_.connector_.handle_ = null; | 72 this.connection_.router_.connector_.handle_ = null; |
| 93 } else { | 73 } else { |
| 94 // This also handles the case when this object is not bound. | 74 // This also handles the case when this object is not bound. |
| 95 result = new InterfacePtrInfo(this.handle_, this.version); | 75 result = new types.InterfacePtrInfo(this.handle_, this.version); |
| 96 this.handle_ = null; | 76 this.handle_ = null; |
| 97 } | 77 } |
| 98 | 78 |
| 99 this.reset(); | 79 this.reset(); |
| 100 return result; | 80 return result; |
| 101 }; | 81 }; |
| 102 | 82 |
| 103 InterfacePtrController.prototype.getProxy = function() { | 83 InterfacePtrController.prototype.getProxy = function() { |
| 104 this.configureProxyIfNecessary_(); | 84 this.configureProxyIfNecessary_(); |
| 105 return this.connection_.remote; | 85 return this.connection_.remote; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 this.stub_ = null; | 121 this.stub_ = null; |
| 142 | 122 |
| 143 if (request) | 123 if (request) |
| 144 this.bind(request); | 124 this.bind(request); |
| 145 } | 125 } |
| 146 | 126 |
| 147 Binding.prototype.isBound = function() { | 127 Binding.prototype.isBound = function() { |
| 148 return this.stub_ !== null; | 128 return this.stub_ !== null; |
| 149 }; | 129 }; |
| 150 | 130 |
| 131 Binding.prototype.createInterfacePtrAndBind = function() { |
| 132 var ptr = new this.interfaceType_.ptrClass(); |
| 133 // TODO(yzshen): Set the version of the interface pointer. |
| 134 this.bind(makeRequest(ptr)); |
| 135 return ptr; |
| 136 } |
| 137 |
| 151 Binding.prototype.bind = function(request) { | 138 Binding.prototype.bind = function(request) { |
| 152 this.close(); | 139 this.close(); |
| 153 if (request.isValid()) { | 140 if (request.isValid()) { |
| 154 this.stub_ = connection.bindHandleToStub(request.handle, | 141 this.stub_ = connection.bindHandleToStub(request.handle, |
| 155 this.interfaceType_); | 142 this.interfaceType_); |
| 156 connection.StubBindings(this.stub_).delegate = this.impl_; | 143 connection.StubBindings(this.stub_).delegate = this.impl_; |
| 157 } | 144 } |
| 158 }; | 145 }; |
| 159 | 146 |
| 160 Binding.prototype.close = function() { | 147 Binding.prototype.close = function() { |
| 161 if (!this.isBound()) | 148 if (!this.isBound()) |
| 162 return; | 149 return; |
| 163 connection.StubBindings(this.stub_).close(); | 150 connection.StubBindings(this.stub_).close(); |
| 164 this.stub_ = null; | 151 this.stub_ = null; |
| 165 }; | 152 }; |
| 166 | 153 |
| 167 Binding.prototype.setConnectionErrorHandler | 154 Binding.prototype.setConnectionErrorHandler |
| 168 = function(callback) { | 155 = function(callback) { |
| 169 if (!this.isBound()) | 156 if (!this.isBound()) |
| 170 throw new Error("Cannot set connection error handler if not bound."); | 157 throw new Error("Cannot set connection error handler if not bound."); |
| 171 connection.StubBindings(this.stub_).connection.router_.setErrorHandler( | 158 connection.StubBindings(this.stub_).connection.router_.setErrorHandler( |
| 172 callback); | 159 callback); |
| 173 }; | 160 }; |
| 174 | 161 |
| 175 Binding.prototype.unbind = function() { | 162 Binding.prototype.unbind = function() { |
| 176 if (!this.isBound()) | 163 if (!this.isBound()) |
| 177 return new InterfaceRequest(null); | 164 return new types.InterfaceRequest(null); |
| 178 | 165 |
| 179 var result = new InterfaceRequest( | 166 var result = new types.InterfaceRequest( |
| 180 connection.StubBindings(this.stub_).connection.router_.connector_ | 167 connection.StubBindings(this.stub_).connection.router_.connector_ |
| 181 .handle_); | 168 .handle_); |
| 182 connection.StubBindings(this.stub_).connection.router_.connector_.handle_ = | 169 connection.StubBindings(this.stub_).connection.router_.connector_.handle_ = |
| 183 null; | 170 null; |
| 184 this.close(); | 171 this.close(); |
| 185 return result; | 172 return result; |
| 186 }; | 173 }; |
| 187 | 174 |
| 188 var exports = {}; | 175 var exports = {}; |
| 189 exports.InterfacePtrInfo = InterfacePtrInfo; | 176 exports.InterfacePtrInfo = types.InterfacePtrInfo; |
| 190 exports.InterfaceRequest = InterfaceRequest; | 177 exports.InterfaceRequest = types.InterfaceRequest; |
| 191 exports.makeRequest = makeRequest; | 178 exports.makeRequest = makeRequest; |
| 192 exports.InterfacePtrController = InterfacePtrController; | 179 exports.InterfacePtrController = InterfacePtrController; |
| 193 exports.Binding = Binding; | 180 exports.Binding = Binding; |
| 194 | 181 |
| 195 // TODO(yzshen): Remove the following exports. | 182 // TODO(yzshen): Remove the following exports. |
| 196 exports.EmptyProxy = connection.EmptyProxy; | 183 exports.EmptyProxy = connection.EmptyProxy; |
| 197 exports.EmptyStub = connection.EmptyStub; | 184 exports.EmptyStub = connection.EmptyStub; |
| 198 exports.ProxyBase = connection.ProxyBase; | 185 exports.ProxyBase = connection.ProxyBase; |
| 199 exports.ProxyBindings = connection.ProxyBindings; | 186 exports.ProxyBindings = connection.ProxyBindings; |
| 200 exports.StubBase = connection.StubBase; | 187 exports.StubBase = connection.StubBase; |
| 201 exports.StubBindings = connection.StubBindings; | 188 exports.StubBindings = connection.StubBindings; |
| 202 | 189 |
| 203 return exports; | 190 return exports; |
| 204 }); | 191 }); |
| OLD | NEW |