Bug 754350 - Clean up Device Storage error strings. r=sicking
authorDoug Turner <dougt@dougt.org>
Sat, 13 Oct 2012 08:20:14 -0700
changeset 110517 838eee8853cafbbdf0fbe40c9584108fb8aed6ad
parent 110516 0674f4f1703ad217f20f7dd6571d6630066a7fb9
child 110518 484a0c8aacea19045ad581e80d2c7b0d651140a3
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewerssicking
bugs754350
milestone19.0a1
Bug 754350 - Clean up Device Storage error strings. r=sicking
dom/devicestorage/nsDeviceStorage.cpp
dom/devicestorage/nsDeviceStorage.h
dom/devicestorage/test/test_addCorrectType.html
dom/devicestorage/test/test_basic.html
dom/devicestorage/test/test_dotdot.html
dom/devicestorage/test/test_overwrite.html
--- a/dom/devicestorage/nsDeviceStorage.cpp
+++ b/dom/devicestorage/nsDeviceStorage.cpp
@@ -846,54 +846,30 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DeviceStorageCursorRequest)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mCursor, nsIDOMDeviceStorageCursor)
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 
 class PostErrorEvent : public nsRunnable
 {
 public:
-  PostErrorEvent(nsRefPtr<DOMRequest>& aRequest, const char* aMessage, DeviceStorageFile* aFile)
+  PostErrorEvent(nsRefPtr<DOMRequest>& aRequest, const char* aMessage)
   {
     mRequest.swap(aRequest);
-    BuildErrorString(aMessage, aFile);
+    CopyASCIItoUTF16(aMessage, mError);
   }
 
-  PostErrorEvent(DOMRequest* aRequest, const char* aMessage, DeviceStorageFile* aFile)
+  PostErrorEvent(DOMRequest* aRequest, const char* aMessage)
     : mRequest(aRequest)
   {
-    BuildErrorString(aMessage, aFile);
+    CopyASCIItoUTF16(aMessage, mError);
   }
 
   ~PostErrorEvent() {}
 
-  void BuildErrorString(const char* aMessage, DeviceStorageFile* aFile)
-  {
-    nsAutoString fullPath;
-
-    if (aFile && aFile->mFile) {
-      aFile->mFile->GetPath(fullPath);
-    }
-    else {
-      fullPath.Assign(NS_LITERAL_STRING("null file"));
-    }
-
-    mError = NS_ConvertASCIItoUTF16(aMessage);
-    mError.Append(NS_LITERAL_STRING(" file path = "));
-    mError.Append(fullPath.get());
-    mError.Append(NS_LITERAL_STRING(" path = "));
-
-    if (aFile) {
-      mError.Append(aFile->mPath);
-    }
-    else {
-      mError.Append(NS_LITERAL_STRING("null path"));
-    }
-  }
-
   NS_IMETHOD Run()
   {
     NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
     mRequest->FireError(mError);
     mRequest = nullptr;
     return NS_OK;
   }
 
@@ -1001,19 +977,17 @@ public:
   ~InitCursorEvent() {}
 
   NS_IMETHOD Run() {
     NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
 
     bool check;
     mFile->mFile->IsDirectory(&check);
     if (!check) {
-      nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest,
-                                                          POST_ERROR_EVENT_FILE_NOT_ENUMERABLE,
-                                                          mFile);
+      nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest, POST_ERROR_EVENT_FILE_NOT_ENUMERABLE);
       NS_DispatchToMainThread(event);
       return NS_OK;
     }
 
     nsDOMDeviceStorageCursor* cursor = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
     mFile->CollectFiles(cursor->mFiles, cursor->mSince);
 
     nsCOMPtr<ContinueCursorEvent> event = new ContinueCursorEvent(mRequest);
