This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
/
remote.js
116 lines (102 loc) · 4.12 KB
/
remote.js
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
window.remoteTroubleshooting = window.remoteTroubleshooting || {};
(function(window, remoteTroubleshooting) {
'use strict';
var data;
/**
* Async method for retrieving remote-troubleshooting data.
*
* Tries to console.log problems to help diagnose issues.
*
* @param {function} callback - Callback function to call when
* the data arrives. It should take a single argument which
* will be the data packet.
*
* @param {integer} timeout - Timeout in milliseconds for calling
* the callback with {} if the event hasn't yet gotten dispatched.
* Defaults to 1000 ms (aka 1 second).
*
* @returns {object} with data in it or {}
*/
remoteTroubleshooting.getData = function(callback, timeout) {
var timeoutId;
// If we've already gotten the data, return that.
if (data) {
window.setTimeout(function() { callback(data); });
return;
}
// If the browser is missing certain features, then asking for
// remote-troubleshooting data is moot, so just drop out as
// soon as we know important things are missing.
if (!window.addEventListener) {
// console.log('remoteTroubleshooting: browser does not support addEventListener');
data = {};
window.setTimeout(function() { callback(data); });
return;
}
if (!(window.hasOwnProperty('CustomEvent') && typeof window.CustomEvent == 'function')) {
// console.log('remoteTroubleshooting: browser does not support CustomEvent');
data = {};
window.setTimeout(function() { callback(data); });
return;
}
timeout = timeout || 1000;
/**
* If the interval passes and the event listener hasn't kicked off then
* there's nothing listening, so we abort.
*/
function eject() {
// console.log('remoteTroubleshooting: interval ' + timeout + ' has passed.');
data = {};
callback(data);
}
// Listen to the WebChannelMessageToContent event and handle
// incoming remote-troubleshooting messages.
window.addEventListener("WebChannelMessageToContent", function (e) {
// FIXME: handle failure cases
if (e.detail.id === "remote-troubleshooting") {
window.clearTimeout(timeoutId);
data = e.detail.message;
if (data === undefined) {
// console.log('remoteTroubleshooting: data is {}. ' +
// 'permission error? using http instead of https?');
data = {};
}
window.setTimeout(function() { callback(data); });
}
});
// Create the remote-troubleshooting event requesting data and
// kick it off.
var event = new window.CustomEvent("WebChannelMessageToChrome", {
detail: {
id: "remote-troubleshooting",
message: {
command: "request"
}
}
});
window.dispatchEvent(event);
timeoutId = window.setTimeout(eject, timeout);
};
/**
* Returns whether or not the remote-troubleshooting data is
* available.
*
* @param {function} callback - Callback function to call when
* the data arrives. It should take a single argument which
* will be the data packet.
*
* @param {integer} timeout - Timeout in milliseconds for calling
* the callback with {} if the event hasn't yet gotten dispatched.
* Defaults to 1000 ms (aka 1 second).
*
* @returns {bool} true if it's available, false if it isn't
*/
remoteTroubleshooting.available = function(callback, timeout) {
remoteTroubleshooting.getData(function (data) {
// FIXME: This relies on the fact that 'application' is a
// valid key with data in it. If it's not then, this will
// be wrong.
callback(!!data.application);
}, timeout);
};
}(window, window.remoteTroubleshooting));