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