| 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) { |
| 11 | 11 |
| 12 function Connector(handle) { | 12 function Connector(handle) { |
| 13 if (!core.isHandle(handle)) | 13 if (!core.isHandle(handle)) |
| 14 throw new Error("Connector: not a handle " + handle); | 14 throw new Error("Connector: not a handle " + handle); |
| 15 this.handle_ = handle; | 15 this.handle_ = handle; |
| 16 this.dropWrites_ = false; | 16 this.dropWrites_ = false; |
| 17 this.error_ = false; | 17 this.error_ = false; |
| 18 this.incomingReceiver_ = null; | 18 this.incomingReceiver_ = null; |
| 19 this.readWatcher_ = null; | 19 this.readWatcher_ = null; |
| 20 this.errorHandler_ = null; | 20 this.errorHandler_ = null; |
| 21 this.paused_ = false; |
| 21 | 22 |
| 22 if (handle) { | 23 if (handle) { |
| 23 this.readWatcher_ = support.watch(handle, | 24 this.readWatcher_ = support.watch(handle, |
| 24 core.HANDLE_SIGNAL_READABLE, | 25 core.HANDLE_SIGNAL_READABLE, |
| 25 this.readMore_.bind(this)); | 26 this.readMore_.bind(this)); |
| 26 } | 27 } |
| 27 } | 28 } |
| 28 | 29 |
| 29 Connector.prototype.close = function() { | 30 Connector.prototype.close = function() { |
| 30 if (this.readWatcher_) { | 31 if (this.readWatcher_) { |
| 31 support.cancelWatch(this.readWatcher_); | 32 support.cancelWatch(this.readWatcher_); |
| 32 this.readWatcher_ = null; | 33 this.readWatcher_ = null; |
| 33 } | 34 } |
| 34 if (this.handle_ != null) { | 35 if (this.handle_ != null) { |
| 35 core.close(this.handle_); | 36 core.close(this.handle_); |
| 36 this.handle_ = null; | 37 this.handle_ = null; |
| 37 } | 38 } |
| 38 }; | 39 }; |
| 39 | 40 |
| 41 Connector.prototype.pauseIncomingMethodCallProcessing = function() { |
| 42 if (this.paused_) { |
| 43 return; |
| 44 } |
| 45 this.paused_= true; |
| 46 |
| 47 if (this.readWatcher_) { |
| 48 support.cancelWatch(this.readWatcher_); |
| 49 this.readWatcher_ = null; |
| 50 } |
| 51 }; |
| 52 |
| 53 Connector.prototype.resumeIncomingMethodCallProcessing = function() { |
| 54 if (!this.paused_) { |
| 55 return; |
| 56 } |
| 57 this.paused_= false; |
| 58 |
| 59 if (this.handle_) { |
| 60 this.readWatcher_ = support.watch(this.handle_, |
| 61 core.HANDLE_SIGNAL_READABLE, |
| 62 this.readMore_.bind(this)); |
| 63 } |
| 64 }; |
| 65 |
| 40 Connector.prototype.accept = function(message) { | 66 Connector.prototype.accept = function(message) { |
| 41 if (this.error_) | 67 if (this.error_) |
| 42 return false; | 68 return false; |
| 43 | 69 |
| 44 if (this.dropWrites_) | 70 if (this.dropWrites_) |
| 45 return true; | 71 return true; |
| 46 | 72 |
| 47 var result = core.writeMessage(this.handle_, | 73 var result = core.writeMessage(this.handle_, |
| 48 new Uint8Array(message.buffer.arrayBuffer), | 74 new Uint8Array(message.buffer.arrayBuffer), |
| 49 message.handles, | 75 message.handles, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 78 this.errorHandler_ = handler; | 104 this.errorHandler_ = handler; |
| 79 }; | 105 }; |
| 80 | 106 |
| 81 Connector.prototype.waitForNextMessageForTesting = function() { | 107 Connector.prototype.waitForNextMessageForTesting = function() { |
| 82 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE); | 108 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE); |
| 83 this.readMore_(wait.result); | 109 this.readMore_(wait.result); |
| 84 }; | 110 }; |
| 85 | 111 |
| 86 Connector.prototype.readMore_ = function(result) { | 112 Connector.prototype.readMore_ = function(result) { |
| 87 for (;;) { | 113 for (;;) { |
| 114 if (this.paused_) { |
| 115 return; |
| 116 } |
| 117 |
| 88 var read = core.readMessage(this.handle_, | 118 var read = core.readMessage(this.handle_, |
| 89 core.READ_MESSAGE_FLAG_NONE); | 119 core.READ_MESSAGE_FLAG_NONE); |
| 90 if (this.handle_ == null) // The connector has been closed. | 120 if (this.handle_ == null) // The connector has been closed. |
| 91 return; | 121 return; |
| 92 if (read.result == core.RESULT_SHOULD_WAIT) | 122 if (read.result == core.RESULT_SHOULD_WAIT) |
| 93 return; | 123 return; |
| 94 if (read.result != core.RESULT_OK) { | 124 if (read.result != core.RESULT_OK) { |
| 95 // TODO(wangjimmy): Add a handleError method to swap the handle to be | 125 // TODO(wangjimmy): Add a handleError method to swap the handle to be |
| 96 // closed with a dummy handle in the case when | 126 // closed with a dummy handle in the case when |
| 97 // read.result != MOJO_RESULT_FAILED_PRECONDITION | 127 // read.result != MOJO_RESULT_FAILED_PRECONDITION |
| 98 this.error_ = true; | 128 this.error_ = true; |
| 99 if (this.errorHandler_) | 129 if (this.errorHandler_) |
| 100 this.errorHandler_.onError(); | 130 this.errorHandler_.onError(); |
| 101 return; | 131 return; |
| 102 } | 132 } |
| 103 var messageBuffer = new buffer.Buffer(read.buffer); | 133 var messageBuffer = new buffer.Buffer(read.buffer); |
| 104 var message = new codec.Message(messageBuffer, read.handles); | 134 var message = new codec.Message(messageBuffer, read.handles); |
| 105 if (this.incomingReceiver_) | 135 if (this.incomingReceiver_) |
| 106 this.incomingReceiver_.accept(message); | 136 this.incomingReceiver_.accept(message); |
| 107 } | 137 } |
| 108 }; | 138 }; |
| 109 | 139 |
| 110 var exports = {}; | 140 var exports = {}; |
| 111 exports.Connector = Connector; | 141 exports.Connector = Connector; |
| 112 return exports; | 142 return exports; |
| 113 }); | 143 }); |
| OLD | NEW |