Bug 1185381 - Make FileList clonable - patch 1 - move code into FileList.h/.cpp, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 22 Jul 2015 17:03:07 +0100
changeset 280215 da598d1d8f737dd3ae3a048fb913ebded6d702e5
parent 280214 a9af8583cd342f85a674e338c866366a906e2d39
child 280216 9b04e954b2b262bd4fbac08fd343596cf4b92c9a
push id3718
push usercmanchester@mozilla.com
push dateWed, 22 Jul 2015 21:11:36 +0000
reviewerssmaug
bugs1185381
milestone42.0a1
Bug 1185381 - Make FileList clonable - patch 1 - move code into FileList.h/.cpp, r=smaug
dom/base/File.cpp
dom/base/File.h
dom/base/FileList.cpp
dom/base/FileList.h
dom/base/PostMessageEvent.cpp
dom/base/moz.build
dom/base/nsDataDocumentContentPolicy.cpp
dom/bindings/Bindings.conf
dom/events/DataTransfer.cpp
dom/events/DataTransfer.h
dom/html/HTMLInputElement.cpp
--- a/dom/base/File.cpp
+++ b/dom/base/File.cpp
@@ -36,18 +36,16 @@
 #include "mozilla/Attributes.h"
 #include "mozilla/dom/BlobBinding.h"
 #include "mozilla/dom/BlobSet.h"
 #include "mozilla/dom/DOMError.h"
 #include "mozilla/dom/FileBinding.h"
 #include "mozilla/dom/WorkerPrivate.h"
 #include "nsThreadUtils.h"
 
-#include "mozilla/dom/FileListBinding.h"
-
 namespace mozilla {
 namespace dom {
 
 // XXXkhuey the input stream that we pass out of a File
 // can outlive the actual File object.  Thus, we must
 // ensure that the buffer underlying the stream we get
 // from NS_NewByteInputStream is held alive as long as the
 // stream is.  We do that by passing back this class instead.
@@ -1228,52 +1226,16 @@ BlobImplTemporaryBlob::GetInternalStream
                                          ErrorResult& aRv)
 {
   nsCOMPtr<nsIInputStream> stream =
     new nsTemporaryFileInputStream(mFileDescOwner, mStartPos, mStartPos + mLength);
   stream.forget(aStream);
 }
 
 ////////////////////////////////////////////////////////////////////////////
-// FileList implementation
-
-NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FileList, mFiles, mParent)
-
-NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileList)
-  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
-  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFileList)
-  NS_INTERFACE_MAP_ENTRY(nsIDOMFileList)
-NS_INTERFACE_MAP_END
-
-NS_IMPL_CYCLE_COLLECTING_ADDREF(FileList)
-NS_IMPL_CYCLE_COLLECTING_RELEASE(FileList)
-
-JSObject*
-FileList::WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto)
-{
-  return mozilla::dom::FileListBinding::Wrap(cx, this, aGivenProto);
-}
-
-NS_IMETHODIMP
-FileList::GetLength(uint32_t* aLength)
-{
-  *aLength = Length();
-
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-FileList::Item(uint32_t aIndex, nsISupports** aFile)
-{
-  nsCOMPtr<nsIDOMBlob> file = Item(aIndex);
-  file.forget(aFile);
-  return NS_OK;
-}
-
-////////////////////////////////////////////////////////////////////////////
 // BlobSet implementation
 
 already_AddRefed<Blob>
 BlobSet::GetBlobInternal(nsISupports* aParent, const nsACString& aContentType)
 {
   nsRefPtr<Blob> blob = Blob::Create(aParent,
     new MultipartBlobImpl(GetBlobImpls(),
                           NS_ConvertASCIItoUTF16(aContentType)));
--- a/dom/base/File.h
+++ b/dom/base/File.h
@@ -16,17 +16,16 @@
 #include "mozilla/dom/Date.h"
 #include "mozilla/dom/indexedDB/FileInfo.h"
 #include "mozilla/dom/indexedDB/FileManager.h"
 #include "mozilla/dom/indexedDB/IndexedDatabaseManager.h"
 #include "nsAutoPtr.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsCOMPtr.h"
 #include "nsIDOMBlob.h"
-#include "nsIDOMFileList.h"
 #include "nsIFile.h"
 #include "nsIMutable.h"
 #include "nsIXMLHttpRequest.h"
 #include "nsString.h"
 #include "nsTemporaryFileInputStream.h"
 #include "nsWrapperCache.h"
 #include "nsWeakReference.h"
 
@@ -971,84 +970,12 @@ private:
   }
 
   nsCOMPtr<nsIFile> mFile;
   bool mWholeFile;
   bool mStoredFile;
   bool mIsTemporary;
 };
 
