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