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