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 ], function(codec, connector) { | 8 "mojo/public/js/bindings/validator", |
| 9 ], function(codec, connector, validator) { |
9 | 10 |
10 function Router(handle) { | 11 function Router(handle) { |
11 this.connector_ = new connector.Connector(handle); | 12 this.connector_ = new connector.Connector(handle); |
12 this.incomingReceiver_ = null; | 13 this.incomingReceiver_ = null; |
13 this.nextRequestID_ = 0; | 14 this.nextRequestID_ = 0; |
14 this.responders_ = {}; | 15 this.responders_ = {}; |
15 | 16 |
16 this.connector_.setIncomingReceiver({ | 17 this.connector_.setIncomingReceiver({ |
17 accept: this.handleIncomingMessage_.bind(this), | 18 accept: this.handleIncomingMessage_.bind(this), |
18 }); | 19 }); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 54 |
54 Router.prototype.setIncomingReceiver = function(receiver) { | 55 Router.prototype.setIncomingReceiver = function(receiver) { |
55 this.incomingReceiver_ = receiver; | 56 this.incomingReceiver_ = receiver; |
56 }; | 57 }; |
57 | 58 |
58 Router.prototype.encounteredError = function() { | 59 Router.prototype.encounteredError = function() { |
59 return this.connector_.encounteredError(); | 60 return this.connector_.encounteredError(); |
60 }; | 61 }; |
61 | 62 |
62 Router.prototype.handleIncomingMessage_ = function(message) { | 63 Router.prototype.handleIncomingMessage_ = function(message) { |
63 var flags = message.getFlags(); | 64 var v = new validator.Validator(message); |
64 if (flags & codec.kMessageExpectsResponse) { | 65 if (v.validateMessage() !== validator.validationError.NONE) |
| 66 this.close(); |
| 67 |
| 68 if (message.expectsResponse()) { |
65 if (this.incomingReceiver_) { | 69 if (this.incomingReceiver_) { |
66 this.incomingReceiver_.acceptWithResponder(message, this); | 70 this.incomingReceiver_.acceptWithResponder(message, this); |
67 } else { | 71 } else { |
68 // If we receive a request expecting a response when the client is not | 72 // If we receive a request expecting a response when the client is not |
69 // listening, then we have no choice but to tear down the pipe. | 73 // listening, then we have no choice but to tear down the pipe. |
70 this.close(); | 74 this.close(); |
71 } | 75 } |
72 } else if (flags & codec.kMessageIsResponse) { | 76 } else if (message.isResponse()) { |
73 var reader = new codec.MessageReader(message); | 77 var reader = new codec.MessageReader(message); |
74 var requestID = reader.requestID; | 78 var requestID = reader.requestID; |
75 var responder = this.responders_[requestID]; | 79 var responder = this.responders_[requestID]; |
76 delete this.responders_[requestID]; | 80 delete this.responders_[requestID]; |
77 responder.accept(message); | 81 responder.accept(message); |
78 } else { | 82 } else { |
79 if (this.incomingReceiver_) | 83 if (this.incomingReceiver_) |
80 this.incomingReceiver_.accept(message); | 84 this.incomingReceiver_.accept(message); |
81 } | 85 } |
82 }; | 86 }; |
83 | 87 |
84 Router.prototype.handleConnectionError_ = function(result) { | 88 Router.prototype.handleConnectionError_ = function(result) { |
85 for (var each in this.responders_) | 89 for (var each in this.responders_) |
86 this.responders_[each].reject(result); | 90 this.responders_[each].reject(result); |
87 this.close(); | 91 this.close(); |
88 }; | 92 }; |
89 | 93 |
90 var exports = {}; | 94 var exports = {}; |
91 exports.Router = Router; | 95 exports.Router = Router; |
92 return exports; | 96 return exports; |
93 }); | 97 }); |
OLD | NEW |