Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef mozilla_dom_DataTransferItem_h
7 : #define mozilla_dom_DataTransferItem_h
8 :
9 : #include "mozilla/ErrorResult.h"
10 : #include "mozilla/dom/DataTransfer.h"
11 : #include "mozilla/dom/DOMString.h"
12 : #include "mozilla/dom/File.h"
13 :
14 : namespace mozilla {
15 : namespace dom {
16 :
17 : class FileSystemEntry;
18 : class FunctionStringCallback;
19 :
20 : class DataTransferItem final : public nsISupports
21 : , public nsWrapperCache
22 : {
23 : public:
24 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
25 0 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DataTransferItem);
26 :
27 : public:
28 : // The spec only talks about the "file" and "string" kinds. Due to the Moz*
29 : // APIs, it is possible to attach any type to a DataTransferItem, meaning that
30 : // we can have other kinds then just FILE and STRING. These others are simply
31 : // marked as "other" and can only be produced throug the Moz* APIs.
32 : enum eKind {
33 : KIND_FILE,
34 : KIND_STRING,
35 : KIND_OTHER,
36 : };
37 :
38 0 : DataTransferItem(DataTransfer* aDataTransfer, const nsAString& aType,
39 : eKind aKind = KIND_OTHER)
40 0 : : mIndex(0)
41 : , mChromeOnly(false)
42 : , mKind(aKind)
43 : , mType(aType)
44 0 : , mDataTransfer(aDataTransfer)
45 : {
46 0 : MOZ_ASSERT(mDataTransfer, "Must be associated with a DataTransfer");
47 0 : }
48 :
49 : already_AddRefed<DataTransferItem> Clone(DataTransfer* aDataTransfer) const;
50 :
51 : virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
52 :
53 : void GetAsString(FunctionStringCallback* aCallback,
54 : nsIPrincipal& aSubjectPrincipal,
55 : ErrorResult& aRv);
56 :
57 0 : void GetKind(nsAString& aKind) const
58 : {
59 0 : switch (mKind) {
60 : case KIND_FILE:
61 0 : aKind = NS_LITERAL_STRING("file");
62 0 : return;
63 : case KIND_STRING:
64 0 : aKind = NS_LITERAL_STRING("string");
65 0 : return;
66 : default:
67 0 : aKind = NS_LITERAL_STRING("other");
68 0 : return;
69 : }
70 : }
71 :
72 0 : void GetInternalType(nsAString& aType) const
73 : {
74 0 : aType = mType;
75 0 : }
76 :
77 : void GetType(nsAString& aType);
78 :
79 0 : eKind Kind() const
80 : {
81 0 : return mKind;
82 : }
83 :
84 : already_AddRefed<File>
85 : GetAsFile(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
86 :
87 : already_AddRefed<FileSystemEntry>
88 : GetAsEntry(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
89 :
90 0 : DataTransfer* GetParentObject() const
91 : {
92 0 : return mDataTransfer;
93 : }
94 :
95 0 : nsIPrincipal* Principal() const
96 : {
97 0 : return mPrincipal;
98 : }
99 0 : void SetPrincipal(nsIPrincipal* aPrincipal)
100 : {
101 0 : mPrincipal = aPrincipal;
102 0 : }
103 :
104 : already_AddRefed<nsIVariant> DataNoSecurityCheck();
105 : already_AddRefed<nsIVariant> Data(nsIPrincipal* aPrincipal, ErrorResult& aRv);
106 :
107 : // Note: This can modify the mKind. Callers of this method must let the
108 : // relevant DataTransfer know, because its types list can change as a result.
109 : void SetData(nsIVariant* aData);
110 :
111 0 : uint32_t Index() const
112 : {
113 0 : return mIndex;
114 : }
115 0 : void SetIndex(uint32_t aIndex)
116 : {
117 0 : mIndex = aIndex;
118 0 : }
119 : void FillInExternalData();
120 :
121 0 : bool ChromeOnly() const
122 : {
123 0 : return mChromeOnly;
124 : }
125 0 : void SetChromeOnly(bool aChromeOnly)
126 : {
127 0 : mChromeOnly = aChromeOnly;
128 0 : }
129 :
130 : static eKind KindFromData(nsIVariant* aData);
131 :
132 : private:
133 0 : ~DataTransferItem() {}
134 : already_AddRefed<File> CreateFileFromInputStream(nsIInputStream* aStream);
135 :
136 : // The index in the 2d mIndexedItems array
137 : uint32_t mIndex;
138 :
139 : bool mChromeOnly;
140 : eKind mKind;
141 : const nsString mType;
142 : nsCOMPtr<nsIVariant> mData;
143 : nsCOMPtr<nsIPrincipal> mPrincipal;
144 : RefPtr<DataTransfer> mDataTransfer;
145 :
146 : // File cache for nsIFile application/x-moz-file entries.
147 : RefPtr<File> mCachedFile;
148 : };
149 :
150 : } // namespace dom
151 : } // namespace mozilla
152 :
153 : #endif /* mozilla_dom_DataTransferItem_h */
|