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

Unified Diff: mojo/public/js/new_bindings/router.js

Issue 2893493002: Mojo JS bindings: update the new bindings with the associated interface feature. (Closed)
Patch Set: . Created 3 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/js/new_bindings/router.js
diff --git a/mojo/public/js/new_bindings/router.js b/mojo/public/js/new_bindings/router.js
index 1272407c1e13ed2af4cc055de84f68ca02e28bd1..7eadb92cba0c2c76ee5c06d7ab6e3051dc61fc57 100644
--- a/mojo/public/js/new_bindings/router.js
+++ b/mojo/public/js/new_bindings/router.js
@@ -5,185 +5,309 @@
(function() {
var internal = mojo.internal;
- function Router(handle, interface_version, connectorFactory) {
- if (!(handle instanceof MojoHandle))
- throw new Error("Router constructor: Not a handle");
- if (connectorFactory === undefined)
- connectorFactory = internal.Connector;
- this.connector_ = new connectorFactory(handle);
- this.incomingReceiver_ = null;
- this.errorHandler_ = null;
- this.nextRequestID_ = 0;
- this.completers_ = new Map();
- this.payloadValidators_ = [];
- this.testingController_ = null;
+ /**
+ * The state of |endpoint|. If both the endpoint and its peer have been
+ * closed, removes it from |endpoints_|.
+ * @enum {string}
+ */
+ var EndpointStateUpdateType = {
+ ENDPOINT_CLOSED: 'endpoint_closed',
+ PEER_ENDPOINT_CLOSED: 'peer_endpoint_closed'
+ };
+
+ function check(condition, output) {
+ if (!condition) {
+ // testharness.js does not rethrow errors so the error stack needs to be
+ // included as a string in the error we throw for debugging layout tests.
+ throw new Error((new Error()).stack);
+ }
+ }
- if (interface_version !== undefined) {
- this.controlMessageHandler_ = new
- internal.ControlMessageHandler(interface_version);
+ function InterfaceEndpoint(router, interfaceId) {
+ this.router_ = router;
+ this.id = interfaceId;
+ this.closed = false;
+ this.peerClosed = false;
+ this.handleCreated = false;
+ this.disconnectReason = null;
+ this.client = null;
+ }
+
+ InterfaceEndpoint.prototype.sendMessage = function(message) {
+ message.setInterfaceId(this.id);
+ return this.router_.connector_.accept(message);
+ };
+
+ function Router(handle, setInterfaceIdNamespaceBit) {
+ if (!(handle instanceof MojoHandle)) {
+ throw new Error("Router constructor: Not a handle");
+ }
+ if (setInterfaceIdNamespaceBit === undefined) {
+ setInterfaceIdNamespaceBit = false;
}
+ this.connector_ = new internal.Connector(handle);
+
this.connector_.setIncomingReceiver({
- accept: this.handleIncomingMessage_.bind(this),
+ accept: this.accept.bind(this),
});
this.connector_.setErrorHandler({
- onError: this.handleConnectionError_.bind(this),
+ onError: this.onPipeConnectionError.bind(this),
});
+
+ this.setInterfaceIdNamespaceBit_ = setInterfaceIdNamespaceBit;
+ // |cachedMessageData| caches infomation about a message, so it can be
+ // processed later if a client is not yet attached to the target endpoint.
+ this.cachedMessageData = null;
+ this.controlMessageHandler_ = new internal.PipeControlMessageHandler(this);
+ this.controlMessageProxy_ =
+ new internal.PipeControlMessageProxy(this.connector_);
+ this.nextInterfaceIdValue_ = 1;
+ this.encounteredError_ = false;
+ this.endpoints_ = new Map();
}
- Router.prototype.close = function() {
- this.completers_.clear(); // Drop any responders.
- this.connector_.close();
- this.testingController_ = null;
- };
+ Router.prototype.associateInterface = function(handleToSend) {
+ if (!handleToSend.pendingAssociation()) {
+ return internal.kInvalidInterfaceId;
+ }
- Router.prototype.accept = function(message) {
- this.connector_.accept(message);
- };
+ var id = 0;
+ do {
+ if (this.nextInterfaceIdValue_ >= internal.kInterfaceIdNamespaceMask) {
+ this.nextInterfaceIdValue_ = 1;
+ }
+ id = this.nextInterfaceIdValue_++;
+ if (this.setInterfaceIdNamespaceBit_) {
+ id += internal.kInterfaceIdNamespaceMask;
+ }
+ } while (this.endpoints_.has(id));
- Router.prototype.reject = function(message) {
- // TODO(mpcomplete): no way to trasmit errors over a Connection.
- };
+ var endpoint = new InterfaceEndpoint(this, id);
+ this.endpoints_.set(id, endpoint);
+ if (this.encounteredError_) {
+ this.updateEndpointStateMayRemove(endpoint,
+ EndpointStateUpdateType.PEER_ENDPOINT_CLOSED);
+ }
+ endpoint.handleCreated = true;
- Router.prototype.acceptAndExpectResponse = function(message) {
- // Reserve 0 in case we want it to convey special meaning in the future.
- var requestID = this.nextRequestID_++;
- if (requestID == 0)
- requestID = this.nextRequestID_++;
-
- message.setRequestID(requestID);
- var result = this.connector_.accept(message);
- if (!result)
- return Promise.reject(Error("Connection error"));
-
- var completer = {};
- this.completers_.set(requestID, completer);
- return new Promise(function(resolve, reject) {
- completer.resolve = resolve;
- completer.reject = reject;
- });
- };
+ if (!handleToSend.notifyAssociation(id, this)) {
+ // The peer handle of |handleToSend|, which is supposed to join this
+ // associated group, has been closed.
+ this.updateEndpointStateMayRemove(endpoint,
+ EndpointStateUpdateType.ENDPOINT_CLOSED);
- Router.prototype.setIncomingReceiver = function(receiver) {
- this.incomingReceiver_ = receiver;
- };
+ pipeControlMessageproxy.notifyPeerEndpointClosed(id,
+ handleToSend.disconnectReason());
+ }
- Router.prototype.setPayloadValidators = function(payloadValidators) {
- this.payloadValidators_ = payloadValidators;
+ return id;
};
- Router.prototype.setErrorHandler = function(handler) {
- this.errorHandler_ = handler;
- };
+ Router.prototype.attachEndpointClient = function(
+ interfaceEndpointHandle, interfaceEndpointClient) {
+ check(internal.isValidInterfaceId(interfaceEndpointHandle.id()));
+ check(interfaceEndpointClient);
- Router.prototype.encounteredError = function() {
- return this.connector_.encounteredError();
- };
+ var endpoint = this.endpoints_.get(interfaceEndpointHandle.id());
+ check(endpoint);
+ check(!endpoint.client);
+ check(!endpoint.closed);
+ endpoint.client = interfaceEndpointClient;
+
+ if (endpoint.peerClosed) {
+ setTimeout(endpoint.client.notifyError.bind(endpoint.client), 0);
+ }
+
+ if (this.cachedMessageData && interfaceEndpointHandle.id() ===
+ this.cachedMessageData.message.getInterfaceId()) {
+ setTimeout((function() {
+ if (!this.cachedMessageData) {
+ return;
+ }
- Router.prototype.enableTestingMode = function() {
- this.testingController_ = new RouterTestingController(this.connector_);
- return this.testingController_;
+ var targetEndpoint = this.endpoints_.get(
+ this.cachedMessageData.message.getInterfaceId());
+ // Check that the target endpoint's client still exists.
+ if (targetEndpoint && targetEndpoint.client) {
+ var message = this.cachedMessageData.message;
+ var messageValidator = this.cachedMessageData.messageValidator;
+ this.cachedMessageData = null;
+ this.connector_.resumeIncomingMethodCallProcessing();
+ var ok = endpoint.client.handleIncomingMessage(message,
+ messageValidator);
+
+ // Handle invalid cached incoming message.
+ if (!internal.isTestingMode() && !ok) {
+ this.connector_.handleError(true, true);
+ }
+ }
+ }).bind(this), 0);
+ }
+
+ return endpoint;
};
- Router.prototype.handleIncomingMessage_ = function(message) {
- var noError = internal.validationError.NONE;
- var messageValidator = new internal.Validator(message);
- var err = messageValidator.validateMessageHeader();
- for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i)
- err = this.payloadValidators_[i](messageValidator);
+ Router.prototype.detachEndpointClient = function(
+ interfaceEndpointHandle) {
+ check(internal.isValidInterfaceId(interfaceEndpointHandle.id()));
+ var endpoint = this.endpoints_.get(interfaceEndpointHandle.id());
+ check(endpoint);
+ check(endpoint.client);
+ check(!endpoint.closed);
- if (err == noError)
- this.handleValidIncomingMessage_(message);
- else
- this.handleInvalidIncomingMessage_(message, err);
+ endpoint.client = null;
};
- Router.prototype.handleValidIncomingMessage_ = function(message) {
- if (this.testingController_)
- return;
+ Router.prototype.createLocalEndpointHandle = function(
+ interfaceId) {
+ if (!internal.isValidInterfaceId(interfaceId)) {
+ return new internal.InterfaceEndpointHandle();
+ }
- if (message.expectsResponse()) {
- if (internal.isInterfaceControlMessage(message)) {
- if (this.controlMessageHandler_) {
- this.controlMessageHandler_.acceptWithResponder(message, this);
- } else {
- this.close();
- }
- } else if (this.incomingReceiver_) {
- this.incomingReceiver_.acceptWithResponder(message, this);
- } else {
- // If we receive a request expecting a response when the client is not
- // listening, then we have no choice but to tear down the pipe.
- this.close();
- }
- } else if (message.isResponse()) {
- var reader = new internal.MessageReader(message);
- var requestID = reader.requestID;
- var completer = this.completers_.get(requestID);
- if (completer) {
- this.completers_.delete(requestID);
- completer.resolve(message);
- } else {
- console.log("Unexpected response with request ID: " + requestID);
+ var endpoint = this.endpoints_.get(interfaceId);
+
+ if (!endpoint) {
+ endpoint = new InterfaceEndpoint(this, interfaceId);
+ this.endpoints_.set(interfaceId, endpoint);
+
+ check(!endpoint.handleCreated);
+
+ if (this.encounteredError_) {
+ this.updateEndpointStateMayRemove(endpoint,
+ EndpointStateUpdateType.PEER_ENDPOINT_CLOSED);
}
} else {
- if (internal.isInterfaceControlMessage(message)) {
- if (this.controlMessageHandler_) {
- var ok = this.controlMessageHandler_.accept(message);
- if (ok) return;
- }
- this.close();
- } else if (this.incomingReceiver_) {
- this.incomingReceiver_.accept(message);
+ // If the endpoint already exist, it is because we have received a
+ // notification that the peer endpoint has closed.
+ check(!endpoint.closed);
+ check(endpoint.peerClosed);
+
+ if (endpoint.handleCreated) {
+ return new internal.InterfaceEndpointHandle();
}
}
+
+ endpoint.handleCreated = true;
+ return new internal.InterfaceEndpointHandle(interfaceId, this);
};
- Router.prototype.handleInvalidIncomingMessage_ = function(message, error) {
- if (!this.testingController_) {
- // TODO(yzshen): Consider notifying the embedder.
- // TODO(yzshen): This should also trigger connection error handler.
- // Consider making accept() return a boolean and let the connector deal
- // with this, as the C++ code does.
- console.log("Invalid message: " + internal.validationError[error]);
+ Router.prototype.accept = function(message) {
+ var messageValidator = new internal.Validator(message);
+ var err = messageValidator.validateMessageHeader();
- this.close();
- return;
+ var ok = false;
+ if (err !== internal.validationError.NONE) {
+ internal.reportValidationError(err);
+ } else if (message.deserializeAssociatedEndpointHandles(this)) {
+ if (internal.isPipeControlMessage(message)) {
+ ok = this.controlMessageHandler_.accept(message);
+ } else {
+ var interfaceId = message.getInterfaceId();
+ var endpoint = this.endpoints_.get(interfaceId);
+ if (!endpoint || endpoint.closed) {
+ return true;
+ }
+
+ if (!endpoint.client) {
+ // We need to wait until a client is attached in order to dispatch
+ // further messages.
+ this.cachedMessageData = {message: message,
+ messageValidator: messageValidator};
+ this.connector_.pauseIncomingMethodCallProcessing();
+ return true;
+ }
+ ok = endpoint.client.handleIncomingMessage(message, messageValidator);
+ }
}
+ return ok;
+ };
- this.testingController_.onInvalidIncomingMessage(error);
+ Router.prototype.close = function() {
+ this.connector_.close();
+ // Closing the message pipe won't trigger connection error handler.
+ // Explicitly call onPipeConnectionError() so that associated endpoints
+ // will get notified.
+ this.onPipeConnectionError();
};
- Router.prototype.handleConnectionError_ = function(result) {
- this.completers_.forEach(function(value) {
- value.reject(result);
- });
- if (this.errorHandler_)
- this.errorHandler_();
- this.close();
+ Router.prototype.waitForNextMessageForTesting = function() {
+ this.connector_.waitForNextMessageForTesting();
};
- // The RouterTestingController is used in unit tests. It defeats valid message
- // handling and delgates invalid message handling.
+ Router.prototype.onPeerAssociatedEndpointClosed = function(interfaceId,
+ reason) {
+ check(!internal.isMasterInterfaceId(interfaceId) || reason);
- function RouterTestingController(connector) {
- this.connector_ = connector;
- this.invalidMessageHandler_ = null;
- }
+ var endpoint = this.endpoints_.get(interfaceId);
+ if (!endpoint) {
+ endpoint = new InterfaceEndpoint(this, interfaceId);
+ this.endpoints_.set(interfaceId, endpoint);
+ }
- RouterTestingController.prototype.waitForNextMessage = function() {
- this.connector_.waitForNextMessageForTesting();
+ if (reason) {
+ endpoint.disconnectReason = reason;
+ }
+
+ if (!endpoint.peerClosed) {
+ if (endpoint.client) {
+ setTimeout(endpoint.client.notifyError.bind(endpoint.client, reason),
+ 0);
+ }
+ this.updateEndpointStateMayRemove(endpoint,
+ EndpointStateUpdateType.PEER_ENDPOINT_CLOSED);
+ }
+ return true;
};
- RouterTestingController.prototype.setInvalidIncomingMessageHandler =
- function(callback) {
- this.invalidMessageHandler_ = callback;
+ Router.prototype.onPipeConnectionError = function() {
+ this.encounteredError_ = true;
+
+ for (var endpoint of this.endpoints_.values()) {
+ if (endpoint.client) {
+ setTimeout(
+ endpoint.client.notifyError.bind(
+ endpoint.client, endpoint.disconnectReason),
+ 0);
+ }
+ this.updateEndpointStateMayRemove(endpoint,
+ EndpointStateUpdateType.PEER_ENDPOINT_CLOSED);
+ }
};
- RouterTestingController.prototype.onInvalidIncomingMessage =
- function(error) {
- if (this.invalidMessageHandler_)
- this.invalidMessageHandler_(error);
+ Router.prototype.closeEndpointHandle = function(interfaceId, reason) {
+ if (!internal.isValidInterfaceId(interfaceId)) {
+ return;
+ }
+ var endpoint = this.endpoints_.get(interfaceId);
+ check(endpoint);
+ check(!endpoint.client);
+ check(!endpoint.closed);
+
+ this.updateEndpointStateMayRemove(endpoint,
+ EndpointStateUpdateType.ENDPOINT_CLOSED);
+
+ if (!internal.isMasterInterfaceId(interfaceId) || reason) {
+ this.controlMessageProxy_.notifyPeerEndpointClosed(interfaceId, reason);
+ }
+
+ if (this.cachedMessageData && interfaceId ===
+ this.cachedMessageData.message.getInterfaceId()) {
+ this.cachedMessageData = null;
+ this.connector_.resumeIncomingMethodCallProcessing();
+ }
+ };
+
+ Router.prototype.updateEndpointStateMayRemove = function(endpoint,
+ endpointStateUpdateType) {
+ if (endpointStateUpdateType === EndpointStateUpdateType.ENDPOINT_CLOSED) {
+ endpoint.closed = true;
+ } else {
+ endpoint.peerClosed = true;
+ }
+ if (endpoint.closed && endpoint.peerClosed) {
+ this.endpoints_.delete(endpoint.id);
+ }
};
internal.Router = Router;
« no previous file with comments | « mojo/public/js/new_bindings/lib/pipe_control_message_proxy.js ('k') | mojo/public/js/new_bindings/validator.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698