Bug 1265767 - Subset of Blink FileSystem API - patch 3 - FileEntry methods, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Tue, 07 Jun 2016 00:55:17 +0200
changeset 300831 7af7af94985a7115d506316e0d19ae841c64446c
parent 300830 f4d2ec0d2be108759ec1b673f7c047254d0e0732
child 300832 d22217515a1e6a08dc023d75361853c84d0275c8
push id19599
push usercbook@mozilla.com
push dateWed, 08 Jun 2016 10:16:21 +0000
treeherderfx-team@81f4cc3f6f4c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1265767
milestone50.0a1
Bug 1265767 - Subset of Blink FileSystem API - patch 3 - FileEntry methods, r=smaug
dom/filesystem/compat/DirectoryEntry.cpp
dom/filesystem/compat/DirectoryEntry.h
dom/filesystem/compat/Entry.cpp
dom/filesystem/compat/ErrorCallbackRunnable.h
dom/filesystem/compat/FileEntry.cpp
dom/filesystem/compat/FileEntry.h
dom/filesystem/compat/tests/test_basic.html
dom/webidl/DOMFileSystem.webidl
--- a/dom/filesystem/compat/DirectoryEntry.cpp
+++ b/dom/filesystem/compat/DirectoryEntry.cpp
@@ -1,15 +1,16 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=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/. */
 
 #include "DirectoryEntry.h"
+#include "ErrorCallbackRunnable.h"
 #include "mozilla/dom/Directory.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_INHERITED(DirectoryEntry, Entry, mDirectory)
 
 NS_IMPL_ADDREF_INHERITED(DirectoryEntry, Entry)
@@ -43,10 +44,23 @@ DirectoryEntry::GetName(nsAString& aName
 }
 
 void
 DirectoryEntry::GetFullPath(nsAString& aPath, ErrorResult& aRv) const
 {
   mDirectory->GetPath(aPath, aRv);
 }
 
+void
+DirectoryEntry::RemoveRecursively(VoidCallback& aSuccessCallback,
+                                  const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback) const
+{
+  if (aErrorCallback.WasPassed()) {
+    RefPtr<ErrorCallbackRunnable> runnable =
+      new ErrorCallbackRunnable(GetParentObject(),
+                                &aErrorCallback.Value());
+    nsresult rv = NS_DispatchToMainThread(runnable);
+    NS_WARN_IF(NS_FAILED(rv));
+  }
+}
+
 } // dom namespace
 } // mozilla namespace
--- a/dom/filesystem/compat/DirectoryEntry.h
+++ b/dom/filesystem/compat/DirectoryEntry.h
@@ -60,21 +60,17 @@ public:
                const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
                ErrorResult& aRv) const
   {
     aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
   }
 
   void
   RemoveRecursively(VoidCallback& aSuccessCallback,
-                    const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
-                    ErrorResult& aRv) const
-  {
-    aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
-  }
+                    const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback) const;
 
 private:
   ~DirectoryEntry();
 
   RefPtr<Directory> mDirectory;
 };
 
 } // namespace dom
--- a/dom/filesystem/compat/Entry.cpp
+++ b/dom/filesystem/compat/Entry.cpp
@@ -2,17 +2,16 @@
 /* vim: set ts=8 sts=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/. */
 
 #include "Entry.h"
 #include "DirectoryEntry.h"
 #include "FileEntry.h"
