Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: mojo/public/js/router.js

Issue 2744963002: Introduce InterfaceEndpointClient(IEC), InterfaceEndpointHandle and (Closed)
Patch Set: Throw the error with the string being the stack trace needed to debug layouts which don't output an… Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/connector",
7 "mojo/public/js/codec",
8 "mojo/public/js/core", 7 "mojo/public/js/core",
9 "mojo/public/js/connector", 8 "mojo/public/js/interface_types",
10 "mojo/public/js/lib/control_message_handler", 9 "mojo/public/js/lib/interface_endpoint_handle",
10 "mojo/public/js/lib/pipe_control_message_handler",
11 "mojo/public/js/lib/pipe_control_message_proxy",
11 "mojo/public/js/validator", 12 "mojo/public/js/validator",
12 ], function(console, codec, core, connector, controlMessageHandler, validator) { 13 "timer",
14 ], function(connector, core, types, interfaceEndpointHandle,
15 controlMessageHandler, controlMessageProxy, validator, timer) {
13 16
14 var Connector = connector.Connector; 17 var Connector = connector.Connector;
15 var MessageReader = codec.MessageReader; 18 var PipeControlMessageHandler =
19 controlMessageHandler.PipeControlMessageHandler;
20 var PipeControlMessageProxy = controlMessageProxy.PipeControlMessageProxy;
16 var Validator = validator.Validator; 21 var Validator = validator.Validator;
17 var ControlMessageHandler = controlMessageHandler.ControlMessageHandler; 22 var InterfaceEndpointHandle = interfaceEndpointHandle.InterfaceEndpointHandle;
18 23
19 function Router(handle, interface_version, connectorFactory) { 24 /**
20 if (!core.isHandle(handle)) 25 * The state of |endpoint|. If both the endpoint and its peer have been
26 * closed, removes it from |endpoints_|.
27 * @enum {string}
28 */
29 var EndpointStateUpdateType = {
30 ENDPOINT_CLOSED: 'endpoint_closed',
31 PEER_ENDPOINT_CLOSED: 'peer_endpoint_closed'
32 };
33
34 function check(condition, output) {
35 if (!condition) {
36 // testharness.js does not rethrow errors so the error stack needs to be
37 // included as a string in the error we throw for debugging layout tests.
38 throw new Error((new Error()).stack);
39 }
40 }
41
42 function InterfaceEndpoint(router, interfaceId) {
43 this.router_ = router;
44 this.id = interfaceId;
45 this.closed = false;
46 this.peerClosed = false;
47 this.handleCreated = false;
48 this.disconnectReason = null;
49 this.client = null;
50 }
51
52 InterfaceEndpoint.prototype.sendMessage = function(message) {
53 message.setInterfaceId(this.id);
54 return this.router_.connector_.accept(message);
55 };
56
57 function Router(handle, setInterfaceIdNamespaceBit) {
58 if (!core.isHandle(handle)) {
21 throw new Error("Router constructor: Not a handle"); 59 throw new Error("Router constructor: Not a handle");
22 if (connectorFactory === undefined) 60 }
23 connectorFactory = Connector; 61 if (setInterfaceIdNamespaceBit === undefined) {
24 this.connector_ = new connectorFactory(handle); 62 setInterfaceIdNamespaceBit = false;
25 this.incomingReceiver_ = null; 63 }
26 this.errorHandler_ = null; 64
27 this.nextRequestID_ = 0; 65 this.connector_ = new Connector(handle);
28 this.completers_ = new Map();
29 this.payloadValidators_ = [];
30 this.testingController_ = null;
31
32 if (interface_version !== undefined) {
33 this.controlMessageHandler_ = new
34 ControlMessageHandler(interface_version);
35 }
36 66
37 this.connector_.setIncomingReceiver({ 67 this.connector_.setIncomingReceiver({
38 accept: this.handleIncomingMessage_.bind(this), 68 accept: this.accept.bind(this),
39 }); 69 });
40 this.connector_.setErrorHandler({ 70 this.connector_.setErrorHandler({
41 onError: this.handleConnectionError_.bind(this), 71 onError: this.onPipeConnectionError.bind(this),
42 }); 72 });
73
74 this.setInterfaceIdNamespaceBit_ = setInterfaceIdNamespaceBit;
75 this.controlMessageHandler_ = new PipeControlMessageHandler(this);
76 this.controlMessageProxy_ = new PipeControlMessageProxy(this.connector_);
77 this.nextInterfaceIdValue = 1;
78 this.encounteredError_ = false;
79 this.endpoints_ = new Map();
43 } 80 }
44 81
45 Router.prototype.close = function() { 82 Router.prototype.attachEndpointClient = function(
46 this.completers_.clear(); // Drop any responders. 83 interfaceEndpointHandle, interfaceEndpointClient) {
47 this.connector_.close(); 84 check(types.isValidInterfaceId(interfaceEndpointHandle.id()));
48 this.testingController_ = null; 85 check(interfaceEndpointClient);
86
87 var endpoint = this.endpoints_.get(interfaceEndpointHandle.id());
88 check(endpoint);
89 check(!endpoint.client);
90 check(!endpoint.closed);
91 endpoint.client = interfaceEndpointClient;
92
93 if (endpoint.peerClosed) {
94 timer.createOneShot(0,
95 endpoint.client.notifyError.bind(endpoint.client));
96 }
97
98 return endpoint;
99 };
100
101 Router.prototype.detachEndpointClient = function(
102 interfaceEndpointHandle) {
103 check(types.isValidInterfaceId(interfaceEndpointHandle.id()));
104 var endpoint = this.endpoints_.get(interfaceEndpointHandle.id());
105 check(endpoint);
106 check(endpoint.client);
107 check(!endpoint.closed);
108
109 endpoint.client = null;
110 };
111
112 Router.prototype.createLocalEndpointHandle = function(
113 interfaceId) {
114 if (!types.isValidInterfaceId(interfaceId)) {
115 return new InterfaceEndpointHandle();
116 }
117
118 var endpoint = this.endpoints_.get(interfaceId);
119
120 if (!endpoint) {
121 endpoint = new InterfaceEndpoint(this, interfaceId);
122 this.endpoints_.set(interfaceId, endpoint);
123
124 check(!endpoint.handleCreated);
125
126 if (this.encounteredError_) {
127 this.updateEndpointStateMayRemove(endpoint,
128 EndpointStateUpdateType.PEER_ENDPOINT_CLOSED);
129 }
130 } else {
131 // If the endpoint already exist, it is because we have received a
132 // notification that the peer endpoint has closed.
133 check(!endpoint.closed);
134 check(endpoint.peerClosed);
135
136 if (endpoint.handleCreated) {
137 return new InterfaceEndpointHandle();
138 }
139 }
140
141 endpoint.handleCreated = true;
142 return new InterfaceEndpointHandle(interfaceId, this);
49 }; 143 };
50 144
51 Router.prototype.accept = function(message) { 145 Router.prototype.accept = function(message) {
52 this.connector_.accept(message);
53 };
54
55 Router.prototype.reject = function(message) {
56 // TODO(mpcomplete): no way to trasmit errors over a Connection.
57 };
58
59 Router.prototype.acceptAndExpectResponse = function(message) {
60 // Reserve 0 in case we want it to convey special meaning in the future.
61 var requestID = this.nextRequestID_++;
62 if (requestID == 0)
63 requestID = this.nextRequestID_++;
64
65 message.setRequestID(requestID);
66 var result = this.connector_.accept(message);
67 if (!result)
68 return Promise.reject(Error("Connection error"));
69
70 var completer = {};
71 this.completers_.set(requestID, completer);
72 return new Promise(function(resolve, reject) {
73 completer.resolve = resolve;
74 completer.reject = reject;
75 });
76 };
77
78 Router.prototype.setIncomingReceiver = function(receiver) {
79 this.incomingReceiver_ = receiver;
80 };
81
82 Router.prototype.setPayloadValidators = function(payloadValidators) {
83 this.payloadValidators_ = payloadValidators;
84 };
85
86 Router.prototype.setErrorHandler = function(handler) {
87 this.errorHandler_ = handler;
88 };
89
90 Router.prototype.encounteredError = function() {
91 return this.connector_.encounteredError();
92 };
93
94 Router.prototype.enableTestingMode = function() {
95 this.testingController_ = new RouterTestingController(this.connector_);
96 return this.testingController_;
97 };
98
99 Router.prototype.handleIncomingMessage_ = function(message) {
100 var noError = validator.validationError.NONE;
101 var messageValidator = new Validator(message); 146 var messageValidator = new Validator(message);
102 var err = messageValidator.validateMessageHeader(); 147 var err = messageValidator.validateMessageHeader();
103 for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i) 148
104 err = this.payloadValidators_[i](messageValidator); 149 var ok = false;
105 150 if (err !== validator.validationError.NONE) {
106 if (err == noError) 151 validator.reportValidationError(err);
107 this.handleValidIncomingMessage_(message); 152 } else if (controlMessageHandler.isPipeControlMessage(message)) {
108 else 153 ok = this.controlMessageHandler_.accept(message);
109 this.handleInvalidIncomingMessage_(message, err);
110 };
111
112 Router.prototype.handleValidIncomingMessage_ = function(message) {
113 if (this.testingController_)
114 return;
115
116 if (message.expectsResponse()) {
117 if (controlMessageHandler.isControlMessage(message)) {
118 if (this.controlMessageHandler_) {
119 this.controlMessageHandler_.acceptWithResponder(message, this);
120 } else {
121 this.close();
122 }
123 } else if (this.incomingReceiver_) {
124 this.incomingReceiver_.acceptWithResponder(message, this);
125 } else {
126 // If we receive a request expecting a response when the client is not
127 // listening, then we have no choice but to tear down the pipe.
128 this.close();
129 }
130 } else if (message.isResponse()) {
131 var reader = new MessageReader(message);
132 var requestID = reader.requestID;
133 var completer = this.completers_.get(requestID);
134 if (completer) {
135 this.completers_.delete(requestID);
136 completer.resolve(message);
137 } else {
138 console.log("Unexpected response with request ID: " + requestID);
139 }
140 } else { 154 } else {
141 if (controlMessageHandler.isControlMessage(message)) { 155 var interfaceId = message.getInterfaceId();
142 if (this.controlMessageHandler_) { 156 var endpoint = this.endpoints_.get(interfaceId);
143 var ok = this.controlMessageHandler_.accept(message); 157 if (!endpoint || endpoint.closed) {
144 if (ok) return; 158 return true;
145 } 159 }
146 this.close(); 160
147 } else if (this.incomingReceiver_) { 161 if (!endpoint.client) {
148 this.incomingReceiver_.accept(message); 162 // We need to wait until a client is attached in order to dispatch
149 } 163 // further messages.
150 } 164 return false;
151 }; 165 }
152 166 ok = endpoint.client.handleIncomingMessage_(message);
153 Router.prototype.handleInvalidIncomingMessage_ = function(message, error) { 167 }
154 if (!this.testingController_) { 168
169 if (!ok) {
170 this.handleInvalidIncomingMessage_();
171 }
172 return ok;
173 };
174
175 Router.prototype.close = function() {
176 this.connector_.close();
177 // Closing the message pipe won't trigger connection error handler.
178 // Explicitly call onPipeConnectionError() so that associated endpoints
179 // will get notified.
180 this.onPipeConnectionError();
181 };
182
183 Router.prototype.waitForNextMessageForTesting = function() {
184 this.connector_.waitForNextMessageForTesting();
185 };
186
187 Router.prototype.handleInvalidIncomingMessage_ = function(message) {
188 if (!validator.isTestingMode()) {
155 // TODO(yzshen): Consider notifying the embedder. 189 // TODO(yzshen): Consider notifying the embedder.
156 // TODO(yzshen): This should also trigger connection error handler. 190 // TODO(yzshen): This should also trigger connection error handler.
157 // Consider making accept() return a boolean and let the connector deal 191 // Consider making accept() return a boolean and let the connector deal
158 // with this, as the C++ code does. 192 // with this, as the C++ code does.
159 console.log("Invalid message: " + validator.validationError[error]);
160
161 this.close(); 193 this.close();
162 return; 194 return;
163 } 195 }
164 196 };
165 this.testingController_.onInvalidIncomingMessage(error); 197
166 }; 198 Router.prototype.onPeerAssociatedEndpointClosed = function(interfaceId,
167 199 reason) {
168 Router.prototype.handleConnectionError_ = function(result) { 200 check(!types.isMasterInterfaceId(interfaceId) || reason);
169 this.completers_.forEach(function(value) { 201
170 value.reject(result); 202 var endpoint = this.endpoints_.get(interfaceId);
171 }); 203 if (!endpoint) {
172 if (this.errorHandler_) 204 endpoint = new InterfaceEndpoint(this, interfaceId);
173 this.errorHandler_(); 205 this.endpoints_.set(interfaceId, endpoint);
174 this.close(); 206 }
175 }; 207
176 208 if (reason) {
177 // The RouterTestingController is used in unit tests. It defeats valid message 209 endpoint.disconnectReason = reason;
178 // handling and delgates invalid message handling. 210 }
179 211
180 function RouterTestingController(connector) { 212 if (!endpoint.peerClosed) {
181 this.connector_ = connector; 213 if (endpoint.client) {
182 this.invalidMessageHandler_ = null; 214 timer.createOneShot(0,
183 } 215 endpoint.client.notifyError.bind(endpoint.client, reason));
184 216 }
185 RouterTestingController.prototype.waitForNextMessage = function() { 217 this.updateEndpointStateMayRemove(endpoint,
186 this.connector_.waitForNextMessageForTesting(); 218 EndpointStateUpdateType.PEER_ENDPOINT_CLOSED);
187 }; 219 }
188 220 return true;
189 RouterTestingController.prototype.setInvalidIncomingMessageHandler = 221 };
190 function(callback) { 222
191 this.invalidMessageHandler_ = callback; 223 Router.prototype.onPipeConnectionError = function() {
192 }; 224 this.encounteredError_ = true;
193 225
194 RouterTestingController.prototype.onInvalidIncomingMessage = 226 for (var endpoint of this.endpoints_.values()) {
195 function(error) { 227 if (endpoint.client) {
196 if (this.invalidMessageHandler_) 228 timer.createOneShot(0,
197 this.invalidMessageHandler_(error); 229 endpoint.client.notifyError.bind(endpoint.client,
230 endpoint.disconnectReason));
231 }
232 this.updateEndpointStateMayRemove(endpoint,
233 EndpointStateUpdateType.PEER_ENDPOINT_CLOSED);
234 }
235 };
236
237 Router.prototype.closeEndpointHandle = function(interfaceId, reason) {
238 if (!types.isValidInterfaceId(interfaceId)) {
239 return;
240 }
241 var endpoint = this.endpoints_.get(interfaceId);
242 check(endpoint);
243 check(!endpoint.client);
244 check(!endpoint.closed);
245
246 this.updateEndpointStateMayRemove(endpoint,
247 EndpointStateUpdateType.ENDPOINT_CLOSED);
248
249 if (!types.isMasterInterfaceId(interfaceId) || reason) {
250 this.controlMessageProxy_.notifyPeerEndpointClosed(interfaceId, reason);
251 }
252 };
253
254 Router.prototype.updateEndpointStateMayRemove = function(endpoint,
255 endpointStateUpdateType) {
256 if (endpointStateUpdateType === EndpointStateUpdateType.ENDPOINT_CLOSED) {
257 endpoint.closed = true;
258 } else {
259 endpoint.peerClosed = true;
260 }
261 if (endpoint.closed && endpoint.peerClosed) {
262 this.endpoints_.delete(endpoint.id);
263 }
198 }; 264 };
199 265
200 var exports = {}; 266 var exports = {};
201 exports.Router = Router; 267 exports.Router = Router;
202 return exports; 268 return exports;
203 }); 269 });
OLDNEW
« no previous file with comments | « mojo/public/js/lib/pipe_control_message_proxy.js ('k') | mojo/public/js/tests/validation_unittest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698