Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 define("mojo/public/js/lib/interface_endpoint_client", [ | |
| 6 "console", | |
| 7 "mojo/public/js/codec", | |
| 8 "mojo/public/js/lib/control_message_handler", | |
| 9 "mojo/public/js/lib/control_message_proxy", | |
| 10 "mojo/public/js/lib/interface_endpoint_handle", | |
| 11 "mojo/public/js/validator", | |
| 12 "timer", | |
| 13 ], function(console, codec, controlMessageHandler, controlMessageProxy, | |
|
yzshen1
2017/03/24 21:20:12
Style nit: For function definitions (as opposed to
wangjimmy
2017/03/27 16:51:28
Done.
| |
| 14 interfaceEndpointHandle, validator, timer) { | |
| 15 | |
| 16 var ControlMessageHandler = controlMessageHandler.ControlMessageHandler; | |
| 17 var ControlMessageProxy = controlMessageProxy.ControlMessageProxy; | |
| 18 var MessageReader = codec.MessageReader; | |
| 19 var Validator = validator.Validator; | |
| 20 var InterfaceEndpointHandle = interfaceEndpointHandle.InterfaceEndpointHandle; | |
| 21 | |
| 22 function InterfaceEndpointClient(interfaceEndpointHandle, receiver, | |
| 23 interfaceVersion) { | |
| 24 this.controller_ = null; | |
| 25 this.encounteredError_ = false; | |
| 26 this.handle_ = interfaceEndpointHandle; | |
| 27 this.incomingReceiver_ = receiver; | |
| 28 | |
| 29 if (interfaceEndpointHandle.pendingAssociation()) { | |
|
yzshen1
2017/03/24 21:20:13
Please move this block to the end, after declarati
wangjimmy
2017/03/27 16:51:27
Done.
| |
| 30 interfaceEndpointHandle.setAssociationEventHandler( | |
| 31 this.onAssociationEvent.bind(this)); | |
| 32 } else { | |
| 33 this.initControllerIfNecessary(); | |
| 34 } | |
| 35 | |
| 36 if (interfaceVersion !== undefined) { | |
| 37 this.controlMessageHandler_ = new ControlMessageHandler( | |
| 38 interfaceVersion); | |
| 39 } else { | |
| 40 this.controlMessageProxy_ = new ControlMessageProxy(this); | |
| 41 } | |
| 42 | |
| 43 this.nextRequestID_ = 0; | |
| 44 this.completers_ = new Map(); | |
| 45 this.payloadValidators_ = []; | |
| 46 this.connectionErrorHandler_ = undefined; | |
|
yzshen1
2017/03/24 21:20:13
I noticed that some fields are inited as "undefine
wangjimmy
2017/03/27 16:51:28
Done. I was trying to use undefined for callbacks.
| |
| 47 } | |
| 48 | |
| 49 InterfaceEndpointClient.prototype.initControllerIfNecessary = function() { | |
|
yzshen1
2017/03/24 21:20:12
private functions should end with trailing undersc
wangjimmy
2017/03/27 16:51:27
Done.
| |
| 50 if (this.controller_ || this.handle_.pendingAssociation()) { | |
| 51 return; | |
| 52 } | |
| 53 | |
| 54 this.controller_ = this.handle_.groupController().attachEndpointClient( | |
| 55 this.handle_, this); | |
| 56 }; | |
| 57 | |
| 58 InterfaceEndpointClient.prototype.onAssociationEvent = function( | |
| 59 associationEvent) { | |
| 60 if (associationEvent === | |
| 61 InterfaceEndpointHandle.AssociationEvent.ASSOCIATED) { | |
|
yzshen1
2017/03/24 21:20:12
Indent should be 4 spaces.
wangjimmy
2017/03/27 16:51:28
Done.
| |
| 62 this.initControllerIfNecessary(); | |
| 63 } else if (associationEvent === | |
| 64 InterfaceEndpointHandle.AssociationEvent.PEER_CLOSED_BEFORE_ASSOCIATION) { | |
|
yzshen1
2017/03/24 21:20:12
ditto
wangjimmy
2017/03/27 16:51:27
Done.
| |
| 65 timer.createOneShot(0, this.notifyError.bind(this, | |
| 66 this.handle_.disconnectReason())); | |
| 67 } | |
| 68 }; | |
| 69 | |
| 70 InterfaceEndpointClient.prototype.passHandle = function() { | |
| 71 if (!this.handle_.isValid()) { | |
| 72 return new InterfaceEndpointHandle(); | |
| 73 } | |
| 74 | |
| 75 // Used to clear the previously set callback. | |
| 76 this.handle_.setAssociationEventHandler(undefined); | |
| 77 | |
| 78 if (this.controller_) { | |
| 79 this.controller_ = null; | |
| 80 this.handle_.groupController().detachEndpointClient(this.handle_); | |
| 81 } | |
| 82 var handle = this.handle_; | |
| 83 this.handle_ = null; | |
| 84 return handle; | |
| 85 }; | |
| 86 | |
| 87 InterfaceEndpointClient.prototype.closeWithReason = function(reason) { | |
| 88 var handle = this.passHandle(); | |
| 89 handle.reset(reason); | |
| 90 }; | |
| 91 | |
| 92 InterfaceEndpointClient.prototype.accept = function(message) { | |
| 93 if (this.encounteredError_) { | |
| 94 return false; | |
| 95 } | |
| 96 | |
| 97 this.initControllerIfNecessary(); | |
| 98 return this.controller_.sendMessage(message); | |
| 99 }; | |
| 100 | |
| 101 InterfaceEndpointClient.prototype.acceptAndExpectResponse = function(message) | |
| 102 { | |
|
yzshen1
2017/03/24 21:20:12
{ should be on the same line as the parameter list
wangjimmy
2017/03/27 16:51:27
Done.
| |
| 103 if (this.encounteredError_) { | |
| 104 return Promise.reject(); | |
| 105 } | |
| 106 | |
| 107 this.initControllerIfNecessary(); | |
| 108 | |
| 109 // Reserve 0 in case we want it to convey special meaning in the future. | |
| 110 var requestID = this.nextRequestID_++; | |
| 111 if (requestID === 0) | |
| 112 requestID = this.nextRequestID_++; | |
| 113 | |
| 114 message.setRequestID(requestID); | |
| 115 var result = this.controller_.sendMessage(message); | |
| 116 if (!result) | |
| 117 return Promise.reject(Error("Connection error")); | |
| 118 | |
| 119 var completer = {}; | |
| 120 this.completers_.set(requestID, completer); | |
| 121 return new Promise(function(resolve, reject) { | |
| 122 completer.resolve = resolve; | |
| 123 completer.reject = reject; | |
| 124 }); | |
| 125 }; | |
| 126 | |
| 127 InterfaceEndpointClient.prototype.reject = function(message) { | |
|
yzshen1
2017/03/24 21:20:13
How is this used?
wangjimmy
2017/03/27 16:51:27
This method is never used.
Looking at the git blam
| |
| 128 // TODO(mpcomplete): no way to trasmit errors over a Connection. | |
| 129 }; | |
| 130 | |
| 131 InterfaceEndpointClient.prototype.setPayloadValidators = function( | |
| 132 payloadValidators) { | |
|
yzshen1
2017/03/24 21:20:12
4-space indent.
wangjimmy
2017/03/27 16:51:27
Done.
| |
| 133 this.payloadValidators_ = payloadValidators; | |
| 134 }; | |
| 135 | |
| 136 InterfaceEndpointClient.prototype.setIncomingReceiver = function(receiver) { | |
| 137 this.incomingReceiver_ = receiver; | |
| 138 }; | |
| 139 | |
| 140 InterfaceEndpointClient.prototype.setConnectionErrorHandler = | |
| 141 function(handler) { | |
|
yzshen1
2017/03/24 21:20:12
ditto
wangjimmy
2017/03/27 16:51:27
Done.
| |
| 142 this.connectionErrorHandler_ = handler; | |
| 143 }; | |
| 144 | |
| 145 InterfaceEndpointClient.prototype.handleIncomingMessage_ = function(message) | |
| 146 { | |
|
yzshen1
2017/03/24 21:20:12
{ should be on the same line as parameter list.
wangjimmy
2017/03/27 16:51:27
Done.
| |
| 147 var noError = validator.validationError.NONE; | |
| 148 var messageValidator = new Validator(message); | |
| 149 var err = noError; | |
| 150 for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i) | |
| 151 err = this.payloadValidators_[i](messageValidator); | |
| 152 | |
| 153 if (err == noError) { | |
| 154 return this.handleValidIncomingMessage_(message); | |
| 155 } | |
| 156 else { | |
|
yzshen1
2017/03/24 21:20:12
This should be on the same line as the previous "}
wangjimmy
2017/03/27 16:51:27
Done.
| |
| 157 validator.reportValidationError(err); | |
| 158 return false; | |
| 159 } | |
| 160 }; | |
| 161 | |
| 162 InterfaceEndpointClient.prototype.handleValidIncomingMessage_ = function( | |
| 163 message) { | |
| 164 if (validator.isTestingMode()) { | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 168 if (this.encounteredError_) { | |
| 169 return false; | |
| 170 } | |
| 171 | |
| 172 var ok = false; | |
| 173 | |
| 174 if (message.expectsResponse()) { | |
| 175 if (controlMessageHandler.isControlMessage(message) && | |
| 176 this.controlMessageHandler_) { | |
| 177 ok = this.controlMessageHandler_.acceptWithResponder(message, this); | |
| 178 } else if (this.incomingReceiver_) { | |
| 179 ok = this.incomingReceiver_.acceptWithResponder(message, this); | |
| 180 } | |
| 181 } else if (message.isResponse()) { | |
| 182 var reader = new MessageReader(message); | |
| 183 var requestID = reader.requestID; | |
| 184 var completer = this.completers_.get(requestID); | |
| 185 if (completer) { | |
| 186 this.completers_.delete(requestID); | |
| 187 completer.resolve(message); | |
| 188 ok = true; | |
| 189 } else { | |
| 190 console.log("Unexpected response with request ID: " + requestID); | |
| 191 } | |
| 192 } else { | |
| 193 if (controlMessageHandler.isControlMessage(message) && | |
| 194 this.controlMessageHandler_) { | |
|
yzshen1
2017/03/24 21:20:13
wrong indent.
wangjimmy
2017/03/27 16:51:27
Done.
| |
| 195 ok = this.controlMessageHandler_.accept(message); | |
| 196 } else if (this.incomingReceiver_) { | |
| 197 ok = this.incomingReceiver_.accept(message); | |
| 198 } | |
| 199 } | |
| 200 return ok; | |
| 201 }; | |
| 202 | |
| 203 InterfaceEndpointClient.prototype.notifyError = function(reason) { | |
| 204 if (this.encounteredError_) { | |
| 205 return; | |
| 206 } | |
| 207 this.encounteredError_ = true; | |
| 208 | |
| 209 this.completers_.forEach(function(value) { | |
| 210 value.reject(); | |
| 211 }); | |
| 212 this.completers_.clear(); // Drop any responders. | |
| 213 | |
| 214 if (this.connectionErrorHandler_) { | |
| 215 this.connectionErrorHandler_(reason); | |
| 216 } | |
| 217 }; | |
| 218 | |
| 219 InterfaceEndpointClient.prototype.queryVersion = function() { | |
| 220 return this.controlMessageProxy_.queryVersion(); | |
| 221 }; | |
| 222 | |
| 223 InterfaceEndpointClient.prototype.requireVersion = function(version) { | |
| 224 this.controlMessageProxy_.requireVersion(version); | |
| 225 }; | |
| 226 | |
| 227 var exports = {}; | |
| 228 exports.InterfaceEndpointClient = InterfaceEndpointClient; | |
| 229 | |
| 230 return exports; | |
| 231 }); | |
| OLD | NEW |