From 3f37cd21a941e6cebeb7badbcd1c66da4e3c4b5c Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 12 May 2015 15:14:20 -0700 Subject: [PATCH] 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. --- background.html | 1 + js/axolotl_store.js | 36 -------- js/background.js | 194 +++++++++++++++++++++-------------------- js/chromium.js | 4 +- js/inbox_controller.js | 6 +- js/notifications.js | 6 +- js/storage.js | 70 +++++++++++++++ 7 files changed, 177 insertions(+), 140 deletions(-) create mode 100644 js/storage.js diff --git a/background.html b/background.html index 04f7604c..f1bea8a2 100644 --- a/background.html +++ b/background.html @@ -230,6 +230,7 @@ + diff --git a/js/axolotl_store.js b/js/axolotl_store.js index 04678081..7f23f3dd 100644 --- a/js/axolotl_store.js +++ b/js/axolotl_store.js @@ -83,42 +83,6 @@ var IdentityKey = Model.extend({ storeName: 'identityKeys' }); var Group = Model.extend({ storeName: 'groups' }); 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() {} diff --git a/js/background.js b/js/background.js index 984a86c6..87237186 100644 --- a/js/background.js +++ b/js/background.js @@ -16,119 +16,121 @@ ;(function() { 'use strict'; - var messageReceiver; + storage.onready(function() { + var messageReceiver; - if (!localStorage.getItem('first_install_ran')) { - localStorage.setItem('first_install_ran', 1); - 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; + if (!storage.get('first_install_ran')) { + storage.put('first_install_ran', 1); + extension.navigator.tabs.create("options.html"); } - }; - function init() { - if (!textsecure.registration.isDone()) { return; } + if (textsecure.registration.isDone()) { + init(); + } + extension.on('registration_done', init); - // initialize the socket and start listening for messages - messageReceiver = new textsecure.MessageReceiver(window); - window.addEventListener('signal', function(ev) { - var proto = ev.proto; - if (proto.type === textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT) { - onDeliveryReceipt(proto); + window.getSocketStatus = function() { + if (messageReceiver) { + return messageReceiver.getStatus(); } else { - onMessageReceived(proto); + return -1; } - }); - messageReceiver.connect(); - } + }; - function onMessageReceived(pushMessage) { - var now = new Date().getTime(); - var timestamp = pushMessage.timestamp.toNumber(); + function init() { + if (!textsecure.registration.isDone()) { return; } - var conversation = getConversation({ - id : pushMessage.source, - type : 'private' - }); + // initialize the socket and start listening for messages + messageReceiver = new textsecure.MessageReceiver(window); + 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() { - var message = conversation.messageCollection.add({ - source : pushMessage.source, - sourceDevice : pushMessage.sourceDevice, - relay : pushMessage.relay, - sent_at : timestamp, - received_at : now, - conversationId : pushMessage.source, - type : 'incoming' + function onMessageReceived(pushMessage) { + var now = new Date().getTime(); + var timestamp = pushMessage.timestamp.toNumber(); + + var conversation = getConversation({ + id : pushMessage.source, + type : 'private' }); - var newUnreadCount = textsecure.storage.get("unreadCount", 0) + 1; - textsecure.storage.put("unreadCount", newUnreadCount); - extension.navigator.setBadgeText(newUnreadCount); + conversation.fetch().always(function() { + var message = conversation.messageCollection.add({ + source : pushMessage.source, + sourceDevice : pushMessage.sourceDevice, + relay : pushMessage.relay, + sent_at : timestamp, + received_at : now, + conversationId : pushMessage.source, + type : 'incoming' + }); - conversation.save().then(function() { - message.save().then(function() { - return new Promise(function(resolve) { - resolve(textsecure.protocol_wrapper.handleIncomingPushMessageProto(pushMessage).then( - function(pushMessageContent) { - message.handlePushMessageContent(pushMessageContent); + var newUnreadCount = storage.get("unreadCount", 0) + 1; + storage.put("unreadCount", newUnreadCount); + extension.navigator.setBadgeText(newUnreadCount); + + conversation.save().then(function() { + 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) { - var timestamp = pushMessage.timestamp.toNumber(); - var messages = new Whisper.MessageCollection(); - var groups = new Whisper.ConversationCollection(); - console.log('delivery receipt', pushMessage.source, timestamp); - messages.fetchSentAt(timestamp).then(function() { - groups.fetchGroups(pushMessage.source).then(function() { - for (var i in messages.where({type: 'outgoing'})) { - var message = messages.at(i); - var deliveries = message.get('delivered') || 0; - var conversationId = message.get('conversationId'); - if (conversationId === pushMessage.source || groups.get(conversationId)) { - message.save({delivered: deliveries + 1}).then( - // notify frontend listeners - updateConversation.bind(window,conversationId) - ); - return; - // TODO: consider keeping a list of numbers we've - // successfully delivered to? + function onDeliveryReceipt(pushMessage) { + var timestamp = pushMessage.timestamp.toNumber(); + var messages = new Whisper.MessageCollection(); + var groups = new Whisper.ConversationCollection(); + console.log('delivery receipt', pushMessage.source, timestamp); + messages.fetchSentAt(timestamp).then(function() { + groups.fetchGroups(pushMessage.source).then(function() { + for (var i in messages.where({type: 'outgoing'})) { + var message = messages.at(i); + var deliveries = message.get('delivered') || 0; + var conversationId = message.get('conversationId'); + if (conversationId === pushMessage.source || groups.get(conversationId)) { + message.save({delivered: deliveries + 1}).then( + // notify frontend listeners + updateConversation.bind(window,conversationId) + ); + return; + // TODO: consider keeping a list of numbers we've + // 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); - }); - } + } + }); })(); diff --git a/js/chromium.js b/js/chromium.js index 2233e832..ae5437dc 100644 --- a/js/chromium.js +++ b/js/chromium.js @@ -98,12 +98,12 @@ window.textsecure = window.textsecure || {}; window.textsecure.registration = { done: function () { - localStorage.setItem("chromiumRegistrationDone", ""); + storage.put("chromiumRegistrationDone", ""); extension.trigger('registration_done'); }, isDone: function () { - return localStorage.getItem("chromiumRegistrationDone") !== null; + return storage.get("chromiumRegistrationDone") === ""; }, }; diff --git a/js/inbox_controller.js b/js/inbox_controller.js index c1a85663..17402ad3 100644 --- a/js/inbox_controller.js +++ b/js/inbox_controller.js @@ -33,9 +33,9 @@ inbox.on('change:unreadCount', function(model, count) { var prev = model.previous('unreadCount') || 0; if (count < prev) { // decreased - var newUnreadCount = textsecure.storage.get("unreadCount", 0) - (prev - count); + var newUnreadCount = storage.get("unreadCount", 0) - (prev - count); setUnreadCount(newUnreadCount); - textsecure.storage.put("unreadCount", newUnreadCount); + storage.put("unreadCount", newUnreadCount); } }); @@ -52,7 +52,7 @@ extension.on('message', fetch); fetch(); - setUnreadCount(textsecure.storage.get("unreadCount", 0)); + setUnreadCount(storage.get("unreadCount", 0)); function setUnreadCount(count) { if (count > 0) { diff --git a/js/notifications.js b/js/notifications.js index 599d8e2e..e1126d23 100644 --- a/js/notifications.js +++ b/js/notifications.js @@ -20,16 +20,16 @@ Whisper.Notifications = { isEnabled: function(callback) { return Notification.permission === 'granted' && - !localStorage.getItem('disable-notifications'); + !storage.get('disable-notifications'); }, enable: function(callback) { - localStorage.removeItem('disable-notifications'); + storage.remove('disable-notifications'); Notification.requestPermission(function(status) { callback(status); }); }, disable: function() { - localStorage.setItem('disable-notifications', true); + storage.put('disable-notifications', true); } }; diff --git a/js/storage.js b/js/storage.js new file mode 100644 index 00000000..1a20c840 --- /dev/null +++ b/js/storage.js @@ -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 . + */ +;(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; +})();