Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: mojo/public/bindings/js/router.js

Issue 207503004: Mojo: add javascript bindings for request/response (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: --similarity=20 Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 };
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)
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 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698