| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/lib/interface_endpoint_client", [ | 5 define("mojo/public/js/lib/interface_endpoint_client", [ |
| 6 "console", | 6 "console", |
| 7 "mojo/public/js/codec", | 7 "mojo/public/js/codec", |
| 8 "mojo/public/js/lib/control_message_handler", | 8 "mojo/public/js/lib/control_message_handler", |
| 9 "mojo/public/js/lib/control_message_proxy", | 9 "mojo/public/js/lib/control_message_proxy", |
| 10 "mojo/public/js/lib/interface_endpoint_handle", | 10 "mojo/public/js/lib/interface_endpoint_handle", |
| 11 "mojo/public/js/validator", | 11 "mojo/public/js/validator", |
| 12 "timer", | 12 "timer", |
| 13 ], function(console, | 13 ], function(console, |
| 14 codec, | 14 codec, |
| 15 controlMessageHandler, | 15 controlMessageHandler, |
| 16 controlMessageProxy, | 16 controlMessageProxy, |
| 17 interfaceEndpointHandle, | 17 interfaceEndpointHandle, |
| 18 validator, | 18 validator, |
| 19 timer) { | 19 timer) { |
| 20 | 20 |
| 21 var ControlMessageHandler = controlMessageHandler.ControlMessageHandler; | 21 var ControlMessageHandler = controlMessageHandler.ControlMessageHandler; |
| 22 var ControlMessageProxy = controlMessageProxy.ControlMessageProxy; | 22 var ControlMessageProxy = controlMessageProxy.ControlMessageProxy; |
| 23 var MessageReader = codec.MessageReader; | 23 var MessageReader = codec.MessageReader; |
| 24 var Validator = validator.Validator; | |
| 25 var InterfaceEndpointHandle = interfaceEndpointHandle.InterfaceEndpointHandle; | 24 var InterfaceEndpointHandle = interfaceEndpointHandle.InterfaceEndpointHandle; |
| 25 var AssociationEvent = interfaceEndpointHandle.AssociationEvent; |
| 26 | 26 |
| 27 function InterfaceEndpointClient(interfaceEndpointHandle, receiver, | 27 function InterfaceEndpointClient(interfaceEndpointHandle, receiver, |
| 28 interfaceVersion) { | 28 interfaceVersion) { |
| 29 this.controller_ = null; | 29 this.controller_ = null; |
| 30 this.encounteredError_ = false; | 30 this.encounteredError_ = false; |
| 31 this.handle_ = interfaceEndpointHandle; | 31 this.handle_ = interfaceEndpointHandle; |
| 32 this.incomingReceiver_ = receiver; | 32 this.incomingReceiver_ = receiver; |
| 33 | 33 |
| 34 if (interfaceVersion !== undefined) { | 34 if (interfaceVersion !== undefined) { |
| 35 this.controlMessageHandler_ = new ControlMessageHandler( | 35 this.controlMessageHandler_ = new ControlMessageHandler( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 55 if (this.controller_ || this.handle_.pendingAssociation()) { | 55 if (this.controller_ || this.handle_.pendingAssociation()) { |
| 56 return; | 56 return; |
| 57 } | 57 } |
| 58 | 58 |
| 59 this.controller_ = this.handle_.groupController().attachEndpointClient( | 59 this.controller_ = this.handle_.groupController().attachEndpointClient( |
| 60 this.handle_, this); | 60 this.handle_, this); |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 InterfaceEndpointClient.prototype.onAssociationEvent = function( | 63 InterfaceEndpointClient.prototype.onAssociationEvent = function( |
| 64 associationEvent) { | 64 associationEvent) { |
| 65 if (associationEvent === | 65 if (associationEvent === AssociationEvent.ASSOCIATED) { |
| 66 InterfaceEndpointHandle.AssociationEvent.ASSOCIATED) { | |
| 67 this.initControllerIfNecessary_(); | 66 this.initControllerIfNecessary_(); |
| 68 } else if (associationEvent === | 67 } else if (associationEvent === |
| 69 InterfaceEndpointHandle.AssociationEvent | 68 AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION) { |
| 70 .PEER_CLOSED_BEFORE_ASSOCIATION) { | |
| 71 timer.createOneShot(0, this.notifyError.bind(this, | 69 timer.createOneShot(0, this.notifyError.bind(this, |
| 72 this.handle_.disconnectReason())); | 70 this.handle_.disconnectReason())); |
| 73 } | 71 } |
| 74 }; | 72 }; |
| 75 | 73 |
| 76 InterfaceEndpointClient.prototype.passHandle = function() { | 74 InterfaceEndpointClient.prototype.passHandle = function() { |
| 77 if (!this.handle_.isValid()) { | 75 if (!this.handle_.isValid()) { |
| 78 return new InterfaceEndpointHandle(); | 76 return new InterfaceEndpointHandle(); |
| 79 } | 77 } |
| 80 | 78 |
| 81 // Used to clear the previously set callback. | 79 // Used to clear the previously set callback. |
| 82 this.handle_.setAssociationEventHandler(undefined); | 80 this.handle_.setAssociationEventHandler(undefined); |
| 83 | 81 |
| 84 if (this.controller_) { | 82 if (this.controller_) { |
| 85 this.controller_ = null; | 83 this.controller_ = null; |
| 86 this.handle_.groupController().detachEndpointClient(this.handle_); | 84 this.handle_.groupController().detachEndpointClient(this.handle_); |
| 87 } | 85 } |
| 88 var handle = this.handle_; | 86 var handle = this.handle_; |
| 89 this.handle_ = null; | 87 this.handle_ = null; |
| 90 return handle; | 88 return handle; |
| 91 }; | 89 }; |
| 92 | 90 |
| 93 InterfaceEndpointClient.prototype.close = function(reason) { | 91 InterfaceEndpointClient.prototype.close = function(reason) { |
| 94 var handle = this.passHandle(); | 92 var handle = this.passHandle(); |
| 95 handle.reset(reason); | 93 handle.reset(reason); |
| 96 }; | 94 }; |
| 97 | 95 |
| 98 InterfaceEndpointClient.prototype.accept = function(message) { | 96 InterfaceEndpointClient.prototype.accept = function(message) { |
| 97 if (message.associatedEndpointHandles.length > 0) { |
| 98 message.serializeAssociatedEndpointHandles( |
| 99 this.handle_.groupController()); |
| 100 } |
| 101 |
| 99 if (this.encounteredError_) { | 102 if (this.encounteredError_) { |
| 100 return false; | 103 return false; |
| 101 } | 104 } |
| 102 | 105 |
| 103 this.initControllerIfNecessary_(); | 106 this.initControllerIfNecessary_(); |
| 104 return this.controller_.sendMessage(message); | 107 return this.controller_.sendMessage(message); |
| 105 }; | 108 }; |
| 106 | 109 |
| 107 InterfaceEndpointClient.prototype.acceptAndExpectResponse = function( | 110 InterfaceEndpointClient.prototype.acceptAndExpectResponse = function( |
| 108 message) { | 111 message) { |
| 112 if (message.associatedEndpointHandles.length > 0) { |
| 113 message.serializeAssociatedEndpointHandles( |
| 114 this.handle_.groupController()); |
| 115 } |
| 116 |
| 109 if (this.encounteredError_) { | 117 if (this.encounteredError_) { |
| 110 return Promise.reject(); | 118 return Promise.reject(); |
| 111 } | 119 } |
| 112 | 120 |
| 113 this.initControllerIfNecessary_(); | 121 this.initControllerIfNecessary_(); |
| 114 | 122 |
| 115 // Reserve 0 in case we want it to convey special meaning in the future. | 123 // Reserve 0 in case we want it to convey special meaning in the future. |
| 116 var requestID = this.nextRequestID_++; | 124 var requestID = this.nextRequestID_++; |
| 117 if (requestID === 0) | 125 if (requestID === 0) |
| 118 requestID = this.nextRequestID_++; | 126 requestID = this.nextRequestID_++; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 137 | 145 |
| 138 InterfaceEndpointClient.prototype.setIncomingReceiver = function(receiver) { | 146 InterfaceEndpointClient.prototype.setIncomingReceiver = function(receiver) { |
| 139 this.incomingReceiver_ = receiver; | 147 this.incomingReceiver_ = receiver; |
| 140 }; | 148 }; |
| 141 | 149 |
| 142 InterfaceEndpointClient.prototype.setConnectionErrorHandler = function( | 150 InterfaceEndpointClient.prototype.setConnectionErrorHandler = function( |
| 143 handler) { | 151 handler) { |
| 144 this.connectionErrorHandler_ = handler; | 152 this.connectionErrorHandler_ = handler; |
| 145 }; | 153 }; |
| 146 | 154 |
| 147 InterfaceEndpointClient.prototype.handleIncomingMessage_ = function( | 155 InterfaceEndpointClient.prototype.handleIncomingMessage = function(message, |
| 148 message) { | 156 messageValidator) { |
| 149 var noError = validator.validationError.NONE; | 157 var noError = validator.validationError.NONE; |
| 150 var messageValidator = new Validator(message); | |
| 151 var err = noError; | 158 var err = noError; |
| 152 for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i) | 159 for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i) |
| 153 err = this.payloadValidators_[i](messageValidator); | 160 err = this.payloadValidators_[i](messageValidator); |
| 154 | 161 |
| 155 if (err == noError) { | 162 if (err == noError) { |
| 156 return this.handleValidIncomingMessage_(message); | 163 return this.handleValidIncomingMessage_(message); |
| 157 } else { | 164 } else { |
| 158 validator.reportValidationError(err); | 165 validator.reportValidationError(err); |
| 159 return false; | 166 return false; |
| 160 } | 167 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 230 |
| 224 InterfaceEndpointClient.prototype.requireVersion = function(version) { | 231 InterfaceEndpointClient.prototype.requireVersion = function(version) { |
| 225 this.controlMessageProxy_.requireVersion(version); | 232 this.controlMessageProxy_.requireVersion(version); |
| 226 }; | 233 }; |
| 227 | 234 |
| 228 var exports = {}; | 235 var exports = {}; |
| 229 exports.InterfaceEndpointClient = InterfaceEndpointClient; | 236 exports.InterfaceEndpointClient = InterfaceEndpointClient; |
| 230 | 237 |
| 231 return exports; | 238 return exports; |
| 232 }); | 239 }); |
| OLD | NEW |