DataTransferItemList.h (5350B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_DataTransferItemList_h 8 #define mozilla_dom_DataTransferItemList_h 9 10 #include "mozilla/dom/DataTransfer.h" 11 #include "mozilla/dom/DataTransferItem.h" 12 #include "mozilla/dom/FileList.h" 13 14 class nsIVariant; 15 16 namespace mozilla::dom { 17 18 class DataTransfer; 19 class DataTransferItem; 20 21 class DataTransferItemList final : public nsISupports, public nsWrapperCache { 22 public: 23 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 24 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DataTransferItemList); 25 26 explicit DataTransferItemList(DataTransfer* aDataTransfer) 27 : mDataTransfer(aDataTransfer) { 28 MOZ_ASSERT(aDataTransfer); 29 // We always allocate an index 0 in our DataTransferItemList. This is done 30 // in order to maintain the invariants according to the spec. Mainly, within 31 // the spec's list, there is intended to be a single copy of each mime type, 32 // for string typed items. File typed items are allowed to have duplicates. 33 // In the old moz* system, this was modeled by having multiple indexes, each 34 // of which was independent. Files were fetched from all indexes, but 35 // strings were only fetched from the first index. In order to maintain this 36 // correlation and avoid breaking code with the new changes, index 0 is now 37 // always present and used to store strings, and all file items are given 38 // their own index starting at index 1. 39 mIndexedItems.SetLength(1); 40 } 41 42 already_AddRefed<DataTransferItemList> Clone( 43 DataTransfer* aDataTransfer) const; 44 45 virtual JSObject* WrapObject(JSContext* aCx, 46 JS::Handle<JSObject*> aGivenProto) override; 47 48 uint32_t Length() const { return mItems.Length(); }; 49 50 DataTransferItem* Add(const nsAString& aData, const nsAString& aType, 51 nsIPrincipal& aSubjectPrincipal, ErrorResult& rv); 52 DataTransferItem* Add(File& aData, nsIPrincipal& aSubjectPrincipal, 53 ErrorResult& aRv); 54 55 void Remove(uint32_t aIndex, nsIPrincipal& aSubjectPrincipal, 56 ErrorResult& aRv); 57 58 DataTransferItem* IndexedGetter(uint32_t aIndex, bool& aFound) const; 59 60 DataTransfer* GetParentObject() const { return mDataTransfer; } 61 62 void Clear(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv); 63 64 // @param aHidden true, iff the item should be hidden from non-chrome code. 65 already_AddRefed<DataTransferItem> SetDataWithPrincipal( 66 const nsAString& aType, nsIVariant* aData, uint32_t aIndex, 67 nsIPrincipal* aPrincipal, bool aInsertOnly, bool aHidden, 68 ErrorResult& aRv); 69 70 already_AddRefed<FileList> Files(nsIPrincipal* aPrincipal); 71 72 // Moz-style helper methods for interacting with the stored data 73 void MozRemoveByTypeAt(const nsAString& aType, uint32_t aIndex, 74 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv); 75 DataTransferItem* MozItemByTypeAt(const nsAString& aType, uint32_t aIndex); 76 const nsTArray<RefPtr<DataTransferItem>>* MozItemsAt(uint32_t aIndex); 77 uint32_t MozItemCount() const; 78 79 // Causes everything in indexes above 0 to shift down one index. 80 void PopIndexZero(); 81 82 // Delete every item in the DataTransferItemList, without checking for 83 // permissions or read-only status (for internal use only). 84 void ClearAllItems(); 85 86 void GetTypes(nsTArray<nsString>& aTypes, CallerType aCallerType) const; 87 bool HasType(const nsAString& aType) const; 88 bool HasFile() const; 89 90 private: 91 void ClearDataHelper(DataTransferItem* aItem, uint32_t aIndexHint, 92 uint32_t aMozOffsetHint, nsIPrincipal& aSubjectPrincipal, 93 ErrorResult& aRv); 94 95 // @param aHidden true, iff the item should be hidden from non-chrome code. 96 DataTransferItem* AppendNewItem(uint32_t aIndex, const nsAString& aType, 97 nsIVariant* aData, nsIPrincipal* aPrincipal, 98 bool aHidden); 99 void RegenerateFiles(); 100 void GenerateFiles(FileList* aFiles, nsIPrincipal* aFilesPrincipal); 101 102 ~DataTransferItemList() = default; 103 104 RefPtr<DataTransfer> mDataTransfer; 105 RefPtr<FileList> mFiles; 106 // The principal for which mFiles is cached 107 nsCOMPtr<nsIPrincipal> mFilesPrincipal; 108 // mItems is the list of items that corresponds to the spec concept of a 109 // DataTransferItemList. That is, this is the thing the spec's indexed getter 110 // operates on. The items in here are a subset of the items present in the 111 // arrays that live in mIndexedItems. 112 nsTArray<RefPtr<DataTransferItem>> mItems; 113 // mIndexedItems represents all our items. For any given index, all items at 114 // that index have different types in the GetType() sense. That means that 115 // representing multiple items with the same type (e.g. multiple files) 116 // requires using multiple indices. 117 // 118 // There is always a (possibly empty) list of items at index 0, so 119 // mIndexedItems.Length() >= 1 at all times. 120 nsTArray<nsTArray<RefPtr<DataTransferItem>>> mIndexedItems; 121 }; 122 123 } // namespace mozilla::dom 124 125 #endif // mozilla_dom_DataTransferItemList_h