Browse Source

Always use general "send" API and determine internally to encrypt.

With that also get rid of the "Api.send2" and "Api.apply" hacks. Now the
respective methods take an optional function to call for sending the data
as first argument. No more magic object binding, yay!
pull/225/head
Joachim Bauch 11 years ago
parent
commit
dbd3db3393
  1. 30
      static/js/directives/chat.js
  2. 7
      static/js/directives/presentation.js
  3. 7
      static/js/directives/screenshare.js
  4. 7
      static/js/directives/youtubevideo.js
  5. 148
      static/js/mediastream/api.js

30
static/js/directives/chat.js

@ -291,21 +291,10 @@ define(['jquery', 'underscore', 'text!partials/chat.html', 'text!partials/chatro
}); });
} }
_.delay(function() { _.delay(function() {
mediaStream.api.apply("sendChat", { mediaStream.api.sendChat(function(type, data) {
send: function(type, data, origType, origData) { data.Type = type;
// We also send to self, to display our own stuff.
if (!noloop) {
var encrypted = (type !== origType);
mediaStream.api.received({
Type: origData.Type,
Data: origData,
From: mediaStream.api.id,
To: peercall.id
}, encrypted);
}
return peercall.peerconnection.send(data); return peercall.peerconnection.send(data);
} }, to, message, status, mid, true);
})(to, message, status, mid);
}, 100); }, 100);
return mid; return mid;
}; };
@ -316,17 +305,8 @@ define(['jquery', 'underscore', 'text!partials/chat.html', 'text!partials/chatro
}); });
} }
_.delay(function() { _.delay(function() {
mediaStream.api.send2("sendChat", function(type, data, encrypted) { // Tell API to loop back message internally.
if (!noloop) { mediaStream.api.sendChat(to, message, status, mid, true);
//console.log("looped to self", type, data);
mediaStream.api.received({
Type: data.Type,
Data: data,
From: mediaStream.api.id,
To: to
}, encrypted);
}
})(to, message, status, mid);
}, 100); }, 100);
return mid; return mid;
}; };

7
static/js/directives/presentation.js

