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