Bug 1073336 part 5 - Add AnimationPlayerCollection::PlayerUpdated; r=dbaron
[gecko.git] / dom / datastore / DataStoreDB.jsm
blobec563875e5438eaaafbb2cf285f1fd7c61f3dc32
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/. */
5 'use strict';
7 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
9 this.EXPORTED_SYMBOLS = ['DataStoreDB'];
11 function debug(s) {
12   //dump('DEBUG DataStoreDB: ' + s + '\n');
15 const DATASTOREDB_VERSION = 1;
16 const DATASTOREDB_OBJECTSTORE_NAME = 'DataStoreDB';
17 const DATASTOREDB_REVISION = 'revision';
18 const DATASTOREDB_REVISION_INDEX = 'revisionIndex';
20 Cu.import('resource://gre/modules/IndexedDBHelper.jsm');
21 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
22 Cu.importGlobalProperties(["indexedDB"]);
24 XPCOMUtils.defineLazyServiceGetter(this, "uuidgen",
25                                    "@mozilla.org/uuid-generator;1",
26                                    "nsIUUIDGenerator");
28 this.DataStoreDB = function DataStoreDB() {}
30 DataStoreDB.prototype = {
32   __proto__: IndexedDBHelper.prototype,
34   upgradeSchema: function(aTransaction, aDb, aOldVersion, aNewVersion) {
35     debug('updateSchema');
36     aDb.createObjectStore(DATASTOREDB_OBJECTSTORE_NAME, { autoIncrement: true });
37     let store = aDb.createObjectStore(DATASTOREDB_REVISION,
38                                       { autoIncrement: true,
39                                         keyPath: 'internalRevisionId' });
40     store.createIndex(DATASTOREDB_REVISION_INDEX, 'revisionId', { unique: true });
41   },
43   init: function(aOwner, aName) {
44     let dbName = aName + '|' + aOwner;
45     this.initDBHelper(dbName, DATASTOREDB_VERSION,
46                       [DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION]);
47   },
49   txn: function(aType, aCallback, aErrorCb) {
50     debug('Transaction request');
51     this.newTxn(
52       aType,
53       aType == 'readonly'
54         ? [ DATASTOREDB_OBJECTSTORE_NAME ] : [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ],
55       function(aTxn, aStores) {
56         aType == 'readonly' ? aCallback(aTxn, aStores[0], null) : aCallback(aTxn, aStores[0], aStores[1]);
57       },
58       function() {},
59       aErrorCb
60     );
61   },
63   cursorTxn: function(aCallback, aErrorCb) {
64     debug('Cursor transaction request');
65     this.newTxn(
66       'readonly',
67        [ DATASTOREDB_OBJECTSTORE_NAME, DATASTOREDB_REVISION ],
68       function(aTxn, aStores) {
69         aCallback(aTxn, aStores[0], aStores[1]);
70       },
71       function() {},
72       aErrorCb
73     );
74   },
76   revisionTxn: function(aType, aCallback, aErrorCb) {
77     debug("Transaction request");
78     this.newTxn(
79       aType,
80       DATASTOREDB_REVISION,
81       aCallback,
82       function() {},
83       aErrorCb
84     );
85   },
87   addRevision: function(aStore, aKey, aType, aSuccessCb) {
88     debug("AddRevision: " + aKey + " - " + aType);
89     let revisionId =  uuidgen.generateUUID().toString();
90     let request = aStore.put({ revisionId: revisionId, objectId: aKey, operation: aType });
91     request.onsuccess = function() {
92       aSuccessCb(revisionId);
93     }
94   },
96   getInternalRevisionId: function(aRevisionId, aStore, aSuccessCb) {
97     debug('GetInternalRevisionId');
98     let request = aStore.index(DATASTOREDB_REVISION_INDEX).getKey(aRevisionId);
99     request.onsuccess = function(aEvent) {
100       aSuccessCb(aEvent.target.result);
101     }
102   },
104   clearRevisions: function(aStore, aSuccessCb) {
105     debug("ClearRevisions");
106     let request = aStore.clear();
107     request.onsuccess = function() {
108       aSuccessCb();
109     }
110   },
112   delete: function() {
113     debug('delete');
114     this.close();
115     indexedDB.deleteDatabase(this.dbName);
116     debug('database deleted');
117   }