-
 #include "mozilla/dom/UnionTypes.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Entry, mParent)
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(Entry)
new file mode 100644
--- /dev/null
+++ b/dom/filesystem/compat/ErrorCallbackRunnable.h
@@ -0,0 +1,50 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=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/. */
+
+#ifndef mozilla_dom_ErrorCallbackRunnable_h
+#define mozilla_dom_ErrorCallbackRunnable_h
+
+#include "mozilla/dom/DOMError.h"
+#include "nsPIDOMWindow.h"
+
+namespace mozilla {
+namespace dom {
+
+class ErrorCallbackRunnable final : public Runnable
+{
+public:
+  explicit ErrorCallbackRunnable(nsIGlobalObject* aGlobalObject,
+                                 ErrorCallback* aCallback)
+    : mGlobal(aGlobalObject)
+    , mCallback(aCallback)
+  {
+    MOZ_ASSERT(aGlobalObject);
+    MOZ_ASSERT(aCallback);
+  }
+
+  NS_IMETHOD
+  Run() override
+  {
+    nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mGlobal);
+    if (NS_WARN_IF(!window)) {
+      return NS_ERROR_FAILURE;
+    }
+
+    RefPtr<DOMError> error =
+      new DOMError(window, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
+    mCallback->HandleEvent(*error);
+    return NS_OK;
+  }
+
+private:
+  nsCOMPtr<nsIGlobalObject> mGlobal;
+  RefPtr<ErrorCallback> mCallback;
+};
+
+} // dom namespace
+} // mozilla namespace
+
+#endif // mozilla_dom_ErrorCallbackRunnable_h
--- a/dom/filesystem/compat/FileEntry.cpp
+++ b/dom/filesystem/compat/FileEntry.cpp
@@ -1,20 +1,48 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=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/. */
 
 #include "FileEntry.h"
+#include "ErrorCallbackRunnable.h"
 #include "mozilla/dom/File.h"
 
 namespace mozilla {
 namespace dom {
 
+namespace {
+
+class BlobCallbackRunnable final : public Runnable
+{
+public:
+  BlobCallbackRunnable(BlobCallback* aCallback, File* aFile)
+    : mCallback(aCallback)
+    , mFile(aFile)
+  {
+    MOZ_ASSERT(aCallback);
+    MOZ_ASSERT(aFile);
+  }
+
+  NS_IMETHOD
+  Run() override
+  {
+    mCallback->HandleEvent(mFile);
+    return NS_OK;
+  }
+
+private:
+  RefPtr<BlobCallback> mCallback;
+  RefPtr<File> mFile;
+};
+
+} // anonymous namespace
+
 NS_IMPL_CYCLE_COLLECTION_INHERITED(FileEntry, Entry, mFile)
 
 NS_IMPL_ADDREF_INHERITED(FileEntry, Entry)
 NS_IMPL_RELEASE_INHERITED(FileEntry, Entry)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileEntry)
 NS_INTERFACE_MAP_END_INHERITING(Entry)
 
@@ -43,10 +71,33 @@ FileEntry::GetName(nsAString& aName, Err
 }
 
 void
 FileEntry::GetFullPath(nsAString& aPath, ErrorResult& aRv) const
 {
   mFile->GetPath(aPath);
 }
 
+void
+FileEntry::CreateWriter(VoidCallback& aSuccessCallback,
+                        const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback) const
+{
+  if (aErrorCallback.WasPassed()) {
+    RefPtr<ErrorCallbackRunnable> runnable =
+      new ErrorCallbackRunnable(GetParentObject(),
+                                &aErrorCallback.Value());
+    nsresult rv = NS_DispatchToMainThread(runnable);
+    NS_WARN_IF(NS_FAILED(rv));
+  }
+}
+
+void
+FileEntry::GetFile(BlobCallback& aSuccessCallback,
+                   const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback) const
+{
+  RefPtr<BlobCallbackRunnable> runnable =
+    new BlobCallbackRunnable(&aSuccessCallback, mFile);
+  nsresult rv = NS_DispatchToMainThread(runnable);
+  NS_WARN_IF(NS_FAILED(rv));
+}
+
 } // dom namespace
 } // mozilla namespace
