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/. */
7 #ifndef mozilla_dom_DataTransferItemList_h
8 #define mozilla_dom_DataTransferItemList_h
10 #include "mozilla/dom/DataTransfer.h"
11 #include "mozilla/dom/DataTransferItem.h"
12 #include "mozilla/dom/FileList.h"
16 namespace mozilla::dom
{
19 class DataTransferItem
;
21 class DataTransferItemList final
: public nsISupports
, public nsWrapperCache
{
23 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
24 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DataTransferItemList
);
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);
42 already_AddRefed
<DataTransferItemList
> Clone(
43 DataTransfer
* aDataTransfer
) const;
45 virtual JSObject
* WrapObject(JSContext
* aCx
,
46 JS::Handle
<JSObject
*> aGivenProto
) override
;
48 uint32_t Length() const { return mItems
.Length(); };
50 DataTransferItem
* Add(const nsAString
& aData
, const nsAString
& aType
,
51 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& rv
);
52 DataTransferItem
* Add(File
& aData
, nsIPrincipal
& aSubjectPrincipal
,
55 void Remove(uint32_t aIndex
, nsIPrincipal
& aSubjectPrincipal
,
58 DataTransferItem
* IndexedGetter(uint32_t aIndex
, bool& aFound
) const;
60 DataTransfer
* GetParentObject() const { return mDataTransfer
; }
62 void Clear(nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
);
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
,
70 already_AddRefed
<FileList
> Files(nsIPrincipal
* aPrincipal
);
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;
79 // Causes everything in indexes above 0 to shift down one index.
82 // Delete every item in the DataTransferItemList, without checking for
83 // permissions or read-only status (for internal use only).
86 void GetTypes(nsTArray
<nsString
>& aTypes
, CallerType aCallerType
) const;
87 bool HasType(const nsAString
& aType
) const;
91 void ClearDataHelper(DataTransferItem
* aItem
, uint32_t aIndexHint
,
92 uint32_t aMozOffsetHint
, nsIPrincipal
& aSubjectPrincipal
,
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
,
99 void RegenerateFiles();
100 void GenerateFiles(FileList
* aFiles
, nsIPrincipal
* aFilesPrincipal
);
102 ~DataTransferItemList() = default;
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.
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
;
123 } // namespace mozilla::dom
125 #endif // mozilla_dom_DataTransferItemList_h