dom/alarm/AlarmDB.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/alarm/AlarmDB.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,155 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ["AlarmDB"];
    1.11 +
    1.12 +/* static functions */
    1.13 +const DEBUG = false;
    1.14 +
    1.15 +function debug(aStr) {
    1.16 +  if (DEBUG)
    1.17 +    dump("AlarmDB: " + aStr + "\n");
    1.18 +}
    1.19 +
    1.20 +const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
    1.21 +
    1.22 +Cu.import("resource://gre/modules/Services.jsm");
    1.23 +Cu.import("resource://gre/modules/IndexedDBHelper.jsm");
    1.24 +
    1.25 +const ALARMDB_NAME    = "alarms";
    1.26 +const ALARMDB_VERSION = 1;
    1.27 +const ALARMSTORE_NAME = "alarms";
    1.28 +
    1.29 +this.AlarmDB = function AlarmDB() {
    1.30 +  debug("AlarmDB()");
    1.31 +}
    1.32 +
    1.33 +AlarmDB.prototype = {
    1.34 +  __proto__: IndexedDBHelper.prototype,
    1.35 +
    1.36 +  init: function init() {
    1.37 +    debug("init()");
    1.38 +
    1.39 +    this.initDBHelper(ALARMDB_NAME, ALARMDB_VERSION, [ALARMSTORE_NAME]);
    1.40 +  },
    1.41 +
    1.42 +  upgradeSchema: function upgradeSchema(aTransaction, aDb, aOldVersion, aNewVersion) {
    1.43 +    debug("upgradeSchema()");
    1.44 +
    1.45 +    let objectStore = aDb.createObjectStore(ALARMSTORE_NAME, { keyPath: "id", autoIncrement: true });
    1.46 +
    1.47 +    objectStore.createIndex("date",           "date",           { unique: false });
    1.48 +    objectStore.createIndex("ignoreTimezone", "ignoreTimezone", { unique: false });
    1.49 +    objectStore.createIndex("timezoneOffset", "timezoneOffset", { unique: false });
    1.50 +    objectStore.createIndex("data",           "data",           { unique: false });
    1.51 +    objectStore.createIndex("pageURL",        "pageURL",        { unique: false });
    1.52 +    objectStore.createIndex("manifestURL",    "manifestURL",    { unique: false });
    1.53 +
    1.54 +    debug("Created object stores and indexes");
    1.55 +  },
    1.56 +
    1.57 +  /**
    1.58 +   * @param aAlarm
    1.59 +   *        The record to be added.
    1.60 +   * @param aSuccessCb
    1.61 +   *        Callback function to invoke with result ID.
    1.62 +   * @param aErrorCb [optional]
    1.63 +   *        Callback function to invoke when there was an error.
    1.64 +   */
    1.65 +  add: function add(aAlarm, aSuccessCb, aErrorCb) {
    1.66 +    debug("add()");
    1.67 +
    1.68 +    this.newTxn(
    1.69 +      "readwrite",
    1.70 +      ALARMSTORE_NAME,
    1.71 +      function txnCb(aTxn, aStore) {
    1.72 +        debug("Going to add " + JSON.stringify(aAlarm));
    1.73 +        aStore.put(aAlarm).onsuccess = function setTxnResult(aEvent) {
    1.74 +          aTxn.result = aEvent.target.result;
    1.75 +          debug("Request successful. New record ID: " + aTxn.result);
    1.76 +        };
    1.77 +      },
    1.78 +      aSuccessCb,
    1.79 +      aErrorCb
    1.80 +    );
    1.81 +  },
    1.82 +
    1.83 +  /**
    1.84 +   * @param aId
    1.85 +   *        The ID of record to be removed.
    1.86 +   * @param aManifestURL
    1.87 +   *        The manifest URL of the app that alarm belongs to.
    1.88 +   *        If null, directly remove the ID record; otherwise,
    1.89 +   *        need to check if the alarm belongs to this app.
    1.90 +   * @param aSuccessCb
    1.91 +   *        Callback function to invoke with result.
    1.92 +   * @param aErrorCb [optional]
    1.93 +   *        Callback function to invoke when there was an error.
    1.94 +   */
    1.95 +  remove: function remove(aId, aManifestURL, aSuccessCb, aErrorCb) {
    1.96 +    debug("remove()");
    1.97 +
    1.98 +    this.newTxn(
    1.99 +      "readwrite",
   1.100 +      ALARMSTORE_NAME,
   1.101 +      function txnCb(aTxn, aStore) {
   1.102 +        debug("Going to remove " + aId);
   1.103 +
   1.104 +        // Look up the existing record and compare the manifestURL
   1.105 +        // to see if the alarm to be removed belongs to this app.
   1.106 +        aStore.get(aId).onsuccess = function doRemove(aEvent) {
   1.107 +          let alarm = aEvent.target.result;
   1.108 +
   1.109 +          if (!alarm) {
   1.110 +            debug("Alarm doesn't exist. No need to remove it.");
   1.111 +            return;
   1.112 +          }
   1.113 +
   1.114 +          if (aManifestURL && aManifestURL != alarm.manifestURL) {
   1.115 +            debug("Cannot remove the alarm added by other apps.");
   1.116 +            return;
   1.117 +          }
   1.118 +
   1.119 +          aStore.delete(aId);
   1.120 +        };
   1.121 +      },
   1.122 +      aSuccessCb,
   1.123 +      aErrorCb
   1.124 +    );
   1.125 +  },
   1.126 +
   1.127 +  /**
   1.128 +   * @param aManifestURL
   1.129 +   *        The manifest URL of the app that alarms belong to.
   1.130 +   *        If null, directly return all alarms; otherwise,
   1.131 +   *        only return the alarms that belong to this app.
   1.132 +   * @param aSuccessCb
   1.133 +   *        Callback function to invoke with result array.
   1.134 +   * @param aErrorCb [optional]
   1.135 +   *        Callback function to invoke when there was an error.
   1.136 +   */
   1.137 +  getAll: function getAll(aManifestURL, aSuccessCb, aErrorCb) {
   1.138 +    debug("getAll()");
   1.139 +
   1.140 +    this.newTxn(
   1.141 +      "readonly",
   1.142 +      ALARMSTORE_NAME,
   1.143 +      function txnCb(aTxn, aStore) {
   1.144 +        if (!aTxn.result) {
   1.145 +          aTxn.result = [];
   1.146 +        }
   1.147 +
   1.148 +        let index = aStore.index("manifestURL");
   1.149 +        index.mozGetAll(aManifestURL).onsuccess = function setTxnResult(aEvent) {
   1.150 +          aTxn.result = aEvent.target.result;
   1.151 +          debug("Request successful. Record count: " + aTxn.result.length);
   1.152 +        };
   1.153 +      },
   1.154 +      aSuccessCb,
   1.155 +      aErrorCb
   1.156 +    );
   1.157 +  }
   1.158 +};

mercurial