--- a/dom/filesystem/compat/FileEntry.h
+++ b/dom/filesystem/compat/FileEntry.h
@@ -34,29 +34,21 @@ public:
   virtual void
   GetName(nsAString& aName, ErrorResult& aRv) const override;
 
   virtual void
   GetFullPath(nsAString& aFullPath, ErrorResult& aRv) const override;
 
   void
   CreateWriter(VoidCallback& aSuccessCallback,
-               const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
-               ErrorResult& aRv) const
-  {
-    aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
-  }
+               const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback) const;
 
   void
   GetFile(BlobCallback& aSuccessCallback,
-          const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
-          ErrorResult& aRv) const
-  {
-    aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
-  }
+          const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback) const;
 
 private:
   ~FileEntry();
 
   RefPtr<File> mFile;
 };
 
 } // namespace dom
--- a/dom/filesystem/compat/tests/test_basic.html
+++ b/dom/filesystem/compat/tests/test_basic.html
@@ -57,31 +57,53 @@ function test_entries() {
 function test_fileEntry() {
   ok("name" in fileEntry, "We have a name.");
   ok("fullPath" in fileEntry, "We have a fullPath.");
   ok("filesystem" in fileEntry, "We have a filesystem.");
 
   next();
 }
 
+function test_fileEntry_file() {
+  fileEntry.file(function(file) {
+    ok(file, "We have a file here!");
+    is(file.name, fileEntry.name, "Same file name.");
+    next();
+  }, function() {
+    ok(false, "Something when wrong!");
+  });
+}
+
+function test_fileEntry_createWriter() {
+  fileEntry.createWriter(function(file) {
+    ok(false, "Something when wrong!");
+  }, function() {
+    ok(true, "We don't support createWrite");
+    next();
+  });
+}
+
 function test_directoryEntry() {
   ok("name" in directoryEntry, "We have a name.");
   ok("fullPath" in directoryEntry, "We have a fullPath.");
   ok("filesystem" in directoryEntry, "We have a filesystem.");
 
   next();
 }
 
 var tests = [
   setup_tests,
   populate_entries,
 
   test_entries,
 
   test_fileEntry,
+  test_fileEntry_file,
+  test_fileEntry_createWriter,
+
   test_directoryEntry,
 ];
 
 function next() {
   if (!tests.length) {
     SimpleTest.finish();
     return;
   }
--- a/dom/webidl/DOMFileSystem.webidl
+++ b/dom/webidl/DOMFileSystem.webidl
@@ -47,17 +47,18 @@ interface DirectoryEntry : Entry {
     DirectoryReader createReader();
 
     [Throws]
     void getFile(DOMString? path, optional FileSystemFlags options, optional EntryCallback successCallback, optional ErrorCallback errorCallback);
 
     [Throws]
     void getDirectory(DOMString? path, optional FileSystemFlags options, optional EntryCallback successCallback, optional ErrorCallback errorCallback);
 
-    [Throws]
+    // This method is not implemented. ErrorCallback will be called with
+    // NS_ERROR_DOM_NOT_SUPPORTED_ERR.
     void removeRecursively(VoidCallback successCallback, optional ErrorCallback errorCallback);
 };
 
 callback interface EntriesCallback {
     void handleEvent(sequence<Entry> entries);
 };
 
 callback interface ErrorCallback {
@@ -73,21 +74,21 @@ interface DirectoryReader {
 
 callback interface BlobCallback {
     void handleEvent(Blob? blob);
 };
 
 [NoInterfaceObject]
 interface FileEntry : Entry {
     // the successCallback should be a FileWriteCallback but this method is not
-    // implemented.
-    [Throws]
+    // implemented. ErrorCallback will be called with
+    // NS_ERROR_DOM_NOT_SUPPORTED_ERR.
     void createWriter (VoidCallback successCallback, optional ErrorCallback errorCallback);
 
-    [Throws,BinaryName="GetFile"]
+    [BinaryName="GetFile"]
     void file (BlobCallback successCallback, optional ErrorCallback errorCallback);
 };
 
 [NoInterfaceObject]
 interface DOMFileSystem {
     [GetterThrows]
     readonly    attribute DOMString      name;