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"
17 class DataTransferItem
;
19 class DataTransferItemList final
: public nsISupports
, public nsWrapperCache
{
21 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
22 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DataTransferItemList
);
24 explicit DataTransferItemList(DataTransfer
* aDataTransfer
)
25 : mDataTransfer(aDataTransfer
) {
26 MOZ_ASSERT(aDataTransfer
);
27 // We always allocate an index 0 in our DataTransferItemList. This is done
28 // in order to maintain the invariants according to the spec. Mainly, within
29 // the spec's list, there is intended to be a single copy of each mime type,
30 // for string typed items. File typed items are allowed to have duplicates.
31 // In the old moz* system, this was modeled by having multiple indexes, each
32 // of which was independent. Files were fetched from all indexes, but
33 // strings were only fetched from the first index. In order to maintain this
34 // correlation and avoid breaking code with the new changes, index 0 is now
35 // always present and used to store strings, and all file items are given
36 // their own index starting at index 1.
37 mIndexedItems
.SetLength(1);
40 already_AddRefed
<DataTransferItemList
> Clone(
41 DataTransfer
* aDataTransfer
) const;
43 virtual JSObject
* WrapObject(JSContext
* aCx
,
44 JS::Handle
<JSObject
*> aGivenProto
) override
;
46 uint32_t Length() const { return mItems
.Length(); };
48 DataTransferItem
* Add(const nsAString
& aData
, const nsAString
& aType
,
49 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& rv
);
50 DataTransferItem
* Add(File
& aData
, nsIPrincipal
& aSubjectPrincipal
,
53 void Remove(uint32_t aIndex
, nsIPrincipal
& aSubjectPrincipal
,
56 DataTransferItem
* IndexedGetter(uint32_t aIndex
, bool& aFound
) const;
58 DataTransfer
* GetParentObject() const { return mDataTransfer
; }
60 void Clear(nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
);
62 already_AddRefed
<DataTransferItem
> SetDataWithPrincipal(
63 const nsAString
& aType
, nsIVariant
* aData
, uint32_t aIndex
,
64 nsIPrincipal
* aPrincipal
, bool aInsertOnly
, bool aHidden
,
67 already_AddRefed
<FileList
> Files(nsIPrincipal
* aPrincipal
);
69 // Moz-style helper methods for interacting with the stored data
70 void MozRemoveByTypeAt(const nsAString
& aType
, uint32_t aIndex
,
71 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
);
72 DataTransferItem
* MozItemByTypeAt(const nsAString
& aType
, uint32_t aIndex
);
73 const nsTArray
<RefPtr
<DataTransferItem
>>* MozItemsAt(uint32_t aIndex
);
74 uint32_t MozItemCount() const;
76 // Causes everything in indexes above 0 to shift down one index.
79 // Delete every item in the DataTransferItemList, without checking for
80 // permissions or read-only status (for internal use only).
83 void GetTypes(nsTArray
<nsString
>& aTypes
, CallerType aCallerType
) const;
84 bool HasType(const nsAString
& aType
) const;
88 void ClearDataHelper(DataTransferItem
* aItem
, uint32_t aIndexHint
,
89 uint32_t aMozOffsetHint
, nsIPrincipal
& aSubjectPrincipal
,
91 DataTransferItem
* AppendNewItem(uint32_t aIndex
, const nsAString
& aType
,
92 nsIVariant
* aData
, nsIPrincipal
* aPrincipal
,
94 void RegenerateFiles();
95 void GenerateFiles(FileList
* aFiles
, nsIPrincipal
* aFilesPrincipal
);
97 ~DataTransferItemList() = default;
99 RefPtr
<DataTransfer
> mDataTransfer
;
100 RefPtr
<FileList
> mFiles
;
101 // The principal for which mFiles is cached
102 nsCOMPtr
<nsIPrincipal
> mFilesPrincipal
;
103 // mItems is the list of items that corresponds to the spec concept of a
104 // DataTransferItemList. That is, this is the thing the spec's indexed getter
105 // operates on. The items in here are a subset of the items present in the
106 // arrays that live in mIndexedItems.
107 nsTArray
<RefPtr
<DataTransferItem
>> mItems
;
108 // mIndexedItems represents all our items. For any given index, all items at
109 // that index have different types in the GetType() sense. That means that
110 // representing multiple items with the same type (e.g. multiple files)
111 // requires using multiple indices.
113 // There is always a (possibly empty) list of items at index 0, so
114 // mIndexedItems.Length() >= 1 at all times.
115 nsTArray
<nsTArray
<RefPtr
<DataTransferItem
>>> mIndexedItems
;
119 } // namespace mozilla
121 #endif // mozilla_dom_DataTransferItemList_h