2014-06-24 22:12:07 -07:00
|
|
|
/* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
|
2013-10-02 10:27:07 -07:00
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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'
|
|
|
|
|
2013-10-02 10:27:09 -07:00
|
|
|
function debug(s) {
|
2013-12-12 02:18:37 -08:00
|
|
|
//dump('DEBUG DataStore: ' + s + '\n');
|
2013-10-02 10:27:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
2013-10-02 10:27:11 -07:00
|
|
|
const REVISION_ADDED = "added";
|
|
|
|
const REVISION_UPDATED = "updated";
|
|
|
|
const REVISION_REMOVED = "removed";
|
|
|
|
const REVISION_VOID = "void";
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
// This value has to be tuned a bit. Currently it's just a guess
|
|
|
|
// and yet we don't know if it's too low or too high.
|
|
|
|
const MAX_REQUESTS = 25;
|
|
|
|
|
2013-12-20 00:17:49 -08:00
|
|
|
Cu.import("resource://gre/modules/DataStoreCursorImpl.jsm");
|
2013-10-02 10:27:09 -07:00
|
|
|
Cu.import("resource://gre/modules/DataStoreDB.jsm");
|
|
|
|
Cu.import('resource://gre/modules/Services.jsm');
|
2013-10-02 10:27:15 -07:00
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
2013-11-18 23:36:33 -08:00
|
|
|
Cu.importGlobalProperties(["indexedDB"]);
|
2013-10-02 10:27:15 -07:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
2013-10-02 10:27:09 -07:00
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
/* Helper functions */
|
2013-10-02 10:27:11 -07:00
|
|
|
function createDOMError(aWindow, aEvent) {
|
2014-01-23 11:31:01 -08:00
|
|
|
return new aWindow.DOMError(aEvent);
|
2013-10-02 10:27:11 -07:00
|
|
|
}
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
function throwInvalidArg(aWindow) {
|
|
|
|
return aWindow.Promise.reject(
|
|
|
|
new aWindow.DOMError("SyntaxError", "Non-numeric or invalid id"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function throwReadOnly(aWindow) {
|
|
|
|
return aWindow.Promise.reject(
|
|
|
|
new aWindow.DOMError("ReadOnlyError", "DataStore in readonly mode"));
|
|
|
|
}
|
|
|
|
|
2013-12-19 01:07:21 -08:00
|
|
|
function validateId(aId) {
|
|
|
|
// If string, it cannot be empty.
|
|
|
|
if (typeof(aId) == 'string') {
|
|
|
|
return aId.length;
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
|
2013-12-19 01:07:21 -08:00
|
|
|
aId = parseInt(aId);
|
|
|
|
return (!isNaN(aId) && aId > 0);
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
/* DataStore object */
|
2014-06-03 07:36:46 -07:00
|
|
|
function DataStore() {
|
2013-10-02 10:27:15 -07:00
|
|
|
debug("DataStore created");
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
DataStore.prototype = {
|
2013-10-02 10:27:15 -07:00
|
|
|
classDescription: "DataStore XPCOM Component",
|
|
|
|
classID: Components.ID("{db5c9602-030f-4bff-a3de-881a8de370f2}"),
|
2013-12-20 00:17:49 -08:00
|
|
|
contractID: "@mozilla.org/dom/datastore-impl;1",
|
2014-06-03 07:36:46 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDataStore, Ci.nsISupports,
|
|
|
|
Ci.nsIObserver]),
|
2013-10-02 10:27:15 -07:00
|
|
|
|
|
|
|
callbacks: [],
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
_window: null,
|
|
|
|
_name: null,
|
|
|
|
_owner: null,
|
|
|
|
_readOnly: null,
|
|
|
|
_revisionId: null,
|
2013-10-02 13:39:54 -07:00
|
|
|
_exposedObject: null,
|
|
|
|
_cursor: null,
|
2014-03-04 08:24:32 -08:00
|
|
|
_shuttingdown: false,
|
2013-12-20 00:17:49 -08:00
|
|
|
_eventTarget: null,
|
2013-10-02 10:27:15 -07:00
|
|
|
|
|
|
|
init: function(aWindow, aName, aOwner, aReadOnly) {
|
|
|
|
debug("DataStore init");
|
|
|
|
|
|
|
|
this._window = aWindow;
|
|
|
|
this._name = aName;
|
|
|
|
this._owner = aOwner;
|
|
|
|
this._readOnly = aReadOnly;
|
|
|
|
|
|
|
|
this._db = new DataStoreDB();
|
|
|
|
this._db.init(aOwner, aName);
|
|
|
|
|
2014-03-19 06:03:11 -07:00
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", false);
|
2013-10-02 10:27:15 -07:00
|
|
|
|
|
|
|
let util = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
this._innerWindowID = util.currentInnerWindowID;
|
|
|
|
|
|
|
|
cpmm.addMessageListener("DataStore:Changed:Return:OK", this);
|
|
|
|
cpmm.sendAsyncMessage("DataStore:RegisterForMessages",
|
2014-06-14 00:05:47 -07:00
|
|
|
{ store: this._name, owner: this._owner,
|
2014-06-19 07:03:54 -07:00
|
|
|
innerWindowID: this._innerWindowID },
|
|
|
|
null,
|
|
|
|
this._window.document.nodePrincipal);
|
2013-10-02 10:27:15 -07:00
|
|
|
},
|
|
|
|
|
2014-03-19 06:03:11 -07:00
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
let wId = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (wId == this._innerWindowID) {
|
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
|
|
|
|
|
|
|
cpmm.removeMessageListener("DataStore:Changed:Return:OK", this);
|
2014-06-14 00:05:47 -07:00
|
|
|
cpmm.sendAsyncMessage("DataStore:UnregisterForMessages",
|
2014-06-19 07:03:54 -07:00
|
|
|
{ innerWindowID: this._innerWindowID },
|
|
|
|
null,
|
|
|
|
this._window.document.nodePrincipal);
|
2014-03-19 06:03:11 -07:00
|
|
|
this._shuttingdown = true;
|
|
|
|
this._db.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-12-20 00:17:49 -08:00
|
|
|
setEventTarget: function(aEventTarget) {
|
|
|
|
this._eventTarget = aEventTarget;
|
|
|
|
},
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
newDBPromise: function(aTxnType, aFunction) {
|
|
|
|
let self = this;
|
|
|
|
return new this._window.Promise(function(aResolve, aReject) {
|
|
|
|
debug("DBPromise started");
|
|
|
|
self._db.txn(
|
|
|
|
aTxnType,
|
|
|
|
function(aTxn, aStore, aRevisionStore) {
|
|
|
|
debug("DBPromise success");
|
|
|
|
aFunction(aResolve, aReject, aTxn, aStore, aRevisionStore);
|
|
|
|
},
|
|
|
|
function(aEvent) {
|
|
|
|
debug("DBPromise error");
|
|
|
|
aReject(createDOMError(self._window, aEvent));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-04-07 13:08:11 -07:00
|
|
|
checkRevision: function(aReject, aRevisionStore, aRevisionId, aCallback) {
|
|
|
|
if (!aRevisionId) {
|
|
|
|
aCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
let request = aRevisionStore.openCursor(null, 'prev');
|
|
|
|
request.onsuccess = function(aEvent) {
|
|
|
|
let cursor = aEvent.target.result;
|
|
|
|
if (!cursor) {
|
|
|
|
dump("This cannot really happen.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursor.value.revisionId != aRevisionId) {
|
|
|
|
aReject(new self._window.DOMError("ConstraintError",
|
|
|
|
"RevisionId is not up-to-date"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aCallback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
getInternal: function(aStore, aIds, aCallback) {
|
|
|
|
debug("GetInternal: " + aIds.toSource());
|
|
|
|
|
|
|
|
// Creation of the results array.
|
2014-08-26 01:20:02 -07:00
|
|
|
let results = new this._window.Array(aIds.length);
|
2013-10-02 10:27:15 -07:00
|
|
|
|
|
|
|
// We're going to create this amount of requests.
|
|
|
|
let pendingIds = aIds.length;
|
|
|
|
let indexPos = 0;
|
|
|
|
|
2013-10-07 09:09:08 -07:00
|
|
|
let self = this;
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
function getInternalSuccess(aEvent, aPos) {
|
|
|
|
debug("GetInternal success. Record: " + aEvent.target.result);
|
2014-01-30 04:45:48 -08:00
|
|
|
results[aPos] = Cu.cloneInto(aEvent.target.result, self._window);
|
2013-10-02 10:27:15 -07:00
|
|
|
if (!--pendingIds) {
|
|
|
|
aCallback(results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (indexPos < aIds.length) {
|
|
|
|
// Just MAX_REQUESTS requests at the same time.
|
|
|
|
let count = 0;
|
|
|
|
while (indexPos < aIds.length && ++count < MAX_REQUESTS) {
|
|
|
|
getInternalRequest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInternalRequest() {
|
|
|
|
let currentPos = indexPos++;
|
|
|
|
let request = aStore.get(aIds[currentPos]);
|
|
|
|
request.onsuccess = function(aEvent) {
|
|
|
|
getInternalSuccess(aEvent, currentPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getInternalRequest();
|
|
|
|
},
|
|
|
|
|
2013-11-08 15:10:29 -08:00
|
|
|
putInternal: function(aResolve, aStore, aRevisionStore, aObj, aId) {
|
|
|
|
debug("putInternal " + aId);
|
2013-10-02 10:27:15 -07:00
|
|
|
|
|
|
|
let self = this;
|
|
|
|
let request = aStore.put(aObj, aId);
|
|
|
|
request.onsuccess = function(aEvent) {
|
2013-11-08 15:10:29 -08:00
|
|
|
debug("putInternal success");
|
2013-10-02 10:27:15 -07:00
|
|
|
|
|
|
|
self.addRevision(aRevisionStore, aId, REVISION_UPDATED,
|
|
|
|
function() {
|
2013-11-08 15:10:29 -08:00
|
|
|
debug("putInternal - revisionId increased");
|
2013-10-02 10:27:15 -07:00
|
|
|
// No wrap here because the result is always a int.
|
|
|
|
aResolve(aEvent.target.result);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2013-11-08 15:10:29 -08:00
|
|
|
addInternal: function(aResolve, aStore, aRevisionStore, aObj, aId) {
|
2013-10-02 10:27:15 -07:00
|
|
|
debug("AddInternal");
|
|
|
|
|
|
|
|
let self = this;
|
2013-11-08 15:10:29 -08:00
|
|
|
let request = aStore.add(aObj, aId);
|
2013-10-02 10:27:15 -07:00
|
|
|
request.onsuccess = function(aEvent) {
|
|
|
|
debug("Request successful. Id: " + aEvent.target.result);
|
|
|
|
self.addRevision(aRevisionStore, aEvent.target.result, REVISION_ADDED,
|
|
|
|
function() {
|
|
|
|
debug("AddInternal - revisionId increased");
|
|
|
|
// No wrap here because the result is always a int.
|
|
|
|
aResolve(aEvent.target.result);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
removeInternal: function(aResolve, aStore, aRevisionStore, aId) {
|
|
|
|
debug("RemoveInternal");
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
let request = aStore.get(aId);
|
|
|
|
request.onsuccess = function(aEvent) {
|
|
|
|
debug("RemoveInternal success. Record: " + aEvent.target.result);
|
|
|
|
if (aEvent.target.result === undefined) {
|
|
|
|
aResolve(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let deleteRequest = aStore.delete(aId);
|
|
|
|
deleteRequest.onsuccess = function() {
|
|
|
|
debug("RemoveInternal success");
|
|
|
|
self.addRevision(aRevisionStore, aId, REVISION_REMOVED,
|
|
|
|
function() {
|
|
|
|
aResolve(true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
clearInternal: function(aResolve, aStore, aRevisionStore) {
|
|
|
|
debug("ClearInternal");
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
let request = aStore.clear();
|
|
|
|
request.onsuccess = function() {
|
|
|
|
debug("ClearInternal success");
|
|
|
|
self._db.clearRevisions(aRevisionStore,
|
|
|
|
function() {
|
|
|
|
debug("Revisions cleared");
|
|
|
|
|
2014-04-28 02:54:38 -07:00
|
|
|
self.addRevision(aRevisionStore, null, REVISION_VOID,
|
2013-10-02 10:27:15 -07:00
|
|
|
function() {
|
|
|
|
debug("ClearInternal - revisionId increased");
|
|
|
|
aResolve();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getLengthInternal: function(aResolve, aStore) {
|
|
|
|
debug("GetLengthInternal");
|
|
|
|
|
|
|
|
let request = aStore.count();
|
|
|
|
request.onsuccess = function(aEvent) {
|
|
|
|
debug("GetLengthInternal success: " + aEvent.target.result);
|
|
|
|
// No wrap here because the result is always a int.
|
|
|
|
aResolve(aEvent.target.result);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
addRevision: function(aRevisionStore, aId, aType, aSuccessCb) {
|
|
|
|
let self = this;
|
|
|
|
this._db.addRevision(aRevisionStore, aId, aType,
|
|
|
|
function(aRevisionId) {
|
|
|
|
self._revisionId = aRevisionId;
|
|
|
|
self.sendNotification(aId, aType, aRevisionId);
|
|
|
|
aSuccessCb();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
retrieveRevisionId: function(aSuccessCb) {
|
|
|
|
let self = this;
|
|
|
|
this._db.revisionTxn(
|
2013-10-02 10:27:23 -07:00
|
|
|
'readonly',
|
2013-10-02 10:27:15 -07:00
|
|
|
function(aTxn, aRevisionStore) {
|
|
|
|
debug("RetrieveRevisionId transaction success");
|
|
|
|
|
|
|
|
let request = aRevisionStore.openCursor(null, 'prev');
|
|
|
|
request.onsuccess = function(aEvent) {
|
|
|
|
let cursor = aEvent.target.result;
|
2013-10-02 10:27:23 -07:00
|
|
|
if (cursor) {
|
|
|
|
self._revisionId = cursor.value.revisionId;
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
2013-10-02 10:27:15 -07:00
|
|
|
|
2013-10-02 10:27:23 -07:00
|
|
|
aSuccessCb(self._revisionId);
|
2013-10-02 10:27:15 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
sendNotification: function(aId, aOperation, aRevisionId) {
|
|
|
|
debug("SendNotification");
|
2014-01-03 13:06:19 -08:00
|
|
|
if (aOperation == REVISION_VOID) {
|
|
|
|
aOperation = "cleared";
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
2014-01-03 13:06:19 -08:00
|
|
|
|
|
|
|
cpmm.sendAsyncMessage("DataStore:Changed",
|
2014-02-27 23:34:38 -08:00
|
|
|
{ store: this.name, owner: this._owner,
|
2014-01-03 13:06:19 -08:00
|
|
|
message: { revisionId: aRevisionId, id: aId,
|
2014-06-19 07:03:54 -07:00
|
|
|
operation: aOperation, owner: this._owner } },
|
|
|
|
null,
|
|
|
|
this._window.document.nodePrincipal);
|
2013-10-02 10:27:15 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
debug("receiveMessage");
|
|
|
|
|
|
|
|
if (aMessage.name != "DataStore:Changed:Return:OK") {
|
|
|
|
debug("Wrong message: " + aMessage.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-26 09:38:38 -08:00
|
|
|
// If this message is not for this DataStore, let's ignore it.
|
|
|
|
if (aMessage.data.owner != this._owner ||
|
|
|
|
aMessage.data.store != this._name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
let self = this;
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
this.retrieveRevisionId(
|
2013-10-02 10:27:15 -07:00
|
|
|
function() {
|
2014-03-04 08:24:32 -08:00
|
|
|
// If the window has been destroyed we don't emit the events.
|
|
|
|
if (self._shuttingdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:39:54 -07:00
|
|
|
// If we have an active cursor we don't emit events.
|
|
|
|
if (self._cursor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-26 09:38:38 -08:00
|
|
|
let event = new self._window.DataStoreChangeEvent('change',
|
|
|
|
aMessage.data.message);
|
2013-12-20 00:17:49 -08:00
|
|
|
self._eventTarget.dispatchEvent(event);
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
2013-10-02 10:27:15 -07:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2013-10-02 13:39:54 -07:00
|
|
|
get exposedObject() {
|
|
|
|
debug("get exposedObject");
|
|
|
|
return this._exposedObject;
|
|
|
|
},
|
|
|
|
|
|
|
|
set exposedObject(aObject) {
|
|
|
|
debug("set exposedObject");
|
|
|
|
this._exposedObject = aObject;
|
|
|
|
},
|
|
|
|
|
|
|
|
syncTerminated: function(aCursor) {
|
|
|
|
// This checks is to avoid that an invalid cursor stops a sync.
|
|
|
|
if (this._cursor == aCursor) {
|
|
|
|
this._cursor = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
// Public interface :
|
|
|
|
|
|
|
|
get name() {
|
2013-10-02 10:27:15 -07:00
|
|
|
return this._name;
|
2013-10-02 10:27:15 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get owner() {
|
2013-10-02 10:27:15 -07:00
|
|
|
return this._owner;
|
2013-10-02 10:27:15 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get readOnly() {
|
2013-10-02 10:27:15 -07:00
|
|
|
return this._readOnly;
|
2013-10-02 10:27:15 -07:00
|
|
|
},
|
|
|
|
|
2013-12-19 01:07:21 -08:00
|
|
|
get: function() {
|
|
|
|
let ids = Array.prototype.slice.call(arguments);
|
|
|
|
for (let i = 0; i < ids.length; ++i) {
|
|
|
|
if (!validateId(ids[i])) {
|
|
|
|
return throwInvalidArg(this._window);
|
|
|
|
}
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
|
2014-08-26 01:20:02 -07:00
|
|
|
if (ids.length == 0) {
|
|
|
|
return this._window.Promise.resolve(new this._window.Array());
|
|
|
|
}
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
let self = this;
|
|
|
|
|
|
|
|
// Promise<Object>
|
2013-10-02 10:27:15 -07:00
|
|
|
return this.newDBPromise("readonly",
|
2013-10-02 10:27:15 -07:00
|
|
|
function(aResolve, aReject, aTxn, aStore, aRevisionStore) {
|
2013-12-19 01:07:21 -08:00
|
|
|
self.getInternal(aStore, ids,
|
2013-10-02 10:27:15 -07:00
|
|
|
function(aResults) {
|
2013-12-19 01:07:21 -08:00
|
|
|
aResolve(ids.length > 1 ? aResults : aResults[0]);
|
2013-10-02 10:27:15 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-04-07 13:08:11 -07:00
|
|
|
put: function(aObj, aId, aRevisionId) {
|
2013-12-19 01:07:21 -08:00
|
|
|
if (!validateId(aId)) {
|
2013-10-02 10:27:15 -07:00
|
|
|
return throwInvalidArg(this._window);
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
if (this._readOnly) {
|
|
|
|
return throwReadOnly(this._window);
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
// Promise<void>
|
2013-10-02 10:27:15 -07:00
|
|
|
return this.newDBPromise("readwrite",
|
2013-10-02 10:27:15 -07:00
|
|
|
function(aResolve, aReject, aTxn, aStore, aRevisionStore) {
|
2014-04-07 13:08:11 -07:00
|
|
|
self.checkRevision(aReject, aRevisionStore, aRevisionId, function() {
|
|
|
|
self.putInternal(aResolve, aStore, aRevisionStore, aObj, aId);
|
|
|
|
});
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-04-07 13:08:11 -07:00
|
|
|
add: function(aObj, aId, aRevisionId) {
|
2013-11-08 15:10:29 -08:00
|
|
|
if (aId) {
|
2013-12-19 01:07:21 -08:00
|
|
|
if (!validateId(aId)) {
|
2013-11-08 15:10:29 -08:00
|
|
|
return throwInvalidArg(this._window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
if (this._readOnly) {
|
|
|
|
return throwReadOnly(this._window);
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
// Promise<int>
|
2013-10-02 10:27:15 -07:00
|
|
|
return this.newDBPromise("readwrite",
|
2013-10-02 10:27:15 -07:00
|
|
|
function(aResolve, aReject, aTxn, aStore, aRevisionStore) {
|
2014-04-07 13:08:11 -07:00
|
|
|
self.checkRevision(aReject, aRevisionStore, aRevisionId, function() {
|
|
|
|
self.addInternal(aResolve, aStore, aRevisionStore, aObj, aId);
|
|
|
|
});
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-04-07 13:08:11 -07:00
|
|
|
remove: function(aId, aRevisionId) {
|
2013-12-19 01:07:21 -08:00
|
|
|
if (!validateId(aId)) {
|
2013-10-02 10:27:15 -07:00
|
|
|
return throwInvalidArg(this._window);
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
|
2013-10-02 10:27:15 -07:00
|
|
|
if (this._readOnly) {
|
|
|
|
return throwReadOnly(this._window);
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
// Promise<void>
|
2013-10-02 10:27:15 -07:00
|
|
|
return this.newDBPromise("readwrite",
|
2013-10-02 10:27:15 -07:00
|
|
|
function(aResolve, aReject, aTxn, aStore, aRevisionStore) {
|
2014-04-07 13:08:11 -07:00
|
|
|
self.checkRevision(aReject, aRevisionStore, aRevisionId, function() {
|
|
|
|
self.removeInternal(aResolve, aStore, aRevisionStore, aId);
|
|
|
|
});
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-04-07 13:08:11 -07:00
|
|
|
clear: function(aRevisionId) {
|
2013-10-02 10:27:15 -07:00
|
|
|
if (this._readOnly) {
|
|
|
|
return throwReadOnly(this._window);
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
// Promise<void>
|
2013-10-02 10:27:15 -07:00
|
|
|
return this.newDBPromise("readwrite",
|
2013-10-02 10:27:15 -07:00
|
|
|
function(aResolve, aReject, aTxn, aStore, aRevisionStore) {
|
2014-04-07 13:08:11 -07:00
|
|
|
self.checkRevision(aReject, aRevisionStore, aRevisionId, function() {
|
|
|
|
self.clearInternal(aResolve, aStore, aRevisionStore);
|
|
|
|
});
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
get revisionId() {
|
2013-10-02 10:27:15 -07:00
|
|
|
return this._revisionId;
|
2013-10-02 10:27:15 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
getLength: function() {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
// Promise<int>
|
2013-10-02 10:27:15 -07:00
|
|
|
return this.newDBPromise("readonly",
|
2013-10-02 10:27:15 -07:00
|
|
|
function(aResolve, aReject, aTxn, aStore, aRevisionStore) {
|
2013-10-02 10:27:15 -07:00
|
|
|
self.getLengthInternal(aResolve, aStore);
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2013-10-02 13:39:54 -07:00
|
|
|
sync: function(aRevisionId) {
|
|
|
|
debug("Sync");
|
|
|
|
this._cursor = new DataStoreCursor(this._window, this, aRevisionId);
|
2013-12-20 00:17:49 -08:00
|
|
|
|
|
|
|
let cursorImpl = this._window.DataStoreCursorImpl.
|
|
|
|
_create(this._window, this._cursor);
|
|
|
|
|
|
|
|
let exposedCursor = new this._window.DataStoreCursor();
|
|
|
|
exposedCursor.setDataStoreCursorImpl(cursorImpl);
|
|
|
|
return exposedCursor;
|
2013-10-02 10:27:15 -07:00
|
|
|
}
|
|
|
|
};
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DataStore]);
|