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"
20 class FileSystemEntry
;
21 class FunctionStringCallback
;
23 class DataTransferItem final
: public nsISupports
, public nsWrapperCache
{
25 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
26 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DataTransferItem
);
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.
39 DataTransferItem(DataTransfer
* aDataTransfer
, const nsAString
& aType
,
40 eKind aKind
= KIND_OTHER
)
45 mDataTransfer(aDataTransfer
) {
46 MOZ_ASSERT(mDataTransfer
, "Must be associated with a DataTransfer");
49 already_AddRefed
<DataTransferItem
> Clone(DataTransfer
* aDataTransfer
) const;
51 virtual JSObject
* WrapObject(JSContext
* aCx
,
52 JS::Handle
<JSObject
*> aGivenProto
) override
;
54 void GetAsString(FunctionStringCallback
* aCallback
,
55 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
);
57 void GetKind(nsAString
& aKind
) const {
71 void GetInternalType(nsAString
& aType
) const { aType
= mType
; }
72 bool IsInternalType(const nsAString
& aType
) const { return aType
== mType
; }
74 void GetType(nsAString
& aType
);
76 eKind
Kind() const { return mKind
; }
78 already_AddRefed
<File
> GetAsFile(nsIPrincipal
& aSubjectPrincipal
,
81 already_AddRefed
<FileSystemEntry
> GetAsEntry(nsIPrincipal
& aSubjectPrincipal
,
84 DataTransfer
* GetParentObject() const { return mDataTransfer
; }
86 nsIPrincipal
* Principal() const { return mPrincipal
; }
87 void SetPrincipal(nsIPrincipal
* aPrincipal
) { mPrincipal
= aPrincipal
; }
89 // @return cached data, if available.
90 // otherwise: if available, `mDataTransfer`'s transferable data.
91 // otherwise the data is retrieved from the clipboard (for
92 // paste events) or the drag session.
93 already_AddRefed
<nsIVariant
> DataNoSecurityCheck();
94 // Data may return null if the clipboard state has changed since the type was
96 already_AddRefed
<nsIVariant
> Data(nsIPrincipal
* aPrincipal
, ErrorResult
& aRv
);
98 // Note: This can modify the mKind. Callers of this method must let the
99 // relevant DataTransfer know, because its types list can change as a result.
100 void SetData(nsIVariant
* aData
);
102 uint32_t Index() const { return mIndex
; }
103 void SetIndex(uint32_t aIndex
) { mIndex
= aIndex
; }
104 void FillInExternalData();
106 bool ChromeOnly() const { return mChromeOnly
; }
107 void SetChromeOnly(bool aChromeOnly
) { mChromeOnly
= aChromeOnly
; }
109 static eKind
KindFromData(nsIVariant
* aData
);
112 ~DataTransferItem() = default;
113 already_AddRefed
<File
> CreateFileFromInputStream(nsIInputStream
* aStream
);
114 already_AddRefed
<File
> CreateFileFromInputStream(
115 nsIInputStream
* aStream
, const char* aFileNameKey
,
116 const nsAString
& aContentType
);
118 already_AddRefed
<nsIGlobalObject
> GetGlobalFromDataTransfer();
120 // The index in the 2d mIndexedItems array
125 const nsString mType
;
126 nsCOMPtr
<nsIVariant
> mData
;
127 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
128 RefPtr
<DataTransfer
> mDataTransfer
;
130 // File cache for nsIFile application/x-moz-file entries.
131 RefPtr
<File
> mCachedFile
;
135 } // namespace mozilla
137 #endif /* mozilla_dom_DataTransferItem_h */