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) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 }; | 71 }; |
72 | 72 |
73 Connector.prototype.setIncomingReceiver = function(receiver) { | 73 Connector.prototype.setIncomingReceiver = function(receiver) { |
74 this.incomingReceiver_ = receiver; | 74 this.incomingReceiver_ = receiver; |
75 }; | 75 }; |
76 | 76 |
77 Connector.prototype.setErrorHandler = function(handler) { | 77 Connector.prototype.setErrorHandler = function(handler) { |
78 this.errorHandler_ = handler; | 78 this.errorHandler_ = handler; |
79 }; | 79 }; |
80 | 80 |
| 81 Connector.prototype.encounteredError = function() { |
| 82 return this.error_; |
| 83 }; |
| 84 |
81 Connector.prototype.waitForNextMessageForTesting = function() { | 85 Connector.prototype.waitForNextMessageForTesting = function() { |
82 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE); | 86 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE); |
83 this.readMore_(wait.result); | 87 this.readMore_(wait.result); |
84 }; | 88 }; |
85 | 89 |
86 Connector.prototype.readMore_ = function(result) { | 90 Connector.prototype.readMore_ = function(result) { |
87 for (;;) { | 91 for (;;) { |
88 var read = core.readMessage(this.handle_, | 92 var read = core.readMessage(this.handle_, |
89 core.READ_MESSAGE_FLAG_NONE); | 93 core.READ_MESSAGE_FLAG_NONE); |
90 if (this.handle_ == null) // The connector has been closed. | 94 if (this.handle_ == null) // The connector has been closed. |
91 return; | 95 return; |
92 if (read.result == core.RESULT_SHOULD_WAIT) | 96 if (read.result == core.RESULT_SHOULD_WAIT) |
93 return; | 97 return; |
94 if (read.result != core.RESULT_OK) { | 98 if (read.result != core.RESULT_OK) { |
95 // TODO(wangjimmy): Add a handleError method to swap the handle to be | |
96 // closed with a dummy handle in the case when | |
97 // read.result != MOJO_RESULT_FAILED_PRECONDITION | |
98 this.error_ = true; | 99 this.error_ = true; |
99 if (this.errorHandler_) | 100 if (this.errorHandler_) |
100 this.errorHandler_.onError(); | 101 this.errorHandler_.onError(read.result); |
101 return; | 102 return; |
102 } | 103 } |
103 var messageBuffer = new buffer.Buffer(read.buffer); | 104 var messageBuffer = new buffer.Buffer(read.buffer); |
104 var message = new codec.Message(messageBuffer, read.handles); | 105 var message = new codec.Message(messageBuffer, read.handles); |
105 if (this.incomingReceiver_) | 106 if (this.incomingReceiver_) |
106 this.incomingReceiver_.accept(message); | 107 this.incomingReceiver_.accept(message); |
107 } | 108 } |
108 }; | 109 }; |
109 | 110 |
110 var exports = {}; | 111 var exports = {}; |
111 exports.Connector = Connector; | 112 exports.Connector = Connector; |
112 return exports; | 113 return exports; |
113 }); | 114 }); |
OLD | NEW |