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 ], function(buffer, codec, core) { |
10 ], function(buffer, codec, core, support) { | |
11 | 10 |
12 function Connector(handle) { | 11 function Connector(handle) { |
13 if (!core.isHandle(handle)) | 12 if (!(handle instanceof MojoHandle)) |
14 throw new Error("Connector: not a handle " + handle); | 13 throw new Error("Connector: not a handle " + handle); |
| 14 |
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 | 21 |
22 if (handle) { | 22 if (handle) { |
23 this.readWatcher_ = support.watch(handle, | 23 this.readWatcher_ = core.watch(handle, |
24 core.HANDLE_SIGNAL_READABLE, | 24 core.HANDLE_SIGNAL_READABLE, |
25 this.readMore_.bind(this)); | 25 this.readMore_.bind(this)); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 Connector.prototype.close = function() { | 29 Connector.prototype.close = function() { |
30 if (this.readWatcher_) { | 30 if (this.readWatcher_) { |
31 support.cancelWatch(this.readWatcher_); | 31 this.readWatcher_.cancel(); |
32 this.readWatcher_ = null; | 32 this.readWatcher_ = null; |
33 } | 33 } |
34 if (this.handle_ != null) { | 34 if (this.handle_ != null) { |
35 core.close(this.handle_); | 35 this.handle_.close(); |
36 this.handle_ = null; | 36 this.handle_ = null; |
37 } | 37 } |
38 }; | 38 }; |
39 | 39 |
40 Connector.prototype.accept = function(message) { | 40 Connector.prototype.accept = function(message) { |
41 if (this.error_) | 41 if (this.error_) |
42 return false; | 42 return false; |
43 | 43 |
44 if (this.dropWrites_) | 44 if (this.dropWrites_) |
45 return true; | 45 return true; |
46 | 46 |
47 var result = core.writeMessage(this.handle_, | 47 var result = core.writeMessage(this.handle_, |
48 new Uint8Array(message.buffer.arrayBuffer), | 48 message.buffer.arrayBuffer, |
49 message.handles, | 49 message.handles, |
50 core.WRITE_MESSAGE_FLAG_NONE); | 50 core.WRITE_MESSAGE_FLAG_NONE); |
51 switch (result) { | 51 switch (result) { |
52 case core.RESULT_OK: | 52 case core.RESULT_OK: |
53 // The handles were successfully transferred, so we don't own them | 53 // The handles were successfully transferred, so we don't own them |
54 // anymore. | 54 // anymore. |
55 message.handles = []; | 55 message.handles = []; |
56 break; | 56 break; |
57 case core.RESULT_FAILED_PRECONDITION: | 57 case core.RESULT_FAILED_PRECONDITION: |
58 // There's no point in continuing to write to this pipe since the other | 58 // There's no point in continuing to write to this pipe since the other |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE, | 117 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE, |
118 core.DEADLINE_INDEFINITE); | 118 core.DEADLINE_INDEFINITE); |
119 this.readMore_(wait.result); | 119 this.readMore_(wait.result); |
120 } | 120 } |
121 | 121 |
122 var exports = {}; | 122 var exports = {}; |
123 exports.Connector = Connector; | 123 exports.Connector = Connector; |
124 exports.TestConnector = TestConnector; | 124 exports.TestConnector = TestConnector; |
125 return exports; | 125 return exports; |
126 }); | 126 }); |
OLD | NEW |