summaryrefslogtreecommitdiffstats
path: root/mojo/public/bindings/js/connector.js
diff options
context:
space:
mode:
Diffstat (limited to 'mojo/public/bindings/js/connector.js')
-rw-r--r--mojo/public/bindings/js/connector.js98
1 files changed, 98 insertions, 0 deletions
diff --git a/mojo/public/bindings/js/connector.js b/mojo/public/bindings/js/connector.js
new file mode 100644
index 0000000..c9d0566
--- /dev/null
+++ b/mojo/public/bindings/js/connector.js
@@ -0,0 +1,98 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+define([
+ "mojo/public/bindings/js/codec",
+ "mojo/public/bindings/js/core",
+ "mojo/public/bindings/js/support",
+], function(codec, core, support) {
+
+ function Connector(handle) {
+ this.handle_ = handle;
+ this.error_ = false;
+ this.incomingReceiver_ = null;
+ this.readWaitCookie_ = null;
+ }
+
+ Connector.prototype.close = function() {
+ if (this.readWaitCookie_) {
+ support.cancelWait(this.readWaitCookie_);
+ this.readWaitCookie_ = null;
+ }
+ if (this.handle_ != core.kInvalidHandle) {
+ core.close(this.handle_);
+ this.handle_ = core.kInvalidHandle;
+ }
+ };
+
+ Connector.prototype.accept = function(message) {
+ if (this.error_)
+ return false;
+ this.write_(message);
+ return !this.error_;
+ };
+
+ Connector.prototype.setIncomingReceiver = function(receiver) {
+ this.incomingReceiver_ = receiver;
+ if (this.incomingReceiver_)
+ this.waitToReadMore_();
+ };
+
+ Connector.prototype.write_ = function(message) {
+ var result = core.writeMessage(this.handle_,
+ message.memory,
+ message.handles,
+ core.WRITE_MESSAGE_FLAG_NONE);
+ if (result != core.RESULT_OK) {
+ this.error_ = true
+ return;
+ }
+ // The handles were successfully transferred, so we don't own them anymore.
+ message.handles = [];
+ };
+
+ Connector.prototype.waitToReadMore_ = function() {
+ this.readWaitCookie_ = support.asyncWait(this.handle_,
+ core.WAIT_FLAG_READABLE,
+ this.readMore_.bind(this));
+ };
+
+ Connector.prototype.readMore_ = function(result) {
+ for (;;) {
+ var read = core.readMessage(this.handle_,
+ core.READ_MESSAGE_FLAG_NONE);
+ if (read.result == core.RESULT_NOT_FOUND) {
+ this.waitToReadMore_();
+ return;
+ }
+ if (read.result != core.RESULT_OK) {
+ this.error_ = true;
+ return;
+ }
+ // TODO(abarth): Should core.readMessage return a Uint8Array?
+ var memory = new Uint8Array(read.buffer);
+ var message = new codec.Message(memory, read.handles);
+ this.incomingReceiver_.accept(message);
+ }
+ };
+
+ function Connection(handle, localFactory, remoteFactory) {
+ this.connector_ = new Connector(handle);
+ this.local = new localFactory();
+ this.remote = new remoteFactory(this.connector_);
+
+ this.connector_.setIncomingReceiver(this.local);
+ }
+
+ Connection.prototype.close = function() {
+ this.connector_.close();
+ this.connector_ = null;
+ this.local = null;
+ this.remote = null;
+ };
+
+ var exports = {};
+ exports.Connection = Connection;
+ return exports;
+});