Remove remaining traces of localStorage
Add window.storage to the background page, which loads all data from the 'items' store in indexeddb, caching them in memory for synchronous access, then override textsecure storage to use that in memory store.
This commit is contained in:
parent
a3c5b0959f
commit
3f37cd21a9
7 changed files with 177 additions and 140 deletions
|
@ -230,6 +230,7 @@
|
||||||
</script>
|
</script>
|
||||||
<script type="text/javascript" src="js/components.js"></script>
|
<script type="text/javascript" src="js/components.js"></script>
|
||||||
<script type="text/javascript" src="js/database.js"></script>
|
<script type="text/javascript" src="js/database.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage.js"></script>
|
||||||
<script type="text/javascript" src="js/axolotl_store.js"></script>
|
<script type="text/javascript" src="js/axolotl_store.js"></script>
|
||||||
<script type="text/javascript" src="js/libtextsecure.js"></script>
|
<script type="text/javascript" src="js/libtextsecure.js"></script>
|
||||||
|
|
||||||
|
|
|
@ -83,42 +83,6 @@
|
||||||
var IdentityKey = Model.extend({ storeName: 'identityKeys' });
|
var IdentityKey = Model.extend({ storeName: 'identityKeys' });
|
||||||
var Group = Model.extend({ storeName: 'groups' });
|
var Group = Model.extend({ storeName: 'groups' });
|
||||||
var Item = Model.extend({ storeName: 'items' });
|
var Item = Model.extend({ storeName: 'items' });
|
||||||
var ItemCollection = Backbone.Collection.extend({
|
|
||||||
model: Item,
|
|
||||||
storeName: 'items',
|
|
||||||
database: Whisper.Database,
|
|
||||||
});
|
|
||||||
|
|
||||||
var items = new ItemCollection();
|
|
||||||
window.textsecure = window.textsecure || {};
|
|
||||||
window.textsecure.storage = window.textsecure.storage || {};
|
|
||||||
window.textsecure.storage.impl = {
|
|
||||||
/*****************************
|
|
||||||
*** Base Storage Routines ***
|
|
||||||
*****************************/
|
|
||||||
put: function(key, value) {
|
|
||||||
if (value === undefined)
|
|
||||||
throw new Error("Tried to store undefined");
|
|
||||||
var item = items.add({id: key, value: value});
|
|
||||||
item.save();
|
|
||||||
},
|
|
||||||
|
|
||||||
get: function(key, defaultValue) {
|
|
||||||
var item = items.get("" + key);
|
|
||||||
if (!item)
|
|
||||||
return defaultValue;
|
|
||||||
return item.get('value');
|
|
||||||
},
|
|
||||||
|
|
||||||
remove: function(key) {
|
|
||||||
var item = items.get("" + key);
|
|
||||||
if (item) {
|
|
||||||
items.remove(item);
|
|
||||||
item.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
items.fetch();
|
|
||||||
|
|
||||||
function AxolotlStore() {}
|
function AxolotlStore() {}
|
||||||
|
|
||||||
|
|
194
js/background.js
194
js/background.js
|
@ -16,119 +16,121 @@
|
||||||
|
|
||||||
;(function() {
|
;(function() {
|
||||||
'use strict';
|
'use strict';
|
||||||
var messageReceiver;
|
storage.onready(function() {
|
||||||
|
var messageReceiver;
|
||||||
|
|
||||||
if (!localStorage.getItem('first_install_ran')) {
|
if (!storage.get('first_install_ran')) {
|
||||||
localStorage.setItem('first_install_ran', 1);
|
storage.put('first_install_ran', 1);
|
||||||
extension.navigator.tabs.create("options.html");
|
extension.navigator.tabs.create("options.html");
|
||||||
}
|
|
||||||
|
|
||||||
if (textsecure.registration.isDone()) {
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
extension.on('registration_done', init);
|
|
||||||
|
|
||||||
window.getSocketStatus = function() {
|
|
||||||
if (messageReceiver) {
|
|
||||||
return messageReceiver.getStatus();
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
function init() {
|
if (textsecure.registration.isDone()) {
|
||||||
if (!textsecure.registration.isDone()) { return; }
|
init();
|
||||||
|
}
|
||||||
|
extension.on('registration_done', init);
|
||||||
|
|
||||||
// initialize the socket and start listening for messages
|
window.getSocketStatus = function() {
|
||||||
messageReceiver = new textsecure.MessageReceiver(window);
|
if (messageReceiver) {
|
||||||
window.addEventListener('signal', function(ev) {
|
return messageReceiver.getStatus();
|
||||||
var proto = ev.proto;
|
|
||||||
if (proto.type === textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT) {
|
|
||||||
onDeliveryReceipt(proto);
|
|
||||||
} else {
|
} else {
|
||||||
onMessageReceived(proto);
|
return -1;
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
messageReceiver.connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
function onMessageReceived(pushMessage) {
|
function init() {
|
||||||
var now = new Date().getTime();
|
if (!textsecure.registration.isDone()) { return; }
|
||||||
var timestamp = pushMessage.timestamp.toNumber();
|
|
||||||
|
|
||||||
var conversation = getConversation({
|
// initialize the socket and start listening for messages
|
||||||
id : pushMessage.source,
|
messageReceiver = new textsecure.MessageReceiver(window);
|
||||||
type : 'private'
|
window.addEventListener('signal', function(ev) {
|
||||||
});
|
var proto = ev.proto;
|
||||||
|
if (proto.type === textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT) {
|
||||||
|
onDeliveryReceipt(proto);
|
||||||
|
} else {
|
||||||
|
onMessageReceived(proto);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
messageReceiver.connect();
|
||||||
|
}
|
||||||
|
|
||||||
conversation.fetch().always(function() {
|
function onMessageReceived(pushMessage) {
|
||||||
var message = conversation.messageCollection.add({
|
var now = new Date().getTime();
|
||||||
source : pushMessage.source,
|
var timestamp = pushMessage.timestamp.toNumber();
|
||||||
sourceDevice : pushMessage.sourceDevice,
|
|
||||||
relay : pushMessage.relay,
|
var conversation = getConversation({
|
||||||
sent_at : timestamp,
|
id : pushMessage.source,
|
||||||
received_at : now,
|
type : 'private'
|
||||||
conversationId : pushMessage.source,
|
|
||||||
type : 'incoming'
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var newUnreadCount = textsecure.storage.get("unreadCount", 0) + 1;
|
conversation.fetch().always(function() {
|
||||||
textsecure.storage.put("unreadCount", newUnreadCount);
|
var message = conversation.messageCollection.add({
|
||||||
extension.navigator.setBadgeText(newUnreadCount);
|
source : pushMessage.source,
|
||||||
|
sourceDevice : pushMessage.sourceDevice,
|
||||||
|
relay : pushMessage.relay,
|
||||||
|
sent_at : timestamp,
|
||||||
|
received_at : now,
|
||||||
|
conversationId : pushMessage.source,
|
||||||
|
type : 'incoming'
|
||||||
|
});
|
||||||
|
|
||||||
conversation.save().then(function() {
|
var newUnreadCount = storage.get("unreadCount", 0) + 1;
|
||||||
message.save().then(function() {
|
storage.put("unreadCount", newUnreadCount);
|
||||||
return new Promise(function(resolve) {
|
extension.navigator.setBadgeText(newUnreadCount);
|
||||||
resolve(textsecure.protocol_wrapper.handleIncomingPushMessageProto(pushMessage).then(
|
|
||||||
function(pushMessageContent) {
|
conversation.save().then(function() {
|
||||||
message.handlePushMessageContent(pushMessageContent);
|
message.save().then(function() {
|
||||||
|
return new Promise(function(resolve) {
|
||||||
|
resolve(textsecure.protocol_wrapper.handleIncomingPushMessageProto(pushMessage).then(
|
||||||
|
function(pushMessageContent) {
|
||||||
|
message.handlePushMessageContent(pushMessageContent);
|
||||||
|
}
|
||||||
|
));
|
||||||
|
}).catch(function(e) {
|
||||||
|
if (e.name === 'IncomingIdentityKeyError') {
|
||||||
|
message.save({ errors : [e] }).then(function() {
|
||||||
|
extension.trigger('message', message);
|
||||||
|
notifyConversation(message);
|
||||||
|
});
|
||||||
|
} else if (e.message === 'Bad MAC') {
|
||||||
|
message.save({ errors : [ _.pick(e, ['name', 'message'])]}).then(function() {
|
||||||
|
extension.trigger('message', message);
|
||||||
|
notifyConversation(message);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log(e);
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
));
|
});
|
||||||
}).catch(function(e) {
|
|
||||||
if (e.name === 'IncomingIdentityKeyError') {
|
|
||||||
message.save({ errors : [e] }).then(function() {
|
|
||||||
extension.trigger('message', message);
|
|
||||||
notifyConversation(message);
|
|
||||||
});
|
|
||||||
} else if (e.message === 'Bad MAC') {
|
|
||||||
message.save({ errors : [ _.pick(e, ['name', 'message'])]}).then(function() {
|
|
||||||
extension.trigger('message', message);
|
|
||||||
notifyConversation(message);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
console.log(e);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function onDeliveryReceipt(pushMessage) {
|
function onDeliveryReceipt(pushMessage) {
|
||||||
var timestamp = pushMessage.timestamp.toNumber();
|
var timestamp = pushMessage.timestamp.toNumber();
|
||||||
var messages = new Whisper.MessageCollection();
|
var messages = new Whisper.MessageCollection();
|
||||||
var groups = new Whisper.ConversationCollection();
|
var groups = new Whisper.ConversationCollection();
|
||||||
console.log('delivery receipt', pushMessage.source, timestamp);
|
console.log('delivery receipt', pushMessage.source, timestamp);
|
||||||
messages.fetchSentAt(timestamp).then(function() {
|
messages.fetchSentAt(timestamp).then(function() {
|
||||||
groups.fetchGroups(pushMessage.source).then(function() {
|
groups.fetchGroups(pushMessage.source).then(function() {
|
||||||
for (var i in messages.where({type: 'outgoing'})) {
|
for (var i in messages.where({type: 'outgoing'})) {
|
||||||
var message = messages.at(i);
|
var message = messages.at(i);
|
||||||
var deliveries = message.get('delivered') || 0;
|
var deliveries = message.get('delivered') || 0;
|
||||||
var conversationId = message.get('conversationId');
|
var conversationId = message.get('conversationId');
|
||||||
if (conversationId === pushMessage.source || groups.get(conversationId)) {
|
if (conversationId === pushMessage.source || groups.get(conversationId)) {
|
||||||
message.save({delivered: deliveries + 1}).then(
|
message.save({delivered: deliveries + 1}).then(
|
||||||
// notify frontend listeners
|
// notify frontend listeners
|
||||||
updateConversation.bind(window,conversationId)
|
updateConversation.bind(window,conversationId)
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
// TODO: consider keeping a list of numbers we've
|
// TODO: consider keeping a list of numbers we've
|
||||||
// successfully delivered to?
|
// successfully delivered to?
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
}).fail(function() {
|
||||||
|
console.log('got delivery receipt for unknown message', pushMessage.source, timestamp);
|
||||||
});
|
});
|
||||||
}).fail(function() {
|
}
|
||||||
console.log('got delivery receipt for unknown message', pushMessage.source, timestamp);
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -98,12 +98,12 @@
|
||||||
window.textsecure = window.textsecure || {};
|
window.textsecure = window.textsecure || {};
|
||||||
window.textsecure.registration = {
|
window.textsecure.registration = {
|
||||||
done: function () {
|
done: function () {
|
||||||
localStorage.setItem("chromiumRegistrationDone", "");
|
storage.put("chromiumRegistrationDone", "");
|
||||||
extension.trigger('registration_done');
|
extension.trigger('registration_done');
|
||||||
},
|
},
|
||||||
|
|
||||||
isDone: function () {
|
isDone: function () {
|
||||||
return localStorage.getItem("chromiumRegistrationDone") !== null;
|
return storage.get("chromiumRegistrationDone") === "";
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -33,9 +33,9 @@
|
||||||
inbox.on('change:unreadCount', function(model, count) {
|
inbox.on('change:unreadCount', function(model, count) {
|
||||||
var prev = model.previous('unreadCount') || 0;
|
var prev = model.previous('unreadCount') || 0;
|
||||||
if (count < prev) { // decreased
|
if (count < prev) { // decreased
|
||||||
var newUnreadCount = textsecure.storage.get("unreadCount", 0) - (prev - count);
|
var newUnreadCount = storage.get("unreadCount", 0) - (prev - count);
|
||||||
setUnreadCount(newUnreadCount);
|
setUnreadCount(newUnreadCount);
|
||||||
textsecure.storage.put("unreadCount", newUnreadCount);
|
storage.put("unreadCount", newUnreadCount);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@
|
||||||
|
|
||||||
extension.on('message', fetch);
|
extension.on('message', fetch);
|
||||||
fetch();
|
fetch();
|
||||||
setUnreadCount(textsecure.storage.get("unreadCount", 0));
|
setUnreadCount(storage.get("unreadCount", 0));
|
||||||
|
|
||||||
function setUnreadCount(count) {
|
function setUnreadCount(count) {
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
|
|
|
@ -20,16 +20,16 @@
|
||||||
Whisper.Notifications = {
|
Whisper.Notifications = {
|
||||||
isEnabled: function(callback) {
|
isEnabled: function(callback) {
|
||||||
return Notification.permission === 'granted' &&
|
return Notification.permission === 'granted' &&
|
||||||
!localStorage.getItem('disable-notifications');
|
!storage.get('disable-notifications');
|
||||||
},
|
},
|
||||||
enable: function(callback) {
|
enable: function(callback) {
|
||||||
localStorage.removeItem('disable-notifications');
|
storage.remove('disable-notifications');
|
||||||
Notification.requestPermission(function(status) {
|
Notification.requestPermission(function(status) {
|
||||||
callback(status);
|
callback(status);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
disable: function() {
|
disable: function() {
|
||||||
localStorage.setItem('disable-notifications', true);
|
storage.put('disable-notifications', true);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
70
js/storage.js
Normal file
70
js/storage.js
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
/* vim: ts=4:sw=4
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
;(function() {
|
||||||
|
'use strict';
|
||||||
|
window.Whisper = window.Whisper || {};
|
||||||
|
var Item = Backbone.Model.extend({
|
||||||
|
database: Whisper.Database,
|
||||||
|
storeName: 'items'
|
||||||
|
});
|
||||||
|
var ItemCollection = Backbone.Collection.extend({
|
||||||
|
model: Item,
|
||||||
|
storeName: 'items',
|
||||||
|
database: Whisper.Database,
|
||||||
|
});
|
||||||
|
|
||||||
|
var ready = false;
|
||||||
|
var items = new ItemCollection();
|
||||||
|
items.on('reset', function() { ready = true; });
|
||||||
|
items.fetch({reset: true});
|
||||||
|
window.storage = {
|
||||||
|
/*****************************
|
||||||
|
*** Base Storage Routines ***
|
||||||
|
*****************************/
|
||||||
|
put: function(key, value) {
|
||||||
|
if (value === undefined)
|
||||||
|
throw new Error("Tried to store undefined");
|
||||||
|
var item = items.add({id: key, value: value});
|
||||||
|
item.save();
|
||||||
|
},
|
||||||
|
|
||||||
|
get: function(key, defaultValue) {
|
||||||
|
var item = items.get("" + key);
|
||||||
|
if (!item)
|
||||||
|
return defaultValue;
|
||||||
|
return item.get('value');
|
||||||
|
},
|
||||||
|
|
||||||
|
remove: function(key) {
|
||||||
|
var item = items.get("" + key);
|
||||||
|
if (item) {
|
||||||
|
items.remove(item);
|
||||||
|
item.destroy();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
onready: function(callback) {
|
||||||
|
if (ready) {
|
||||||
|
callback();
|
||||||
|
} else {
|
||||||
|
items.on('reset', callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.textsecure = window.textsecure || {};
|
||||||
|
window.textsecure.storage = window.textsecure.storage || {};
|
||||||
|
window.textsecure.storage.impl = window.storage;
|
||||||
|
})();
|
Loading…
Reference in a new issue