@@ -1088,30 +1062,26 @@ nsDOMDeviceStorageCursor::GetElement(nsI
 {
   *aRequestingElement = nullptr;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDOMDeviceStorageCursor::Cancel()
 {
-  nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(this,
-                                                      POST_ERROR_EVENT_PERMISSION_DENIED,
-                                                      mFile);
+  nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(this, POST_ERROR_EVENT_PERMISSION_DENIED);
   NS_DispatchToMainThread(event);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDOMDeviceStorageCursor::Allow()
 {
   if (!mFile->IsSafePath()) {
-    nsCOMPtr<nsIRunnable> r = new PostErrorEvent(this,
-                                                 POST_ERROR_EVENT_ILLEGAL_FILE_NAME,
-                                                 mFile);
+    nsCOMPtr<nsIRunnable> r = new PostErrorEvent(this, POST_ERROR_EVENT_PERMISSION_DENIED);
     NS_DispatchToMainThread(r);
     return NS_OK;
   }
 
   if (XRE_GetProcessType() != GeckoProcessType_Default) {
 
     nsString fullpath;
     nsresult rv = mFile->mFile->GetPath(fullpath);
@@ -1283,31 +1253,27 @@ public:
     NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
 
     nsCOMPtr<nsIInputStream> stream;
     mBlob->GetInternalStream(getter_AddRefs(stream));
 
     bool check = false;
     mFile->mFile->Exists(&check);
     if (check) {
-      nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest,
-                                                          POST_ERROR_EVENT_FILE_EXISTS,
-                                                          mFile);
+      nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest, POST_ERROR_EVENT_FILE_EXISTS);
       NS_DispatchToMainThread(event);
       return NS_OK;
     }
 
     nsresult rv = mFile->Write(stream);
 
     if (NS_FAILED(rv)) {
       mFile->mFile->Remove(false);
 
-      nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest,
-                                                          POST_ERROR_EVENT_UNKNOWN,
-                                                          mFile);
+      nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest, POST_ERROR_EVENT_UNKNOWN);
       NS_DispatchToMainThread(event);
       return NS_OK;
     }
 
     nsCOMPtr<PostResultEvent> event = new PostResultEvent(mRequest, mFile->mPath);
     NS_DispatchToMainThread(event);
     return NS_OK;
   }
@@ -1334,17 +1300,17 @@ public:
   {
     NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
 
     nsRefPtr<nsRunnable> r;
     if (!mFile->mEditable) {
       bool check = false;
       mFile->mFile->Exists(&check);
       if (!check) {
-        r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST, mFile);
+        r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST);
       }
     }
 
     if (!r) {
       r = new PostResultEvent(mRequest, mFile);
     }
     NS_DispatchToMainThread(r);
     return NS_OK;
@@ -1371,17 +1337,17 @@ public:
   {
     NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
     mFile->Remove();
 
     nsRefPtr<nsRunnable> r;
     bool check = false;
     mFile->mFile->Exists(&check);
     if (check) {
-      r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST, mFile);
+      r = new PostErrorEvent(mRequest, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST);
     }
     else {
       r = new PostResultEvent(mRequest, mFile->mPath);
     }
     NS_DispatchToMainThread(r);
     return NS_OK;
   }
 
@@ -1532,19 +1498,17 @@ public:
   NS_IMETHOD GetElement(nsIDOMElement * *aRequestingElement)
   {
     *aRequestingElement = nullptr;
     return NS_OK;
   }
 
   NS_IMETHOD Cancel()
   {
-    nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest,
-                                                        POST_ERROR_EVENT_PERMISSION_DENIED,
-                                                        mFile);
+    nsCOMPtr<PostErrorEvent> event = new PostErrorEvent(mRequest, POST_ERROR_EVENT_PERMISSION_DENIED);
     NS_DispatchToMainThread(event);
     return NS_OK;
   }
 
   NS_IMETHOD Allow()
   {
     nsCOMPtr<nsIRunnable> r;
 
@@ -1842,22 +1806,21 @@ nsDOMDeviceStorage::AddNamed(nsIDOMBlob 
   }
 
   nsRefPtr<DOMRequest> request = new DOMRequest(win);
   NS_ADDREF(*_retval = request);
 
   nsCOMPtr<nsIRunnable> r;
 
   nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory, aPath);
