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 | |
85 Connector.prototype.waitForNextMessageForTesting = function() { | 81 Connector.prototype.waitForNextMessageForTesting = function() { |
86 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE); | 82 var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE); |
87 this.readMore_(wait.result); | 83 this.readMore_(wait.result); |
88 }; | 84 }; |
89 | 85 |
90 Connector.prototype.readMore_ = function(result) { | 86 Connector.prototype.readMore_ = function(result) { |
91 for (;;) { | 87 for (;;) { |
92 var read = core.readMessage(this.handle_, | 88 var read = core.readMessage(this.handle_, |
93 core.READ_MESSAGE_FLAG_NONE); | 89 core.READ_MESSAGE_FLAG_NONE); |
94 if (this.handle_ == null) // The connector has been closed. | 90 if (this.handle_ == null) // The connector has been closed. |
95 return; | 91 return; |
96 if (read.result == core.RESULT_SHOULD_WAIT) | 92 if (read.result == core.RESULT_SHOULD_WAIT) |
97 return; | 93 return; |
98 if (read.result != core.RESULT_OK) { | 94 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 |
99 this.error_ = true; | 98 this.error_ = true; |
100 if (this.errorHandler_) | 99 if (this.errorHandler_) |
101 this.errorHandler_.onError(read.result); | 100 this.errorHandler_.onError(); |
102 return; | 101 return; |
103 } | 102 } |
104 var messageBuffer = new buffer.Buffer(read.buffer); | 103 var messageBuffer = new buffer.Buffer(read.buffer); |
105 var message = new codec.Message(messageBuffer, read.handles); | 104 var message = new codec.Message(messageBuffer, read.handles); |
106 if (this.incomingReceiver_) | 105 if (this.incomingReceiver_) |
107 this.incomingReceiver_.accept(message); | 106 this.incomingReceiver_.accept(message); |
108 } | 107 } |
109 }; | 108 }; |
110 | 109 |
111 var exports = {}; | 110 var exports = {}; |
112 exports.Connector = Connector; | 111 exports.Connector = Connector; |
113 return exports; | 112 return exports; |
114 }); | 113 }); |
OLD | NEW |