This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
call_screen_manager.js
276 lines (250 loc) · 8.58 KB
/
call_screen_manager.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* exported CallScreenManager */
'use strict';
(function(exports) {
var debug = true;
// Global flag to indicate that we are in a call and so we will be rejecting
// any incoming call with the 'busy' state and forbiding any outgoing call
// until we support multi-party calls.
var _inCall = false;
function _callback(cb, args) {
if (cb && typeof cb === 'function') {
cb.apply(null, args);
}
}
function _onAttentionLoaded(attention, callback) {
if (typeof callback !== 'function') {
console.error('Error when waiting for attention onload');
return;
}
// Flag for handling both methods
var isAttentionLoaded = false;
// Method to use when available. Currently is not working
// so tracked in bug XXX
function _onloaded() {
// Update flag
isAttentionLoaded = true;
_inCall = true;
// Execute CB
callback();
}
attention.onload = function() {
_onloaded();
};
attention.onunload = function() {
_inCall = false;
};
// Workaround while the bug is being fixed
window.addEventListener(
'message',
function onPingMessageListener(event) {
try {
var pongObjectCandidate = JSON.parse(event.data);
if (pongObjectCandidate.message === 'pong') {
attention.onload = null;
window.removeEventListener('message', onPingMessageListener);
_onloaded();
}
} catch(e) {
console.error('Message from iFrame not related with Loop ' + e);
}
}
);
// Function to fake the 'onload' method which is not working
var pingMessage = {
id: 'controller',
message: 'ping'
};
function _fakeLoaded() {
if (isAttentionLoaded) {
return;
}
// Send to the attention screen
attention.postMessage(JSON.stringify(pingMessage), '*');
// Enable polling
setTimeout(function() {
_fakeLoaded();
}, 20);
}
// Boot the fake loader
_fakeLoaded();
}
var attention;
function _launchAttention(type, params, incomingCall, contact) {
// Retrieve the params and pass them as part of the URL
var attentionParams = 'layout=' + type;
if (params) {
Object.keys(params).forEach(function(key) {
attentionParams += '&' + key + '=' + encodeURIComponent(params[key]);
});
}
// Launch the Attention
var host = document.location.host;
var protocol = document.location.protocol;
var urlBase = protocol + '//' + host +
'/call_screen/call.html?' + attentionParams;
attention = window.open(urlBase, 'call_screen', 'attention');
// Enable handshaking with the Call Screen
_onAttentionLoaded(
attention,
function onLoaded() {
// Function to post data from the server
function _postCall(type, call, identities, video) {
attention.postMessage(JSON.stringify({
id: 'controller',
message: 'call',
params: {
type: type,
call: call,
identities: identities,
video: video || null
}
}), '*');
}
// Now it's time to send to the attention the info regarding the
// call object
switch(type) {
case 'incoming':
// Call was retrieved previously in order to accelerate the UI
_postCall(type, incomingCall, params.identities);
break;
case 'outgoing':
if (!params.token) {
CallHelper.callUser(
params.identities,
params.video,
function onLoopIdentity(call) {
_postCall(type, call, params.identities, params.video);
},
function onFallback() {
// Get URL to share and show prompt
CallHelper.generateCallUrl(params.identities[0],
function onCallUrlSuccess(result) {
Share.show(result, params.identities, function onShareShown() {
attention.close();
});
},
function(e) {
console.error('Unable to retrieve link to share ' + e);
attention.close();
}
);
}
);
} else {
CallHelper.callUrl(
params.token,
params.video,
function(call, calleeFriendlyName) {
params.identities = [calleeFriendlyName];
_postCall(type, call, params.identities, params.video);
},
function() {
console.error('Unable to connect');
// Close attention
attention.close();
}
);
}
break;
}
var attentionLoadedDate = new Date();
window.addEventListener(
'message',
function onHandShakingEvent(event) {
try {
var messageFromCallScreen = JSON.parse(event.data);
if (messageFromCallScreen.id != 'call_screen') {
debug && console.log('CallScreen: PostMessage not from CallScreen');
return;
}
switch(messageFromCallScreen.message) {
case 'hangout':
// Stop listener
window.removeEventListener('message', onHandShakingEvent);
// Clean attention params
attention.close();
attention = null;
// Create CALL object
var callscreenParams = messageFromCallScreen.params;
// Create object to store
var callObject = {
date: attentionLoadedDate,
identities: params.identities || [],
video: params.video || null,
type: type,
connected: callscreenParams.connected,
duration: callscreenParams.duration,
url: callscreenParams.call.callUrl || null,
urlToken: callscreenParams.call.callToken || null,
contactId: null,
contactPrimaryInfo: null,
contactPhoto: null
};
ContactsHelper.find({
identities: params.identities
}, function(result) {
CallLog.addCall(callObject, result);
}, function() {
CallLog.addCall(callObject);
});
break;
}
} catch(e) {}
}
);
}
);
}
function _rejectCall(call) {
LazyLoader.load([
'../js/helpers/call_progress_helper.js'
], function() {
var callProgressHelper = new CallProgressHelper(call.callId,
call.progressURL,
call.websocketToken);
callProgressHelper.terminate('busy');
});
}
var CallScreenManager = {
launch: function(type, params) {
if (type !== 'incoming') {
// If we are already on a call, we shouldn't allow calling another user
// until we have a proper multi-party feature in place.
// Bug 1058628 - Do not allow make a call while there is another one in
// place
if (_inCall) {
return;
}
_launchAttention(type, params);
return;
}
ClientRequestHelper.getCalls(
params.version,
function onsuccess(callsArray) {
var call = callsArray.calls[0];
// If we are already in a call (outgoing or incoming) we can't
// automatically attend the new incoming call without hanging up the
// on in progress. In the future we might want to allow the user to
// accept or reject it manually, but for now we simply reject it
// with the busy state without launching the attention screen.
if (_inCall) {
_rejectCall(call);
return;
}
params.identities = [call.callerId];
_launchAttention(type, params, call);
},
function onerror(e) {
debug && console.error('Error: ClientRequestHelper.getCalls ' + e);
}
);
},
close: function() {
if (attention) {
attention.close();
attention = null;
}
}
};
exports.CallScreenManager = CallScreenManager;
}(this));