dom/alarm/AlarmDB.jsm

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = ["AlarmDB"];
michael@0 8
michael@0 9 /* static functions */
michael@0 10 const DEBUG = false;
michael@0 11
michael@0 12 function debug(aStr) {
michael@0 13 if (DEBUG)
michael@0 14 dump("AlarmDB: " + aStr + "\n");
michael@0 15 }
michael@0 16
michael@0 17 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
michael@0 18
michael@0 19 Cu.import("resource://gre/modules/Services.jsm");
michael@0 20 Cu.import("resource://gre/modules/IndexedDBHelper.jsm");
michael@0 21
michael@0 22 const ALARMDB_NAME = "alarms";
michael@0 23 const ALARMDB_VERSION = 1;
michael@0 24 const ALARMSTORE_NAME = "alarms";
michael@0 25
michael@0 26 this.AlarmDB = function AlarmDB() {
michael@0 27 debug("AlarmDB()");
michael@0 28 }
michael@0 29
michael@0 30 AlarmDB.prototype = {
michael@0 31 __proto__: IndexedDBHelper.prototype,
michael@0 32
michael@0 33 init: function init() {
michael@0 34 debug("init()");
michael@0 35
michael@0 36 this.initDBHelper(ALARMDB_NAME, ALARMDB_VERSION, [ALARMSTORE_NAME]);
michael@0 37 },
michael@0 38
michael@0 39 upgradeSchema: function upgradeSchema(aTransaction, aDb, aOldVersion, aNewVersion) {
michael@0 40 debug("upgradeSchema()");
michael@0 41
michael@0 42 let objectStore = aDb.createObjectStore(ALARMSTORE_NAME, { keyPath: "id", autoIncrement: true });
michael@0 43
michael@0 44 objectStore.createIndex("date", "date", { unique: false });
michael@0 45 objectStore.createIndex("ignoreTimezone", "ignoreTimezone", { unique: false });
michael@0 46 objectStore.createIndex("timezoneOffset", "timezoneOffset", { unique: false });
michael@0 47 objectStore.createIndex("data", "data", { unique: false });
michael@0 48 objectStore.createIndex("pageURL", "pageURL", { unique: false });
michael@0 49 objectStore.createIndex("manifestURL", "manifestURL", { unique: false });
michael@0 50
michael@0 51 debug("Created object stores and indexes");
michael@0 52 },
michael@0 53
michael@0 54 /**
michael@0 55 * @param aAlarm
michael@0 56 * The record to be added.
michael@0 57 * @param aSuccessCb
michael@0 58 * Callback function to invoke with result ID.
michael@0 59 * @param aErrorCb [optional]
michael@0 60 * Callback function to invoke when there was an error.
michael@0 61 */
michael@0 62 add: function add(aAlarm, aSuccessCb, aErrorCb) {
michael@0 63 debug("add()");
michael@0 64
michael@0 65 this.newTxn(
michael@0 66 "readwrite",
michael@0 67 ALARMSTORE_NAME,
michael@0 68 function txnCb(aTxn, aStore) {
michael@0 69 debug("Going to add " + JSON.stringify(aAlarm));
michael@0 70 aStore.put(aAlarm).onsuccess = function setTxnResult(aEvent) {
michael@0 71 aTxn.result = aEvent.target.result;
michael@0 72 debug("Request successful. New record ID: " + aTxn.result);
michael@0 73 };
michael@0 74 },
michael@0 75 aSuccessCb,
michael@0 76 aErrorCb
michael@0 77 );
michael@0 78 },
michael@0 79
michael@0 80 /**
michael@0 81 * @param aId
michael@0 82 * The ID of record to be removed.
michael@0 83 * @param aManifestURL
michael@0 84 * The manifest URL of the app that alarm belongs to.
michael@0 85 * If null, directly remove the ID record; otherwise,
michael@0 86 * need to check if the alarm belongs to this app.
michael@0 87 * @param aSuccessCb
michael@0 88 * Callback function to invoke with result.
michael@0 89 * @param aErrorCb [optional]
michael@0 90 * Callback function to invoke when there was an error.
michael@0 91 */
michael@0 92 remove: function remove(aId, aManifestURL, aSuccessCb, aErrorCb) {
michael@0 93 debug("remove()");
michael@0 94
michael@0 95 this.newTxn(
michael@0 96 "readwrite",
michael@0 97 ALARMSTORE_NAME,
michael@0 98 function txnCb(aTxn, aStore) {
michael@0 99 debug("Going to remove " + aId);
michael@0 100
michael@0 101 // Look up the existing record and compare the manifestURL
michael@0 102 // to see if the alarm to be removed belongs to this app.
michael@0 103 aStore.get(aId).onsuccess = function doRemove(aEvent) {
michael@0 104 let alarm = aEvent.target.result;
michael@0 105
michael@0 106 if (!alarm) {
michael@0 107 debug("Alarm doesn't exist. No need to remove it.");
michael@0 108 return;
michael@0 109 }
michael@0 110
michael@0 111 if (aManifestURL && aManifestURL != alarm.manifestURL) {
michael@0 112 debug("Cannot remove the alarm added by other apps.");
michael@0 113 return;
michael@0 114 }
michael@0 115
michael@0 116 aStore.delete(aId);
michael@0 117 };
michael@0 118 },
michael@0 119 aSuccessCb,
michael@0 120 aErrorCb
michael@0 121 );
michael@0 122 },
michael@0 123
michael@0 124 /**
michael@0 125 * @param aManifestURL
michael@0 126 * The manifest URL of the app that alarms belong to.
michael@0 127 * If null, directly return all alarms; otherwise,
michael@0 128 * only return the alarms that belong to this app.
michael@0 129 * @param aSuccessCb
michael@0 130 * Callback function to invoke with result array.
michael@0 131 * @param aErrorCb [optional]
michael@0 132 * Callback function to invoke when there was an error.
michael@0 133 */
michael@0 134 getAll: function getAll(aManifestURL, aSuccessCb, aErrorCb) {
michael@0 135 debug("getAll()");
michael@0 136
michael@0 137 this.newTxn(
michael@0 138 "readonly",
michael@0 139 ALARMSTORE_NAME,
michael@0 140 function txnCb(aTxn, aStore) {
michael@0 141 if (!aTxn.result) {
michael@0 142 aTxn.result = [];
michael@0 143 }
michael@0 144
michael@0 145 let index = aStore.index("manifestURL");
michael@0 146 index.mozGetAll(aManifestURL).onsuccess = function setTxnResult(aEvent) {
michael@0 147 aTxn.result = aEvent.target.result;
michael@0 148 debug("Request successful. Record count: " + aTxn.result.length);
michael@0 149 };
michael@0 150 },
michael@0 151 aSuccessCb,
michael@0 152 aErrorCb
michael@0 153 );
michael@0 154 }
michael@0 155 };

mercurial