| 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 "mojo/public/js/core", |
| 9 "mojo/public/js/support", | 9 "mojo/public/js/support", |
| 10 ], function(buffer, codec, core, support) { | 10 ], function(buffer, codec, core, support) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 Connector.prototype.setErrorHandler = function(handler) { | 77 Connector.prototype.setErrorHandler = function(handler) { |
| 78 this.errorHandler_ = handler; | 78 this.errorHandler_ = handler; |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 Connector.prototype.encounteredError = function() { | 81 Connector.prototype.encounteredError = function() { |
| 82 return this.error_; | 82 return this.error_; |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 Connector.prototype.waitForNextMessageForTesting = function() { |
| 86 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE, |
| 87 core.DEADLINE_INDEFINITE); |
| 88 this.readMore_(wait.result); |
| 89 }; |
| 90 |
| 85 Connector.prototype.readMore_ = function(result) { | 91 Connector.prototype.readMore_ = function(result) { |
| 86 for (;;) { | 92 for (;;) { |
| 87 var read = core.readMessage(this.handle_, | 93 var read = core.readMessage(this.handle_, |
| 88 core.READ_MESSAGE_FLAG_NONE); | 94 core.READ_MESSAGE_FLAG_NONE); |
| 89 if (this.handle_ == null) // The connector has been closed. | 95 if (this.handle_ == null) // The connector has been closed. |
| 90 return; | 96 return; |
| 91 if (read.result == core.RESULT_SHOULD_WAIT) | 97 if (read.result == core.RESULT_SHOULD_WAIT) |
| 92 return; | 98 return; |
| 93 if (read.result != core.RESULT_OK) { | 99 if (read.result != core.RESULT_OK) { |
| 94 this.error_ = true; | 100 this.error_ = true; |
| 95 if (this.errorHandler_) | 101 if (this.errorHandler_) |
| 96 this.errorHandler_.onError(read.result); | 102 this.errorHandler_.onError(read.result); |
| 97 return; | 103 return; |
| 98 } | 104 } |
| 99 var messageBuffer = new buffer.Buffer(read.buffer); | 105 var messageBuffer = new buffer.Buffer(read.buffer); |
| 100 var message = new codec.Message(messageBuffer, read.handles); | 106 var message = new codec.Message(messageBuffer, read.handles); |
| 101 if (this.incomingReceiver_) { | 107 if (this.incomingReceiver_) |
| 102 this.incomingReceiver_.accept(message); | 108 this.incomingReceiver_.accept(message); |
| 103 } | |
| 104 } | 109 } |
| 105 }; | 110 }; |
| 106 | 111 |
| 107 // The TestConnector subclass is only intended to be used in unit tests. It | |
| 108 // doesn't automatically listen for input messages. Instead, you need to | |
| 109 // call waitForNextMessage to block and wait for the next incoming message. | |
| 110 function TestConnector(handle) { | |
| 111 Connector.call(this, handle); | |
| 112 } | |
| 113 | |
| 114 TestConnector.prototype = Object.create(Connector.prototype); | |
| 115 | |
| 116 TestConnector.prototype.waitForNextMessage = function() { | |
| 117 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE, | |
| 118 core.DEADLINE_INDEFINITE); | |
| 119 this.readMore_(wait.result); | |
| 120 } | |
| 121 | |
| 122 var exports = {}; | 112 var exports = {}; |
| 123 exports.Connector = Connector; | 113 exports.Connector = Connector; |
| 124 exports.TestConnector = TestConnector; | |
| 125 return exports; | 114 return exports; |
| 126 }); | 115 }); |
| OLD | NEW |