| 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 (function() { | 5 define("mojo/public/js/router", [ |
| 6 var internal = mojoBindings.internal; | 6 "console", |
| 7 "mojo/public/js/codec", |
| 8 "mojo/public/js/core", |
| 9 "mojo/public/js/connector", |
| 10 "mojo/public/js/lib/control_message_handler", |
| 11 "mojo/public/js/validator", |
| 12 ], function(console, codec, core, connector, controlMessageHandler, validator) { |
| 13 |
| 14 var Connector = connector.Connector; |
| 15 var MessageReader = codec.MessageReader; |
| 16 var Validator = validator.Validator; |
| 17 var ControlMessageHandler = controlMessageHandler.ControlMessageHandler; |
| 7 | 18 |
| 8 function Router(handle, interface_version, connectorFactory) { | 19 function Router(handle, interface_version, connectorFactory) { |
| 9 if (!(handle instanceof MojoHandle)) | 20 if (!core.isHandle(handle)) |
| 10 throw new Error("Router constructor: Not a handle"); | 21 throw new Error("Router constructor: Not a handle"); |
| 11 if (connectorFactory === undefined) | 22 if (connectorFactory === undefined) |
| 12 connectorFactory = internal.Connector; | 23 connectorFactory = Connector; |
| 13 this.connector_ = new connectorFactory(handle); | 24 this.connector_ = new connectorFactory(handle); |
| 14 this.incomingReceiver_ = null; | 25 this.incomingReceiver_ = null; |
| 15 this.errorHandler_ = null; | 26 this.errorHandler_ = null; |
| 16 this.nextRequestID_ = 0; | 27 this.nextRequestID_ = 0; |
| 17 this.completers_ = new Map(); | 28 this.completers_ = new Map(); |
| 18 this.payloadValidators_ = []; | 29 this.payloadValidators_ = []; |
| 19 this.testingController_ = null; | 30 this.testingController_ = null; |
| 20 | 31 |
| 21 if (interface_version !== undefined) { | 32 if (interface_version !== undefined) { |
| 22 this.controlMessageHandler_ = new | 33 this.controlMessageHandler_ = new |
| 23 internal.ControlMessageHandler(interface_version); | 34 ControlMessageHandler(interface_version); |
| 24 } | 35 } |
| 25 | 36 |
| 26 this.connector_.setIncomingReceiver({ | 37 this.connector_.setIncomingReceiver({ |
| 27 accept: this.handleIncomingMessage_.bind(this), | 38 accept: this.handleIncomingMessage_.bind(this), |
| 28 }); | 39 }); |
| 29 this.connector_.setErrorHandler({ | 40 this.connector_.setErrorHandler({ |
| 30 onError: this.handleConnectionError_.bind(this), | 41 onError: this.handleConnectionError_.bind(this), |
| 31 }); | 42 }); |
| 32 } | 43 } |
| 33 | 44 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 Router.prototype.encounteredError = function() { | 90 Router.prototype.encounteredError = function() { |
| 80 return this.connector_.encounteredError(); | 91 return this.connector_.encounteredError(); |
| 81 }; | 92 }; |
| 82 | 93 |
| 83 Router.prototype.enableTestingMode = function() { | 94 Router.prototype.enableTestingMode = function() { |
| 84 this.testingController_ = new RouterTestingController(this.connector_); | 95 this.testingController_ = new RouterTestingController(this.connector_); |
| 85 return this.testingController_; | 96 return this.testingController_; |
| 86 }; | 97 }; |
| 87 | 98 |
| 88 Router.prototype.handleIncomingMessage_ = function(message) { | 99 Router.prototype.handleIncomingMessage_ = function(message) { |
| 89 var noError = internal.validationError.NONE; | 100 var noError = validator.validationError.NONE; |
| 90 var messageValidator = new internal.Validator(message); | 101 var messageValidator = new Validator(message); |
| 91 var err = messageValidator.validateMessageHeader(); | 102 var err = messageValidator.validateMessageHeader(); |
| 92 for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i) | 103 for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i) |
| 93 err = this.payloadValidators_[i](messageValidator); | 104 err = this.payloadValidators_[i](messageValidator); |
| 94 | 105 |
| 95 if (err == noError) | 106 if (err == noError) |
| 96 this.handleValidIncomingMessage_(message); | 107 this.handleValidIncomingMessage_(message); |
| 97 else | 108 else |
| 98 this.handleInvalidIncomingMessage_(message, err); | 109 this.handleInvalidIncomingMessage_(message, err); |
| 99 }; | 110 }; |
| 100 | 111 |
| 101 Router.prototype.handleValidIncomingMessage_ = function(message) { | 112 Router.prototype.handleValidIncomingMessage_ = function(message) { |
| 102 if (this.testingController_) | 113 if (this.testingController_) |
| 103 return; | 114 return; |
| 104 | 115 |
| 105 if (message.expectsResponse()) { | 116 if (message.expectsResponse()) { |
| 106 if (internal.isInterfaceControlMessage(message)) { | 117 if (controlMessageHandler.isControlMessage(message)) { |
| 107 if (this.controlMessageHandler_) { | 118 if (this.controlMessageHandler_) { |
| 108 this.controlMessageHandler_.acceptWithResponder(message, this); | 119 this.controlMessageHandler_.acceptWithResponder(message, this); |
| 109 } else { | 120 } else { |
| 110 this.close(); | 121 this.close(); |
| 111 } | 122 } |
| 112 } else if (this.incomingReceiver_) { | 123 } else if (this.incomingReceiver_) { |
| 113 this.incomingReceiver_.acceptWithResponder(message, this); | 124 this.incomingReceiver_.acceptWithResponder(message, this); |
| 114 } else { | 125 } else { |
| 115 // If we receive a request expecting a response when the client is not | 126 // If we receive a request expecting a response when the client is not |
| 116 // listening, then we have no choice but to tear down the pipe. | 127 // listening, then we have no choice but to tear down the pipe. |
| 117 this.close(); | 128 this.close(); |
| 118 } | 129 } |
| 119 } else if (message.isResponse()) { | 130 } else if (message.isResponse()) { |
| 120 var reader = new internal.MessageReader(message); | 131 var reader = new MessageReader(message); |
| 121 var requestID = reader.requestID; | 132 var requestID = reader.requestID; |
| 122 var completer = this.completers_.get(requestID); | 133 var completer = this.completers_.get(requestID); |
| 123 if (completer) { | 134 if (completer) { |
| 124 this.completers_.delete(requestID); | 135 this.completers_.delete(requestID); |
| 125 completer.resolve(message); | 136 completer.resolve(message); |
| 126 } else { | 137 } else { |
| 127 console.log("Unexpected response with request ID: " + requestID); | 138 console.log("Unexpected response with request ID: " + requestID); |
| 128 } | 139 } |
| 129 } else { | 140 } else { |
| 130 if (internal.isInterfaceControlMessage(message)) { | 141 if (controlMessageHandler.isControlMessage(message)) { |
| 131 if (this.controlMessageHandler_) { | 142 if (this.controlMessageHandler_) { |
| 132 var ok = this.controlMessageHandler_.accept(message); | 143 var ok = this.controlMessageHandler_.accept(message); |
| 133 if (ok) return; | 144 if (ok) return; |
| 134 } | 145 } |
| 135 this.close(); | 146 this.close(); |
| 136 } else if (this.incomingReceiver_) { | 147 } else if (this.incomingReceiver_) { |
| 137 this.incomingReceiver_.accept(message); | 148 this.incomingReceiver_.accept(message); |
| 138 } | 149 } |
| 139 } | 150 } |
| 140 }; | 151 }; |
| 141 | 152 |
| 142 Router.prototype.handleInvalidIncomingMessage_ = function(message, error) { | 153 Router.prototype.handleInvalidIncomingMessage_ = function(message, error) { |
| 143 if (!this.testingController_) { | 154 if (!this.testingController_) { |
| 144 // TODO(yzshen): Consider notifying the embedder. | 155 // TODO(yzshen): Consider notifying the embedder. |
| 145 // TODO(yzshen): This should also trigger connection error handler. | 156 // TODO(yzshen): This should also trigger connection error handler. |
| 146 // Consider making accept() return a boolean and let the connector deal | 157 // Consider making accept() return a boolean and let the connector deal |
| 147 // with this, as the C++ code does. | 158 // with this, as the C++ code does. |
| 148 console.log("Invalid message: " + internal.validationError[error]); | 159 console.log("Invalid message: " + validator.validationError[error]); |
| 149 | 160 |
| 150 this.close(); | 161 this.close(); |
| 151 return; | 162 return; |
| 152 } | 163 } |
| 153 | 164 |
| 154 this.testingController_.onInvalidIncomingMessage(error); | 165 this.testingController_.onInvalidIncomingMessage(error); |
| 155 }; | 166 }; |
| 156 | 167 |
| 157 Router.prototype.handleConnectionError_ = function(result) { | 168 Router.prototype.handleConnectionError_ = function(result) { |
| 158 this.completers_.forEach(function(value) { | 169 this.completers_.forEach(function(value) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 179 function(callback) { | 190 function(callback) { |
| 180 this.invalidMessageHandler_ = callback; | 191 this.invalidMessageHandler_ = callback; |
| 181 }; | 192 }; |
| 182 | 193 |
| 183 RouterTestingController.prototype.onInvalidIncomingMessage = | 194 RouterTestingController.prototype.onInvalidIncomingMessage = |
| 184 function(error) { | 195 function(error) { |
| 185 if (this.invalidMessageHandler_) | 196 if (this.invalidMessageHandler_) |
| 186 this.invalidMessageHandler_(error); | 197 this.invalidMessageHandler_(error); |
| 187 }; | 198 }; |
| 188 | 199 |
| 189 internal.Router = Router; | 200 var exports = {}; |
| 190 })(); | 201 exports.Router = Router; |
| 202 return exports; |
| 203 }); |
| OLD | NEW |