| 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/connector", [ | 5 define("mojo/public/js/connector", [ |
| 6 "mojo/public/js/buffer", | 6 "mojo/public/js/buffer", |
| 7 "mojo/public/js/codec", | 7 "mojo/public/js/codec", |
| 8 "mojo/public/js/core", | 8 ], function(buffer, codec) { |
| 9 "mojo/public/js/support", | |
| 10 ], function(buffer, codec, core, support) { | |
| 11 | 9 |
| 12 function Connector(handle) { | 10 function Connector(handle) { |
| 13 if (!core.isHandle(handle)) | 11 if (!(handle instanceof MojoHandle)) |
| 14 throw new Error("Connector: not a handle " + handle); | 12 throw new Error("Connector: not a handle " + handle); |
| 13 |
| 15 this.handle_ = handle; | 14 this.handle_ = handle; |
| 16 this.dropWrites_ = false; | 15 this.dropWrites_ = false; |
| 17 this.error_ = false; | 16 this.error_ = false; |
| 18 this.incomingReceiver_ = null; | 17 this.incomingReceiver_ = null; |
| 19 this.readWatcher_ = null; | 18 this.readWatcher_ = null; |
| 20 this.errorHandler_ = null; | 19 this.errorHandler_ = null; |
| 21 | 20 |
| 22 if (handle) { | 21 if (handle) { |
| 23 this.readWatcher_ = support.watch(handle, | 22 this.readWatcher_ = handle.watch({ readable: true }, |
| 24 core.HANDLE_SIGNAL_READABLE, | 23 this.readMore_.bind(this)); |
| 25 this.readMore_.bind(this)); | |
| 26 } | 24 } |
| 27 } | 25 } |
| 28 | 26 |
| 29 Connector.prototype.close = function() { | 27 Connector.prototype.close = function() { |
| 30 if (this.readWatcher_) { | 28 if (this.readWatcher_) { |
| 31 support.cancelWatch(this.readWatcher_); | 29 this.readWatcher_.cancel(); |
| 32 this.readWatcher_ = null; | 30 this.readWatcher_ = null; |
| 33 } | 31 } |
| 34 if (this.handle_ != null) { | 32 if (this.handle_ != null) { |
| 35 core.close(this.handle_); | 33 this.handle_.close(); |
| 36 this.handle_ = null; | 34 this.handle_ = null; |
| 37 } | 35 } |
| 38 }; | 36 }; |
| 39 | 37 |
| 40 Connector.prototype.accept = function(message) { | 38 Connector.prototype.accept = function(message) { |
| 41 if (this.error_) | 39 if (this.error_) |
| 42 return false; | 40 return false; |
| 43 | 41 |
| 44 if (this.dropWrites_) | 42 if (this.dropWrites_) |
| 45 return true; | 43 return true; |
| 46 | 44 |
| 47 var result = core.writeMessage(this.handle_, | 45 var result = this.handle_.writeMessage(message.buffer.arrayBuffer, |
| 48 new Uint8Array(message.buffer.arrayBuffer), | 46 message.handles); |
| 49 message.handles, | |
| 50 core.WRITE_MESSAGE_FLAG_NONE); | |
| 51 switch (result) { | 47 switch (result) { |
| 52 case core.RESULT_OK: | 48 case Mojo.RESULT_OK: |
| 53 // The handles were successfully transferred, so we don't own them | 49 // The handles were successfully transferred, so we don't own them |
| 54 // anymore. | 50 // anymore. |
| 55 message.handles = []; | 51 message.handles = []; |
| 56 break; | 52 break; |
| 57 case core.RESULT_FAILED_PRECONDITION: | 53 case Mojo.RESULT_FAILED_PRECONDITION: |
| 58 // There's no point in continuing to write to this pipe since the other | 54 // There's no point in continuing to write to this pipe since the other |
| 59 // end is gone. Avoid writing any future messages. Hide write failures | 55 // end is gone. Avoid writing any future messages. Hide write failures |
| 60 // from the caller since we'd like them to continue consuming any | 56 // from the caller since we'd like them to continue consuming any |
| 61 // backlog of incoming messages before regarding the message pipe as | 57 // backlog of incoming messages before regarding the message pipe as |
| 62 // closed. | 58 // closed. |
| 63 this.dropWrites_ = true; | 59 this.dropWrites_ = true; |
| 64 break; | 60 break; |
| 65 default: | 61 default: |
| 66 // This particular write was rejected, presumably because of bad input. | 62 // This particular write was rejected, presumably because of bad input. |
| 67 // The pipe is not necessarily in a bad state. | 63 // The pipe is not necessarily in a bad state. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 83 }; | 79 }; |
| 84 | 80 |
| 85 Connector.prototype.waitForNextMessageForTesting = function() { | 81 Connector.prototype.waitForNextMessageForTesting = function() { |
| 86 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE, | 82 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE, |
| 87 core.DEADLINE_INDEFINITE); | 83 core.DEADLINE_INDEFINITE); |
| 88 this.readMore_(wait.result); | 84 this.readMore_(wait.result); |
| 89 }; | 85 }; |
| 90 | 86 |
| 91 Connector.prototype.readMore_ = function(result) { | 87 Connector.prototype.readMore_ = function(result) { |
| 92 for (;;) { | 88 for (;;) { |
| 93 var read = core.readMessage(this.handle_, | 89 var read = this.handle_.readMessage(); |
| 94 core.READ_MESSAGE_FLAG_NONE); | |
| 95 if (this.handle_ == null) // The connector has been closed. | 90 if (this.handle_ == null) // The connector has been closed. |
| 96 return; | 91 return; |
| 97 if (read.result == core.RESULT_SHOULD_WAIT) | 92 if (read.result == Mojo.RESULT_SHOULD_WAIT) |
| 98 return; | 93 return; |
| 99 if (read.result != core.RESULT_OK) { | 94 if (read.result != Mojo.RESULT_OK) { |
| 100 this.error_ = true; | 95 this.error_ = true; |
| 101 if (this.errorHandler_) | 96 if (this.errorHandler_) |
| 102 this.errorHandler_.onError(read.result); | 97 this.errorHandler_.onError(read.result); |
| 103 return; | 98 return; |
| 104 } | 99 } |
| 105 var messageBuffer = new buffer.Buffer(read.buffer); | 100 var messageBuffer = new buffer.Buffer(read.buffer); |
| 106 var message = new codec.Message(messageBuffer, read.handles); | 101 var message = new codec.Message(messageBuffer, read.handles); |
| 107 if (this.incomingReceiver_) | 102 if (this.incomingReceiver_) |
| 108 this.incomingReceiver_.accept(message); | 103 this.incomingReceiver_.accept(message); |
| 109 } | 104 } |
| 110 }; | 105 }; |
| 111 | 106 |
| 112 var exports = {}; | 107 var exports = {}; |
| 113 exports.Connector = Connector; | 108 exports.Connector = Connector; |
| 114 return exports; | 109 return exports; |
| 115 }); | 110 }); |
| OLD | NEW |