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