This commit is contained in:
Matt Corallo 2014-01-12 04:07:13 -10:00
parent 7e811c2855
commit f4755cb4b1
6 changed files with 90 additions and 17 deletions

View file

@ -1,8 +1,11 @@
function check_first_install() { if (localStorage.getItem('first_install_ran')) {
if (localStorage.getItem('first_install_ran'))
return;
localStorage.setItem('first_install_ran', 1); localStorage.setItem('first_install_ran', 1);
chrome.tabs.create({url: "options.html"}); chrome.tabs.create({url: "options.html"});
} else {
if (isRegistrationDone()) {
subscribeToPush(function(message) {
console.log("Got message from " + message.source + ": \"" + getString(message.message));
storeMessage(message);
});
}
} }
check_first_install();

View file

@ -37,9 +37,13 @@ function base64DecToArr (sBase64, nBlocksSize) {
/********************************* /*********************************
*** Type conversion utilities *** *** Type conversion utilities ***
*********************************/ *********************************/
var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
var StaticUint8ArrayProto = new Uint8Array().__proto__;
function getString(thing) { function getString(thing) {
if (thing == "[object Uint8Array]") if (thing.__proto__ == StaticUint8ArrayProto)
return String.fromCharCode.apply(null, thing); return String.fromCharCode.apply(null, thing);
if (thing != undefined && thing.__proto__ == StaticByteBufferProto)
return thing.toString("utf8");
return thing; return thing;
} }
@ -85,6 +89,8 @@ var storage = {};
storage.putEncrypted = function(key, value) { storage.putEncrypted = function(key, value) {
//TODO //TODO
if (value === undefined)
throw "Tried to store undefined";
localStorage.setItem("e" + key, JSON.stringify(getString(value))); localStorage.setItem("e" + key, JSON.stringify(getString(value)));
} }
@ -97,6 +103,8 @@ storage.getEncrypted = function(key, defaultValue) {
} }
storage.putUnencrypted = function(key, value) { storage.putUnencrypted = function(key, value) {
if (value === undefined)
throw "Tried to store undefined";
localStorage.setItem("u" + key, JSON.stringify(getString(value))); localStorage.setItem("u" + key, JSON.stringify(getString(value)));
} }
@ -107,6 +115,34 @@ storage.getUnencrypted = function(key, defaultValue) {
return JSON.parse(value); return JSON.parse(value);
} }
function registrationDone() {
storage.putUnencrypted("registration_done", "");
}
function isRegistrationDone() {
return storage.getUnencrypted("registration_done") !== undefined;
}
function getMessageMap() {
return storage.getEncrypted("messageMap", {});
}
function storeMessage(outgoingMessageSignal) {
var messageMap = getMessageMap();
var conversation = messageMap[outgoingMessageSignal.source]; //TODO: Also support Group message IDs here
if (conversation === undefined) {
conversation = []
messageMap[outgoingMessageSignal.source] = conversation;
}
conversation[conversation.length] = { message: getString(outgoingMessageSignal.message),
destinations: outgoingMessageSignal.destinations,
sender: outgoingMessageSignal.source,
timestamp: outgoingMessageSignal.timestamp.div(dcodeIO.Long.fromNumber(1000)).toNumber() };
storage.putEncrypted("messageMap", messageMap);
chrome.runtime.sendMessage(conversation[conversation.length - 1]);
}
/******************************************* /*******************************************
*** Utilities to manage keys/randomness *** *** Utilities to manage keys/randomness ***
*******************************************/ *******************************************/

View file

@ -16,7 +16,7 @@
}, },
"background": { "background": {
"scripts": ["background.js"] "pages": ["background.html"]
}, },
"options_page": "options.html" "options_page": "options.html"

View file

@ -52,7 +52,10 @@ $('#init-go').click(function() {
$('#verify3done').html('done'); $('#verify3done').html('done');
doAjax({call: 'keys', httpType: 'PUT', do_auth: true, jsonData: keys, doAjax({call: 'keys', httpType: 'PUT', do_auth: true, jsonData: keys,
success_callback: function(response) { success_callback: function(response) {
$('#verify4done').html('done'); $('#complete-number').html(number);
$('#verify').hide();
$('#setup-complete').show();
registrationDone();
}, error_callback: function(code) { }, error_callback: function(code) {
alert(code); //TODO alert(code); //TODO
} }
@ -81,7 +84,7 @@ $('#init-go').click(function() {
} }
}); });
if (storage.getUnencrypted("number_id") === undefined) { if (!isRegistrationDone()) {
$('#init-setup').show(); $('#init-setup').show();
} else { } else {
$('#complete-number').html(storage.getUnencrypted("number_id").split(".")[0]); $('#complete-number').html(storage.getUnencrypted("number_id").split(".")[0]);

View file

@ -1,12 +1,12 @@
<html> <html>
<head> <head>
<script type="text/javascript" src="jquery-2.0.3.min.js"></script>
</head> </head>
<body> <body>
<a id="inbox_link">Inbox</a> <a id="inbox_link">Inbox</a>
<a id="send_link">Send</a> <a id="send_link">Send</a>
<div id="inbox"> <div id="inbox">
<!-- stuff --> <ul id="messages">
</ul>
</div> </div>
<div id="send" style="display:none;"></div> <div id="send" style="display:none;"></div>

View file

@ -1,14 +1,45 @@
$('#inbox_link').onclick = function() { $('#inbox_link').onclick = function() {
$('#inbox').style('display: none;'); $('#send').style('display: block;'); $('#inbox').show();
$('#send').hide();
} }
$('#send_link').onclick = function() { $('#send_link').onclick = function() {
$('#send').style('display: block;'); $('#send').style('display: none;'); $('#inbox').hide();
$('#send').show();
} }
if (storage.getUnencrypted("number_id") === undefined) if (storage.getUnencrypted("number_id") === undefined) {
chrome.tabs.create({url: "options.html"}); chrome.tabs.create({url: "options.html"});
else { } else {
subscribeToPush(function(message) { function fillMessages() {
console.log("GOT MESSAGE IN POPUP! " + message); var MAX_MESSAGES_PER_CONVERSATION = 4;
var MAX_CONVERSATIONS = 5;
var conversations = [];
var messageMap = getMessageMap();
for (conversation in messageMap) {
var messages = messageMap[conversation];
messages.sort(function(a, b) { return b.timestamp - a.timestamp; });
conversations[conversations.length] = messages;
}
conversations.sort(function(a, b) { return b[0].timestamp - a[0].timestamp });
var ul = $('#messages');
ul.html('');
for (var i = 0; i < MAX_CONVERSATIONS && i < conversations.length; i++) {
var conversation = conversations[i];
ul.append('<li>');
for (var j = 0; j < MAX_MESSAGES_PER_CONVERSATION && conversation.length; j++) {
ul.append(JSON.stringify(conversation[j]));
}
ul.append('</li>');
}
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log("Got push message from background.js " + JSON.stringify(request));
fillMessages();
}); });
fillMessages();
} }