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.readWaitCookie_ = null; | 19 this.readWatcher_ = null; |
20 this.errorHandler_ = null; | 20 this.errorHandler_ = null; |
21 | 21 |
22 if (handle) | 22 if (handle) { |
23 this.waitToReadMore_(); | 23 this.readWatcher_ = support.watch(handle, |
| 24 core.HANDLE_SIGNAL_READABLE, |
| 25 this.readMore_.bind(this)); |
| 26 } |
24 } | 27 } |
25 | 28 |
26 Connector.prototype.close = function() { | 29 Connector.prototype.close = function() { |
27 if (this.readWaitCookie_) { | 30 if (this.readWatcher_) { |
28 support.cancelWait(this.readWaitCookie_); | 31 support.cancelWatch(this.readWatcher_); |
29 this.readWaitCookie_ = null; | 32 this.readWatcher_ = null; |
30 } | 33 } |
31 if (this.handle_ != null) { | 34 if (this.handle_ != null) { |
32 core.close(this.handle_); | 35 core.close(this.handle_); |
33 this.handle_ = null; | 36 this.handle_ = null; |
34 } | 37 } |
35 }; | 38 }; |
36 | 39 |
37 Connector.prototype.accept = function(message) { | 40 Connector.prototype.accept = function(message) { |
38 if (this.error_) | 41 if (this.error_) |
39 return false; | 42 return false; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 }; | 75 }; |
73 | 76 |
74 Connector.prototype.setErrorHandler = function(handler) { | 77 Connector.prototype.setErrorHandler = function(handler) { |
75 this.errorHandler_ = handler; | 78 this.errorHandler_ = handler; |
76 }; | 79 }; |
77 | 80 |
78 Connector.prototype.encounteredError = function() { | 81 Connector.prototype.encounteredError = function() { |
79 return this.error_; | 82 return this.error_; |
80 }; | 83 }; |
81 | 84 |
82 Connector.prototype.waitToReadMore_ = function() { | |
83 this.readWaitCookie_ = support.asyncWait(this.handle_, | |
84 core.HANDLE_SIGNAL_READABLE, | |
85 this.readMore_.bind(this)); | |
86 }; | |
87 | |
88 Connector.prototype.readMore_ = function(result) { | 85 Connector.prototype.readMore_ = function(result) { |
89 for (;;) { | 86 for (;;) { |
90 var read = core.readMessage(this.handle_, | 87 var read = core.readMessage(this.handle_, |
91 core.READ_MESSAGE_FLAG_NONE); | 88 core.READ_MESSAGE_FLAG_NONE); |
92 if (this.handle_ == null) // The connector has been closed. | 89 if (this.handle_ == null) // The connector has been closed. |
93 return; | 90 return; |
94 if (read.result == core.RESULT_SHOULD_WAIT) { | 91 if (read.result == core.RESULT_SHOULD_WAIT) |
95 this.waitToReadMore_(); | |
96 return; | 92 return; |
97 } | |
98 if (read.result != core.RESULT_OK) { | 93 if (read.result != core.RESULT_OK) { |
99 this.error_ = true; | 94 this.error_ = true; |
100 if (this.errorHandler_) | 95 if (this.errorHandler_) |
101 this.errorHandler_.onError(read.result); | 96 this.errorHandler_.onError(read.result); |
102 return; | 97 return; |
103 } | 98 } |
104 var messageBuffer = new buffer.Buffer(read.buffer); | 99 var messageBuffer = new buffer.Buffer(read.buffer); |
105 var message = new codec.Message(messageBuffer, read.handles); | 100 var message = new codec.Message(messageBuffer, read.handles); |
106 if (this.incomingReceiver_) { | 101 if (this.incomingReceiver_) { |
107 this.incomingReceiver_.accept(message); | 102 this.incomingReceiver_.accept(message); |
108 } | 103 } |
109 } | 104 } |
110 }; | 105 }; |
111 | 106 |
112 // The TestConnector subclass is only intended to be used in unit tests. It | 107 // The TestConnector subclass is only intended to be used in unit tests. It |
113 // doesn't automatically listen for input messages. Instead, you need to | 108 // doesn't automatically listen for input messages. Instead, you need to |
114 // call waitForNextMessage to block and wait for the next incoming message. | 109 // call waitForNextMessage to block and wait for the next incoming message. |
115 function TestConnector(handle) { | 110 function TestConnector(handle) { |
116 Connector.call(this, handle); | 111 Connector.call(this, handle); |
117 } | 112 } |
118 | 113 |
119 TestConnector.prototype = Object.create(Connector.prototype); | 114 TestConnector.prototype = Object.create(Connector.prototype); |
120 | 115 |
121 TestConnector.prototype.waitToReadMore_ = function() { | |
122 } | |
123 | |
124 TestConnector.prototype.waitForNextMessage = function() { | 116 TestConnector.prototype.waitForNextMessage = function() { |
125 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE, | 117 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE, |
126 core.DEADLINE_INDEFINITE); | 118 core.DEADLINE_INDEFINITE); |
127 this.readMore_(wait.result); | 119 this.readMore_(wait.result); |
128 } | 120 } |
129 | 121 |
130 var exports = {}; | 122 var exports = {}; |
131 exports.Connector = Connector; | 123 exports.Connector = Connector; |
132 exports.TestConnector = TestConnector; | 124 exports.TestConnector = TestConnector; |
133 return exports; | 125 return exports; |
134 }); | 126 }); |
OLD | NEW |