Chromium Code Reviews| 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 "console", |
| 7 "mojo/public/js/codec", | 7 "mojo/public/js/codec", |
| 8 "mojo/public/js/core", | 8 "mojo/public/js/core", |
| 9 "mojo/public/js/connector", | 9 "mojo/public/js/connector", |
| 10 "mojo/public/js/validator", | 10 "mojo/public/js/validator", |
| 11 ], function(console, codec, core, connector, validator) { | 11 "mojo/public/js/lib/control_message_handler", |
| 12 ], function(console, codec, core, connector, validator, controlMessageHandler) { | |
| 12 | 13 |
| 13 var Connector = connector.Connector; | 14 var Connector = connector.Connector; |
| 14 var MessageReader = codec.MessageReader; | 15 var MessageReader = codec.MessageReader; |
| 15 var Validator = validator.Validator; | 16 var Validator = validator.Validator; |
| 17 var ControlMessageHandler = controlMessageHandler.ControlMessageHandler; | |
| 16 | 18 |
| 17 function Router(handle, connectorFactory) { | 19 function Router(handle, interface_version, connectorFactory) { |
| 18 if (!core.isHandle(handle)) | 20 if (!core.isHandle(handle)) |
| 19 throw new Error("Router constructor: Not a handle"); | 21 throw new Error("Router constructor: Not a handle"); |
| 20 if (connectorFactory === undefined) | 22 if (connectorFactory === undefined) |
| 21 connectorFactory = Connector; | 23 connectorFactory = Connector; |
| 22 this.connector_ = new connectorFactory(handle); | 24 this.connector_ = new connectorFactory(handle); |
| 23 this.incomingReceiver_ = null; | 25 this.incomingReceiver_ = null; |
| 24 this.errorHandler_ = null; | 26 this.errorHandler_ = null; |
| 25 this.nextRequestID_ = 0; | 27 this.nextRequestID_ = 0; |
| 26 this.completers_ = new Map(); | 28 this.completers_ = new Map(); |
| 27 this.payloadValidators_ = []; | 29 this.payloadValidators_ = []; |
| 28 this.testingController_ = null; | 30 this.testingController_ = null; |
| 29 | 31 |
| 32 if (interface_version !== undefined) { | |
| 33 this.controlMsgHandler_ = new ControlMessageHandler(this, | |
|
yzshen1
2017/02/11 01:11:29
It is preferred to use full name: Msg -> Message.
wangjimmy
2017/02/13 20:43:07
Done.
| |
| 34 interface_version); | |
|
yzshen1
2017/02/11 01:11:29
4 spaces. Please see: https://google.github.io/sty
wangjimmy
2017/02/13 20:43:07
Done.
| |
| 35 } | |
| 36 | |
| 30 this.connector_.setIncomingReceiver({ | 37 this.connector_.setIncomingReceiver({ |
| 31 accept: this.handleIncomingMessage_.bind(this), | 38 accept: this.handleIncomingMessage_.bind(this), |
| 32 }); | 39 }); |
| 33 this.connector_.setErrorHandler({ | 40 this.connector_.setErrorHandler({ |
| 34 onError: this.handleConnectionError_.bind(this), | 41 onError: this.handleConnectionError_.bind(this), |
| 35 }); | 42 }); |
| 36 } | 43 } |
| 37 | 44 |
| 38 Router.prototype.close = function() { | 45 Router.prototype.close = function() { |
| 39 this.completers_.clear(); // Drop any responders. | 46 this.completers_.clear(); // Drop any responders. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 this.handleValidIncomingMessage_(message); | 107 this.handleValidIncomingMessage_(message); |
| 101 else | 108 else |
| 102 this.handleInvalidIncomingMessage_(message, err); | 109 this.handleInvalidIncomingMessage_(message, err); |
| 103 }; | 110 }; |
| 104 | 111 |
| 105 Router.prototype.handleValidIncomingMessage_ = function(message) { | 112 Router.prototype.handleValidIncomingMessage_ = function(message) { |
| 106 if (this.testingController_) | 113 if (this.testingController_) |
| 107 return; | 114 return; |
| 108 | 115 |
| 109 if (message.expectsResponse()) { | 116 if (message.expectsResponse()) { |
| 110 if (this.incomingReceiver_) { | 117 if (this.controlMsgHandler_ && |
| 118 controlMessageHandler.isControlMessage(message)) { | |
|
yzshen1
2017/02/11 01:11:29
if the message is a control message but we don't h
wangjimmy
2017/02/13 20:43:07
Done.
| |
| 119 this.controlMsgHandler_.acceptWithResponder(message, this); | |
| 120 } else if (this.incomingReceiver_) { | |
| 111 this.incomingReceiver_.acceptWithResponder(message, this); | 121 this.incomingReceiver_.acceptWithResponder(message, this); |
| 112 } else { | 122 } else { |
| 113 // If we receive a request expecting a response when the client is not | 123 // If we receive a request expecting a response when the client is not |
| 114 // listening, then we have no choice but to tear down the pipe. | 124 // listening, then we have no choice but to tear down the pipe. |
| 115 this.close(); | 125 this.close(); |
| 116 } | 126 } |
| 117 } else if (message.isResponse()) { | 127 } else if (message.isResponse()) { |
| 118 var reader = new MessageReader(message); | 128 var reader = new MessageReader(message); |
| 119 var requestID = reader.requestID; | 129 var requestID = reader.requestID; |
| 120 var completer = this.completers_.get(requestID); | 130 var completer = this.completers_.get(requestID); |
| 121 if (completer) { | 131 if (completer) { |
| 122 this.completers_.delete(requestID); | 132 this.completers_.delete(requestID); |
| 123 completer.resolve(message); | 133 completer.resolve(message); |
| 124 } else { | 134 } else { |
| 125 console.log("Unexpected response with request ID: " + requestID); | 135 console.log("Unexpected response with request ID: " + requestID); |
| 126 } | 136 } |
| 127 } else { | 137 } else { |
| 128 if (this.incomingReceiver_) | 138 if (this.controlMsgHandler_ && |
| 139 controlMessageHandler.isControlMessage(message)) { | |
| 140 var ok = this.controlMsgHandler_.accept(message); | |
| 141 if (!ok) { | |
| 142 this.close(); | |
| 143 } | |
| 144 } else if (this.incomingReceiver_) { | |
| 129 this.incomingReceiver_.accept(message); | 145 this.incomingReceiver_.accept(message); |
| 146 } | |
| 130 } | 147 } |
| 131 }; | 148 }; |
| 132 | 149 |
| 133 Router.prototype.handleInvalidIncomingMessage_ = function(message, error) { | 150 Router.prototype.handleInvalidIncomingMessage_ = function(message, error) { |
| 134 if (!this.testingController_) { | 151 if (!this.testingController_) { |
| 135 // TODO(yzshen): Consider notifying the embedder. | 152 // TODO(yzshen): Consider notifying the embedder. |
| 136 // TODO(yzshen): This should also trigger connection error handler. | 153 // TODO(yzshen): This should also trigger connection error handler. |
| 137 // Consider making accept() return a boolean and let the connector deal | 154 // Consider making accept() return a boolean and let the connector deal |
| 138 // with this, as the C++ code does. | 155 // with this, as the C++ code does. |
| 139 console.log("Invalid message: " + validator.validationError[error]); | 156 console.log("Invalid message: " + validator.validationError[error]); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 RouterTestingController.prototype.onInvalidIncomingMessage = | 191 RouterTestingController.prototype.onInvalidIncomingMessage = |
| 175 function(error) { | 192 function(error) { |
| 176 if (this.invalidMessageHandler_) | 193 if (this.invalidMessageHandler_) |
| 177 this.invalidMessageHandler_(error); | 194 this.invalidMessageHandler_(error); |
| 178 }; | 195 }; |
| 179 | 196 |
| 180 var exports = {}; | 197 var exports = {}; |
| 181 exports.Router = Router; | 198 exports.Router = Router; |
| 182 return exports; | 199 return exports; |
| 183 }); | 200 }); |
| OLD | NEW |