Bug 749551 - Alarm API (AlarmDB). r=vivien
authorGene Lian <clian@mozilla.com>
Fri, 22 Jun 2012 15:38:59 +0800
changeset 100895 ed768b821da17bed5d0f932fa3b81b43f29ed9fb
parent 100894 23fd9a8d0c9afbd3dc247301304156c3c4949346
child 100896 1dba66cfad9ab1aac43a7a1dec8f219192f9566b
push idunknown
push userunknown
push dateunknown
reviewersvivien
bugs749551
milestone16.0a1
Bug 749551 - Alarm API (AlarmDB). r=vivien
dom/alarm/AlarmDB.jsm
dom/alarm/Makefile.in
new file mode 100644
--- /dev/null
+++ b/dom/alarm/AlarmDB.jsm
@@ -0,0 +1,124 @@
+/* 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";
+
+const EXPORTED_SYMBOLS = ["AlarmDB"];
+
+/* 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";
+
+function AlarmDB(aGlobal) {
+  debug("AlarmDB()");
+  this._global = aGlobal;
+}
+
+AlarmDB.prototype = {
+  __proto__: IndexedDBHelper.prototype,
+
+  init: function init(aGlobal) {
+    debug("init()");
+
+    this.initDBHelper(ALARMDB_NAME, ALARMDB_VERSION, ALARMSTORE_NAME, aGlobal);
+  },
+
+  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 });
+
+    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(
+      "readwrite", 
+      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);
+        };
+      },
+      aSuccessCb, 
+      aErrorCb
+    );
+  },
+
+  /**
+   * @param aId
+   *        The ID of record to be removed.
+   * @param aSuccessCb
+   *        Callback function to invoke with result.
+   * @param aErrorCb [optional]
+   *        Callback function to invoke when there was an error.
+   */
+  remove: function remove(aId, aSuccessCb, aErrorCb) {
+    debug("remove()");
+    
+    this.newTxn(
+      "readwrite", 
+      function txnCb(aTxn, aStore) {
+        debug("Going to remove " + aId);
+        aStore.delete(aId);
+      }, 
+      aSuccessCb, 
+      aErrorCb
+    );
+  },
+
+  /**
+   * @param aSuccessCb
+   *        Callback function to invoke with result array.
+   * @param aErrorCb [optional]
+   *        Callback function to invoke when there was an error.
+   */
+  getAll: function getAll(aSuccessCb, aErrorCb) {
+    debug("getAll()");
+
+    this.newTxn(
+      "readonly", 
+      function txnCb(aTxn, aStore) {
+        if (!aTxn.result)
+          aTxn.result = {};
+        
+        aStore.getAll().onsuccess = function setTxnResult(aEvent) {
+          aTxn.result = aEvent.target.result;
+          debug("Request successful. Record count: " + aTxn.result.length);
+        };
+      }, 
+      aSuccessCb, 
+      aErrorCb
+    );
+  }
+};
--- a/dom/alarm/Makefile.in
+++ b/dom/alarm/Makefile.in
@@ -20,16 +20,20 @@ include $(topsrcdir)/dom/dom-config.mk
 
 EXPORTS_NAMESPACES = mozilla/dom/alarm
 
 EXTRA_COMPONENTS =       \
   AlarmsManager.js       \
   AlarmsManager.manifest \
   $(NULL)
 
+EXTRA_JS_MODULES = \
+  AlarmDB.jsm      \
+  $(NULL)
+
 XPIDLSRCS =               \
   nsIDOMAlarmsManager.idl \
   nsIAlarmHalService.idl  \
   $(NULL)
 
 EXPORTS_mozilla/dom/alarm = \
   AlarmHalService.h         \
   $(NULL)