Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 define("mojo/public/bindings/js/router", [ | |
| 6 "mojo/public/bindings/js/codec", | |
| 7 "mojo/public/bindings/js/connector", | |
| 8 ], function(codec, connector) { | |
| 9 | |
| 10 function Router(handle) { | |
| 11 this.connector_ = new connector.Connector(handle); | |
| 12 this.incomingReceiver_ = null; | |
| 13 this.nextRequestID_ = 0; | |
| 14 this.responders_ = {}; | |
| 15 | |
| 16 this.connector_.setIncomingReceiver({ | |
| 17 accept: this.handleIncomingMessage_.bind(this), | |
| 18 }); | |
| 19 } | |
| 20 | |
| 21 Router.prototype.close = function() { | |
| 22 this.connector_.close(); | |
| 23 }; | |
|
abarth-chromium
2014/03/26 19:05:45
Should we drop the responders_ too?
| |
| 24 | |
| 25 Router.prototype.accept = function(message) { | |
| 26 this.connector_.accept(message); | |
| 27 }; | |
| 28 | |
| 29 Router.prototype.acceptWithResponder = function(message, responder) { | |
| 30 // Reserve 0 in case we want it to convey special meaning in the future. | |
| 31 var requestID = this.nextRequestID_++; | |
| 32 if (requestID == 0) | |
|
Matt Perry
2014/03/26 18:19:43
why not just start nextRequestId at 1? or are you
| |
| 33 requestID = this.nextRequestID_++; | |
| 34 | |
| 35 message.setRequestID(requestID); | |
| 36 this.connector_.accept(message); | |
| 37 | |
| 38 this.responders_[requestID] = responder; | |
| 39 }; | |
| 40 | |
| 41 Router.prototype.setIncomingReceiver = function(receiver) { | |
| 42 this.incomingReceiver_ = receiver; | |
| 43 }; | |
| 44 | |
| 45 Router.prototype.handleIncomingMessage_ = function(message) { | |
| 46 var flags = message.getFlags(); | |
| 47 if (flags & codec.kMessageExpectsResponse) { | |
| 48 if (this.incomingReceiver_) { | |
| 49 this.incomingReceiver_.acceptWithResponder(message, this); | |
| 50 } else { | |
| 51 // If we receive a request expecting a response when the client is not | |
| 52 // listening, then we have no choice but to tear down the pipe. | |
| 53 this.close(); | |
| 54 } | |
| 55 } else if (flags & codec.kMessageIsResponse) { | |
| 56 var reader = new codec.MessageReader(message); | |
| 57 var requestID = reader.requestID; | |
| 58 var responder = this.responders_[requestID]; | |
| 59 delete this.responders_[requestID]; | |
| 60 responder.accept(message); | |
| 61 } else { | |
| 62 if (this.incomingReceiver_) | |
| 63 this.incomingReceiver_.accept(message); | |
| 64 } | |
| 65 }; | |
| 66 | |
| 67 var exports = {}; | |
| 68 exports.Router = Router; | |
| 69 return exports; | |
| 70 }); | |
| OLD | NEW |