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/codec", | 7 "mojo/public/js/codec", |
7 "mojo/public/js/core", | 8 "mojo/public/js/core", |
8 "mojo/public/js/connector", | 9 "mojo/public/js/connector", |
9 "mojo/public/js/validator", | 10 "mojo/public/js/validator", |
10 ], function(codec, core, connector, validator) { | 11 ], function(console, codec, core, connector, validator) { |
11 | 12 |
12 var Connector = connector.Connector; | 13 var Connector = connector.Connector; |
13 var MessageReader = codec.MessageReader; | 14 var MessageReader = codec.MessageReader; |
14 var Validator = validator.Validator; | 15 var Validator = validator.Validator; |
15 | 16 |
16 function Router(handle, connectorFactory) { | 17 function Router(handle, connectorFactory) { |
17 if (!core.isHandle(handle)) | 18 if (!core.isHandle(handle)) |
18 throw new Error("Router constructor: Not a handle"); | 19 throw new Error("Router constructor: Not a handle"); |
19 if (connectorFactory === undefined) | 20 if (connectorFactory === undefined) |
20 connectorFactory = Connector; | 21 connectorFactory = Connector; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 this.incomingReceiver_.acceptWithResponder(message, this); | 111 this.incomingReceiver_.acceptWithResponder(message, this); |
111 } else { | 112 } else { |
112 // If we receive a request expecting a response when the client is not | 113 // If we receive a request expecting a response when the client is not |
113 // listening, then we have no choice but to tear down the pipe. | 114 // listening, then we have no choice but to tear down the pipe. |
114 this.close(); | 115 this.close(); |
115 } | 116 } |
116 } else if (message.isResponse()) { | 117 } else if (message.isResponse()) { |
117 var reader = new MessageReader(message); | 118 var reader = new MessageReader(message); |
118 var requestID = reader.requestID; | 119 var requestID = reader.requestID; |
119 var completer = this.completers_.get(requestID); | 120 var completer = this.completers_.get(requestID); |
120 this.completers_.delete(requestID); | 121 if (completer) { |
121 completer.resolve(message); | 122 this.completers_.delete(requestID); |
| 123 completer.resolve(message); |
| 124 } else { |
| 125 console.log("Unexpected response with request ID: " + requestID); |
| 126 } |
122 } else { | 127 } else { |
123 if (this.incomingReceiver_) | 128 if (this.incomingReceiver_) |
124 this.incomingReceiver_.accept(message); | 129 this.incomingReceiver_.accept(message); |
125 } | 130 } |
126 }; | 131 }; |
127 | 132 |
128 Router.prototype.handleInvalidIncomingMessage_ = function(message, error) { | 133 Router.prototype.handleInvalidIncomingMessage_ = function(message, error) { |
129 if (!this.testingController_) { | 134 if (!this.testingController_) { |
130 // TODO(yzshen): Consider logging and notifying the embedder. | 135 // TODO(yzshen): Consider notifying the embedder. |
131 // TODO(yzshen): This should also trigger connection error handler. | 136 // TODO(yzshen): This should also trigger connection error handler. |
132 // Consider making accept() return a boolean and let the connector deal | 137 // Consider making accept() return a boolean and let the connector deal |
133 // with this, as the C++ code does. | 138 // with this, as the C++ code does. |
| 139 console.log("Invalid message: " + validator.validationError[error]); |
| 140 |
134 this.close(); | 141 this.close(); |
135 return; | 142 return; |
136 } | 143 } |
137 | 144 |
138 this.testingController_.onInvalidIncomingMessage(error); | 145 this.testingController_.onInvalidIncomingMessage(error); |
139 }; | 146 }; |
140 | 147 |
141 Router.prototype.handleConnectionError_ = function(result) { | 148 Router.prototype.handleConnectionError_ = function(result) { |
142 this.completers_.forEach(function(value) { | 149 this.completers_.forEach(function(value) { |
143 value.reject(result); | 150 value.reject(result); |
(...skipping 23 matching lines...) Expand all Loading... |
167 RouterTestingController.prototype.onInvalidIncomingMessage = | 174 RouterTestingController.prototype.onInvalidIncomingMessage = |
168 function(error) { | 175 function(error) { |
169 if (this.invalidMessageHandler_) | 176 if (this.invalidMessageHandler_) |
170 this.invalidMessageHandler_(error); | 177 this.invalidMessageHandler_(error); |
171 }; | 178 }; |
172 | 179 |
173 var exports = {}; | 180 var exports = {}; |
174 exports.Router = Router; | 181 exports.Router = Router; |
175 return exports; | 182 return exports; |
176 }); | 183 }); |
OLD | NEW |