-class FileList final : public nsIDOMFileList,
-                       public nsWrapperCache
-{
-  ~FileList() {}
-
-public:
-  explicit FileList(nsISupports *aParent) : mParent(aParent)
-  {
-  }
-
-  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
-  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FileList)
-
-  NS_DECL_NSIDOMFILELIST
-
-  virtual JSObject* WrapObject(JSContext *cx,
-                               JS::Handle<JSObject*> aGivenProto) override;
-
-  nsISupports* GetParentObject()
-  {
-    return mParent;
-  }
-
-  bool Append(File *aFile) { return mFiles.AppendElement(aFile); }
-
-  bool Remove(uint32_t aIndex) {
-    if (aIndex < mFiles.Length()) {
-      mFiles.RemoveElementAt(aIndex);
-      return true;
-    }
-
-    return false;
-  }
-
-  void Clear() { return mFiles.Clear(); }
-
-  static FileList* FromSupports(nsISupports* aSupports)
-  {
-#ifdef DEBUG
-    {
-      nsCOMPtr<nsIDOMFileList> list_qi = do_QueryInterface(aSupports);
-
-      // If this assertion fires the QI implementation for the object in
-      // question doesn't use the nsIDOMFileList pointer as the nsISupports
-      // pointer. That must be fixed, or we'll crash...
-      NS_ASSERTION(list_qi == static_cast<nsIDOMFileList*>(aSupports),
-                   "Uh, fix QI!");
-    }
-#endif
-
-    return static_cast<FileList*>(aSupports);
-  }
-
-  File* Item(uint32_t aIndex)
-  {
-    return mFiles.SafeElementAt(aIndex);
-  }
-  File* IndexedGetter(uint32_t aIndex, bool& aFound)
-  {
-    aFound = aIndex < mFiles.Length();
-    return aFound ? mFiles.ElementAt(aIndex) : nullptr;
-  }
-  uint32_t Length()
-  {
-    return mFiles.Length();
-  }
-
-private:
-  nsTArray<nsRefPtr<File>> mFiles;
-  nsCOMPtr<nsISupports> mParent;
-};
-
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_File_h
new file mode 100644
--- /dev/null
+++ b/dom/base/FileList.cpp
@@ -0,0 +1,47 @@
+/* -*- 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 "mozilla/dom/FileList.h"
+#include "mozilla/dom/FileListBinding.h"
+
+namespace mozilla {
+namespace dom {
+
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FileList, mFiles, mParent)
+
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileList)
+  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
+  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFileList)
+  NS_INTERFACE_MAP_ENTRY(nsIDOMFileList)
+NS_INTERFACE_MAP_END
+
+NS_IMPL_CYCLE_COLLECTING_ADDREF(FileList)
+NS_IMPL_CYCLE_COLLECTING_RELEASE(FileList)
+
+JSObject*
+FileList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
+{
+  return mozilla::dom::FileListBinding::Wrap(aCx, this, aGivenProto);
+}
+
+NS_IMETHODIMP
+FileList::GetLength(uint32_t* aLength)
+{
+  *aLength = Length();
+
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+FileList::Item(uint32_t aIndex, nsISupports** aFile)
+{
+  nsCOMPtr<nsIDOMBlob> file = Item(aIndex);
+  file.forget(aFile);
+  return NS_OK;
+}
+
+} // namespace dom
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/base/FileList.h
@@ -0,0 +1,102 @@
+/* -*- 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_FileList_h
+#define mozilla_dom_FileList_h
+
+#include "nsIDOMFileList.h"
+#include "nsWrapperCache.h"
+
+namespace mozilla {
+namespace dom {
+
+class File;
+
+class FileList final : public nsIDOMFileList,
+                       public nsWrapperCache
+{
+public:
+  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
+  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FileList)
+
+  NS_DECL_NSIDOMFILELIST
+
+  explicit FileList(nsISupports* aParent)
+    : mParent(aParent)
+  {}
+
+  virtual JSObject* WrapObject(JSContext* aCx,
+                               JS::Handle<JSObject*> aGivenProto) override;
+
+  nsISupports* GetParentObject()
+  {
+    return mParent;
+  }
+
+  bool Append(File* aFile)
+  {
+    return mFiles.AppendElement(aFile);
+  }
+
+  bool Remove(uint32_t aIndex)
+  {
+    if (aIndex < mFiles.Length()) {
+      mFiles.RemoveElementAt(aIndex);
+      return true;
+    }
+
+    return false;
+  }
+
+  void Clear()
+  {
+    return mFiles.Clear();
+  }
+
+  static FileList* FromSupports(nsISupports* aSupports)
+  {
+#ifdef DEBUG
+    {
+      nsCOMPtr<nsIDOMFileList> list_qi = do_QueryInterface(aSupports);
+
+      // If this assertion fires the QI implementation for the object in
+      // question doesn't use the nsIDOMFileList pointer as the nsISupports
+      // pointer. That must be fixed, or we'll crash...
+      NS_ASSERTION(list_qi == static_cast<nsIDOMFileList*>(aSupports),
+                   "Uh, fix QI!");
+    }
+#endif
+
+    return static_cast<FileList*>(aSupports);
+  }
+
+  File* Item(uint32_t aIndex)
+  {
+    return mFiles.SafeElementAt(aIndex);
+  }
+
+  File* IndexedGetter(uint32_t aIndex, bool& aFound)
+  {
+    aFound = aIndex < mFiles.Length();
+    return aFound ? mFiles.ElementAt(aIndex) : nullptr;
+  }
+
+  uint32_t Length()
+  {
+    return mFiles.Length();
+  }
+
+private:
+  ~FileList() {}
+
+  nsTArray<nsRefPtr<File>> mFiles;
+  nsCOMPtr<nsISupports> mParent;
+};
+
+} // namespace dom
+} // namespace mozilla
+
+#endif // mozilla_dom_FileList_h
--- a/dom/base/PostMessageEvent.cpp
+++ b/dom/base/PostMessageEvent.cpp
@@ -3,16 +3,17 @@
 /* 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 "PostMessageEvent.h"
 
 #include "MessageEvent.h"
 #include "mozilla/dom/BlobBinding.h"
+#include "mozilla/dom/FileList.h"
 #include "mozilla/dom/MessagePort.h"
 #include "mozilla/dom/MessagePortBinding.h"
 #include "mozilla/dom/PMessagePort.h"
 #include "mozilla/dom/StructuredCloneTags.h"
 #include "mozilla/EventDispatcher.h"
 #include "nsGlobalWindow.h"
 #include "nsIPresShell.h"
 #include "nsIPrincipal.h"
--- a/dom/base/moz.build
+++ b/dom/base/moz.build
@@ -170,16 +170,17 @@ EXPORTS.mozilla.dom += [
     'DOMQuad.h',
     'DOMRect.h',
     'DOMRequest.h',
     'DOMStringList.h',
     'Element.h',
     'ElementInlines.h',
     'EventSource.h',
     'File.h',
+    'FileList.h',
     'FragmentOrElement.h',
     'FromParser.h',
     'ImageEncoder.h',
     'ImportManager.h',
     'Link.h',
     'NameSpaceConstants.h',
     'Navigator.h',
     'NodeInfo.h',
@@ -228,16 +229,17 @@ UNIFIED_SOURCES += [
     'DOMQuad.cpp',
     'DOMRect.cpp',
     'DOMRequest.cpp',
     'DOMStringList.cpp',
     'Element.cpp',
     'EventSource.cpp',
     'File.cpp',
     'FileIOObject.cpp',
+    'FileList.cpp',
     'FragmentOrElement.cpp',
     'ImageEncoder.cpp',
     'ImportManager.cpp',
     'Link.cpp',
     'MultipartBlobImpl.cpp',
     'Navigator.cpp',
     'NodeInfo.cpp',
     'NodeIterator.cpp',
--- a/dom/base/nsDataDocumentContentPolicy.cpp
+++ b/dom/base/nsDataDocumentContentPolicy.cpp
@@ -13,16 +13,17 @@
 #include "nsContentUtils.h"
 #include "nsDataDocumentContentPolicy.h"
 #include "nsNetUtil.h"
 #include "nsIProtocolHandler.h"
 #include "nsScriptSecurityManager.h"
 #include "nsIDocument.h"
 #include "nsINode.h"
 #include "nsIDOMWindow.h"
+#include "nsIURI.h"
 
 NS_IMPL_ISUPPORTS(nsDataDocumentContentPolicy, nsIContentPolicy)
 
 // Helper method for ShouldLoad()
 // Checks a URI for the given flags.  Returns true if the URI has the flags,
 // and false if not (or if we weren't able to tell).
 static bool
 HasFlags(nsIURI* aURI, uint32_t aURIFlags)
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -476,20 +476,16 @@ DOMInterfaces = {
 'FetchEvent': {
     'headerFile': 'ServiceWorkerEvents.h',
     'nativeType': 'mozilla::dom::workers::FetchEvent',
     'binaryNames': {
         'request': 'request_'
     },
 },
 
-'FileList': {
-    'headerFile': 'mozilla/dom/File.h',
-},
-
 'FileReader': {
     'nativeType': 'nsDOMFileReader',
     'implicitJSContext': [ 'readAsArrayBuffer' ],
 },
 
 'FileReaderSync': {
     'workers': True,
     'wrapperCache': False,
--- a/dom/events/DataTransfer.cpp
+++ b/dom/events/DataTransfer.cpp
@@ -23,16 +23,17 @@
 #include "nsIScriptObjectPrincipal.h"
 #include "nsIScriptContext.h"
 #include "nsIDocument.h"
 #include "nsIScriptGlobalObject.h"
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/dom/DataTransferBinding.h"
 #include "mozilla/dom/Directory.h"
 #include "mozilla/dom/Element.h"
+#include "mozilla/dom/FileList.h"
 #include "mozilla/dom/BindingUtils.h"
 #include "mozilla/dom/OSFileSystem.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(DataTransfer)
 
--- a/dom/events/DataTransfer.h
+++ b/dom/events/DataTransfer.h
@@ -29,16 +29,17 @@ class nsILoadContext;
 namespace mozilla {
 
 class EventStateManager;
 
 namespace dom {
 
 class DOMStringList;
 class Element;
+class FileList;
 template<typename T> class Optional;
 
 /**
  * TransferItem is used to hold data for a particular format. Each piece of
  * data has a principal set from the caller which added it. This allows a
  * caller that wishes to retrieve the data to only be able to access the data
  * it is allowed to, yet still allow a chrome caller to retrieve any of the
  * data.
--- a/dom/html/HTMLInputElement.cpp
+++ b/dom/html/HTMLInputElement.cpp
@@ -75,16 +75,17 @@
 #include "nsRuleData.h"
 #include <algorithm>
 
 // input type=radio
 #include "nsIRadioGroupContainer.h"
 
 // input type=file
 #include "mozilla/dom/File.h"
+#include "mozilla/dom/FileList.h"
 #include "nsIFile.h"
 #include "nsNetCID.h"
 #include "nsNetUtil.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsIContentPrefService.h"
 #include "nsIMIMEService.h"
 #include "nsIObserverService.h"
 #include "nsIPopupWindowManager.h"