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 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 DataTransferItem(DataTransfer* aDataTransfer, const nsAString& aType, 39 eKind aKind = KIND_OTHER) 40 : mIndex(0) 41 , mChromeOnly(false) 42 , mKind(aKind) 43 , mType(aType) 44 , mDataTransfer(aDataTransfer) 45 { 46 MOZ_ASSERT(mDataTransfer, "Must be associated with a DataTransfer"); 47 } 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 GetKind(nsAString & aKind)57 void GetKind(nsAString& aKind) const 58 { 59 switch (mKind) { 60 case KIND_FILE: 61 aKind = NS_LITERAL_STRING("file"); 62 return; 63 case KIND_STRING: 64 aKind = NS_LITERAL_STRING("string"); 65 return; 66 default: 67 aKind = NS_LITERAL_STRING("other"); 68 return; 69 } 70 } 71 GetInternalType(nsAString & aType)72 void GetInternalType(nsAString& aType) const 73 { 74 aType = mType; 75 } 76 77 void GetType(nsAString& aType); 78 Kind()79 eKind Kind() const 80 { 81 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 GetParentObject()90 DataTransfer* GetParentObject() const 91 { 92 return mDataTransfer; 93 } 94 Principal()95 nsIPrincipal* Principal() const 96 { 97 return mPrincipal; 98 } SetPrincipal(nsIPrincipal * aPrincipal)99 void SetPrincipal(nsIPrincipal* aPrincipal) 100 { 101 mPrincipal = aPrincipal; 102 } 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 Index()111 uint32_t Index() const 112 { 113 return mIndex; 114 } SetIndex(uint32_t aIndex)115 void SetIndex(uint32_t aIndex) 116 { 117 mIndex = aIndex; 118 } 119 void FillInExternalData(); 120 ChromeOnly()121 bool ChromeOnly() const 122 { 123 return mChromeOnly; 124 } SetChromeOnly(bool aChromeOnly)125 void SetChromeOnly(bool aChromeOnly) 126 { 127 mChromeOnly = aChromeOnly; 128 } 129 130 static eKind KindFromData(nsIVariant* aData); 131 132 private: ~DataTransferItem()133 ~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 */ 154