From 367d4612a65165cf211354c765ca055c9b9f1151 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Wed, 22 Jul 2015 17:03:07 +0100 Subject: [PATCH] Bug 1185381 - Make FileList clonable - patch 1 - move code into FileList.h/.cpp, r=smaug --- dom/base/File.cpp | 38 --------- dom/base/File.h | 73 ---------------- dom/base/FileList.cpp | 47 +++++++++++ dom/base/FileList.h | 102 +++++++++++++++++++++++ dom/base/PostMessageEvent.cpp | 1 + dom/base/moz.build | 2 + dom/base/nsDataDocumentContentPolicy.cpp | 1 + dom/bindings/Bindings.conf | 4 - dom/events/DataTransfer.cpp | 1 + dom/events/DataTransfer.h | 1 + dom/html/HTMLInputElement.cpp | 1 + 11 files changed, 156 insertions(+), 115 deletions(-) create mode 100644 dom/base/FileList.cpp create mode 100644 dom/base/FileList.h diff --git a/dom/base/File.cpp b/dom/base/File.cpp index f2e9bb6396fb..fbf3d0992707 100644 --- a/dom/base/File.cpp +++ b/dom/base/File.cpp @@ -41,8 +41,6 @@ #include "mozilla/dom/WorkerPrivate.h" #include "nsThreadUtils.h" -#include "mozilla/dom/FileListBinding.h" - namespace mozilla { namespace dom { @@ -1232,42 +1230,6 @@ BlobImplTemporaryBlob::GetInternalStream(nsIInputStream** aStream, 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 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 file = Item(aIndex); - file.forget(aFile); - return NS_OK; -} - //////////////////////////////////////////////////////////////////////////// // BlobSet implementation diff --git a/dom/base/File.h b/dom/base/File.h index 957d6104291b..5a2eedbb402a 100644 --- a/dom/base/File.h +++ b/dom/base/File.h @@ -21,7 +21,6 @@ #include "nsCycleCollectionParticipant.h" #include "nsCOMPtr.h" #include "nsIDOMBlob.h" -#include "nsIDOMFileList.h" #include "nsIFile.h" #include "nsIMutable.h" #include "nsIXMLHttpRequest.h" @@ -976,78 +975,6 @@ private: 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 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 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(aSupports), - "Uh, fix QI!"); - } -#endif - - return static_cast(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> mFiles; - nsCOMPtr mParent; -}; - } // namespace dom } // namespace mozilla diff --git a/dom/base/FileList.cpp b/dom/base/FileList.cpp new file mode 100644 index 000000000000..642b167f982d --- /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 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 file = Item(aIndex); + file.forget(aFile); + return NS_OK; +} + +} // namespace dom +} // namespace mozilla diff --git a/dom/base/FileList.h b/dom/base/FileList.h new file mode 100644 index 000000000000..f382536c9402 --- /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 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 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(aSupports), + "Uh, fix QI!"); + } +#endif + + return static_cast(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> mFiles; + nsCOMPtr mParent; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_FileList_h diff --git a/dom/base/PostMessageEvent.cpp b/dom/base/PostMessageEvent.cpp index cbcb7bdba3f9..262125ba571e 100644 --- a/dom/base/PostMessageEvent.cpp +++ b/dom/base/PostMessageEvent.cpp @@ -8,6 +8,7 @@ #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" diff --git a/dom/base/moz.build b/dom/base/moz.build index d4e4499bdfe5..f383d804bd3c 100644 --- a/dom/base/moz.build +++ b/dom/base/moz.build @@ -175,6 +175,7 @@ EXPORTS.mozilla.dom += [ 'ElementInlines.h', 'EventSource.h', 'File.h', + 'FileList.h', 'FragmentOrElement.h', 'FromParser.h', 'ImageEncoder.h', @@ -233,6 +234,7 @@ UNIFIED_SOURCES += [ 'EventSource.cpp', 'File.cpp', 'FileIOObject.cpp', + 'FileList.cpp', 'FragmentOrElement.cpp', 'ImageEncoder.cpp', 'ImportManager.cpp', diff --git a/dom/base/nsDataDocumentContentPolicy.cpp b/dom/base/nsDataDocumentContentPolicy.cpp index 0c6edafe12df..085f982ad765 100644 --- a/dom/base/nsDataDocumentContentPolicy.cpp +++ b/dom/base/nsDataDocumentContentPolicy.cpp @@ -18,6 +18,7 @@ #include "nsIDocument.h" #include "nsINode.h" #include "nsIDOMWindow.h" +#include "nsIURI.h" NS_IMPL_ISUPPORTS(nsDataDocumentContentPolicy, nsIContentPolicy) diff --git a/dom/bindings/Bindings.conf b/dom/bindings/Bindings.conf index 45e0c3160a6b..f98d6df2fefa 100644 --- a/dom/bindings/Bindings.conf +++ b/dom/bindings/Bindings.conf @@ -481,10 +481,6 @@ DOMInterfaces = { }, }, -'FileList': { - 'headerFile': 'mozilla/dom/File.h', -}, - 'FileReader': { 'nativeType': 'nsDOMFileReader', 'implicitJSContext': [ 'readAsArrayBuffer' ], diff --git a/dom/events/DataTransfer.cpp b/dom/events/DataTransfer.cpp index 322bd68960f9..20d9cb979779 100644 --- a/dom/events/DataTransfer.cpp +++ b/dom/events/DataTransfer.cpp @@ -28,6 +28,7 @@ #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" diff --git a/dom/events/DataTransfer.h b/dom/events/DataTransfer.h index db89f2cf708e..8615bbf15ad4 100644 --- a/dom/events/DataTransfer.h +++ b/dom/events/DataTransfer.h @@ -34,6 +34,7 @@ namespace dom { class DOMStringList; class Element; +class FileList; template class Optional; /** diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp index 51b115984fca..0e46cb180c62 100644 --- a/dom/html/HTMLInputElement.cpp +++ b/dom/html/HTMLInputElement.cpp @@ -80,6 +80,7 @@ // input type=file #include "mozilla/dom/File.h" +#include "mozilla/dom/FileList.h" #include "nsIFile.h" #include "nsNetCID.h" #include "nsNetUtil.h"