 Chromium Code Reviews
 Chromium Code Reviews Issue 2744963002:
  Introduce InterfaceEndpointClient(IEC), InterfaceEndpointHandle and  (Closed)
    
  
    Issue 2744963002:
  Introduce InterfaceEndpointClient(IEC), InterfaceEndpointHandle and  (Closed) 
  | 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/router", [ | 5 define("mojo/public/js/router", [ | 
| 6 "console", | 6 "mojo/public/js/connector", | 
| 7 "mojo/public/js/codec", | |
| 8 "mojo/public/js/core", | 7 "mojo/public/js/core", | 
| 9 "mojo/public/js/connector", | 8 "mojo/public/js/interface_types", | 
| 10 "mojo/public/js/lib/control_message_handler", | 9 "mojo/public/js/lib/interface_endpoint_handle", | 
| 10 "mojo/public/js/lib/pipe_control_message_handler", | |
| 11 "mojo/public/js/lib/pipe_control_message_proxy", | |
| 11 "mojo/public/js/validator", | 12 "mojo/public/js/validator", | 
| 12 ], function(console, codec, core, connector, controlMessageHandler, validator) { | 13 "timer", | 
| 14 ], function(connector, core, types, interfaceEndpointHandle, | |
| 15 controlMessageHandler, controlMessageProxy, validator, timer) { | |
| 13 | 16 | 
| 14 var Connector = connector.Connector; | 17 var Connector = connector.Connector; | 
| 15 var MessageReader = codec.MessageReader; | 18 var PipeControlMessageHandler = | 
| 19 controlMessageHandler.PipeControlMessageHandler; | |
| 20 var PipeControlMessageProxy = controlMessageProxy.PipeControlMessageProxy; | |
| 16 var Validator = validator.Validator; | 21 var Validator = validator.Validator; | 
| 17 var ControlMessageHandler = controlMessageHandler.ControlMessageHandler; | 22 var InterfaceEndpointHandle = interfaceEndpointHandle.InterfaceEndpointHandle; | 
| 18 | 23 | 
| 19 function Router(handle, interface_version, connectorFactory) { | 24 /** | 
| 20 if (!core.isHandle(handle)) | 25 * The state of |endpoint|. If both the endpoint and its peer have been | 
| 26 * closed, removes it from |endpoints_|. | |
| 27 * @enum {string} | |
| 28 */ | |
| 29 var EndpointStateUpdateType = { | |
| 30 ENDPOINT_CLOSED: 'endpoint_closed', | |
| 31 PEER_ENDPOINT_CLOSED: 'peer_endpoint_closed' | |
| 32 }; | |
| 33 | |
| 34 function check(condition, output) { | |
| 35 if (!condition) { | |
| 36 throw new Error(output); | |
| 
yzshen1
2017/03/29 22:05:34
[Just to double check] It seems the callers never
 | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 function InterfaceEndpoint(router, interfaceId) { | |
| 41 this.router_ = router; | |
| 42 this.id = interfaceId; | |
| 43 this.closed = false; | |
| 44 this.peerClosed = false; | |
| 45 this.handleCreated = false; | |
| 46 this.disconnectReason = null; | |
| 47 this.client = null; | |
| 48 } | |
| 49 | |
| 50 InterfaceEndpoint.prototype.sendMessage = function(message) { | |
| 51 message.setInterfaceId(this.id); | |
| 52 return this.router_.connector_.accept(message); | |
| 53 }; | |
| 54 | |
| 55 function Router(handle, setInterfaceIdNamespaceBit) { | |
| 56 if (!core.isHandle(handle)) { | |
| 21 throw new Error("Router constructor: Not a handle"); | 57 throw new Error("Router constructor: Not a handle"); | 
| 22 if (connectorFactory === undefined) | 58 } | 
| 23 connectorFactory = Connector; | 59 if (setInterfaceIdNamespaceBit === undefined) { | 
| 24 this.connector_ = new connectorFactory(handle); | 60 setInterfaceIdNamespaceBit = false; | 
| 25 this.incomingReceiver_ = null; | 61 } | 
| 26 this.errorHandler_ = null; | 62 | 
| 27 this.nextRequestID_ = 0; | 63 this.connector_ = new Connector(handle); | 
| 28 this.completers_ = new Map(); | |
| 29 this.payloadValidators_ = []; | |
| 30 this.testingController_ = null; | |
| 31 | |
| 32 if (interface_version !== undefined) { | |
| 33 this.controlMessageHandler_ = new | |
| 34 ControlMessageHandler(interface_version); | |
| 35 } | |
| 36 | 64 | 
| 37 this.connector_.setIncomingReceiver({ | 65 this.connector_.setIncomingReceiver({ | 
| 38 accept: this.handleIncomingMessage_.bind(this), | 66 accept: this.accept.bind(this), | 
| 39 }); | 67 }); | 
| 40 this.connector_.setErrorHandler({ | 68 this.connector_.setErrorHandler({ | 
| 41 onError: this.handleConnectionError_.bind(this), | 69 onError: this.onPipeConnectionError.bind(this), | 
| 42 }); | 70 }); | 
| 71 | |
| 72 this.setInterfaceIdNamespaceBit_ = setInterfaceIdNamespaceBit; | |
| 73 this.controlMessageHandler_ = new PipeControlMessageHandler(this); | |
| 74 this.controlMessageProxy_ = new PipeControlMessageProxy(this.connector_); | |
| 75 this.nextInterfaceIdValue = 1; | |
| 76 this.encounteredError_ = false; | |
| 77 this.endpoints_ = new Map(); | |
| 43 } | 78 } | 
| 44 | 79 | 
| 45 Router.prototype.close = function() { | 80 Router.prototype.attachEndpointClient = function( | 
| 46 this.completers_.clear(); // Drop any responders. | 81 interfaceEndpointHandle, interfaceEndpointClient) { | 
| 47 this.connector_.close(); | 82 check(types.isValidInterfaceId(interfaceEndpointHandle.id())); | 
| 48 this.testingController_ = null; | 83 check(interfaceEndpointClient); | 
| 84 | |
| 85 var endpoint = this.endpoints_.get(interfaceEndpointHandle.id()); | |
| 86 check(endpoint); | |
| 87 check(!endpoint.client); | |
| 88 check(!endpoint.closed); | |
| 89 endpoint.client = interfaceEndpointClient; | |
| 90 | |
| 91 if (endpoint.peerClosed) { | |
| 92 timer.createOneShot(0, | |
| 93 endpoint.client.notifyError.bind(endpoint.client)); | |
| 94 } | |
| 95 | |
| 96 return endpoint; | |
| 97 }; | |
| 98 | |
| 99 Router.prototype.detachEndpointClient = function( | |
| 100 interfaceEndpointHandle) { | |
| 101 check(types.isValidInterfaceId(interfaceEndpointHandle.id())); | |
| 102 var endpoint = this.endpoints_.get(interfaceEndpointHandle.id()); | |
| 103 check(endpoint); | |
| 104 check(endpoint.client); | |
| 105 check(!endpoint.closed); | |
| 106 | |
| 107 endpoint.client = null; | |
| 108 }; | |
| 109 | |
| 110 Router.prototype.createLocalEndpointHandle = function( | |
| 111 interfaceId) { | |
| 112 if (!types.isValidInterfaceId(interfaceId)) { | |
| 113 return new InterfaceEndpointHandle(); | |
| 114 } | |
| 115 | |
| 116 var endpoint = this.endpoints_.get(interfaceId); | |
| 117 | |
| 118 if (!endpoint) { | |
| 119 endpoint = new InterfaceEndpoint(this, interfaceId); | |
| 120 this.endpoints_.set(interfaceId, endpoint); | |
| 121 | |
| 122 check(!endpoint.handleCreated); | |
| 123 | |
| 124 if (this.encounteredError_) { | |
| 125 this.updateEndpointStateMayRemove(endpoint, | |
| 126 EndpointStateUpdateType.PEER_ENDPOINT_CLOSED); | |
| 127 } | |
| 128 } else { | |
| 129 // If the endpoint already exist, it is because we have received a | |
| 130 // notification that the peer endpoint has closed. | |
| 131 check(!endpoint.closed); | |
| 132 check(endpoint.peerClosed); | |
| 133 | |
| 134 if (endpoint.handleCreated) { | |
| 135 return new InterfaceEndpointHandle(); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 endpoint.handleCreated = true; | |
| 140 return new InterfaceEndpointHandle(interfaceId, this); | |
| 49 }; | 141 }; | 
| 50 | 142 | 
| 51 Router.prototype.accept = function(message) { | 143 Router.prototype.accept = function(message) { | 
| 52 this.connector_.accept(message); | |
| 53 }; | |
| 54 | |
| 55 Router.prototype.reject = function(message) { | |
| 56 // TODO(mpcomplete): no way to trasmit errors over a Connection. | |
| 57 }; | |
| 58 | |
| 59 Router.prototype.acceptAndExpectResponse = function(message) { | |
| 60 // Reserve 0 in case we want it to convey special meaning in the future. | |
| 61 var requestID = this.nextRequestID_++; | |
| 62 if (requestID == 0) | |
| 63 requestID = this.nextRequestID_++; | |
| 64 | |
| 65 message.setRequestID(requestID); | |
| 66 var result = this.connector_.accept(message); | |
| 67 if (!result) | |
| 68 return Promise.reject(Error("Connection error")); | |
| 69 | |
| 70 var completer = {}; | |
| 71 this.completers_.set(requestID, completer); | |
| 72 return new Promise(function(resolve, reject) { | |
| 73 completer.resolve = resolve; | |
| 74 completer.reject = reject; | |
| 75 }); | |
| 76 }; | |
| 77 | |
| 78 Router.prototype.setIncomingReceiver = function(receiver) { | |
| 79 this.incomingReceiver_ = receiver; | |
| 80 }; | |
| 81 | |
| 82 Router.prototype.setPayloadValidators = function(payloadValidators) { | |
| 83 this.payloadValidators_ = payloadValidators; | |
| 84 }; | |
| 85 | |
| 86 Router.prototype.setErrorHandler = function(handler) { | |
| 87 this.errorHandler_ = handler; | |
| 88 }; | |
| 89 | |
| 90 Router.prototype.encounteredError = function() { | |
| 91 return this.connector_.encounteredError(); | |
| 92 }; | |
| 93 | |
| 94 Router.prototype.enableTestingMode = function() { | |
| 95 this.testingController_ = new RouterTestingController(this.connector_); | |
| 96 return this.testingController_; | |
| 97 }; | |
| 98 | |
| 99 Router.prototype.handleIncomingMessage_ = function(message) { | |
| 100 var noError = validator.validationError.NONE; | |
| 101 var messageValidator = new Validator(message); | 144 var messageValidator = new Validator(message); | 
| 102 var err = messageValidator.validateMessageHeader(); | 145 var err = messageValidator.validateMessageHeader(); | 
| 103 for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i) | 146 | 
| 104 err = this.payloadValidators_[i](messageValidator); | 147 var ok = false; | 
| 105 | 148 if (err !== validator.validationError.NONE) { | 
| 106 if (err == noError) | 149 validator.reportValidationError(err); | 
| 107 this.handleValidIncomingMessage_(message); | 150 } else if (controlMessageHandler.isPipeControlMessage(message)) { | 
| 108 else | 151 ok = this.controlMessageHandler_.accept(message); | 
| 109 this.handleInvalidIncomingMessage_(message, err); | |
| 110 }; | |
| 111 | |
| 112 Router.prototype.handleValidIncomingMessage_ = function(message) { | |
| 113 if (this.testingController_) | |
| 114 return; | |
| 115 | |
| 116 if (message.expectsResponse()) { | |
| 117 if (controlMessageHandler.isControlMessage(message)) { | |
| 118 if (this.controlMessageHandler_) { | |
| 119 this.controlMessageHandler_.acceptWithResponder(message, this); | |
| 120 } else { | |
| 121 this.close(); | |
| 122 } | |
| 123 } else if (this.incomingReceiver_) { | |
| 124 this.incomingReceiver_.acceptWithResponder(message, this); | |
| 125 } else { | |
| 126 // If we receive a request expecting a response when the client is not | |
| 127 // listening, then we have no choice but to tear down the pipe. | |
| 128 this.close(); | |
| 129 } | |
| 130 } else if (message.isResponse()) { | |
| 131 var reader = new MessageReader(message); | |
| 132 var requestID = reader.requestID; | |
| 133 var completer = this.completers_.get(requestID); | |
| 134 if (completer) { | |
| 135 this.completers_.delete(requestID); | |
| 136 completer.resolve(message); | |
| 137 } else { | |
| 138 console.log("Unexpected response with request ID: " + requestID); | |
| 139 } | |
| 140 } else { | 152 } else { | 
| 141 if (controlMessageHandler.isControlMessage(message)) { | 153 var interfaceId = message.getInterfaceId(); | 
| 142 if (this.controlMessageHandler_) { | 154 var endpoint = this.endpoints_.get(interfaceId); | 
| 143 var ok = this.controlMessageHandler_.accept(message); | 155 if (!endpoint || endpoint.closed) { | 
| 144 if (ok) return; | 156 return true; | 
| 145 } | 157 } | 
| 146 this.close(); | 158 | 
| 147 } else if (this.incomingReceiver_) { | 159 if (!endpoint.client) { | 
| 148 this.incomingReceiver_.accept(message); | 160 // We need to wait until a client is attached in order to dispatch | 
| 149 } | 161 // further messages. | 
| 150 } | 162 return false; | 
| 151 }; | 163 } | 
| 152 | 164 ok = endpoint.client.handleIncomingMessage_(message); | 
| 153 Router.prototype.handleInvalidIncomingMessage_ = function(message, error) { | 165 } | 
| 154 if (!this.testingController_) { | 166 | 
| 167 if (!ok) { | |
| 168 this.handleInvalidIncomingMessage_(); | |
| 169 } | |
| 170 return ok; | |
| 171 }; | |
| 172 | |
| 173 Router.prototype.close = function() { | |
| 174 this.connector_.close(); | |
| 175 // Closing the message pipe won't trigger connection error handler. | |
| 176 // Explicitly call onPipeConnectionError() so that associated endpoints | |
| 177 // will get notified. | |
| 178 this.onPipeConnectionError(); | |
| 179 }; | |
| 180 | |
| 181 Router.prototype.waitForNextMessageForTesting = function() { | |
| 182 this.connector_.waitForNextMessageForTesting(); | |
| 183 }; | |
| 184 | |
| 185 Router.prototype.handleInvalidIncomingMessage_ = function(message) { | |
| 186 if (!validator.isTestingMode()) { | |
| 155 // TODO(yzshen): Consider notifying the embedder. | 187 // TODO(yzshen): Consider notifying the embedder. | 
| 156 // TODO(yzshen): This should also trigger connection error handler. | 188 // TODO(yzshen): This should also trigger connection error handler. | 
| 157 // Consider making accept() return a boolean and let the connector deal | 189 // Consider making accept() return a boolean and let the connector deal | 
| 158 // with this, as the C++ code does. | 190 // with this, as the C++ code does. | 
| 159 console.log("Invalid message: " + validator.validationError[error]); | |
| 160 | |
| 161 this.close(); | 191 this.close(); | 
| 162 return; | 192 return; | 
| 163 } | 193 } | 
| 164 | 194 }; | 
| 165 this.testingController_.onInvalidIncomingMessage(error); | 195 | 
| 166 }; | 196 Router.prototype.onPeerAssociatedEndpointClosed = function(interfaceId, | 
| 167 | 197 reason) { | 
| 168 Router.prototype.handleConnectionError_ = function(result) { | 198 check(!types.isMasterInterfaceId(interfaceId) || reason); | 
| 169 this.completers_.forEach(function(value) { | 199 | 
| 170 value.reject(result); | 200 var endpoint = this.endpoints_.get(interfaceId); | 
| 171 }); | 201 if (!endpoint) { | 
| 172 if (this.errorHandler_) | 202 endpoint = new InterfaceEndpoint(this, interfaceId); | 
| 173 this.errorHandler_(); | 203 this.endpoints_.set(interfaceId, endpoint); | 
| 174 this.close(); | 204 } | 
| 175 }; | 205 | 
| 176 | 206 if (reason) { | 
| 177 // The RouterTestingController is used in unit tests. It defeats valid message | 207 endpoint.disconnectReason = reason; | 
| 178 // handling and delgates invalid message handling. | 208 } | 
| 179 | 209 | 
| 180 function RouterTestingController(connector) { | 210 if (!endpoint.peerClosed) { | 
| 181 this.connector_ = connector; | 211 if (endpoint.client) { | 
| 182 this.invalidMessageHandler_ = null; | 212 timer.createOneShot(0, | 
| 183 } | 213 endpoint.client.notifyError.bind(endpoint.client, reason)); | 
| 184 | 214 } | 
| 185 RouterTestingController.prototype.waitForNextMessage = function() { | 215 this.updateEndpointStateMayRemove(endpoint, | 
| 186 this.connector_.waitForNextMessageForTesting(); | 216 EndpointStateUpdateType.PEER_ENDPOINT_CLOSED); | 
| 187 }; | 217 } | 
| 188 | 218 return true; | 
| 189 RouterTestingController.prototype.setInvalidIncomingMessageHandler = | 219 }; | 
| 190 function(callback) { | 220 | 
| 191 this.invalidMessageHandler_ = callback; | 221 Router.prototype.onPipeConnectionError = function() { | 
| 192 }; | 222 this.encounteredError_ = true; | 
| 193 | 223 | 
| 194 RouterTestingController.prototype.onInvalidIncomingMessage = | 224 for (var endpoint of this.endpoints_.values()) { | 
| 195 function(error) { | 225 if (endpoint.client) { | 
| 196 if (this.invalidMessageHandler_) | 226 timer.createOneShot(0, | 
| 197 this.invalidMessageHandler_(error); | 227 endpoint.client.notifyError.bind(endpoint.client, | 
| 228 endpoint.disconnectReason)); | |
| 229 } | |
| 230 this.updateEndpointStateMayRemove(endpoint, | |
| 231 EndpointStateUpdateType.PEER_ENDPOINT_CLOSED); | |
| 232 } | |
| 233 }; | |
| 234 | |
| 235 Router.prototype.closeEndpointHandle = function(interfaceId, reason) { | |
| 236 if (!types.isValidInterfaceId(interfaceId)) { | |
| 237 return; | |
| 238 } | |
| 239 var endpoint = this.endpoints_.get(interfaceId); | |
| 240 check(endpoint); | |
| 241 check(!endpoint.client); | |
| 242 check(!endpoint.closed); | |
| 243 | |
| 244 this.updateEndpointStateMayRemove(endpoint, | |
| 245 EndpointStateUpdateType.ENDPOINT_CLOSED); | |
| 246 | |
| 247 if (!types.isMasterInterfaceId(interfaceId) || reason) { | |
| 248 this.controlMessageProxy_.notifyPeerEndpointClosed(interfaceId, reason); | |
| 249 } | |
| 250 }; | |
| 251 | |
| 252 Router.prototype.updateEndpointStateMayRemove = function(endpoint, | |
| 253 endpointStateUpdateType) { | |
| 254 if (endpointStateUpdateType === EndpointStateUpdateType.ENDPOINT_CLOSED) { | |
| 255 endpoint.closed = true; | |
| 256 } else { | |
| 257 endpoint.peerClosed = true; | |
| 258 } | |
| 259 if (endpoint.closed && endpoint.peerClosed) { | |
| 260 this.endpoints_.delete(endpoint.id); | |
| 261 } | |
| 198 }; | 262 }; | 
| 199 | 263 | 
| 200 var exports = {}; | 264 var exports = {}; | 
| 201 exports.Router = Router; | 265 exports.Router = Router; | 
| 202 return exports; | 266 return exports; | 
| 203 }); | 267 }); | 
| OLD | NEW |