| 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 ], function(connection, core) { |
| 9 | 9 |
| 10 // --------------------------------------------------------------------------- | 10 // --------------------------------------------------------------------------- |
| 11 | 11 |
| 12 function InterfacePtrInfo(handle, version) { | 12 function InterfacePtrInfo(handle, version) { |
| 13 this.handle = handle; | 13 this.handle = handle; |
| 14 this.version = version; | 14 this.version = version; |
| 15 } | 15 } |
| 16 | 16 |
| 17 InterfacePtrInfo.prototype.isValid = function() { | 17 InterfacePtrInfo.prototype.isValid = function() { |
| 18 return core.isHandle(this.handle); | 18 return core.isHandle(this.handle); |
| 19 } | 19 }; |
| 20 | 20 |
| 21 // --------------------------------------------------------------------------- | 21 // --------------------------------------------------------------------------- |
| 22 | 22 |
| 23 function InterfaceRequest(handle) { | 23 function InterfaceRequest(handle) { |
| 24 this.handle = handle; | 24 this.handle = handle; |
| 25 } | 25 } |
| 26 | 26 |
| 27 InterfaceRequest.prototype.isValid = function() { | 27 InterfaceRequest.prototype.isValid = function() { |
| 28 return core.isHandle(this.handle); | 28 return core.isHandle(this.handle); |
| 29 } | 29 }; |
| 30 | 30 |
| 31 // --------------------------------------------------------------------------- | 31 // --------------------------------------------------------------------------- |
| 32 | 32 |
| 33 function makeRequest(interfacePtr) { | 33 function makeRequest(interfacePtr) { |
| 34 var pipe = core.createMessagePipe(); | 34 var pipe = core.createMessagePipe(); |
| 35 interfacePtr.ptr.bind(new InterfacePtrInfo(pipe.handle0, 0)); | 35 interfacePtr.ptr.bind(new InterfacePtrInfo(pipe.handle0, 0)); |
| 36 return new InterfaceRequest(pipe.handle1); | 36 return new InterfaceRequest(pipe.handle1); |
| 37 } | 37 } |
| 38 | 38 |
| 39 // --------------------------------------------------------------------------- | 39 // --------------------------------------------------------------------------- |
| 40 | 40 |
| 41 // Operations used to setup/configure an interface pointer. Exposed as the | 41 // Operations used to setup/configure an interface pointer. Exposed as the |
| 42 // |ptr| field of generated interface pointer classes. | 42 // |ptr| field of generated interface pointer classes. |
| 43 function InterfacePtrController(interfaceType) { | 43 function InterfacePtrController(interfaceType) { |
| 44 this.version = 0; | 44 this.version = 0; |
| 45 this.connection = null; | |
| 46 | 45 |
| 47 this.interfaceType_ = interfaceType; | 46 this.interfaceType_ = interfaceType; |
| 47 this.connection_ = null; |
| 48 // |connection_| is lazily initialized. |handle_| is valid between bind() |
| 49 // and the initialization of |connection_|. |
| 50 this.handle_ = null; |
| 48 } | 51 } |
| 49 | 52 |
| 50 InterfacePtrController.prototype.bind = function(interfacePtrInfo) { | 53 InterfacePtrController.prototype.bind = function(interfacePtrInfo) { |
| 51 this.reset(); | 54 this.reset(); |
| 55 |
| 52 this.version = interfacePtrInfo.version; | 56 this.version = interfacePtrInfo.version; |
| 53 this.connection = new connection.Connection( | 57 this.handle_ = interfacePtrInfo.handle; |
| 54 interfacePtrInfo.handle, undefined, this.interfaceType_.proxyClass); | 58 }; |
| 55 } | |
| 56 | 59 |
| 57 InterfacePtrController.prototype.isBound = function() { | 60 InterfacePtrController.prototype.isBound = function() { |
| 58 return this.connection !== null; | 61 return this.connection_ !== null || this.handle_ !== null; |
| 59 } | 62 }; |
| 60 | 63 |
| 61 // Although users could just discard the object, reset() closes the pipe | 64 // Although users could just discard the object, reset() closes the pipe |
| 62 // immediately. | 65 // immediately. |
| 63 InterfacePtrController.prototype.reset = function() { | 66 InterfacePtrController.prototype.reset = function() { |
| 64 if (!this.isBound()) | |
| 65 return; | |
| 66 | |
| 67 this.version = 0; | 67 this.version = 0; |
| 68 this.connection.close(); | 68 if (this.connection_) { |
| 69 this.connection = null; | 69 this.connection_.close(); |
| 70 } | 70 this.connection_ = null; |
| 71 } |
| 72 if (this.handle_) { |
| 73 core.close(this.handle_); |
| 74 this.handle_ = null; |
| 75 } |
| 76 }; |
| 71 | 77 |
| 72 InterfacePtrController.prototype.setConnectionErrorHandler | 78 InterfacePtrController.prototype.setConnectionErrorHandler |
| 73 = function(callback) { | 79 = function(callback) { |
| 74 if (!this.isBound()) | 80 if (!this.isBound()) |
| 75 throw new Error("Cannot set connection error handler if not bound."); | 81 throw new Error("Cannot set connection error handler if not bound."); |
| 76 this.connection.router_.setErrorHandler(callback); | 82 |
| 77 } | 83 this.configureProxyIfNecessary_(); |
| 84 this.connection_.router_.setErrorHandler(callback); |
| 85 }; |
| 78 | 86 |
| 79 InterfacePtrController.prototype.passInterface = function() { | 87 InterfacePtrController.prototype.passInterface = function() { |
| 80 if (!this.isBound()) | 88 var result; |
| 81 return new InterfacePtrInfo(null, 0); | 89 if (this.connection_) { |
| 90 result = new InterfacePtrInfo( |
| 91 this.connection_.router_.connector_.handle_, this.version); |
| 92 this.connection_.router_.connector_.handle_ = null; |
| 93 } else { |
| 94 // This also handles the case when this object is not bound. |
| 95 result = new InterfacePtrInfo(this.handle_, this.version); |
| 96 this.handle_ = null; |
| 97 } |
| 82 | 98 |
| 83 var result = new InterfacePtrInfo( | |
| 84 this.connection.router_.connector_.handle_, this.version); | |
| 85 this.connection.router_.connector_.handle_ = null; | |
| 86 this.reset(); | 99 this.reset(); |
| 87 return result; | 100 return result; |
| 88 } | 101 }; |
| 102 |
| 103 InterfacePtrController.prototype.getProxy = function() { |
| 104 this.configureProxyIfNecessary_(); |
| 105 return this.connection_.remote; |
| 106 }; |
| 107 |
| 108 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { |
| 109 if (!this.handle_) |
| 110 return; |
| 111 |
| 112 this.connection_ = new connection.Connection( |
| 113 this.handle_, undefined, this.interfaceType_.proxyClass); |
| 114 this.handle_ = null; |
| 115 }; |
| 89 | 116 |
| 90 // TODO(yzshen): Implement the following methods. | 117 // TODO(yzshen): Implement the following methods. |
| 91 // InterfacePtrController.prototype.queryVersion | 118 // InterfacePtrController.prototype.queryVersion |
| 92 // InterfacePtrController.prototype.requireVersion | 119 // InterfacePtrController.prototype.requireVersion |
| 93 | 120 |
| 94 // --------------------------------------------------------------------------- | 121 // --------------------------------------------------------------------------- |
| 95 | 122 |
| 96 // |request| could be omitted and passed into bind() later. | 123 // |request| could be omitted and passed into bind() later. |
| 97 // NOTE: |impl| shouldn't hold a reference to this object, because that | 124 // NOTE: |impl| shouldn't hold a reference to this object, because that |
| 98 // results in circular references. | 125 // results in circular references. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 112 this.interfaceType_ = interfaceType; | 139 this.interfaceType_ = interfaceType; |
| 113 this.impl_ = impl; | 140 this.impl_ = impl; |
| 114 this.stub_ = null; | 141 this.stub_ = null; |
| 115 | 142 |
| 116 if (request) | 143 if (request) |
| 117 this.bind(request); | 144 this.bind(request); |
| 118 } | 145 } |
| 119 | 146 |
| 120 Binding.prototype.isBound = function() { | 147 Binding.prototype.isBound = function() { |
| 121 return this.stub_ !== null; | 148 return this.stub_ !== null; |
| 122 } | 149 }; |
| 123 | 150 |
| 124 Binding.prototype.bind = function(request) { | 151 Binding.prototype.bind = function(request) { |
| 125 this.close(); | 152 this.close(); |
| 126 this.stub_ = connection.bindHandleToStub(request.handle, | 153 if (request.isValid()) { |
| 127 this.interfaceType_); | 154 this.stub_ = connection.bindHandleToStub(request.handle, |
| 128 connection.StubBindings(this.stub_).delegate = this.impl_; | 155 this.interfaceType_); |
| 129 } | 156 connection.StubBindings(this.stub_).delegate = this.impl_; |
| 157 } |
| 158 }; |
| 130 | 159 |
| 131 Binding.prototype.close = function() { | 160 Binding.prototype.close = function() { |
| 132 if (!this.isBound()) | 161 if (!this.isBound()) |
| 133 return; | 162 return; |
| 134 connection.StubBindings(this.stub_).close(); | 163 connection.StubBindings(this.stub_).close(); |
| 135 this.stub_ = null; | 164 this.stub_ = null; |
| 136 } | 165 }; |
| 137 | 166 |
| 138 Binding.prototype.setConnectionErrorHandler | 167 Binding.prototype.setConnectionErrorHandler |
| 139 = function(callback) { | 168 = function(callback) { |
| 140 if (!this.isBound()) | 169 if (!this.isBound()) |
| 141 throw new Error("Cannot set connection error handler if not bound."); | 170 throw new Error("Cannot set connection error handler if not bound."); |
| 142 connection.StubBindings(this.stub_).connection.router_.setErrorHandler( | 171 connection.StubBindings(this.stub_).connection.router_.setErrorHandler( |
| 143 callback); | 172 callback); |
| 144 } | 173 }; |
| 145 | 174 |
| 146 Binding.prototype.unbind = function() { | 175 Binding.prototype.unbind = function() { |
| 147 if (!this.isBound()) | 176 if (!this.isBound()) |
| 148 return new InterfaceRequest(null); | 177 return new InterfaceRequest(null); |
| 149 | 178 |
| 150 var result = new InterfaceRequest( | 179 var result = new InterfaceRequest( |
| 151 connection.StubBindings(this.stub_).connection.router_.connector_ | 180 connection.StubBindings(this.stub_).connection.router_.connector_ |
| 152 .handle_); | 181 .handle_); |
| 153 connection.StubBindings(this.stub_).connection.router_.connector_.handle_ = | 182 connection.StubBindings(this.stub_).connection.router_.connector_.handle_ = |
| 154 null; | 183 null; |
| 155 this.close(); | 184 this.close(); |
| 156 return result; | 185 return result; |
| 157 } | 186 }; |
| 158 | 187 |
| 159 var exports = {}; | 188 var exports = {}; |
| 160 exports.InterfacePtrInfo = InterfacePtrInfo; | 189 exports.InterfacePtrInfo = InterfacePtrInfo; |
| 161 exports.InterfaceRequest = InterfaceRequest; | 190 exports.InterfaceRequest = InterfaceRequest; |
| 162 exports.makeRequest = makeRequest; | 191 exports.makeRequest = makeRequest; |
| 163 exports.InterfacePtrController = InterfacePtrController; | 192 exports.InterfacePtrController = InterfacePtrController; |
| 164 exports.Binding = Binding; | 193 exports.Binding = Binding; |
| 165 | 194 |
| 166 // TODO(yzshen): Remove the following exports. | 195 // TODO(yzshen): Remove the following exports. |
| 167 exports.EmptyProxy = connection.EmptyProxy; | 196 exports.EmptyProxy = connection.EmptyProxy; |
| 168 exports.EmptyStub = connection.EmptyStub; | 197 exports.EmptyStub = connection.EmptyStub; |
| 169 exports.ProxyBase = connection.ProxyBase; | 198 exports.ProxyBase = connection.ProxyBase; |
| 170 exports.ProxyBindings = connection.ProxyBindings; | 199 exports.ProxyBindings = connection.ProxyBindings; |
| 171 exports.StubBase = connection.StubBase; | 200 exports.StubBase = connection.StubBase; |
| 172 exports.StubBindings = connection.StubBindings; | 201 exports.StubBindings = connection.StubBindings; |
| 173 | 202 |
| 174 return exports; | 203 return exports; |
| 175 }); | 204 }); |
| OLD | NEW |