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 @@ -291,21 +291,10 @@ define(['jquery', 'underscore', 'text!partials/chat.html', 'text!partials/chatro
});
}
_.delay(function() {
mediaStream.api.apply("sendChat", {
send: function(type, data, origType, origData) {
// 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);
}
mediaStream.api.sendChat(function(type, data) {
data.Type = type;
return peercall.peerconnection.send(data);
}
})(to, message, status, mid);
}, to, message, status, mid, true);
}, 100);
return mid;
};
@ -316,17 +305,8 @@ define(['jquery', 'underscore', 'text!partials/chat.html', 'text!partials/chatro @@ -316,17 +305,8 @@ define(['jquery', 'underscore', 'text!partials/chat.html', 'text!partials/chatro
});
}
_.delay(function() {
mediaStream.api.send2("sendChat", function(type, data, encrypted) {
if (!noloop) {
//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);
// Tell API to loop back message internally.
mediaStream.api.sendChat(to, message, status, mid, true);
}, 100);
return mid;
};

7
static/js/directives/presentation.js

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

7
static/js/directives/screenshare.js

@ -205,12 +205,11 @@ define(['jquery', 'underscore', 'text!partials/screenshare.html', 'text!partials @@ -205,12 +205,11 @@ define(['jquery', 'underscore', 'text!partials/screenshare.html', 'text!partials
delete peers[currentcall.id];
console.log("Removed closed call from screen sharing.", currentcall.id);
});
mediaStream.api.apply("sendScreenshare", {
send: function(type, data) {
mediaStream.api.sendScreenshare(function(type, data) {
data.Type = type;
//console.log("sent screenshare", data, peercall);
return peercall.peerconnection.send(data);
}
})(peercall.from, token);
}, peercall.from, token);
};
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 @@ -482,8 +482,8 @@ define(['require', 'jquery', 'underscore', 'moment', 'text!partials/youtubevideo
var tokenHandler = null;
var mediaStreamSendYouTubeVideo = function(peercall, token, params) {
mediaStream.api.apply("sendYouTubeVideo", {
send: function(type, data) {
mediaStream.api.sendYouTubeVideo(function(type, data) {
data.Type = type;
if (!peercall.peerconnection.datachannelReady) {
return peercall.e.one("dataReady", function() {
peercall.peerconnection.send(data);
@ -491,8 +491,7 @@ define(['require', 'jquery', 'underscore', 'moment', 'text!partials/youtubevideo @@ -491,8 +491,7 @@ define(['require', 'jquery', 'underscore', 'moment', 'text!partials/youtubevideo
} else {
return peercall.peerconnection.send(data);
}
}
})(peercall.id, token, params);
}, peercall.id, token, params);
};
var connector = function(token, peercall) {

148
static/js/mediastream/api.js

@ -22,6 +22,21 @@ @@ -22,6 +22,21 @@
"use strict";
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) {
this.e = $({});
this.version = version;
@ -81,48 +96,64 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -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 = {
Type: type
};
payload[type] = data;
//console.log("<<<<<<<<<<<<", JSON.stringify(payload, null, 2));
this.connector.send(payload, noqueue);
};
Api.prototype.send2 = function(name, cb) {
var obj = {
send: _.bind(function(type, data) {
if (cb) {
cb(type, data);
Api.prototype.supportsEncryption = function(peer, type) {
// Broadcast or server messages are never encrypted.
if (!peer) {
return false;
}
this.send(type, data);
}, this),
sendEncrypted: _.bind(function(type, data) {
if (cb) {
var to = data.To;
var encrypted = (to && this.endToEndEncryption);
cb(type, data, encrypted);
// Need encryption support in the current browser environment.
if (!this.endToEndEncryption) {
return false;
}
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;
if (!to || !this.endToEndEncryption) {
return this.send(type, data, noqueue);
if (this.supportsEncryption(to, type)) {
this.endToEndEncryption.encrypt(to, type, data, _.bind(function(encryptedType, encrypted) {
if (loopSelf) {
this.loopSelf(type, data, true);
}
this.endToEndEncryption.encrypt(to, type, data, _.bind(function(type, encrypted) {
encrypted.To = to
this.send(type, encrypted, noqueue);
encrypted.To = to;
sendFunc(encryptedType, encrypted, noqueue);
}, this));
};
return;
}
if (loopSelf) {
this.loopSelf(type, data, false);
}
sendFunc(type, data, noqueue);
});
Api.prototype.request = function(type, data, cb, noqueue) {
@ -139,29 +170,6 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -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) {
// Store received timestamp.
@ -331,7 +339,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -331,7 +339,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
Offer: payload
}
return this.sendEncrypted("Offer", data);
return this.send("Offer", data);
};
@ -343,7 +351,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -343,7 +351,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
Candidate: payload
}
return this.sendEncrypted("Candidate", data);
return this.send("Candidate", data);
}
@ -355,7 +363,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -355,7 +363,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
Answer: payload
}
return this.sendEncrypted("Answer", data);
return this.send("Answer", data);
}
@ -420,11 +428,15 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -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 = {
To: to,
@ -433,13 +445,12 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -433,13 +445,12 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
Mid: mid,
Message: message,
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) {
@ -453,8 +464,7 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -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 = {
Id: id,
Type: "Screenshare",
@ -463,12 +473,11 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -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 = {
Id: id,
Type: "Presentation",
@ -480,12 +489,11 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -480,12 +489,11 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
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 = {
Id: id,
Type: "YouTubeVideo",
@ -497,9 +505,9 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { @@ -497,9 +505,9 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) {
data.YouTubeVideo = _.extend(data.YouTubeVideo, video_data);
}
return this.send("YouTubeVideo", data);
return this.send(sendFunc, "YouTubeVideo", data);
};
});
Api.prototype.sendAlive = function(timestamp) {

Loading…
Cancel
Save