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

Side by Side Diff: mojo/public/js/connector.js

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 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
« no previous file with comments | « mojo/public/js/connection.js ('k') | mojo/public/js/constants.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/js/connector", [
6 "mojo/public/js/buffer",
7 "mojo/public/js/codec",
8 "mojo/public/js/core",
9 "mojo/public/js/support",
10 ], function(buffer, codec, core, support) {
11
12 function Connector(handle) {
13 if (!core.isHandle(handle))
14 throw new Error("Connector: not a handle " + handle);
15 this.handle_ = handle;
16 this.dropWrites_ = false;
17 this.error_ = false;
18 this.incomingReceiver_ = null;
19 this.readWaitCookie_ = null;
20 this.errorHandler_ = null;
21
22 if (handle)
23 this.waitToReadMore_();
24 }
25
26 Connector.prototype.close = function() {
27 if (this.readWaitCookie_) {
28 support.cancelWait(this.readWaitCookie_);
29 this.readWaitCookie_ = null;
30 }
31 if (this.handle_ != null) {
32 core.close(this.handle_);
33 this.handle_ = null;
34 }
35 };
36
37 Connector.prototype.accept = function(message) {
38 if (this.error_)
39 return false;
40
41 if (this.dropWrites_)
42 return true;
43
44 var result = core.writeMessage(this.handle_,
45 new Uint8Array(message.buffer.arrayBuffer),
46 message.handles,
47 core.WRITE_MESSAGE_FLAG_NONE);
48 switch (result) {
49 case core.RESULT_OK:
50 // The handles were successfully transferred, so we don't own them
51 // anymore.
52 message.handles = [];
53 break;
54 case core.RESULT_FAILED_PRECONDITION:
55 // There's no point in continuing to write to this pipe since the other
56 // end is gone. Avoid writing any future messages. Hide write failures
57 // from the caller since we'd like them to continue consuming any
58 // backlog of incoming messages before regarding the message pipe as
59 // closed.
60 this.dropWrites_ = true;
61 break;
62 default:
63 // This particular write was rejected, presumably because of bad input.
64 // The pipe is not necessarily in a bad state.
65 return false;
66 }
67 return true;
68 };
69
70 Connector.prototype.setIncomingReceiver = function(receiver) {
71 this.incomingReceiver_ = receiver;
72 };
73
74 Connector.prototype.setErrorHandler = function(handler) {
75 this.errorHandler_ = handler;
76 };
77
78 Connector.prototype.encounteredError = function() {
79 return this.error_;
80 };
81
82 Connector.prototype.waitToReadMore_ = function() {
83 this.readWaitCookie_ = support.asyncWait(this.handle_,
84 core.HANDLE_SIGNAL_READABLE,
85 this.readMore_.bind(this));
86 };
87
88 Connector.prototype.readMore_ = function(result) {
89 for (;;) {
90 var read = core.readMessage(this.handle_,
91 core.READ_MESSAGE_FLAG_NONE);
92 if (this.handle_ == null) // The connector has been closed.
93 return;
94 if (read.result == core.RESULT_SHOULD_WAIT) {
95 this.waitToReadMore_();
96 return;
97 }
98 if (read.result != core.RESULT_OK) {
99 this.error_ = true;
100 if (this.errorHandler_)
101 this.errorHandler_.onError(read.result);
102 return;
103 }
104 var messageBuffer = new buffer.Buffer(read.buffer);
105 var message = new codec.Message(messageBuffer, read.handles);
106 if (this.incomingReceiver_) {
107 this.incomingReceiver_.accept(message);
108 }
109 }
110 };
111
112 // The TestConnector subclass is only intended to be used in unit tests. It
113 // enables delivering a message to the pipe's handle without an async wait.
114
115 function TestConnector(handle) {
116 Connector.call(this, handle);
117 }
118
119 TestConnector.prototype = Object.create(Connector.prototype);
120
121 TestConnector.prototype.waitToReadMore_ = function() {
122 };
123
124 TestConnector.prototype.deliverMessage = function() {
125 this.readMore_(core.RESULT_OK);
126 }
127
128 var exports = {};
129 exports.Connector = Connector;
130 exports.TestConnector = TestConnector;
131 return exports;
132 });
OLDNEW
« no previous file with comments | « mojo/public/js/connection.js ('k') | mojo/public/js/constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698