OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var Event = require('event_bindings').Event; | 5 var Event = require('event_bindings').Event; |
6 var messagingNatives = requireNative('messaging_natives'); | 6 var messagingNatives = requireNative('messaging_natives'); |
7 var utils = require('utils'); | 7 var utils = require('utils'); |
8 | 8 |
9 // Port object. Represents a connection to another script context through | 9 // Port object. Represents a connection to another script context through |
10 // which messages can be passed. | 10 // which messages can be passed. |
11 function Port(portId, opt_name) { | 11 function Port(portId, opt_name) { |
12 this.portId_ = portId; | 12 this.portId_ = portId; |
13 this.name = opt_name; | 13 this.name = opt_name; |
14 | 14 |
15 var portSchema = {name: 'port', $ref: 'runtime.Port'}; | 15 var portSchema = {name: 'port', $ref: 'runtime.Port'}; |
16 var messageSchema = {name: 'message', type: 'any', optional: true}; | 16 var messageSchema = {name: 'message', type: 'any', optional: true}; |
17 var options = {unmanaged: true}; | 17 var options = {unmanaged: true}; |
18 | 18 |
| 19 this.onMessage = new Event(null, [messageSchema, portSchema], options); |
19 this.onDisconnect = new Event(null, [portSchema], options); | 20 this.onDisconnect = new Event(null, [portSchema], options); |
20 this.onMessage = new Event(null, [messageSchema, portSchema], options); | |
21 this.onDestroy_ = null; | 21 this.onDestroy_ = null; |
22 } | 22 } |
23 | 23 |
24 // Sends a message asynchronously to the context on the other end of this | 24 // Sends a message asynchronously to the context on the other end of this |
25 // port. | 25 // port. |
26 Port.prototype.postMessage = function(msg) { | 26 Port.prototype.postMessage = function(msg) { |
27 // JSON.stringify doesn't support a root object which is undefined. | 27 // JSON.stringify doesn't support a root object which is undefined. |
28 if (msg === undefined) | 28 if (msg === undefined) |
29 msg = null; | 29 msg = null; |
30 msg = $JSON.stringify(msg); | 30 msg = $JSON.stringify(msg); |
(...skipping 11 matching lines...) Expand all Loading... |
42 messagingNatives.PostMessage(this.portId_, msg); | 42 messagingNatives.PostMessage(this.portId_, msg); |
43 }; | 43 }; |
44 | 44 |
45 // Disconnects the port from the other end. | 45 // Disconnects the port from the other end. |
46 Port.prototype.disconnect = function() { | 46 Port.prototype.disconnect = function() { |
47 messagingNatives.CloseChannel(this.portId_, true); | 47 messagingNatives.CloseChannel(this.portId_, true); |
48 this.destroy_(); | 48 this.destroy_(); |
49 }; | 49 }; |
50 | 50 |
51 Port.prototype.destroy_ = function() { | 51 Port.prototype.destroy_ = function() { |
52 // Note: it's not necessary to destroy the onDisconnect/onMessage events | |
53 // because they're unmanaged. | |
54 if (this.onDestroy_) | 52 if (this.onDestroy_) |
55 this.onDestroy_(); | 53 this.onDestroy_(); |
| 54 // Destroy the onMessage/onDisconnect events in case the extension added |
| 55 // listeners, but didn't remove them, when the port closed. |
| 56 privates(this.onMessage).impl.destroy_(); |
| 57 privates(this.onDisconnect).impl.destroy_(); |
56 messagingNatives.PortRelease(this.portId_); | 58 messagingNatives.PortRelease(this.portId_); |
57 }; | 59 }; |
58 | 60 |
59 exports.Port = utils.expose('Port', Port, { | 61 exports.Port = utils.expose('Port', Port, { |
60 functions: ['disconnect', 'postMessage'], | 62 functions: ['disconnect', 'postMessage'], |
61 properties: ['name', 'onDisconnect', 'onMessage'] | 63 properties: ['name', 'onMessage', 'onDisconnect'] |
62 }); | 64 }); |
OLD | NEW |