Bug 1914004 - Part 1: Add RootedTuple and RootedField to allow rooting multiple thing...
[gecko.git] / dom / events / DataTransferItem.h
blob1e98ba8b921c861a6e64cfe6998fdfa0a54fd708
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_DataTransferItem_h
8 #define mozilla_dom_DataTransferItem_h
10 #include "mozilla/dom/DataTransfer.h"
11 #include "mozilla/dom/DOMString.h"
12 #include "mozilla/dom/File.h"
14 namespace mozilla {
15 class ErrorResult;
17 namespace dom {
19 class DataTransfer;
20 class FileSystemEntry;
21 class FunctionStringCallback;
23 class DataTransferItem final : public nsISupports, public nsWrapperCache {
24 public:
25 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
26 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DataTransferItem);
28 public:
29 // The spec only talks about the "file" and "string" kinds. Due to the Moz*
30 // APIs, it is possible to attach any type to a DataTransferItem, meaning that
31 // we can have other kinds then just FILE and STRING. These others are simply
32 // marked as "other" and can only be produced throug the Moz* APIs.
33 enum eKind {
34 KIND_FILE,
35 KIND_STRING,
36 KIND_OTHER,
39 DataTransferItem(DataTransfer* aDataTransfer, const nsAString& aType,
40 eKind aKind = KIND_OTHER)
41 : mIndex(0),
42 mChromeOnly(false),
43 mKind(aKind),
44 mType(aType),
45 mDoNotAttemptToLoadData(false),
46 mDataTransfer(aDataTransfer) {
47 MOZ_ASSERT(mDataTransfer, "Must be associated with a DataTransfer");
50 already_AddRefed<DataTransferItem> Clone(DataTransfer* aDataTransfer) const;
52 virtual JSObject* WrapObject(JSContext* aCx,
53 JS::Handle<JSObject*> aGivenProto) override;
55 void GetAsString(FunctionStringCallback* aCallback,
56 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
58 void GetKind(nsAString& aKind) const {
59 switch (mKind) {
60 case KIND_FILE:
61 aKind = u"file"_ns;
62 return;
63 case KIND_STRING:
64 aKind = u"string"_ns;
65 return;
66 default:
67 aKind = u"other"_ns;
68 return;
72 void GetInternalType(nsAString& aType) const { aType = mType; }
73 bool IsInternalType(const nsAString& aType) const { return aType == mType; }
75 void GetType(nsAString& aType);
77 eKind Kind() const { return mKind; }
79 already_AddRefed<File> GetAsFile(nsIPrincipal& aSubjectPrincipal,
80 ErrorResult& aRv);
82 already_AddRefed<FileSystemEntry> GetAsEntry(nsIPrincipal& aSubjectPrincipal,
83 ErrorResult& aRv);
85 DataTransfer* GetParentObject() const { return mDataTransfer; }
87 nsIPrincipal* Principal() const { return mPrincipal; }
88 void SetPrincipal(nsIPrincipal* aPrincipal) { mPrincipal = aPrincipal; }
90 // @return cached data, if available.
91 // otherwise: if available, `mDataTransfer`'s transferable data.
92 // otherwise the data is retrieved from the clipboard (for
93 // paste events) or the drag session.
94 already_AddRefed<nsIVariant> DataNoSecurityCheck();
95 // Data may return null if the clipboard state has changed since the type was
96 // detected.
97 already_AddRefed<nsIVariant> Data(nsIPrincipal* aPrincipal, ErrorResult& aRv);
99 // Note: This can modify the mKind. Callers of this method must let the
100 // relevant DataTransfer know, because its types list can change as a result.
101 void SetData(nsIVariant* aData);
103 uint32_t Index() const { return mIndex; }
104 void SetIndex(uint32_t aIndex) { mIndex = aIndex; }
105 void FillInExternalData();
107 bool ChromeOnly() const { return mChromeOnly; }
108 void SetChromeOnly(bool aChromeOnly) { mChromeOnly = aChromeOnly; }
110 static eKind KindFromData(nsIVariant* aData);
112 private:
113 ~DataTransferItem() = default;
114 already_AddRefed<File> CreateFileFromInputStream(nsIInputStream* aStream);
115 already_AddRefed<File> CreateFileFromInputStream(
116 nsIInputStream* aStream, const char* aFileNameKey,
117 const nsAString& aContentType);
119 // The index in the 2d mIndexedItems array
120 uint32_t mIndex;
122 bool mChromeOnly;
123 eKind mKind;
124 const nsString mType;
125 nsCOMPtr<nsIVariant> mData;
126 bool mDoNotAttemptToLoadData;
127 nsCOMPtr<nsIPrincipal> mPrincipal;
128 RefPtr<DataTransfer> mDataTransfer;
130 // File cache for nsIFile application/x-moz-file entries.
131 RefPtr<File> mCachedFile;
134 } // namespace dom
135 } // namespace mozilla
137 #endif /* mozilla_dom_DataTransferItem_h */