2012-06-22 00:38:59 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["AlarmDB"];
|
2012-06-22 00:38:59 -07:00
|
|
|
|
|
|
|
/* static functions */
|
|
|
|
const DEBUG = false;
|
|
|
|
|
|
|
|
function debug(aStr) {
|
|
|
|
if (DEBUG)
|
|
|
|
dump("AlarmDB: " + aStr + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/IndexedDBHelper.jsm");
|
|
|
|
|
|
|
|
const ALARMDB_NAME = "alarms";
|
|
|
|
const ALARMDB_VERSION = 1;
|
|
|
|
const ALARMSTORE_NAME = "alarms";
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.AlarmDB = function AlarmDB(aGlobal) {
|
2012-06-22 00:38:59 -07:00
|
|
|
debug("AlarmDB()");
|
|
|
|
this._global = aGlobal;
|
|
|
|
}
|
|
|
|
|
|
|
|
AlarmDB.prototype = {
|
|
|
|
__proto__: IndexedDBHelper.prototype,
|
|
|
|
|
|
|
|
init: function init(aGlobal) {
|
|
|
|
debug("init()");
|
|
|
|
|
2013-01-30 18:03:46 -08:00
|
|
|
this.initDBHelper(ALARMDB_NAME, ALARMDB_VERSION, [ALARMSTORE_NAME], aGlobal);
|
2012-06-22 00:38:59 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
upgradeSchema: function upgradeSchema(aTransaction, aDb, aOldVersion, aNewVersion) {
|
|
|
|
debug("upgradeSchema()");
|
|
|
|
|
|
|
|
let objectStore = aDb.createObjectStore(ALARMSTORE_NAME, { keyPath: "id", autoIncrement: true });
|
|
|
|
|
|
|
|
objectStore.createIndex("date", "date", { unique: false });
|
|
|
|
objectStore.createIndex("ignoreTimezone", "ignoreTimezone", { unique: false });
|
|
|
|
objectStore.createIndex("timezoneOffset", "timezoneOffset", { unique: false });
|
|
|
|
objectStore.createIndex("data", "data", { unique: false });
|
2012-08-14 19:40:12 -07:00
|
|
|
objectStore.createIndex("pageURL", "pageURL", { unique: false });
|
2012-07-05 00:53:08 -07:00
|
|
|
objectStore.createIndex("manifestURL", "manifestURL", { unique: false });
|
2012-06-22 00:38:59 -07:00
|
|
|
|
|
|
|
debug("Created object stores and indexes");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param aAlarm
|
|
|
|
* The record to be added.
|
|
|
|
* @param aSuccessCb
|
|
|
|
* Callback function to invoke with result ID.
|
|
|
|
* @param aErrorCb [optional]
|
|
|
|
* Callback function to invoke when there was an error.
|
|
|
|
*/
|
|
|
|
add: function add(aAlarm, aSuccessCb, aErrorCb) {
|
|
|
|
debug("add()");
|
|
|
|
|
|
|
|
this.newTxn(
|
2013-01-30 18:03:46 -08:00
|
|
|
"readwrite",
|
|
|
|
ALARMSTORE_NAME,
|
2012-06-22 00:38:59 -07:00
|
|
|
function txnCb(aTxn, aStore) {
|
|
|
|
debug("Going to add " + JSON.stringify(aAlarm));
|
|
|
|
aStore.put(aAlarm).onsuccess = function setTxnResult(aEvent) {
|
|
|
|
aTxn.result = aEvent.target.result;
|
|
|
|
debug("Request successful. New record ID: " + aTxn.result);
|
|
|
|
};
|
|
|
|
},
|
2013-01-30 18:03:46 -08:00
|
|
|
aSuccessCb,
|
2012-06-22 00:38:59 -07:00
|
|
|
aErrorCb
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param aId
|
|
|
|
* The ID of record to be removed.
|
2012-07-31 22:58:49 -07:00
|
|
|
* @param aManifestURL
|
|
|
|
* The manifest URL of the app that alarm belongs to.
|
|
|
|
* If null, directly remove the ID record; otherwise,
|
|
|
|
* need to check if the alarm belongs to this app.
|
2012-06-22 00:38:59 -07:00
|
|
|
* @param aSuccessCb
|
|
|
|
* Callback function to invoke with result.
|
|
|
|
* @param aErrorCb [optional]
|
|
|
|
* Callback function to invoke when there was an error.
|
|
|
|
*/
|
2012-07-31 22:58:49 -07:00
|
|
|
remove: function remove(aId, aManifestURL, aSuccessCb, aErrorCb) {
|
2012-06-22 00:38:59 -07:00
|
|
|
debug("remove()");
|
|
|
|
|
|
|
|
this.newTxn(
|
2013-01-30 18:03:46 -08:00
|
|
|
"readwrite",
|
|
|
|
ALARMSTORE_NAME,
|
2012-06-22 00:38:59 -07:00
|
|
|
function txnCb(aTxn, aStore) {
|
|
|
|
debug("Going to remove " + aId);
|
2012-07-31 22:58:49 -07:00
|
|
|
|
|
|
|
// Look up the existing record and compare the manifestURL
|
|
|
|
// to see if the alarm to be removed belongs to this app.
|
|
|
|
aStore.get(aId).onsuccess = function doRemove(aEvent) {
|
|
|
|
let alarm = aEvent.target.result;
|
|
|
|
|
|
|
|
if (!alarm) {
|
|
|
|
debug("Alarm doesn't exist. No need to remove it.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aManifestURL && aManifestURL != alarm.manifestURL) {
|
|
|
|
debug("Cannot remove the alarm added by other apps.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aStore.delete(aId);
|
|
|
|
};
|
2013-01-30 18:03:46 -08:00
|
|
|
},
|
|
|
|
aSuccessCb,
|
2012-06-22 00:38:59 -07:00
|
|
|
aErrorCb
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-07-31 22:58:49 -07:00
|
|
|
* @param aManifestURL
|
|
|
|
* The manifest URL of the app that alarms belong to.
|
|
|
|
* If null, directly return all alarms; otherwise,
|
|
|
|
* only return the alarms that belong to this app.
|
2012-06-22 00:38:59 -07:00
|
|
|
* @param aSuccessCb
|
|
|
|
* Callback function to invoke with result array.
|
|
|
|
* @param aErrorCb [optional]
|
|
|
|
* Callback function to invoke when there was an error.
|
|
|
|
*/
|
2012-07-31 22:58:49 -07:00
|
|
|
getAll: function getAll(aManifestURL, aSuccessCb, aErrorCb) {
|
2012-06-22 00:38:59 -07:00
|
|
|
debug("getAll()");
|
|
|
|
|
|
|
|
this.newTxn(
|
2013-01-30 18:03:46 -08:00
|
|
|
"readonly",
|
|
|
|
ALARMSTORE_NAME,
|
2012-06-22 00:38:59 -07:00
|
|
|
function txnCb(aTxn, aStore) {
|
2013-05-20 04:47:38 -07:00
|
|
|
if (!aTxn.result) {
|
2012-07-31 22:58:49 -07:00
|
|
|
aTxn.result = [];
|
2013-05-20 04:47:38 -07:00
|
|
|
}
|
2012-06-22 00:38:59 -07:00
|
|
|
|
2013-05-20 04:47:38 -07:00
|
|
|
let index = aStore.index("manifestURL");
|
|
|
|
index.mozGetAll(aManifestURL).onsuccess = function setTxnResult(aEvent) {
|
|
|
|
aTxn.result = aEvent.target.result;
|
2012-06-22 00:38:59 -07:00
|
|
|
debug("Request successful. Record count: " + aTxn.result.length);
|
|
|
|
};
|
2013-01-30 18:03:46 -08:00
|
|
|
},
|
|
|
|
aSuccessCb,
|
2012-06-22 00:38:59 -07:00
|
|
|
aErrorCb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|