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, connectorFactory) { |
12 this.connector_ = new connector.Connector(handle); | 12 if (connectorFactory === undefined) |
13 connectorFactory = connector.Connector; | |
14 this.connector_ = new connectorFactory(handle); | |
13 this.incomingReceiver_ = null; | 15 this.incomingReceiver_ = null; |
14 this.nextRequestID_ = 0; | 16 this.nextRequestID_ = 0; |
15 this.responders_ = {}; | 17 this.responders_ = {}; |
16 this.payloadValidators_ = []; | 18 this.payloadValidators_ = []; |
17 | 19 |
18 this.connector_.setIncomingReceiver({ | 20 this.connector_.setIncomingReceiver({ |
19 accept: this.handleIncomingMessage_.bind(this), | 21 accept: this.handleIncomingMessage_.bind(this), |
20 }); | 22 }); |
21 this.connector_.setErrorHandler({ | 23 this.connector_.setErrorHandler({ |
22 onError: this.handleConnectionError_.bind(this), | 24 onError: this.handleConnectionError_.bind(this), |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 Router.prototype.encounteredError = function() { | 66 Router.prototype.encounteredError = function() { |
65 return this.connector_.encounteredError(); | 67 return this.connector_.encounteredError(); |
66 }; | 68 }; |
67 | 69 |
68 Router.prototype.handleIncomingMessage_ = function(message) { | 70 Router.prototype.handleIncomingMessage_ = function(message) { |
69 var noError = validator.validationError.NONE; | 71 var noError = validator.validationError.NONE; |
70 var messageValidator = new validator.Validator(message); | 72 var messageValidator = new validator.Validator(message); |
71 var err = messageValidator.validateMessageHeader(); | 73 var err = messageValidator.validateMessageHeader(); |
72 for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i) | 74 for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i) |
73 err = this.payloadValidators_[i](messageValidator); | 75 err = this.payloadValidators_[i](messageValidator); |
74 if (err !== noError) | |
75 this.close(); | |
76 | 76 |
77 if (err == noError) | |
78 this.handleValidIncomingMessage_(message); | |
79 else | |
80 this.handleInvalidIncomingMessage_(message, err); | |
81 }; | |
82 | |
83 Router.prototype.handleValidIncomingMessage_ = function(message) { | |
77 if (message.expectsResponse()) { | 84 if (message.expectsResponse()) { |
78 if (this.incomingReceiver_) { | 85 if (this.incomingReceiver_) { |
79 this.incomingReceiver_.acceptWithResponder(message, this); | 86 this.incomingReceiver_.acceptWithResponder(message, this); |
80 } else { | 87 } else { |
81 // If we receive a request expecting a response when the client is not | 88 // If we receive a request expecting a response when the client is not |
82 // listening, then we have no choice but to tear down the pipe. | 89 // listening, then we have no choice but to tear down the pipe. |
83 this.close(); | 90 this.close(); |
84 } | 91 } |
85 } else if (message.isResponse()) { | 92 } else if (message.isResponse()) { |
86 var reader = new codec.MessageReader(message); | 93 var reader = new codec.MessageReader(message); |
87 var requestID = reader.requestID; | 94 var requestID = reader.requestID; |
88 var responder = this.responders_[requestID]; | 95 var responder = this.responders_[requestID]; |
89 delete this.responders_[requestID]; | 96 delete this.responders_[requestID]; |
90 responder.accept(message); | 97 responder.accept(message); |
91 } else { | 98 } else { |
92 if (this.incomingReceiver_) | 99 if (this.incomingReceiver_) |
93 this.incomingReceiver_.accept(message); | 100 this.incomingReceiver_.accept(message); |
94 } | 101 } |
95 }; | 102 } |
103 | |
104 Router.prototype.handleInvalidIncomingMessage_ = function(message, error) { | |
105 this.close(); | |
106 } | |
96 | 107 |
97 Router.prototype.handleConnectionError_ = function(result) { | 108 Router.prototype.handleConnectionError_ = function(result) { |
98 for (var each in this.responders_) | 109 for (var each in this.responders_) |
99 this.responders_[each].reject(result); | 110 this.responders_[each].reject(result); |
100 this.close(); | 111 this.close(); |
101 }; | 112 }; |
102 | 113 |
114 // The TestRouter subclass is only intended to be used in unit tests. | |
115 // It defeats valid message handling and delgates invalid message handling. | |
116 | |
117 function TestRouter(handle, connectorFactory) { | |
118 Router.call(this, handle, connectorFactory); | |
119 } | |
120 | |
121 TestRouter.prototype = Object.create(Router.prototype); | |
122 | |
123 TestRouter.prototype.handleValidIncomingMessage_ = function() { | |
124 }; | |
125 | |
126 TestRouter.prototype.handleInvalidIncomingMessage_ = | |
127 function(message, error) { | |
yzshen1
2014/09/15 17:32:19
formatting nit:
I guess we want line 127 to have 4
hansmuller
2014/09/15 18:07:34
As far as I can tell from the style guide, either
| |
128 this.validationErrorHandler(error); | |
129 }; | |
130 | |
103 var exports = {}; | 131 var exports = {}; |
104 exports.Router = Router; | 132 exports.Router = Router; |
133 exports.TestRouter = TestRouter; | |
105 return exports; | 134 return exports; |
106 }); | 135 }); |
OLD | NEW |