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