@ -415,8 +415,8 @@ define(['jquery', 'underscore', 'text!partials/presentation.html', 'bigscreen'],
var tokenHandler = null; var tokenHandler = null;
var mediaStreamSendPresentation = function(peercall, token, params) { var mediaStreamSendPresentation = function(peercall, token, params) {
mediaStream.api.apply("sendPresentation", { mediaStream.api.sendPresentation(function(type, data) {
send: function(type, data) { data.Type = type;
if (!peercall.peerconnection.datachannelReady) { if (!peercall.peerconnection.datachannelReady) {
return peercall.e.one("dataReady", function() { return peercall.e.one("dataReady", function() {
peercall.peerconnection.send(data); peercall.peerconnection.send(data);
@ -424,8 +424,7 @@ define(['jquery', 'underscore', 'text!partials/presentation.html', 'bigscreen'],
} else { } else {
return peercall.peerconnection.send(data); return peercall.peerconnection.send(data);
} }
} }, peercall.id, token, params);
})(peercall.id, token, params);
}; };
var connector = function(token, peercall) { var connector = function(token, peercall) {

7
static/js/directives/screenshare.js

@ -205,12 +205,11 @@ define(['jquery', 'underscore', 'text!partials/screenshare.html', 'text!partials
delete peers[currentcall.id]; delete peers[currentcall.id];
console.log("Removed closed call from screen sharing.", currentcall.id); console.log("Removed closed call from screen sharing.", currentcall.id);
}); });
mediaStream.api.apply("sendScreenshare", { mediaStream.api.sendScreenshare(function(type, data) {
send: function(type, data) { data.Type = type;
//console.log("sent screenshare", data, peercall); //console.log("sent screenshare", data, peercall);
return peercall.peerconnection.send(data); return peercall.peerconnection.send(data);
} }, peercall.from, token);
})(peercall.from, token);
}; };
usermedia.e.one("mediasuccess", function(event, usermedia) { usermedia.e.one("mediasuccess", function(event, usermedia) {

7
static/js/directives/youtubevideo.js

@ -482,8 +482,8 @@ define(['require', 'jquery', 'underscore', 'moment', 'text!partials/youtubevideo
var tokenHandler = null; var tokenHandler = null;
var mediaStreamSendYouTubeVideo = function(peercall, token, params) { var mediaStreamSendYouTubeVideo = function(peercall, token, params) {
mediaStream.api.apply("sendYouTubeVideo", { mediaStream.api.sendYouTubeVideo(function(type, data) {
send: function(type, data) { data.Type = type;
if (!peercall.peerconnection.datachannelReady) { if (!peercall.peerconnection.datachannelReady) {
return peercall.e.one("dataReady", function() { return peercall.e.one("dataReady", function() {
peercall.peerconnection.send(data); peercall.peerconnection.send(data);
@ -491,8 +491,7 @@ define(['require', 'jquery', 'underscore', 'moment', 'text!partials/youtubevideo
} else { } else {
return peercall.peerconnection.send(data); return peercall.peerconnection.send(data);
} }
} }, peercall.id, token, params);
})(peercall.id, token, params);
}; };
var connector = function(token, peercall) { var connector = function(token, peercall) {

148
static/js/mediastream/api.js

@ -22,6 +22,21 @@
"use strict"; "use strict";
define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
// "Decorate" the passed method to make sure the first argument is a
// function to use for sending data. Use the default send function if
// no function is passed.
var addDefaultSendFunc = function(f) {
var func = function(sendFunc) {
if (!_.isFunction(sendFunc)) {
var args = Array.prototype.slice.call(arguments);
args.unshift(_.bind(this.defaultSendFunc, this));
return func.apply(this, args);
}
return f.apply(this, arguments);
};
return func;
};
var Api = function(version, connector, endToEndEncryption) { var Api = function(version, connector, endToEndEncryption) {
this.e = $({}); this.e = $({});
this.version = version; this.version = version;
@ -81,48 +96,64 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
}; };
Api.prototype.send = function(type, data, noqueue) { Api.prototype.defaultSendFunc = function(type, data, noqueue) {
var payload = { var payload = {
Type: type Type: type
}; };
payload[type] = data; payload[type] = data;
//console.log("<<<<<<<<<<<<", JSON.stringify(payload, null, 2)); //console.log("<<<<<<<<<<<<", JSON.stringify(payload, null, 2));
this.connector.send(payload, noqueue); this.connector.send(payload, noqueue);
}; };
Api.prototype.send2 = function(name, cb) { Api.prototype.supportsEncryption = function(peer, type) {
var obj = { // Broadcast or server messages are never encrypted.
send: _.bind(function(type, data) { if (!peer) {
if (cb) { return false;
cb(type, data);
} }
this.send(type, data);
}, this), // Need encryption support in the current browser environment.
sendEncrypted: _.bind(function(type, data) { if (!this.endToEndEncryption) {
if (cb) { return false;
var to = data.To;
var encrypted = (to && this.endToEndEncryption);
cb(type, data, encrypted);
} }
this.sendEncrypted(type, data);
}, this) // Messages to setup encryption are never encrypted.
if (type === "EncryptionRegister" ||
type === "EncryptionKeyBundle" ||
type === "EncryptionRequestKeyBundle") {
return false;
} }
return this.apply(name, obj);
// TODO(fancycode): Check if remote peer supports encryption.
return true;
}; };
Api.prototype.sendEncrypted = function(type, data, noqueue) { Api.prototype.loopSelf = function(type, data, encrypted) {
this.received({
Type: data.Type,
Data: data,
From: this.id,
To: data.To
}, encrypted);
};
Api.prototype.send = addDefaultSendFunc(function(sendFunc, type, data, noqueue, loopSelf) {
var to = data.To; var to = data.To;
if (!to || !this.endToEndEncryption) { if (this.supportsEncryption(to, type)) {
return this.send(type, data, noqueue); this.endToEndEncryption.encrypt(to, type, data, _.bind(function(encryptedType, encrypted) {
if (loopSelf) {
this.loopSelf(type, data, true);
} }
encrypted.To = to;
this.endToEndEncryption.encrypt(to, type, data, _.bind(function(type, encrypted) { sendFunc(encryptedType, encrypted, noqueue);
encrypted.To = to
this.send(type, encrypted, noqueue);
}, this)); }, this));
}; return;
}
if (loopSelf) {
this.loopSelf(type, data, false);
}
sendFunc(type, data, noqueue);
});
Api.prototype.request = function(type, data, cb, noqueue) { Api.prototype.request = function(type, data, cb, noqueue) {
@ -139,29 +170,6 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
} }
// Helper hack function to send API requests to other destinations.
// Simply provide an alternative send function on the obj Object. The
// alternative send function will get the type and data to send, together
// with the original type and data (which are potentially unencrypted).
Api.prototype.apply = function(name, obj) {
var f = this[name];
if (!obj.hasOwnProperty("sendEncrypted")) {
obj.sendEncrypted = _.bind(function(type, data) {
var to = data.To;
if (!to || !this.endToEndEncryption) {
return obj.send(type, data, type, data);
}
this.endToEndEncryption.encrypt(to, type, data, _.bind(function(encryptedType, encrypted) {
encrypted.To = to
encrypted.Type = encryptedType
obj.send(encryptedType, encrypted, type, data);
}, this));
}, this);
}
return _.bind(f, obj);
};
Api.prototype.received = function(d, encrypted) { Api.prototype.received = function(d, encrypted) {
// Store received timestamp. // Store received timestamp.
@ -331,7 +339,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
Offer: payload Offer: payload
} }
return this.sendEncrypted("Offer", data); return this.send("Offer", data);
}; };
@ -343,7 +351,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
Candidate: payload Candidate: payload
} }
return this.sendEncrypted("Candidate", data); return this.send("Candidate", data);
} }
@ -355,7 +363,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
Answer: payload Answer: payload
} }
return this.sendEncrypted("Answer", data); return this.send("Answer", data);
} }
@ -420,11 +428,15 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
} }
} }
return this.sendEncrypted("Bye", data); return this.send("Bye", data);
}; };
Api.prototype.sendChat = function(to, message, status, mid) { Api.prototype.sendChat = addDefaultSendFunc(function(sendFunc, to, message, status, mid, loopSelf) {
if (!loopSelf && this.supportsEncryption(to, "Chat")) {
// We can't let the server loop back the encrypted message.
loopSelf = true;
}
var data = { var data = {
To: to, To: to,
@ -433,13 +445,12 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
Mid: mid, Mid: mid,
Message: message, Message: message,
Status: status, Status: status,
NoEcho: true // This client shows own messages internally. NoEcho: !!loopSelf
} }
} }
return this.sendEncrypted("Chat", data); return this.send(sendFunc, "Chat", data, false, loopSelf);
});
};
Api.prototype.sendConference = function(id, ids) { Api.prototype.sendConference = function(id, ids) {
@ -453,8 +464,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
}; };
Api.prototype.sendScreenshare = function(id, screen_id) { Api.prototype.sendScreenshare = addDefaultSendFunc(function(sendFunc, id, screen_id) {
var data = { var data = {
Id: id, Id: id,
Type: "Screenshare", Type: "Screenshare",
@ -463,12 +473,11 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
} }
} }
return this.send("Screenshare", data); return this.send(sendFunc, "Screenshare", data);
}; });
Api.prototype.sendPresentation = function(id, viewer_id, viewer_data) {
Api.prototype.sendPresentation = addDefaultSendFunc(function(sendFunc, id, viewer_id, viewer_data) {
var data = { var data = {
Id: id, Id: id,
Type: "Presentation", Type: "Presentation",
@ -480,12 +489,11 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
data.Presentation = _.extend(data.Presentation, viewer_data); data.Presentation = _.extend(data.Presentation, viewer_data);
} }
return this.send("Presentation", data); return this.send(sendFunc, "Presentation", data);
};
Api.prototype.sendYouTubeVideo = function(id, video_id, video_data) { });
Api.prototype.sendYouTubeVideo = addDefaultSendFunc(function(sendFunc, id, video_id, video_data) {
var data = { var data = {
Id: id, Id: id,
Type: "YouTubeVideo", Type: "YouTubeVideo",
@ -497,9 +505,9 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
data.YouTubeVideo = _.extend(data.YouTubeVideo, video_data); data.YouTubeVideo = _.extend(data.YouTubeVideo, video_data);
} }
return this.send("YouTubeVideo", data); return this.send(sendFunc, "YouTubeVideo", data);
}; });
Api.prototype.sendAlive = function(timestamp) { Api.prototype.sendAlive = function(timestamp) {

Loading…
Cancel
Save