Bug 1299500 - Get rid of DeviceStorage API - part 8 - Directory::Get, r=ehsan
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 08 Mar 2017 20:15:45 +0100
changeset 346616 f4c12243de1898567212361d896f5412dee1b089
parent 346615 f28b200fd93d6c83400df490b8a1244ef63b1f9f
child 346617 3364716e5765cfe44b16cb1d25c5a9ed43b8b45f
push id31472
push userkwierso@gmail.com
push dateThu, 09 Mar 2017 01:08:10 +0000
treeherdermozilla-central@19289cc8bf6f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1299500
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1299500 - Get rid of DeviceStorage API - part 8 - Directory::Get, r=ehsan
dom/filesystem/Directory.cpp
dom/filesystem/Directory.h
dom/filesystem/compat/CallbackRunnables.cpp
dom/webidl/Directory.webidl
--- a/dom/filesystem/Directory.cpp
+++ b/dom/filesystem/Directory.cpp
@@ -45,26 +45,16 @@ NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCA
 NS_IMPL_CYCLE_COLLECTING_ADDREF(Directory)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(Directory)
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Directory)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
 /* static */ bool
-Directory::DeviceStorageEnabled(JSContext* aCx, JSObject* aObj)
-{
-  if (!NS_IsMainThread()) {
-    return false;
-  }
-
-  return Preferences::GetBool("device.storage.enabled", false);
-}
-
-/* static */ bool
 Directory::WebkitBlinkDirectoryPickerEnabled(JSContext* aCx, JSObject* aObj)
 {
   if (NS_IsMainThread()) {
     return Preferences::GetBool("dom.webkitBlink.dirPicker.enabled", false);
   }
 
   // aCx can be null when this function is called by something else than WebIDL
   // binding code.
@@ -174,41 +164,16 @@ Directory::GetName(nsAString& aRetval, E
   RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
   fs->GetDirectoryName(mFile, aRetval, aRv);
 }
 
-already_AddRefed<Promise>
-Directory::Get(const nsAString& aPath, ErrorResult& aRv)
-{
-  // Only exposed for DeviceStorage.
-  MOZ_ASSERT(NS_IsMainThread());
-
-  nsCOMPtr<nsIFile> realPath;
-  nsresult error = DOMPathToRealPath(aPath, getter_AddRefs(realPath));
-
-  RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
-  if (NS_WARN_IF(aRv.Failed())) {
-    return nullptr;
-  }
-
-  RefPtr<GetFileOrDirectoryTaskChild> task =
-    GetFileOrDirectoryTaskChild::Create(fs, realPath, false, aRv);
-  if (NS_WARN_IF(aRv.Failed())) {
-    return nullptr;
-  }
-
-  task->SetError(error);
-  FileSystemPermissionRequest::RequestForTask(task);
-  return task->GetPromise();
-}
-
 void
 Directory::GetPath(nsAString& aRetval, ErrorResult& aRv)
 {
   // This operation is expensive. Better to cache the result.
   if (mPath.IsEmpty()) {
     RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
     if (NS_WARN_IF(aRv.Failed())) {
       return;
@@ -293,47 +258,16 @@ Directory::GetFileSystem(ErrorResult& aR
     fs->Init(mParent);
 
     mFileSystem = fs;
   }
 
   return mFileSystem;
 }
 
-nsresult
-Directory::DOMPathToRealPath(const nsAString& aPath, nsIFile** aFile) const
-{
-  nsString relativePath;
-  relativePath = aPath;
-
-  // Trim white spaces.
-  static const char kWhitespace[] = "\b\t\r\n ";
-  relativePath.Trim(kWhitespace);
-
-  nsTArray<nsString> parts;
-  if (!FileSystemUtils::IsValidRelativeDOMPath(relativePath, parts)) {
-    return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
-  }
-
-  nsCOMPtr<nsIFile> file;
-  nsresult rv = mFile->Clone(getter_AddRefs(file));
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-
-  for (uint32_t i = 0; i < parts.Length(); ++i) {
-    rv = file->AppendRelativePath(parts[i]);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-  }
-
-  file.forget(aFile);
-  return NS_OK;
-}
 
 bool
 Directory::ClonableToDifferentThreadOrProcess() const
 {
   // If we don't have a fileSystem we are going to create a OSFileSystem that is
   // clonable everywhere.
   if (!mFileSystem) {
     return true;
--- a/dom/filesystem/Directory.h
+++ b/dom/filesystem/Directory.h
@@ -25,19 +25,16 @@ class Directory final
   : public nsISupports
   , public nsWrapperCache
 {
 public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Directory)
 
   static bool
-  DeviceStorageEnabled(JSContext* aCx, JSObject* aObj);
-
-  static bool
   WebkitBlinkDirectoryPickerEnabled(JSContext* aCx, JSObject* aObj);
 
   static already_AddRefed<Promise>
   GetRoot(FileSystemBase* aFileSystem, ErrorResult& aRv);
 
   static already_AddRefed<Directory>
   Constructor(const GlobalObject& aGlobal,
               const nsAString& aRealPath,
@@ -53,19 +50,16 @@ public:
   GetParentObject() const;
 
   virtual JSObject*
   WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   void
   GetName(nsAString& aRetval, ErrorResult& aRv);
 
-  already_AddRefed<Promise>
-  Get(const nsAString& aPath, ErrorResult& aRv);
-
   // From https://microsoftedge.github.io/directory-upload/proposal.html#directory-interface :
 
   void
   GetPath(nsAString& aRetval, ErrorResult& aRv);
 
   nsresult
   GetFullRealPath(nsAString& aPath);
 
--- a/dom/filesystem/compat/CallbackRunnables.cpp
+++ b/dom/filesystem/compat/CallbackRunnables.cpp
@@ -7,21 +7,26 @@
 #include "CallbackRunnables.h"
 #include "mozilla/dom/Directory.h"
 #include "mozilla/dom/DirectoryBinding.h"
 #include "mozilla/dom/DOMException.h"
 #include "mozilla/dom/File.h"
 #include "mozilla/dom/FileBinding.h"
 #include "mozilla/dom/FileSystemDirectoryReaderBinding.h"
 #include "mozilla/dom/FileSystemFileEntry.h"
+#include "mozilla/dom/FileSystemUtils.h"
 #include "mozilla/dom/Promise.h"
 #include "mozilla/Unused.h"
 #include "nsIGlobalObject.h"
+#include "nsIFile.h"
 #include "nsPIDOMWindow.h"
 
+#include "../GetFileOrDirectoryTask.h"
+#include "../FileSystemPermissionRequest.h"
+
 namespace mozilla {
 namespace dom {
 
 EntryCallbackRunnable::EntryCallbackRunnable(FileSystemEntryCallback* aCallback,
                                              FileSystemEntry* aEntry)
   : mCallback(aCallback)
   , mEntry(aEntry)
 {
@@ -90,29 +95,82 @@ GetEntryHelper::GetEntryHelper(FileSyste
   MOZ_ASSERT(!aParts.IsEmpty());
   MOZ_ASSERT(aFileSystem);
   MOZ_ASSERT(aSuccessCallback || aErrorCallback);
 }
 
 GetEntryHelper::~GetEntryHelper()
 {}
 
+namespace {
+
+nsresult
+DOMPathToRealPath(Directory* aDirectory, const nsAString& aPath,
+                  nsIFile** aFile)
+{
+  nsString relativePath;
+  relativePath = aPath;
+
+  // Trim white spaces.
+  static const char kWhitespace[] = "\b\t\r\n ";
+  relativePath.Trim(kWhitespace);
+
+  nsTArray<nsString> parts;
+  if (!FileSystemUtils::IsValidRelativeDOMPath(relativePath, parts)) {
+    return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
+  }
+
+  nsCOMPtr<nsIFile> file;
+  nsresult rv = aDirectory->GetInternalNsIFile()->Clone(getter_AddRefs(file));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  for (uint32_t i = 0; i < parts.Length(); ++i) {
+    rv = file->AppendRelativePath(parts[i]);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+  }
+
+  file.forget(aFile);
+  return NS_OK;
+}
+
+} // anonymous
+
 void
 GetEntryHelper::Run()
 {
   MOZ_ASSERT(!mParts.IsEmpty());
 
+  nsCOMPtr<nsIFile> realPath;
+  nsresult error = DOMPathToRealPath(mDirectory, mParts[0],
+                                     getter_AddRefs(realPath));
+
   ErrorResult rv;
-  RefPtr<Promise> promise = mDirectory->Get(mParts[0], rv);
+  RefPtr<FileSystemBase> fs = mDirectory->GetFileSystem(rv);
   if (NS_WARN_IF(rv.Failed())) {
     rv.SuppressException();
     Error(NS_ERROR_DOM_INVALID_STATE_ERR);
     return;
   }
 
+  RefPtr<GetFileOrDirectoryTaskChild> task =
+    GetFileOrDirectoryTaskChild::Create(fs, realPath, false, rv);
+  if (NS_WARN_IF(rv.Failed())) {
+    rv.SuppressException();
+    Error(NS_ERROR_DOM_INVALID_STATE_ERR);
+    return;
+  }
+
+  task->SetError(error);
+  FileSystemPermissionRequest::RequestForTask(task);
+  RefPtr<Promise> promise = task->GetPromise();
+
   mParts.RemoveElementAt(0);
   promise->AppendNativeHandler(this);
 }
 
 void
 GetEntryHelper::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
 {
   if(NS_WARN_IF(!aValue.isObject())) {
--- a/dom/webidl/Directory.webidl
+++ b/dom/webidl/Directory.webidl
@@ -20,27 +20,16 @@
 [ChromeConstructor(DOMString path),
  Exposed=(Window,Worker)]
 interface Directory {
   /*
    * The leaf name of the directory.
    */
   [Throws]
   readonly attribute DOMString name;
-
-  /*
-   * Gets a descendent file or directory with the given path.
-   *
-   * @param path The descendent entry's relative path to current directory.
-   * @return If the path exists and no error occurs, the promise is resolved
-   * with a File or Directory object, depending on the entry's type. Otherwise,
-   * rejected with a DOM error.
-   */
-  [Func="mozilla::dom::Directory::DeviceStorageEnabled", NewObject]
-  Promise<(File or Directory)> get(DOMString path);
 };
 
 [Exposed=(Window,Worker)]
 partial interface Directory {
   // Already defined in the main interface declaration:
   //readonly attribute DOMString name;
 
   /*