| 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/bindings/router", [ | 5 define("mojo/public/js/bindings/router", [ |
| 6 "mojo/public/js/bindings/codec", | 6 "mojo/public/js/bindings/codec", |
| 7 "mojo/public/js/bindings/connector", | 7 "mojo/public/js/bindings/connector", |
| 8 "mojo/public/js/bindings/validator", | 8 "mojo/public/js/bindings/validator", |
| 9 ], function(codec, connector, validator) { | 9 ], function(codec, connector, validator) { |
| 10 | 10 |
| 11 function Router(handle) { | 11 function Router(handle) { |
| 12 this.connector_ = new connector.Connector(handle); | 12 this.connector_ = new connector.Connector(handle); |
| 13 this.incomingReceiver_ = null; | 13 this.incomingReceiver_ = null; |
| 14 this.nextRequestID_ = 0; | 14 this.nextRequestID_ = 0; |
| 15 this.responders_ = {}; | 15 this.responders_ = {}; |
| 16 this.payloadValidators_ = []; |
| 16 | 17 |
| 17 this.connector_.setIncomingReceiver({ | 18 this.connector_.setIncomingReceiver({ |
| 18 accept: this.handleIncomingMessage_.bind(this), | 19 accept: this.handleIncomingMessage_.bind(this), |
| 19 }); | 20 }); |
| 20 this.connector_.setErrorHandler({ | 21 this.connector_.setErrorHandler({ |
| 21 onError: this.handleConnectionError_.bind(this), | 22 onError: this.handleConnectionError_.bind(this), |
| 22 }); | 23 }); |
| 23 } | 24 } |
| 24 | 25 |
| 25 Router.prototype.close = function() { | 26 Router.prototype.close = function() { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 49 // TODO(mpcomplete): accept should return a Promise too, maybe? | 50 // TODO(mpcomplete): accept should return a Promise too, maybe? |
| 50 if (result) | 51 if (result) |
| 51 return Promise.resolve(); | 52 return Promise.resolve(); |
| 52 return Promise.reject(Error("Connection error")); | 53 return Promise.reject(Error("Connection error")); |
| 53 }; | 54 }; |
| 54 | 55 |
| 55 Router.prototype.setIncomingReceiver = function(receiver) { | 56 Router.prototype.setIncomingReceiver = function(receiver) { |
| 56 this.incomingReceiver_ = receiver; | 57 this.incomingReceiver_ = receiver; |
| 57 }; | 58 }; |
| 58 | 59 |
| 60 Router.prototype.setPayloadValidators = function(payloadValidators) { |
| 61 this.payloadValidators_ = payloadValidators; |
| 62 }; |
| 63 |
| 59 Router.prototype.encounteredError = function() { | 64 Router.prototype.encounteredError = function() { |
| 60 return this.connector_.encounteredError(); | 65 return this.connector_.encounteredError(); |
| 61 }; | 66 }; |
| 62 | 67 |
| 63 Router.prototype.handleIncomingMessage_ = function(message) { | 68 Router.prototype.handleIncomingMessage_ = function(message) { |
| 64 var v = new validator.Validator(message); | 69 var noError = validator.validationError.NONE; |
| 65 if (v.validateMessageHeader() !== validator.validationError.NONE) | 70 var messageValidator = new validator.Validator(message); |
| 71 var err = messageValidator.validateMessageHeader(); |
| 72 for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i) |
| 73 err = this.payloadValidators_[i](messageValidator); |
| 74 if (err !== noError) |
| 66 this.close(); | 75 this.close(); |
| 67 | 76 |
| 68 if (message.expectsResponse()) { | 77 if (message.expectsResponse()) { |
| 69 if (this.incomingReceiver_) { | 78 if (this.incomingReceiver_) { |
| 70 this.incomingReceiver_.acceptWithResponder(message, this); | 79 this.incomingReceiver_.acceptWithResponder(message, this); |
| 71 } else { | 80 } else { |
| 72 // If we receive a request expecting a response when the client is not | 81 // If we receive a request expecting a response when the client is not |
| 73 // listening, then we have no choice but to tear down the pipe. | 82 // listening, then we have no choice but to tear down the pipe. |
| 74 this.close(); | 83 this.close(); |
| 75 } | 84 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 88 Router.prototype.handleConnectionError_ = function(result) { | 97 Router.prototype.handleConnectionError_ = function(result) { |
| 89 for (var each in this.responders_) | 98 for (var each in this.responders_) |
| 90 this.responders_[each].reject(result); | 99 this.responders_[each].reject(result); |
| 91 this.close(); | 100 this.close(); |
| 92 }; | 101 }; |
| 93 | 102 |
| 94 var exports = {}; | 103 var exports = {}; |
| 95 exports.Router = Router; | 104 exports.Router = Router; |
| 96 return exports; | 105 return exports; |
| 97 }); | 106 }); |
| OLD | NEW |