Index: mojo/public/bindings/js/connector.js |
diff --git a/mojo/public/bindings/js/connector.js b/mojo/public/bindings/js/connector.js |
index 9a1bfc7d9a8fbeb64ecf7a19abe20f01ef268a7f..2aa9a9f23f428a67518c5d7bd317adab15a1ba29 100644 |
--- a/mojo/public/bindings/js/connector.js |
+++ b/mojo/public/bindings/js/connector.js |
@@ -10,9 +10,12 @@ define("mojo/public/bindings/js/connector", [ |
function Connector(handle) { |
this.handle_ = handle; |
+ this.dropWrites_ = false; |
this.error_ = false; |
this.incomingReceiver_ = null; |
this.readWaitCookie_ = null; |
+ |
+ this.waitToReadMore_(); |
} |
Connector.prototype.close = function() { |
@@ -29,29 +32,45 @@ define("mojo/public/bindings/js/connector", [ |
Connector.prototype.accept = function(message) { |
if (this.error_) |
return false; |
- this.write_(message); |
- return !this.error_; |
- }; |
- Connector.prototype.setIncomingReceiver = function(receiver) { |
- this.incomingReceiver_ = receiver; |
- if (this.incomingReceiver_) |
- this.waitToReadMore_(); |
- }; |
+ if (this.dropWrites_) |
+ return true; |
- Connector.prototype.write_ = function(message) { |
var result = core.writeMessage(this.handle_, |
message.memory, |
message.handles, |
core.WRITE_MESSAGE_FLAG_NONE); |
- if (result != core.RESULT_OK) { |
- this.error_ = true |
- return; |
+ |
+ switch (result) { |
+ case core.RESULT_OK: |
+ // The handles were successfully transferred, so we don't own them |
+ // anymore. |
+ message.handles = []; |
+ break; |
+ case core.RESULT_FAILED_PRECONDITION: |
+ // There's no point in continuing to write to this pipe since the other |
+ // end is gone. Avoid writing any future messages. Hide write failures |
+ // from the caller since we'd like them to continue consuming any |
+ // backlog of incoming messages before regarding the message pipe as |
+ // closed. |
+ this.dropWrites_ = true; |
+ break; |
+ default: |
+ // This particular write was rejected, presumably because of bad input. |
+ // The pipe is not necessarily in a bad state. |
+ return false; |
} |
- // The handles were successfully transferred, so we don't own them anymore. |
- message.handles = []; |
+ return true; |
+ }; |
+ |
+ Connector.prototype.setIncomingReceiver = function(receiver) { |
+ this.incomingReceiver_ = receiver; |
}; |
+ Connector.prototype.encounteredError = function() { |
+ return this.error_; |
+ } |
+ |
Connector.prototype.waitToReadMore_ = function() { |
this.readWaitCookie_ = support.asyncWait(this.handle_, |
core.WAIT_FLAG_READABLE, |
@@ -73,25 +92,12 @@ define("mojo/public/bindings/js/connector", [ |
// TODO(abarth): Should core.readMessage return a Uint8Array? |
var memory = new Uint8Array(read.buffer); |
var message = new codec.Message(memory, read.handles); |
- this.incomingReceiver_.accept(message); |
+ if (this.incomingReceiver_) |
+ this.incomingReceiver_.accept(message); |
} |
}; |
- function Connection(handle, localFactory, remoteFactory) { |
- this.connector_ = new Connector(handle); |
- this.remote = new remoteFactory(this.connector_); |
- this.local = new localFactory(this.remote); |
- this.connector_.setIncomingReceiver(this.local); |
- } |
- |
- Connection.prototype.close = function() { |
- this.connector_.close(); |
- this.connector_ = null; |
- this.local = null; |
- this.remote = null; |
- }; |
- |
var exports = {}; |
- exports.Connection = Connection; |
+ exports.Connector = Connector; |
return exports; |
}); |