-  if (!typeChecker->Check(mStorageType, dsf->mFile) ||
+  if (!dsf->IsSafePath()) {
+    r = new PostErrorEvent(request, POST_ERROR_EVENT_PERMISSION_DENIED);
+  } else if (!typeChecker->Check(mStorageType, dsf->mFile) ||
       !typeChecker->Check(mStorageType, aBlob)) {
-    r = new PostErrorEvent(request, POST_ERROR_EVENT_ILLEGAL_TYPE, dsf);
-  }
-  else if (!dsf->IsSafePath()) {
-    r = new PostErrorEvent(request, POST_ERROR_EVENT_ILLEGAL_FILE_NAME, dsf);
+    r = new PostErrorEvent(request, POST_ERROR_EVENT_ILLEGAL_TYPE);
   }
   else {
     r = new DeviceStorageRequest(DeviceStorageRequest::DEVICE_STORAGE_REQUEST_WRITE,
                                  win, mPrincipal, dsf, request, aBlob);
   }
 
   NS_DispatchToMainThread(r);
   return NS_OK;
@@ -1893,28 +1856,25 @@ nsDOMDeviceStorage::GetInternal(const JS
   nsRefPtr<DOMRequest> request = new DOMRequest(win);
   NS_ADDREF(*_retval = request);
 
   nsCOMPtr<nsIRunnable> r;
 
   JSString* jsstr = JS_ValueToString(aCx, aPath);
   nsDependentJSString path;
   if (!path.init(aCx, jsstr)) {
-    nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory);
-    r = new PostErrorEvent(request,
-                           POST_ERROR_EVENT_NON_STRING_TYPE_UNSUPPORTED,
-                           dsf);
+    r = new PostErrorEvent(request, POST_ERROR_EVENT_UNKNOWN);
     NS_DispatchToMainThread(r);
     return NS_OK;
   }
 
   nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory, path);
   dsf->SetEditable(aEditable);
   if (!dsf->IsSafePath()) {
-    r = new PostErrorEvent(request, POST_ERROR_EVENT_ILLEGAL_FILE_NAME, dsf);
+    r = new PostErrorEvent(request, POST_ERROR_EVENT_PERMISSION_DENIED);
   } else {
     r = new DeviceStorageRequest(DeviceStorageRequest::DEVICE_STORAGE_REQUEST_READ,
                                  win, mPrincipal, dsf, request);
   }
   NS_DispatchToMainThread(r);
   return NS_OK;
 }
 
@@ -1929,26 +1889,25 @@ nsDOMDeviceStorage::Delete(const JS::Val
   }
 
   nsRefPtr<DOMRequest> request = new DOMRequest(win);
   NS_ADDREF(*_retval = request);
 
   JSString* jsstr = JS_ValueToString(aCx, aPath);
   nsDependentJSString path;
   if (!path.init(aCx, jsstr)) {
-    nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory);
-    r = new PostErrorEvent(request, POST_ERROR_EVENT_NON_STRING_TYPE_UNSUPPORTED, dsf);
+    r = new PostErrorEvent(request, POST_ERROR_EVENT_UNKNOWN);
     NS_DispatchToMainThread(r);
     return NS_OK;
   }
 
   nsRefPtr<DeviceStorageFile> dsf = new DeviceStorageFile(mStorageType, mRootDirectory, path);
 
   if (!dsf->IsSafePath()) {
-    r = new PostErrorEvent(request, POST_ERROR_EVENT_ILLEGAL_FILE_NAME, dsf);
+    r = new PostErrorEvent(request, POST_ERROR_EVENT_PERMISSION_DENIED);
   }
   else {
     r = new DeviceStorageRequest(DeviceStorageRequest::DEVICE_STORAGE_REQUEST_DELETE,
                                  win, mPrincipal, dsf, request);
   }
   NS_DispatchToMainThread(r);
   return NS_OK;
 }
--- a/dom/devicestorage/nsDeviceStorage.h
+++ b/dom/devicestorage/nsDeviceStorage.h
@@ -27,25 +27,22 @@ class nsPIDOMWindow;
 #include "nsIObserver.h"
 #include "nsIStringBundle.h"
 #include "mozilla/Mutex.h"
 #include "prtime.h"
 #include "DeviceStorage.h"
 
 #include "DeviceStorageRequestChild.h"
 
