| 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/router", [ | 5 define("mojo/public/js/router", [ |
| 6 "mojo/public/js/connector", | 6 "mojo/public/js/connector", |
| 7 "mojo/public/js/core", | 7 "mojo/public/js/core", |
| 8 "mojo/public/js/interface_types", | 8 "mojo/public/js/interface_types", |
| 9 "mojo/public/js/lib/interface_endpoint_handle", | 9 "mojo/public/js/lib/interface_endpoint_handle", |
| 10 "mojo/public/js/lib/pipe_control_message_handler", | 10 "mojo/public/js/lib/pipe_control_message_handler", |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 this.connector_.setIncomingReceiver({ | 67 this.connector_.setIncomingReceiver({ |
| 68 accept: this.accept.bind(this), | 68 accept: this.accept.bind(this), |
| 69 }); | 69 }); |
| 70 this.connector_.setErrorHandler({ | 70 this.connector_.setErrorHandler({ |
| 71 onError: this.onPipeConnectionError.bind(this), | 71 onError: this.onPipeConnectionError.bind(this), |
| 72 }); | 72 }); |
| 73 | 73 |
| 74 this.setInterfaceIdNamespaceBit_ = setInterfaceIdNamespaceBit; | 74 this.setInterfaceIdNamespaceBit_ = setInterfaceIdNamespaceBit; |
| 75 this.controlMessageHandler_ = new PipeControlMessageHandler(this); | 75 this.controlMessageHandler_ = new PipeControlMessageHandler(this); |
| 76 this.controlMessageProxy_ = new PipeControlMessageProxy(this.connector_); | 76 this.controlMessageProxy_ = new PipeControlMessageProxy(this.connector_); |
| 77 this.nextInterfaceIdValue = 1; | 77 this.nextInterfaceIdValue_ = 1; |
| 78 this.encounteredError_ = false; | 78 this.encounteredError_ = false; |
| 79 this.endpoints_ = new Map(); | 79 this.endpoints_ = new Map(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 Router.prototype.associateInterface = function(handleToSend) { |
| 83 if (!handleToSend.pendingAssociation()) { |
| 84 return types.kInvalidInterfaceId; |
| 85 } |
| 86 |
| 87 var id = 0; |
| 88 do { |
| 89 if (this.nextInterfaceIdValue_ >= types.kInterfaceIdNamespaceMask) { |
| 90 this.nextInterfaceIdValue_ = 1; |
| 91 } |
| 92 id = this.nextInterfaceIdValue_++; |
| 93 if (this.setInterfaceIdNamespaceBit_) { |
| 94 id += types.kInterfaceIdNamespaceMask; |
| 95 } |
| 96 } while (this.endpoints_.has(id)); |
| 97 |
| 98 var endpoint = new InterfaceEndpoint(this, id); |
| 99 this.endpoints_.set(id, endpoint); |
| 100 if (this.encounteredError_) { |
| 101 this.updateEndpointStateMayRemove(endpoint, |
| 102 EndpointStateUpdateType.PEER_ENDPOINT_CLOSED); |
| 103 } |
| 104 endpoint.handleCreated = true; |
| 105 |
| 106 if (!handleToSend.notifyAssociation(id, this)) { |
| 107 // The peer handle of |handleToSend|, which is supposed to join this |
| 108 // associated group, has been closed. |
| 109 this.updateEndpointStateMayRemove(endpoint, |
| 110 EndpointStateUpdateType.ENDPOINT_CLOSED); |
| 111 |
| 112 pipeControlMessageproxy.notifyPeerEndpointClosed(id, |
| 113 handleToSend.disconnectReason()); |
| 114 } |
| 115 |
| 116 return id; |
| 117 }; |
| 118 |
| 82 Router.prototype.attachEndpointClient = function( | 119 Router.prototype.attachEndpointClient = function( |
| 83 interfaceEndpointHandle, interfaceEndpointClient) { | 120 interfaceEndpointHandle, interfaceEndpointClient) { |
| 84 check(types.isValidInterfaceId(interfaceEndpointHandle.id())); | 121 check(types.isValidInterfaceId(interfaceEndpointHandle.id())); |
| 85 check(interfaceEndpointClient); | 122 check(interfaceEndpointClient); |
| 86 | 123 |
| 87 var endpoint = this.endpoints_.get(interfaceEndpointHandle.id()); | 124 var endpoint = this.endpoints_.get(interfaceEndpointHandle.id()); |
| 88 check(endpoint); | 125 check(endpoint); |
| 89 check(!endpoint.client); | 126 check(!endpoint.client); |
| 90 check(!endpoint.closed); | 127 check(!endpoint.closed); |
| 91 endpoint.client = interfaceEndpointClient; | 128 endpoint.client = interfaceEndpointClient; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 return new InterfaceEndpointHandle(interfaceId, this); | 179 return new InterfaceEndpointHandle(interfaceId, this); |
| 143 }; | 180 }; |
| 144 | 181 |
| 145 Router.prototype.accept = function(message) { | 182 Router.prototype.accept = function(message) { |
| 146 var messageValidator = new Validator(message); | 183 var messageValidator = new Validator(message); |
| 147 var err = messageValidator.validateMessageHeader(); | 184 var err = messageValidator.validateMessageHeader(); |
| 148 | 185 |
| 149 var ok = false; | 186 var ok = false; |
| 150 if (err !== validator.validationError.NONE) { | 187 if (err !== validator.validationError.NONE) { |
| 151 validator.reportValidationError(err); | 188 validator.reportValidationError(err); |
| 152 } else if (controlMessageHandler.isPipeControlMessage(message)) { | 189 } else if (message.deserializeAssociatedEndpointHandles(this)) { |
| 153 ok = this.controlMessageHandler_.accept(message); | 190 if (controlMessageHandler.isPipeControlMessage(message)) { |
| 154 } else { | 191 ok = this.controlMessageHandler_.accept(message); |
| 155 var interfaceId = message.getInterfaceId(); | 192 } else { |
| 156 var endpoint = this.endpoints_.get(interfaceId); | 193 var interfaceId = message.getInterfaceId(); |
| 157 if (!endpoint || endpoint.closed) { | 194 var endpoint = this.endpoints_.get(interfaceId); |
| 158 return true; | 195 if (!endpoint || endpoint.closed) { |
| 196 return true; |
| 197 } |
| 198 |
| 199 if (!endpoint.client) { |
| 200 // We need to wait until a client is attached in order to dispatch |
| 201 // further messages. |
| 202 // TODO(wangjimmy): Cache the message and send when the appropriate |
| 203 // endpoint client is attached. |
| 204 return false; |
| 205 } |
| 206 ok = endpoint.client.handleIncomingMessage(message, messageValidator); |
| 159 } | 207 } |
| 160 | |
| 161 if (!endpoint.client) { | |
| 162 // We need to wait until a client is attached in order to dispatch | |
| 163 // further messages. | |
| 164 return false; | |
| 165 } | |
| 166 ok = endpoint.client.handleIncomingMessage_(message); | |
| 167 } | 208 } |
| 168 | 209 |
| 169 if (!ok) { | 210 if (!ok) { |
| 170 this.handleInvalidIncomingMessage_(); | 211 this.handleInvalidIncomingMessage_(); |
| 171 } | 212 } |
| 172 return ok; | 213 return ok; |
| 173 }; | 214 }; |
| 174 | 215 |
| 175 Router.prototype.close = function() { | 216 Router.prototype.close = function() { |
| 176 this.connector_.close(); | 217 this.connector_.close(); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 } | 301 } |
| 261 if (endpoint.closed && endpoint.peerClosed) { | 302 if (endpoint.closed && endpoint.peerClosed) { |
| 262 this.endpoints_.delete(endpoint.id); | 303 this.endpoints_.delete(endpoint.id); |
| 263 } | 304 } |
| 264 }; | 305 }; |
| 265 | 306 |
| 266 var exports = {}; | 307 var exports = {}; |
| 267 exports.Router = Router; | 308 exports.Router = Router; |
| 268 return exports; | 309 return exports; |
| 269 }); | 310 }); |
| OLD | NEW |