Bug 758273 - Device Storage - enumeration option to filter based on last modification date. r=smaug

This commit is contained in:
Doug Turner 2012-06-08 20:15:04 -07:00
parent 5b8b65b151
commit 6a10675539
9 changed files with 314 additions and 38 deletions

View File

@ -16,6 +16,7 @@
#include "nsIPrincipal.h"
#include "mozilla/Preferences.h"
#include "nsJSUtils.h"
#include "DictionaryHelpers.h"
using namespace mozilla::dom;
@ -307,7 +308,8 @@ public:
nsDOMDeviceStorageCursor(nsIDOMWindow* aWindow,
nsIURI* aURI,
DeviceStorageFile* aFile,
bool aEditable);
bool aEditable,
PRUint64 aSince);
private:
~nsDOMDeviceStorageCursor();
@ -319,6 +321,7 @@ protected:
nsRefPtr<DeviceStorageFile> mFile;
nsCOMPtr<nsIURI> mURI;
bool mEditable;
PRUint64 mSince;
// to access mFiles
friend class InitCursorEvent;
@ -508,6 +511,15 @@ public:
nsCOMPtr<nsIFile> f;
while (NS_SUCCEEDED(files->GetNextFile(getter_AddRefs(f))) && f) {
nsDOMDeviceStorageCursor* cursor = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
PRInt64 msecs;
f->GetLastModifiedTime(&msecs);
if (msecs < (PRInt64) cursor->mSince) {
continue;
}
bool isDir;
f->IsDirectory(&isDir);
@ -534,7 +546,6 @@ public:
collectFiles(dsf);
}
else if (isFile) {
nsDOMDeviceStorageCursor* cursor = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
cursor->mFiles.AppendElement(dsf);
}
}
@ -561,12 +572,14 @@ NS_IMPL_RELEASE_INHERITED(nsDOMDeviceStorageCursor, DOMRequest)
nsDOMDeviceStorageCursor::nsDOMDeviceStorageCursor(nsIDOMWindow* aWindow,
nsIURI* aURI,
DeviceStorageFile* aFile,
bool aEditable)
bool aEditable,
PRUint64 aSince)
: DOMRequest(aWindow)
, mOkToCallContinue(false)
, mFile(aFile)
, mURI(aURI)
, mEditable(aEditable)
, mSince(aSince)
{
}
@ -1153,7 +1166,7 @@ nsDOMDeviceStorage::AddNamed(nsIDOMBlob *aBlob,
}
NS_IMETHODIMP
nsDOMDeviceStorage::Get(const JS::Value & aPath,
nsDOMDeviceStorage::Get(const JS::Value& aPath,
JSContext* aCx,
nsIDOMDOMRequest * *_retval NS_OUTPARAM)
{
@ -1161,7 +1174,7 @@ nsDOMDeviceStorage::Get(const JS::Value & aPath,
}
NS_IMETHODIMP
nsDOMDeviceStorage::GetEditable(const JS::Value & aPath,
nsDOMDeviceStorage::GetEditable(const JS::Value& aPath,
JSContext* aCx,
nsIDOMDOMRequest * *_retval NS_OUTPARAM)
{
@ -1169,7 +1182,7 @@ nsDOMDeviceStorage::GetEditable(const JS::Value & aPath,
}
nsresult
nsDOMDeviceStorage::GetInternal(const JS::Value & aPath,
nsDOMDeviceStorage::GetInternal(const JS::Value& aPath,
JSContext* aCx,
nsIDOMDOMRequest * *_retval NS_OUTPARAM,
bool aEditable)
@ -1208,7 +1221,7 @@ nsDOMDeviceStorage::GetInternal(const JS::Value & aPath,
}
NS_IMETHODIMP
nsDOMDeviceStorage::Delete(const JS::Value & aPath, JSContext* aCx, nsIDOMDOMRequest * *_retval NS_OUTPARAM)
nsDOMDeviceStorage::Delete(const JS::Value& aPath, JSContext* aCx, nsIDOMDOMRequest * *_retval NS_OUTPARAM)
{
nsCOMPtr<nsIRunnable> r;
@ -1243,35 +1256,85 @@ nsDOMDeviceStorage::Delete(const JS::Value & aPath, JSContext* aCx, nsIDOMDOMReq
}
NS_IMETHODIMP
nsDOMDeviceStorage::Enumerate(const nsAString & aPath,
nsIDOMDeviceStorageCursor * *_retval NS_OUTPARAM)
nsDOMDeviceStorage::Enumerate(const JS::Value& aName,
const JS::Value& aOptions,
JSContext* aCx,
PRUint8 aArgc,
nsIDOMDeviceStorageCursor** aRetval)
{
return EnumerateInternal(aPath, _retval, false);
return EnumerateInternal(aName, aOptions, aCx, aArgc, false, aRetval);
}
NS_IMETHODIMP
nsDOMDeviceStorage::EnumerateEditable(const nsAString & aPath,
nsIDOMDeviceStorageCursor * *_retval NS_OUTPARAM)
nsDOMDeviceStorage::EnumerateEditable(const JS::Value& aName,
const JS::Value& aOptions,
JSContext* aCx,
PRUint8 aArgc,
nsIDOMDeviceStorageCursor** aRetval)
{
return EnumerateInternal(aPath, _retval, true);
return EnumerateInternal(aName, aOptions, aCx, aArgc, true, aRetval);
}
static PRTime
ExtractDateFromOptions(JSContext* aCx, const JS::Value& aOptions)
{
PRTime result = 0;
DeviceStorageEnumerationParameters params;
if (!JSVAL_IS_VOID(aOptions) && !aOptions.isNull()) {
nsresult rv = params.Init(aCx, &aOptions);
if (NS_SUCCEEDED(rv) && !JSVAL_IS_VOID(params.since) && !params.since.isNull() && params.since.isObject()) {
JSObject* obj = JSVAL_TO_OBJECT(params.since);
if (JS_ObjectIsDate(aCx, obj) && js_DateIsValid(aCx, obj)) {
result = js_DateGetMsecSinceEpoch(aCx, obj);
}
}
}
return result;
}
nsresult
nsDOMDeviceStorage::EnumerateInternal(const nsAString & aPath,
nsIDOMDeviceStorageCursor * *_retval NS_OUTPARAM,
bool aEditable)
nsDOMDeviceStorage::EnumerateInternal(const JS::Value& aName,
const JS::Value& aOptions,
JSContext* aCx,
PRUint8 aArgc,
bool aEditable,
nsIDOMDeviceStorageCursor** aRetval)
{
nsCOMPtr<nsPIDOMWindow> win = do_QueryReferent(mOwner);
if (!win)
return NS_ERROR_UNEXPECTED;
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mFile, aPath);
nsRefPtr<nsDOMDeviceStorageCursor> cursor = new nsDOMDeviceStorageCursor(win, mURI, dsf, aEditable);
NS_ADDREF(*_retval = cursor);
PRTime since = 0;
nsString path;
path.SetIsVoid(true);
if (aArgc > 0) {
// inspect the first value to see if it is a string
if (JSVAL_IS_STRING(aName)) {
JSString* jsstr = JS_ValueToString(aCx, aName);
nsDependentJSString jspath;
jspath.init(aCx, jsstr);
path.Assign(jspath);
} else if (!JSVAL_IS_PRIMITIVE(aName)) {
// it also might be an options object
since = ExtractDateFromOptions(aCx, aName);
} else {
return NS_ERROR_FAILURE;
}
if (aArgc == 2 && (JSVAL_IS_VOID(aOptions) || aOptions.isNull() || !aOptions.isObject())) {
return NS_ERROR_FAILURE;
}
since = ExtractDateFromOptions(aCx, aOptions);
}
nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mFile, path);
nsRefPtr<nsDOMDeviceStorageCursor> cursor = new nsDOMDeviceStorageCursor(win, mURI, dsf, aEditable, since);
nsRefPtr<DeviceStorageCursorRequest> r = new DeviceStorageCursorRequest(cursor);
NS_ADDREF(*aRetval = cursor);
if (mozilla::Preferences::GetBool("device.storage.prompt.testing", false)) {
r->Allow();
return NS_OK;

View File

@ -28,18 +28,29 @@ public:
nsDOMDeviceStorage();
nsresult Init(nsPIDOMWindow* aWindow, const nsAString &aType, const PRInt32 aIndex);
nsresult Init(nsPIDOMWindow* aWindow, const nsAString&aType, const PRInt32 aIndex);
PRInt32 SetRootFileForType(const nsAString& aType, const PRInt32 aIndex);
static void CreateDeviceStoragesFor(nsPIDOMWindow* aWin, const nsAString &aType, nsIVariant** _retval);
static void CreateDeviceStoragesFor(nsPIDOMWindow* aWin,
const nsAString& aType,
nsIVariant** aRetval);
private:
~nsDOMDeviceStorage();
nsresult GetInternal(const JS::Value & aName, JSContext* aCx, nsIDOMDOMRequest * *_retval NS_OUTPARAM, bool aEditable);
nsresult EnumerateInternal(const nsAString & aName, nsIDOMDeviceStorageCursor * *_retval NS_OUTPARAM, bool aEditable);
nsresult GetInternal(const JS::Value& aName,
JSContext* aCx,
nsIDOMDOMRequest** aRetval,
bool aEditable);
nsresult EnumerateInternal(const JS::Value& aName,
const JS::Value& aOptions,
JSContext* aCx,
PRUint8 aArgc,
bool aEditable,
nsIDOMDeviceStorageCursor** aRetval);
PRInt32 mStorageType;
nsCOMPtr<nsIFile> mFile;

View File

@ -7,7 +7,12 @@ interface nsIDOMBlob;
interface nsIDOMDOMRequest;
interface nsIDOMDeviceStorageCursor;
[scriptable, uuid(05C0D0C8-D698-4CCD-899C-7198A33BD7EC)]
dictionary DeviceStorageEnumerationParameters
{
jsval since;
};
[scriptable, uuid(6cdf3d8e-4a9f-4582-9d99-769bf9922cfb)]
interface nsIDOMDeviceStorage : nsISupports
{
/*
@ -28,7 +33,9 @@ interface nsIDOMDeviceStorage : nsISupports
[implicit_jscontext]
nsIDOMDOMRequest delete(in jsval aName);
nsIDOMDeviceStorageCursor enumerate([optional] in DOMString directory);
[optional_argc, implicit_jscontext]
nsIDOMDeviceStorageCursor enumerate([optional] in jsval aName, /* DeviceStorageEnumerationParameters */ [optional] in jsval options);
nsIDOMDeviceStorageCursor enumerateEditable([optional] in DOMString directory);
[optional_argc, implicit_jscontext]
nsIDOMDeviceStorageCursor enumerateEditable([optional] in jsval aName, /* DeviceStorageEnumerationParameters */ [optional] in jsval options);
};

View File

@ -19,6 +19,8 @@ _TEST_FILES = \
test_enumerateMultipleContinue.html \
test_overwrite.html \
test_dotdot.html \
test_enumerateOptions.html \
test_lastModificationFilter.html \
devicestorage_common.js \
$(NULL)

View File

@ -5,6 +5,13 @@
var oldVal = false;
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
function devicestorage_setup() {
SimpleTest.waitForExplicitFinish();
try {
@ -46,3 +53,7 @@ function randomFilename(l) {
return result;
}
function reportErrorAndQuit(e) {
ok(false, "handleError was called : " + e.target.error.name);
devicestorage_cleanup();
}

View File

@ -22,27 +22,16 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=717103
<pre id="test">
<script class="testbody" type="text/javascript">
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
devicestorage_setup();
function enumerateSuccess(e) {
if (e.target.result == null) {
ok(files.length == 0, "when the enumeration is done, we shouldn't have any files in this array")
dump("We still have length = " + files.length);
devicestorage_cleanup();
return;
}
dump("asdfasdf"+ e.target.result + "\n");
dump("asdfasdf"+ e.target.result.name + "\n");
var filename = e.target.result.name;
var index = files.indexOf(filename);

View File

@ -0,0 +1,81 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=717103
-->
<head>
<title>Test for basic sanity of the device storage API </title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="devicestorage_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=717103">Mozilla Bug 717103</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
devicestorage_setup()
storage = navigator.getDeviceStorage("profile");
throws = false;
try {
var cursor = storage[0].enumerate();
} catch(e) {throws = true}
ok(!throws, "enumerate no parameter");
throws = false;
try {
var cursor = storage[0].enumerate("string");
} catch(e) {throws = true}
ok(!throws, "enumerate one string parameter");
throws = false;
try {
var cursor = storage[0].enumerate("string", "string2");
} catch(e) {throws = true}
ok(throws, "enumerate two string parameter");
throws = false;
try {
var cursor = storage[0].enumerate("string", {"since": 1});
} catch(e) {throws = true}
ok(!throws, "enumerate a string and object parameter");
throws = false;
try {
var cursor = storage[0].enumerate({"path": "a"});
} catch(e) {throws = true}
ok(!throws, "enumerate object parameter with path");
throws = false;
try {
var cursor = storage[0].enumerate({}, "string");
} catch(e) {throws = true}
ok(throws, "enumerate object then a string");
throws = false;
try {
var cursor = storage[0].enumerate({"path": "a", "since": 0});
} catch(e) {throws = true}
ok(!throws, "enumerate object parameter with path");
devicestorage_cleanup()
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,111 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html> <!--
https://bugzilla.mozilla.org/show_bug.cgi?id=717103
-->
<head>
<title>Test for the device storage API </title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="devicestorage_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=717103">Mozilla Bug 717103</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
function verifyAndDelete(prefix, files, e) {
if (e.target.result == null) {
ok(files.length == 0, "when the enumeration is done, we shouldn't have any files in this array")
dump("We still have length = " + files.length + "\n");
dump(files + "\n");
devicestorage_cleanup();
return;
}
var filename = e.target.result.name;
var index = files.indexOf(filename);
ok(index > -1, "filename should be in the enumeration : " + e.target.result.name);
if (index == -1)
return;
files.remove(index);
// clean up
var storage = navigator.getDeviceStorage("profile");
var cleanup = storage[0].delete(prefix + "/" + filename);
cleanup.onsuccess = function(e) {}
}
function addFiles(prefix, files, date, callback) {
const Cc = SpecialPowers.wrap(Components).classes;
const Ci = Components.interfaces;
var directoryService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
for (var i=0; i<files.length; i++) {
var f = directoryService.get("ProfD", Components.interfaces.nsIFile);
var path = prefix + '/' + files[i];
path.split("/").forEach(function(p) {
f.appendRelativePath(p);
});
f.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0644);
f.lastModifiedTime = date;
}
callback();
}
devicestorage_setup();
var prefix = "devicestorage/" + randomFilename(12)
var oldFiles = ["a", "b", "c"];
var newFiles = ["d", "e", "f"];
// 157795200 is a long long time ago.
addFiles(prefix, oldFiles, 157795200, addNewFiles);
function enumerateNew() {
var storage = navigator.getDeviceStorage("profile");
ok(navigator.getDeviceStorage, "Should have getDeviceStorage");
// 836031600 is a long time ago
var cursor = storage[0].enumerate(prefix, {"since": new Date(836031600)});
cursor.onsuccess = function(e) {
verifyAndDelete(prefix, newFiles, e);
if (e.target.result) {
e.target.continue();
}
}
cursor.onerror = function (e) {
ok(false, "handleError was called : " + e.target.error.name);
devicestorage_cleanup();
}
}
function addNewFiles() {
addFiles(prefix, newFiles, Date.now(), enumerateNew);
}
</script>
</pre>
</body>
</html>

View File

@ -24,6 +24,7 @@ dictionaries = [
[ 'DeviceLightEventInit', 'nsIDOMDeviceLightEvent.idl' ],
[ 'DOMFileMetadataParameters', 'nsIDOMLockedFile.idl' ],
[ 'XMLHttpRequestParameters', 'nsIXMLHttpRequest.idl' ],
[ 'DeviceStorageEnumerationParameters', 'nsIDOMDeviceStorage.idl' ]
]
# include file names