-#define POST_ERROR_EVENT_FILE_EXISTS                 "File already exists"
-#define POST_ERROR_EVENT_FILE_DOES_NOT_EXIST         "File location doesn't exists"
-#define POST_ERROR_EVENT_FILE_NOT_ENUMERABLE         "File location is not enumerable"
-#define POST_ERROR_EVENT_PERMISSION_DENIED           "Permission Denied"
-#define POST_ERROR_EVENT_ILLEGAL_FILE_NAME           "Illegal file name"
-#define POST_ERROR_EVENT_ILLEGAL_TYPE                "Illegal content type"
+#define POST_ERROR_EVENT_FILE_EXISTS                 "NoModificationAllowedError"
+#define POST_ERROR_EVENT_FILE_DOES_NOT_EXIST         "NotFoundError"
+#define POST_ERROR_EVENT_FILE_NOT_ENUMERABLE         "TypeMismatchError"
+#define POST_ERROR_EVENT_PERMISSION_DENIED           "SecurityError"
+#define POST_ERROR_EVENT_ILLEGAL_TYPE                "TypeMismatchError"
 #define POST_ERROR_EVENT_UNKNOWN                     "Unknown"
-#define POST_ERROR_EVENT_NON_STRING_TYPE_UNSUPPORTED "Non-string type unsupported"
-#define POST_ERROR_EVENT_NOT_IMPLEMENTED             "Not implemented"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 using namespace mozilla::dom::devicestorage;
 
 class DeviceStorageTypeChecker MOZ_FINAL
 {
 public:
--- a/dom/devicestorage/test/test_addCorrectType.html
+++ b/dom/devicestorage/test/test_addCorrectType.html
@@ -38,16 +38,18 @@ var tests = [
   function () { return addNamed("music",    "kyle/smash", ".mp3")},
   function () { return addNamed("music",    "music/mp3",  ".poo")},
   function () { return addNamed("videos",   "kyle/smash", ".ogv")},
   function () { return addNamed("videos",   "video/ogv",  ".poo")},
 ];
 
 function fail(e) {
   ok(false, "addSuccess was called");
+  ok(e.target.error.name == "TypeMismatchError", "Error must be TypeMismatchError");
+
   devicestorage_cleanup();
 }
 
 function next(e) {
 
   if (e != undefined)
     ok(true, "addError was called");
   
--- a/dom/devicestorage/test/test_basic.html
+++ b/dom/devicestorage/test/test_basic.html
@@ -8,34 +8,25 @@ https://bugzilla.mozilla.org/show_bug.cg
 -->
 <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 onunload="unload()">
+<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 unload() {
-  delete gDataBlob;
-  gDataBlob = null;
-
-  delete gFileReader;
-  gFileReader = null;
-}
-
-
 devicestorage_setup();
 
 var gFileName = "devicestorage/hi.png";
 var gData = "My name is Doug Turner.  My IRC nick is DougT.  I like Maple cookies."
 var gDataBlob = new Blob([gData], {type: 'image/png'});
 var gFileReader = new FileReader();
 
 function getAfterDeleteSuccess(e) {
--- a/dom/devicestorage/test/test_dotdot.html
+++ b/dom/devicestorage/test/test_dotdot.html
@@ -40,19 +40,21 @@ var gFileName = "../owned.png";
 function fail(e) {
   ok(false, "addSuccess was called");
   dump(request);
   devicestorage_cleanup();
 }
 
 function next(e) {
 
-  if (e != undefined)
-    ok(true, "addError was called");
-  
+  if (e != undefined) {
+    ok(true, "addError was called");  
+    ok(e.target.error.name == "SecurityError", "Error must be SecurityError");
+  }
+
   var f = tests.pop();
 
   if (f == undefined) {
     devicestorage_cleanup();
     return;
   }
 
   request = f();
--- a/dom/devicestorage/test/test_overwrite.html
+++ b/dom/devicestorage/test/test_overwrite.html
@@ -39,16 +39,17 @@ function deleteError(e) {
 
 function addOverwritingSuccess(e) {
   ok(false, "addOverwritingSuccess was called.");
   devicestorage_cleanup();
 }
 
 function addOverwritingError(e) {
   ok(true, "Adding to the same location should fail");
+  ok(e.target.error.name == "NoModificationAllowedError", "Error must be NoModificationAllowedError");
 
   var storage = navigator.getDeviceStorage("pictures");
   request = storage.delete(filename)
   request.onsuccess = deleteSuccess;
   request.onerror = deleteError;
 }
 
 function addSuccess(e) {