Bug 910412 - WebIDL and DOM binding for filesystem API. r=baku, r=smaug, f=dhylands, f=jonas
authorYuan Xulei <xyuan@mozilla.com>
Wed, 05 Mar 2014 11:26:39 +0800
changeset 190813 ed100a12c75fa1d06b87c431d32a9579173881a4
parent 190812 16cc3a7db9391e275a105d1ecabdaa0d141c9976
child 190814 7e61acb41c3f60f4f220072a0be40d6323b6dde5
push idunknown
push userunknown
push dateunknown
reviewersbaku, smaug
bugs910412
milestone30.0a1
Bug 910412 - WebIDL and DOM binding for filesystem API. r=baku, r=smaug, f=dhylands, f=jonas
dom/devicestorage/DeviceStorage.h
dom/devicestorage/nsDeviceStorage.cpp
dom/filesystem/Directory.cpp
dom/filesystem/Directory.h
dom/filesystem/moz.build
dom/moz.build
dom/webidl/DeviceStorage.webidl
dom/webidl/Directory.webidl
dom/webidl/moz.build
layout/build/moz.build
--- a/dom/devicestorage/DeviceStorage.h
+++ b/dom/devicestorage/DeviceStorage.h
@@ -26,16 +26,17 @@
 class DeviceStorageFile;
 class nsIInputStream;
 
 namespace mozilla {
 namespace dom {
 class DeviceStorageEnumerationParameters;
 class DOMCursor;
 class DOMRequest;
+class Promise;
 } // namespace dom
 namespace ipc {
 class FileDescriptor;
 }
 } // namespace mozilla
 
 class DeviceStorageFile MOZ_FINAL
   : public nsISupports {
@@ -152,16 +153,17 @@ class nsDOMDeviceStorage MOZ_FINAL
   , public nsIDOMDeviceStorage
   , public nsIObserver
 {
   typedef mozilla::ErrorResult ErrorResult;
   typedef mozilla::dom::DeviceStorageEnumerationParameters
     EnumerationParameters;
   typedef mozilla::dom::DOMCursor DOMCursor;
   typedef mozilla::dom::DOMRequest DOMRequest;
+  typedef mozilla::dom::Promise Promise;
 public:
   typedef nsTArray<nsString> VolumeNameArray;
 
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_NSIDOMDEVICESTORAGE
 
   NS_DECL_NSIOBSERVER
   NS_DECL_NSIDOMEVENTTARGET
@@ -250,16 +252,19 @@ public:
   already_AddRefed<DOMRequest> StorageStatus(ErrorResult& aRv);
   already_AddRefed<DOMRequest> Mount(ErrorResult& aRv);
   already_AddRefed<DOMRequest> Unmount(ErrorResult& aRv);
 
   bool Default();
 
   // Uses XPCOM GetStorageName
 
+  already_AddRefed<Promise>
+  GetRoot();
+
   static void
   CreateDeviceStorageFor(nsPIDOMWindow* aWin,
                          const nsAString& aType,
                          nsDOMDeviceStorage** aStore);
 
   static void
   CreateDeviceStoragesFor(nsPIDOMWindow* aWin,
                           const nsAString& aType,
--- a/dom/devicestorage/nsDeviceStorage.cpp
+++ b/dom/devicestorage/nsDeviceStorage.cpp
@@ -7,20 +7,22 @@
 #include "nsDeviceStorage.h"
 
 #include "mozilla/Attributes.h"
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/DebugOnly.h"
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/dom/DeviceStorageBinding.h"
 #include "mozilla/dom/devicestorage/PDeviceStorageRequestChild.h"
+#include "mozilla/dom/Directory.h"
 #include "mozilla/dom/ipc/Blob.h"
 #include "mozilla/dom/PBrowserChild.h"
 #include "mozilla/dom/PContentPermissionRequestChild.h"
 #include "mozilla/dom/PermissionMessageUtils.h"
+#include "mozilla/dom/Promise.h"
 #include "mozilla/LazyIdleThread.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/Scoped.h"
 #include "mozilla/Services.h"
 
 #include "nsAutoPtr.h"
 #include "nsServiceManagerUtils.h"
 #include "nsIFile.h"
@@ -3912,16 +3914,23 @@ nsDOMDeviceStorage::CreateFileDescriptor
 bool
 nsDOMDeviceStorage::Default()
 {
   nsString defaultStorageName;
   GetDefaultStorageName(mStorageType, defaultStorageName);
   return mStorageName.Equals(defaultStorageName);
 }
 
+already_AddRefed<Promise>
+nsDOMDeviceStorage::GetRoot()
+{
+  // TODO
+  return nullptr;
+}
+
 NS_IMETHODIMP
 nsDOMDeviceStorage::GetDefault(bool* aDefault)
 {
   *aDefault = Default();
   return NS_OK;
 }
 
 NS_IMETHODIMP
new file mode 100644
--- /dev/null
+++ b/dom/filesystem/Directory.cpp
@@ -0,0 +1,74 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set 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/. */
+
+#include "mozilla/dom/Directory.h"
+
+#include "nsString.h"
+#include "mozilla/dom/DirectoryBinding.h"
+
+// Resolve the name collision of Microsoft's API name with macros defined in
+// Windows header files. Undefine the macro of CreateDirectory to avoid
+// Directory#CreateDirectory being replaced by Directory#CreateDirectoryW.
+#ifdef CreateDirectory
+#undef CreateDirectory
+#endif
+
+namespace mozilla {
+namespace dom {
+
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(Directory)
+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
+
+Directory::Directory()
+{
+  SetIsDOMBinding();
+}
+
+Directory::~Directory()
+{
+}
+
+nsPIDOMWindow*
+Directory::GetParentObject() const
+{
+  // TODO
+  return nullptr;
+}
+
+JSObject*
+Directory::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
+{
+  return DirectoryBinding::Wrap(aCx, aScope, this);
+}
+
+void
+Directory::GetName(nsString& aRetval) const
+{
+  aRetval.Truncate();
+  // TODO
+}
+
+already_AddRefed<Promise>
+Directory::CreateDirectory(const nsAString& aPath)
+{
+  // TODO
+  return nullptr;
+}
+
+already_AddRefed<Promise>
+Directory::Get(const nsAString& aPath)
+{
+  // TODO
+  return nullptr;
+}
+
+} // namespace dom
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/filesystem/Directory.h
@@ -0,0 +1,64 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set 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/. */
+
+#ifndef mozilla_dom_Directory_h
+#define mozilla_dom_Directory_h
+
+#include "mozilla/Attributes.h"
+#include "mozilla/dom/BindingDeclarations.h"
+#include "nsAutoPtr.h"
+#include "nsCycleCollectionParticipant.h"
+#include "nsPIDOMWindow.h"
+#include "nsWrapperCache.h"
+
+// Resolve the name collision of Microsoft's API name with macros defined in
+// Windows header files. Undefine the macro of CreateDirectory to avoid
+// Directory#CreateDirectory being replaced by Directory#CreateDirectoryW.
+#ifdef CreateDirectory
+#undef CreateDirectory
+#endif
+
+namespace mozilla {
+namespace dom {
+
+class Promise;
+
+class Directory MOZ_FINAL
+  : public nsISupports
+  , public nsWrapperCache
+{
+public:
+  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
+  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Directory)
+
+public:
+  Directory();
+  ~Directory();
+
+  // ========= Begin WebIDL bindings. ===========
+
+  nsPIDOMWindow*
+  GetParentObject() const;
+
+  virtual JSObject*
+  WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
+
+  void
+  GetName(nsString& aRetval) const;
+
+  already_AddRefed<Promise>
+  CreateDirectory(const nsAString& aPath);
+
+  already_AddRefed<Promise>
+  Get(const nsAString& aPath);
+
+  // =========== End WebIDL bindings.============
+};
+
+} // namespace dom
+} // namespace mozilla
+
+#endif // mozilla_dom_Directory_h
new file mode 100644
--- /dev/null
+++ b/dom/filesystem/moz.build
@@ -0,0 +1,22 @@
+# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+EXPORTS.mozilla.dom += [
+    'Directory.h',
+]
+
+SOURCES += [
+    'Directory.cpp',
+]
+
+FINAL_LIBRARY = 'gklayout'
+
+include('/ipc/chromium/chromium-config.mozbuild')
+
+LOCAL_INCLUDES += [
+    '/dom/base',
+]
+
--- a/dom/moz.build
+++ b/dom/moz.build
@@ -45,16 +45,17 @@ PARALLEL_DIRS += [
     'contacts',
     'phonenumberutils',
     'alarm',
     'datastore',
     'devicestorage',
     'encoding',
     'events',
     'file',
+    'filesystem',
     'fmradio',
     'asmjscache',
     'media',
     'messages',
     'power',
     'push',
     'quota',
     'settings',
--- a/dom/webidl/DeviceStorage.webidl
+++ b/dom/webidl/DeviceStorage.webidl
@@ -50,9 +50,12 @@ interface DeviceStorage : EventTarget {
 
   // Note that the storageName is just a name (like sdcard), and doesn't
   // include any path information.
   readonly attribute DOMString storageName;
 
   // Determines if this storage area is the one which will be used by default
   // for storing new files.
   readonly attribute boolean default;
+
+  [NewObject]
+  Promise getRoot();
 };
new file mode 100644
--- /dev/null
+++ b/dom/webidl/Directory.webidl
@@ -0,0 +1,49 @@
+/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/.
+ */
+
+interface File;
+
+/*
+ * All functions on Directory that accept DOMString arguments for file or
+ * directory names only allow relative path to current directory itself. The
+ * path should be a descendent path like "path/to/file.txt" and not contain a
+ * segment of ".." or ".". So the paths aren't allowed to walk up the directory
+ * tree. For example, paths like "../foo", "..", "/foo/bar" or "foo/../bar" are
+ * not allowed.
+ */
+[NoInterfaceObject]
+interface Directory {
+  /*
+   * The leaf name of the directory.
+   */
+  readonly attribute DOMString name;
+
+  /*
+   * Creates a descendent directory. This method will create any intermediate
+   * directories specified by the path segments.
+   *
+   * @param path The relative path of the new directory to current directory.
+   * If path exists, createDirectory must fail.
+   * @return If succeeds, the promise is resolved with the new created
+   * Directory object. Otherwise, rejected with a DOM error.
+   */
+  [NewObject]
+  // Promise<Directory>
+  Promise createDirectory(DOMString path);
+
+  /*
+   * 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.
+   */
+  [NewObject]
+  // Promise<(File or Directory)>
+  Promise get(DOMString path);
+};
+
--- a/dom/webidl/moz.build
+++ b/dom/webidl/moz.build
@@ -68,16 +68,17 @@ WEBIDL_FILES = [
     'DataContainerEvent.webidl',
     'DataStore.webidl',
     'DataTransfer.webidl',
     'DedicatedWorkerGlobalScope.webidl',
     'DelayNode.webidl',
     'DesktopNotification.webidl',
     'DeviceMotionEvent.webidl',
     'DeviceStorage.webidl',
+    'Directory.webidl',
     'Document.webidl',
     'DocumentFragment.webidl',
     'DocumentType.webidl',
     'DOMCursor.webidl',
     'DOMError.webidl',
     'DOMException.webidl',
     'DOMImplementation.webidl',
     'DOMMMIError.webidl',
--- a/layout/build/moz.build
+++ b/layout/build/moz.build
@@ -48,16 +48,17 @@ LOCAL_INCLUDES += [
     '/content/xul/document/src',
     '/content/xul/templates/src',
     '/docshell/base',
     '/dom/audiochannel',
     '/dom/base',
     '/dom/camera',
     '/dom/events',
     '/dom/file',
+    '/dom/filesystem',
     '/dom/media',
     '/dom/speakermanager',
     '/dom/src/geolocation',
     '/dom/src/json',
     '/dom/src/jsurl',
     '/dom/src/offline',
     '/dom/src/storage',
     '/dom/telephony',