Index: extensions/renderer/resources/port.js |
diff --git a/extensions/renderer/resources/port.js b/extensions/renderer/resources/port.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a3bb7af42c764338c36ff57aca25818306e5458b |
--- /dev/null |
+++ b/extensions/renderer/resources/port.js |
@@ -0,0 +1,62 @@ |
+// Copyright 2015 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. |
+ |
+var Event = require('event_bindings').Event; |
+var messagingNatives = requireNative('messaging_natives'); |
+var utils = require('utils'); |
+ |
+// Port object. Represents a connection to another script context through |
+// which messages can be passed. |
+function Port(portId, opt_name) { |
+ this.portId_ = portId; |
+ this.name = opt_name; |
+ |
+ var portSchema = {name: 'port', $ref: 'runtime.Port'}; |
+ var messageSchema = {name: 'message', type: 'any', optional: true}; |
+ var options = {unmanaged: true}; |
+ |
+ this.onDisconnect = new Event(null, [portSchema], options); |
+ this.onMessage = new Event(null, [messageSchema, portSchema], options); |
+ this.onDestroy_ = null; |
+} |
+ |
+// Sends a message asynchronously to the context on the other end of this |
+// port. |
+Port.prototype.postMessage = function(msg) { |
+ // JSON.stringify doesn't support a root object which is undefined. |
+ if (msg === undefined) |
+ msg = null; |
+ msg = $JSON.stringify(msg); |
+ if (msg === undefined) { |
+ // JSON.stringify can fail with unserializable objects. Log an error and |
+ // drop the message. |
+ // |
+ // TODO(kalman/mpcomplete): it would be better to do the same validation |
+ // here that we do for runtime.sendMessage (and variants), i.e. throw an |
+ // schema validation Error, but just maintain the old behaviour until |
+ // there's a good reason not to (http://crbug.com/263077). |
+ console.error('Illegal argument to Port.postMessage'); |
+ return; |
+ } |
+ messagingNatives.PostMessage(this.portId_, msg); |
+}; |
+ |
+// Disconnects the port from the other end. |
+Port.prototype.disconnect = function() { |
+ messagingNatives.CloseChannel(this.portId_, true); |
+ this.destroy_(); |
+}; |
+ |
+Port.prototype.destroy_ = function() { |
+ // Note: it's not necessary to destroy the onDisconnect/onMessage events |
+ // because they're unmanaged. |
+ if (this.onDestroy_) |
+ this.onDestroy_(); |
+ messagingNatives.PortRelease(this.portId_); |
+}; |
+ |
+exports.Port = utils.expose('Port', Port, { |
+ functions: ['disconnect', 'postMessage'], |
+ properties: ['name', 'onDisconnect', 'onMessage'] |
+}); |