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 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 {
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
,
82 already_AddRefed
<FileSystemEntry
> GetAsEntry(nsIPrincipal
& aSubjectPrincipal
,
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
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
);
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
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
;
135 } // namespace mozilla
137 #endif /* mozilla_dom_DataTransferItem_h */