Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / events / DataTransferItem.h
blob3978aaa7a9d1cc6fa086c34c1415a89455eedef5
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 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 {
58 switch (mKind) {
59 case KIND_FILE:
60 aKind = u"file"_ns;
61 return;
62 case KIND_STRING:
63 aKind = u"string"_ns;
64 return;
65 default:
66 aKind = u"other"_ns;
67 return;
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,
79 ErrorResult& aRv);
81 already_AddRefed<FileSystemEntry> GetAsEntry(nsIPrincipal& aSubjectPrincipal,
82 ErrorResult& aRv);
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
95 // detected.
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);
111 private:
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
121 uint32_t mIndex;
123 bool mChromeOnly;
124 eKind mKind;
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;
134 } // namespace dom
135 } // namespace mozilla
137 #endif /* mozilla_dom_DataTransferItem_h */