Chromium Code Reviews| 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/bindings/js/connector", [ | 5 define("mojo/public/bindings/js/connector", [ |
| 6 "mojo/public/bindings/js/codec", | 6 "mojo/public/bindings/js/codec", |
| 7 "mojo/bindings/js/core", | 7 "mojo/bindings/js/core", |
| 8 "mojo/bindings/js/support", | 8 "mojo/bindings/js/support", |
| 9 ], function(codec, core, support) { | 9 ], function(codec, core, support) { |
| 10 | 10 |
| 11 function Connector(handle) { | 11 function Connector(handle) { |
| 12 this.handle_ = handle; | 12 this.handle_ = handle; |
| 13 this.drop_writes_ = false; | |
|
abarth-chromium
2014/03/26 16:45:11
dropWrites_ ?
| |
| 13 this.error_ = false; | 14 this.error_ = false; |
| 14 this.incomingReceiver_ = null; | 15 this.incomingReceiver_ = null; |
| 15 this.readWaitCookie_ = null; | 16 this.readWaitCookie_ = null; |
| 17 | |
| 18 this.waitToReadMore_(); | |
| 16 } | 19 } |
| 17 | 20 |
| 18 Connector.prototype.close = function() { | 21 Connector.prototype.close = function() { |
| 19 if (this.readWaitCookie_) { | 22 if (this.readWaitCookie_) { |
| 20 support.cancelWait(this.readWaitCookie_); | 23 support.cancelWait(this.readWaitCookie_); |
| 21 this.readWaitCookie_ = null; | 24 this.readWaitCookie_ = null; |
| 22 } | 25 } |
| 23 if (this.handle_ != core.kInvalidHandle) { | 26 if (this.handle_ != core.kInvalidHandle) { |
| 24 core.close(this.handle_); | 27 core.close(this.handle_); |
| 25 this.handle_ = core.kInvalidHandle; | 28 this.handle_ = core.kInvalidHandle; |
| 26 } | 29 } |
| 27 }; | 30 }; |
| 28 | 31 |
| 29 Connector.prototype.accept = function(message) { | 32 Connector.prototype.accept = function(message) { |
| 30 if (this.error_) | 33 if (this.error_) |
| 31 return false; | 34 return false; |
| 32 this.write_(message); | 35 |
| 33 return !this.error_; | 36 if (this.drop_writes_) |
| 37 return true; | |
| 38 | |
| 39 var result = core.writeMessage(this.handle_, | |
| 40 message.memory, | |
| 41 message.handles, | |
| 42 core.WRITE_MESSAGE_FLAG_NONE); | |
| 43 | |
| 44 switch (result) { | |
| 45 case core.RESULT_OK: | |
| 46 // The handles were successfully transferred, so we don't own them | |
| 47 // anymore. | |
| 48 message.handles = []; | |
| 49 break; | |
| 50 case core.RESULT_FAILED_PRECONDITION: | |
| 51 // There's no point in continuing to write to this pipe since the other | |
| 52 // end is gone. Avoid writing any future messages. Hide write failures | |
| 53 // from the caller since we'd like them to continue consuming any | |
| 54 // backlog of incoming messages before regarding the message pipe as | |
| 55 // closed. | |
| 56 drop_writes_ = true; | |
|
abarth-chromium
2014/03/26 16:45:11
this.dropWrites_ ?
| |
| 57 break; | |
| 58 default: | |
| 59 // This particular write was rejected, presumably because of bad input. | |
| 60 // The pipe is not necessarily in a bad state. | |
| 61 return false; | |
| 62 } | |
| 63 return true; | |
| 34 }; | 64 }; |
| 35 | 65 |
| 36 Connector.prototype.setIncomingReceiver = function(receiver) { | 66 Connector.prototype.setIncomingReceiver = function(receiver) { |
| 37 this.incomingReceiver_ = receiver; | 67 this.incomingReceiver_ = receiver; |
| 38 if (this.incomingReceiver_) | |
| 39 this.waitToReadMore_(); | |
| 40 }; | |
| 41 | |
| 42 Connector.prototype.write_ = function(message) { | |
| 43 var result = core.writeMessage(this.handle_, | |
| 44 message.memory, | |
| 45 message.handles, | |
| 46 core.WRITE_MESSAGE_FLAG_NONE); | |
| 47 if (result != core.RESULT_OK) { | |
| 48 this.error_ = true | |
| 49 return; | |
| 50 } | |
| 51 // The handles were successfully transferred, so we don't own them anymore. | |
| 52 message.handles = []; | |
| 53 }; | 68 }; |
| 54 | 69 |
| 55 Connector.prototype.waitToReadMore_ = function() { | 70 Connector.prototype.waitToReadMore_ = function() { |
| 56 this.readWaitCookie_ = support.asyncWait(this.handle_, | 71 this.readWaitCookie_ = support.asyncWait(this.handle_, |
| 57 core.WAIT_FLAG_READABLE, | 72 core.WAIT_FLAG_READABLE, |
| 58 this.readMore_.bind(this)); | 73 this.readMore_.bind(this)); |
| 59 }; | 74 }; |
| 60 | 75 |
| 61 Connector.prototype.readMore_ = function(result) { | 76 Connector.prototype.readMore_ = function(result) { |
| 62 for (;;) { | 77 for (;;) { |
| 63 var read = core.readMessage(this.handle_, | 78 var read = core.readMessage(this.handle_, |
| 64 core.READ_MESSAGE_FLAG_NONE); | 79 core.READ_MESSAGE_FLAG_NONE); |
| 65 if (read.result == core.RESULT_SHOULD_WAIT) { | 80 if (read.result == core.RESULT_SHOULD_WAIT) { |
| 66 this.waitToReadMore_(); | 81 this.waitToReadMore_(); |
| 67 return; | 82 return; |
| 68 } | 83 } |
| 69 if (read.result != core.RESULT_OK) { | 84 if (read.result != core.RESULT_OK) { |
| 70 this.error_ = true; | 85 this.error_ = true; |
| 71 return; | 86 return; |
| 72 } | 87 } |
| 73 // TODO(abarth): Should core.readMessage return a Uint8Array? | 88 // TODO(abarth): Should core.readMessage return a Uint8Array? |
| 74 var memory = new Uint8Array(read.buffer); | 89 var memory = new Uint8Array(read.buffer); |
| 75 var message = new codec.Message(memory, read.handles); | 90 var message = new codec.Message(memory, read.handles); |
| 76 this.incomingReceiver_.accept(message); | 91 if (this.incomingReceiver_) |
| 92 this.incomingReceiver_.accept(message); | |
| 77 } | 93 } |
| 78 }; | 94 }; |
| 79 | 95 |
| 80 function Connection(handle, localFactory, remoteFactory) { | |
| 81 this.connector_ = new Connector(handle); | |
| 82 this.remote = new remoteFactory(this.connector_); | |
| 83 this.local = new localFactory(this.remote); | |
| 84 this.connector_.setIncomingReceiver(this.local); | |
| 85 } | |
| 86 | |
| 87 Connection.prototype.close = function() { | |
| 88 this.connector_.close(); | |
| 89 this.connector_ = null; | |
| 90 this.local = null; | |
| 91 this.remote = null; | |
| 92 }; | |
| 93 | |
| 94 var exports = {}; | 96 var exports = {}; |
| 95 exports.Connection = Connection; | 97 exports.Connector = Connector; |
| 96 return exports; | 98 return exports; |
| 97 }); | 99 }); |
| OLD | NEW |