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 (function() { |
6 var internal = mojo.internal; | 6 var internal = mojo.internal; |
7 | 7 |
8 function Connector(handle) { | 8 function Connector(handle) { |
9 if (!(handle instanceof MojoHandle)) | 9 if (!(handle instanceof MojoHandle)) |
10 throw new Error("Connector: not a handle " + handle); | 10 throw new Error("Connector: not a handle " + handle); |
11 this.handle_ = handle; | 11 this.handle_ = handle; |
12 this.dropWrites_ = false; | 12 this.dropWrites_ = false; |
13 this.error_ = false; | 13 this.error_ = false; |
14 this.incomingReceiver_ = null; | 14 this.incomingReceiver_ = null; |
15 this.readWatcher_ = null; | 15 this.readWatcher_ = null; |
16 this.errorHandler_ = null; | 16 this.errorHandler_ = null; |
| 17 this.paused_ = false; |
17 | 18 |
18 if (handle) { | 19 this.waitToReadMore(); |
19 this.readWatcher_ = handle.watch({readable: true}, | |
20 this.readMore_.bind(this)); | |
21 } | |
22 } | 20 } |
23 | 21 |
24 Connector.prototype.close = function() { | 22 Connector.prototype.close = function() { |
25 if (this.readWatcher_) { | 23 this.cancelWait(); |
26 this.readWatcher_.cancel(); | |
27 this.readWatcher_ = null; | |
28 } | |
29 if (this.handle_ != null) { | 24 if (this.handle_ != null) { |
30 this.handle_.close(); | 25 this.handle_.close(); |
31 this.handle_ = null; | 26 this.handle_ = null; |
32 } | 27 } |
33 }; | 28 }; |
34 | 29 |
| 30 Connector.prototype.pauseIncomingMethodCallProcessing = function() { |
| 31 if (this.paused_) { |
| 32 return; |
| 33 } |
| 34 this.paused_= true; |
| 35 this.cancelWait(); |
| 36 }; |
| 37 |
| 38 Connector.prototype.resumeIncomingMethodCallProcessing = function() { |
| 39 if (!this.paused_) { |
| 40 return; |
| 41 } |
| 42 this.paused_= false; |
| 43 this.waitToReadMore(); |
| 44 }; |
| 45 |
35 Connector.prototype.accept = function(message) { | 46 Connector.prototype.accept = function(message) { |
36 if (this.error_) | 47 if (this.error_) |
37 return false; | 48 return false; |
38 | 49 |
39 if (this.dropWrites_) | 50 if (this.dropWrites_) |
40 return true; | 51 return true; |
41 | 52 |
42 var result = this.handle_.writeMessage( | 53 var result = this.handle_.writeMessage( |
43 new Uint8Array(message.buffer.arrayBuffer), message.handles); | 54 new Uint8Array(message.buffer.arrayBuffer), message.handles); |
44 switch (result) { | 55 switch (result) { |
(...skipping 19 matching lines...) Expand all Loading... |
64 }; | 75 }; |
65 | 76 |
66 Connector.prototype.setIncomingReceiver = function(receiver) { | 77 Connector.prototype.setIncomingReceiver = function(receiver) { |
67 this.incomingReceiver_ = receiver; | 78 this.incomingReceiver_ = receiver; |
68 }; | 79 }; |
69 | 80 |
70 Connector.prototype.setErrorHandler = function(handler) { | 81 Connector.prototype.setErrorHandler = function(handler) { |
71 this.errorHandler_ = handler; | 82 this.errorHandler_ = handler; |
72 }; | 83 }; |
73 | 84 |
74 Connector.prototype.encounteredError = function() { | |
75 return this.error_; | |
76 }; | |
77 | |
78 Connector.prototype.waitForNextMessageForTesting = function() { | 85 Connector.prototype.waitForNextMessageForTesting = function() { |
79 // TODO(yzshen): Change the tests that use this method. | 86 // TODO(yzshen): Change the tests that use this method. |
80 throw new Error("Not supported!"); | 87 throw new Error("Not supported!"); |
81 }; | 88 }; |
82 | 89 |
83 Connector.prototype.readMore_ = function(result) { | 90 Connector.prototype.readMore_ = function(result) { |
84 for (;;) { | 91 for (;;) { |
| 92 if (this.paused_) { |
| 93 return; |
| 94 } |
| 95 |
85 var read = this.handle_.readMessage(); | 96 var read = this.handle_.readMessage(); |
86 if (this.handle_ == null) // The connector has been closed. | 97 if (this.handle_ == null) // The connector has been closed. |
87 return; | 98 return; |
88 if (read.result == Mojo.RESULT_SHOULD_WAIT) | 99 if (read.result == Mojo.RESULT_SHOULD_WAIT) |
89 return; | 100 return; |
90 if (read.result != Mojo.RESULT_OK) { | 101 if (read.result != Mojo.RESULT_OK) { |
91 this.error_ = true; | 102 this.handleError(read.result !== Mojo.RESULT_FAILED_PRECONDITION, |
92 if (this.errorHandler_) | 103 false); |
93 this.errorHandler_.onError(read.result); | |
94 return; | 104 return; |
95 } | 105 } |
96 var messageBuffer = new internal.Buffer(read.buffer); | 106 var messageBuffer = new internal.Buffer(read.buffer); |
97 var message = new internal.Message(messageBuffer, read.handles); | 107 var message = new internal.Message(messageBuffer, read.handles); |
98 if (this.incomingReceiver_) | 108 var receiverResult = this.incomingReceiver_ && |
99 this.incomingReceiver_.accept(message); | 109 this.incomingReceiver_.accept(message); |
| 110 |
| 111 // Handle invalid incoming message. |
| 112 if (!internal.isTestingMode() && !receiverResult) { |
| 113 // TODO(yzshen): Consider notifying the embedder. |
| 114 this.handleError(true, false); |
| 115 } |
100 } | 116 } |
101 }; | 117 }; |
102 | 118 |
| 119 Connector.prototype.cancelWait = function() { |
| 120 if (this.readWatcher_) { |
| 121 this.readWatcher_.cancel(); |
| 122 this.readWatcher_ = null; |
| 123 } |
| 124 }; |
| 125 |
| 126 Connector.prototype.waitToReadMore = function() { |
| 127 if (this.handle_) { |
| 128 this.readWatcher_ = this.handle_.watch({readable: true}, |
| 129 this.readMore_.bind(this)); |
| 130 } |
| 131 }; |
| 132 |
| 133 Connector.prototype.handleError = function(forcePipeReset, |
| 134 forceAsyncHandler) { |
| 135 if (this.error_ || this.handle_ === null) { |
| 136 return; |
| 137 } |
| 138 |
| 139 if (this.paused_) { |
| 140 // Enforce calling the error handler asynchronously if the user has |
| 141 // paused receiving messages. We need to wait until the user starts |
| 142 // receiving messages again. |
| 143 forceAsyncHandler = true; |
| 144 } |
| 145 |
| 146 if (!forcePipeReset && forceAsyncHandler) { |
| 147 forcePipeReset = true; |
| 148 } |
| 149 |
| 150 this.cancelWait(); |
| 151 if (forcePipeReset) { |
| 152 this.handle_.close(); |
| 153 var dummyPipe = Mojo.createMessagePipe(); |
| 154 this.handle_ = dummyPipe.handle0; |
| 155 } |
| 156 |
| 157 if (forceAsyncHandler) { |
| 158 if (!this.paused_) { |
| 159 this.waitToReadMore(); |
| 160 } |
| 161 } else { |
| 162 this.error_ = true; |
| 163 if (this.errorHandler_) { |
| 164 this.errorHandler_.onError(); |
| 165 } |
| 166 } |
| 167 }; |
| 168 |
103 internal.Connector = Connector; | 169 internal.Connector = Connector; |
104 })(); | 170 })(); |
OLD | NEW |