This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathipc.js
More file actions
126 lines (112 loc) · 3.86 KB
/
Copy pathipc.js
File metadata and controls
126 lines (112 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Copyright 2012 Google, Inc. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/**
* @fileoverview Browser simple IPC.
* There should only be one channel open at a time.
*
* @author [email protected] (Ben Vanik)
*/
goog.provide('wtf.ipc');
goog.require('goog.asserts');
goog.require('goog.events.EventType');
goog.require('wtf.ipc.MessageChannel');
goog.require('wtf.timing');
goog.require('wtf.trace.util');
goog.require('wtf.util');
/**
* Connects to the window that opened this window, if any.
* When running inside a Chrome extension this will connect to the background
* page.
* @param {!function(this: T, wtf.ipc.Channel)} callback Connect callback.
* @param {T=} opt_scope Scope for the callback.
* @template T
*/
wtf.ipc.connectToParentWindow = function(callback, opt_scope) {
function connectTo(sendPort) {
var channel = null;
if (sendPort) {
channel = new wtf.ipc.MessageChannel(window, sendPort);
channel.postMessage({
'hello': true
});
}
callback.call(opt_scope, channel);
}
var chrome = goog.global['chrome'];
if (chrome && chrome['runtime'] &&
chrome['runtime']['getBackgroundPage']) {
chrome['runtime']['getBackgroundPage'](function(backgroundPage) {
connectTo(backgroundPage);
});
} else {
// Connect to our opener (asynchronously for consistency with the background
// page case). Opener my be undefined.
wtf.timing.setImmediate(function() {
connectTo(window.opener);
});
}
};
/**
* Waits for a single child window to connect.
* @param {!function(this: T, !wtf.ipc.Channel)} callback Connect callback.
* @param {T=} opt_scope Scope for the callback.
* @template T
*/
wtf.ipc.waitForChildWindow = function(callback, opt_scope) {
var boundHandler = wtf.trace.util.ignoreListener(function(e) {
if (e.data && e.data[wtf.ipc.MessageChannel.PACKET_TOKEN] &&
e.data.data && e.data.data['hello'] == true) {
e.stopPropagation();
window.removeEventListener(
goog.events.EventType.MESSAGE, boundHandler, true);
goog.asserts.assert(e.source);
var channel = new wtf.ipc.MessageChannel(window, e.source);
callback.call(opt_scope, channel);
}
});
window.addEventListener(
goog.events.EventType.MESSAGE, boundHandler, true);
};
/**
* Waits for a child window to connect.
* This hooks the global message handler and sniffs for packets.
* @param {!function(this: T, !wtf.ipc.Channel)} callback Connect callback.
* @param {T=} opt_scope Scope for the callback.
* @template T
*/
wtf.ipc.listenForChildWindows = function(callback, opt_scope) {
var boundHandler = wtf.trace.util.ignoreListener(function(e) {
if (e.data && e.data[wtf.ipc.MessageChannel.PACKET_TOKEN] &&
e.data.data && e.data.data['hello'] == true) {
e.stopPropagation();
window.removeEventListener(
goog.events.EventType.MESSAGE, boundHandler, true);
goog.asserts.assert(e.source);
var channel = new wtf.ipc.MessageChannel(window, e.source);
callback.call(opt_scope, channel);
}
});
window.addEventListener(
goog.events.EventType.MESSAGE, boundHandler, true);
};
/**
* Gets a MessageChannel for the given window, creating it if needed.
* @param {Window=} opt_window The window to monitor; defaults to the window in
* which this code is executing.
* @return {!wtf.ipc.MessageChannel} Message channel.
*/
wtf.ipc.getWindowMessageChannel = function(opt_window) {
var targetWindow = opt_window || window;
var stash = wtf.util.getGlobalCacheObject('ipc', targetWindow);
if (stash.messageChannel) {
return /** @type {!wtf.ipc.MessageChannel} */ (stash.messageChannel);
} else {
var channel = new wtf.ipc.MessageChannel(targetWindow, targetWindow);
stash.messageChannel = channel;
return channel;
}
};