1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/events/DataTransfer.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,302 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_dom_DataTransfer_h 1.10 +#define mozilla_dom_DataTransfer_h 1.11 + 1.12 +#include "nsString.h" 1.13 +#include "nsTArray.h" 1.14 +#include "nsIVariant.h" 1.15 +#include "nsIPrincipal.h" 1.16 +#include "nsIDOMDataTransfer.h" 1.17 +#include "nsIDOMElement.h" 1.18 +#include "nsIDragService.h" 1.19 +#include "nsCycleCollectionParticipant.h" 1.20 + 1.21 +#include "nsAutoPtr.h" 1.22 +#include "nsDOMFile.h" 1.23 +#include "mozilla/Attributes.h" 1.24 + 1.25 +class nsINode; 1.26 +class nsITransferable; 1.27 +class nsISupportsArray; 1.28 +class nsILoadContext; 1.29 + 1.30 +namespace mozilla { 1.31 + 1.32 +class EventStateManager; 1.33 + 1.34 +namespace dom { 1.35 + 1.36 +class DOMStringList; 1.37 +class Element; 1.38 +template<typename T> class Optional; 1.39 + 1.40 +/** 1.41 + * TransferItem is used to hold data for a particular format. Each piece of 1.42 + * data has a principal set from the caller which added it. This allows a 1.43 + * caller that wishes to retrieve the data to only be able to access the data 1.44 + * it is allowed to, yet still allow a chrome caller to retrieve any of the 1.45 + * data. 1.46 + */ 1.47 +struct TransferItem { 1.48 + nsString mFormat; 1.49 + nsCOMPtr<nsIPrincipal> mPrincipal; 1.50 + nsCOMPtr<nsIVariant> mData; 1.51 +}; 1.52 + 1.53 +#define NS_DATATRANSFER_IID \ 1.54 +{ 0x43ee0327, 0xde5d, 0x463d, \ 1.55 + { 0x9b, 0xd0, 0xf1, 0x79, 0x09, 0x69, 0xf2, 0xfb } } 1.56 + 1.57 +class DataTransfer MOZ_FINAL : public nsIDOMDataTransfer, 1.58 + public nsWrapperCache 1.59 +{ 1.60 +public: 1.61 + NS_DECLARE_STATIC_IID_ACCESSOR(NS_DATATRANSFER_IID) 1.62 + 1.63 + NS_DECL_CYCLE_COLLECTING_ISUPPORTS 1.64 + NS_DECL_NSIDOMDATATRANSFER 1.65 + 1.66 + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DataTransfer) 1.67 + 1.68 + friend class mozilla::EventStateManager; 1.69 + 1.70 +protected: 1.71 + 1.72 + // hide the default constructor 1.73 + DataTransfer(); 1.74 + 1.75 + // this constructor is used only by the Clone method to copy the fields as 1.76 + // needed to a new data transfer. 1.77 + DataTransfer(nsISupports* aParent, 1.78 + uint32_t aEventType, 1.79 + const uint32_t aEffectAllowed, 1.80 + bool aCursorState, 1.81 + bool aIsExternal, 1.82 + bool aUserCancelled, 1.83 + bool aIsCrossDomainSubFrameDrop, 1.84 + int32_t aClipboardType, 1.85 + nsTArray<nsTArray<TransferItem> >& aItems, 1.86 + Element* aDragImage, 1.87 + uint32_t aDragImageX, 1.88 + uint32_t aDragImageY); 1.89 + 1.90 + ~DataTransfer(); 1.91 + 1.92 + static const char sEffects[8][9]; 1.93 + 1.94 +public: 1.95 + 1.96 + // Constructor for DataTransfer. 1.97 + // 1.98 + // aEventType is an event constant (such as NS_DRAGDROP_START) 1.99 + // 1.100 + // aIsExternal must only be true when used to create a dataTransfer for a 1.101 + // paste or a drag that was started without using a data transfer. The 1.102 + // latter will occur when an external drag occurs, that is, a drag where the 1.103 + // source is another application, or a drag is started by calling the drag 1.104 + // service directly. For clipboard operations, aClipboardType indicates 1.105 + // which clipboard to use, from nsIClipboard, or -1 for non-clipboard operations, 1.106 + // or if access to the system clipboard should not be allowed. 1.107 + DataTransfer(nsISupports* aParent, uint32_t aEventType, bool aIsExternal, 1.108 + int32_t aClipboardType); 1.109 + 1.110 + virtual JSObject* WrapObject(JSContext* aCx); 1.111 + nsISupports* GetParentObject() 1.112 + { 1.113 + return mParent; 1.114 + } 1.115 + 1.116 + void SetParentObject(nsISupports* aNewParent) 1.117 + { 1.118 + MOZ_ASSERT(aNewParent); 1.119 + // Setting the parent after we've been wrapped is pointless, so 1.120 + // make sure we aren't wrapped yet. 1.121 + MOZ_ASSERT(!GetWrapperPreserveColor()); 1.122 + mParent = aNewParent; 1.123 + } 1.124 + 1.125 + static already_AddRefed<DataTransfer> 1.126 + Constructor(const GlobalObject& aGlobal, const nsAString& aEventType, 1.127 + bool aIsExternal, ErrorResult& aRv); 1.128 + 1.129 + void GetDropEffect(nsString& aDropEffect) 1.130 + { 1.131 + aDropEffect.AssignASCII(sEffects[mDropEffect]); 1.132 + } 1.133 + void GetEffectAllowed(nsString& aEffectAllowed) 1.134 + { 1.135 + if (mEffectAllowed == nsIDragService::DRAGDROP_ACTION_UNINITIALIZED) { 1.136 + aEffectAllowed.AssignLiteral("uninitialized"); 1.137 + } else { 1.138 + aEffectAllowed.AssignASCII(sEffects[mEffectAllowed]); 1.139 + } 1.140 + } 1.141 + void SetDragImage(Element& aElement, int32_t aX, int32_t aY, 1.142 + ErrorResult& aRv); 1.143 + already_AddRefed<DOMStringList> Types(); 1.144 + void GetData(const nsAString& aFormat, nsAString& aData, ErrorResult& aRv); 1.145 + void SetData(const nsAString& aFormat, const nsAString& aData, 1.146 + ErrorResult& aRv); 1.147 + void ClearData(const mozilla::dom::Optional<nsAString>& aFormat, 1.148 + mozilla::ErrorResult& aRv); 1.149 + nsDOMFileList* GetFiles(mozilla::ErrorResult& aRv); 1.150 + void AddElement(Element& aElement, mozilla::ErrorResult& aRv); 1.151 + uint32_t MozItemCount() 1.152 + { 1.153 + return mItems.Length(); 1.154 + } 1.155 + void GetMozCursor(nsString& aCursor) 1.156 + { 1.157 + if (mCursorState) { 1.158 + aCursor.AssignLiteral("default"); 1.159 + } else { 1.160 + aCursor.AssignLiteral("auto"); 1.161 + } 1.162 + } 1.163 + already_AddRefed<DOMStringList> MozTypesAt(uint32_t aIndex, 1.164 + mozilla::ErrorResult& aRv); 1.165 + void MozClearDataAt(const nsAString& aFormat, uint32_t aIndex, 1.166 + mozilla::ErrorResult& aRv); 1.167 + void MozSetDataAt(JSContext* aCx, const nsAString& aFormat, 1.168 + JS::Handle<JS::Value> aData, uint32_t aIndex, 1.169 + mozilla::ErrorResult& aRv); 1.170 + void MozGetDataAt(JSContext* aCx, const nsAString& aFormat, 1.171 + uint32_t aIndex, JS::MutableHandle<JS::Value> aRetval, 1.172 + mozilla::ErrorResult& aRv); 1.173 + bool MozUserCancelled() 1.174 + { 1.175 + return mUserCancelled; 1.176 + } 1.177 + already_AddRefed<nsINode> GetMozSourceNode(); 1.178 + 1.179 + mozilla::dom::Element* GetDragTarget() 1.180 + { 1.181 + return mDragTarget; 1.182 + } 1.183 + 1.184 + // a readonly dataTransfer cannot have new data added or existing data removed. 1.185 + // Only the dropEffect and effectAllowed may be modified. 1.186 + void SetReadOnly() { mReadOnly = true; } 1.187 + 1.188 + // converts the data into an array of nsITransferable objects to be used for 1.189 + // drag and drop or clipboard operations. 1.190 + already_AddRefed<nsISupportsArray> GetTransferables(nsIDOMNode* aDragTarget); 1.191 + 1.192 + // converts the data for a single item at aIndex into an nsITransferable object. 1.193 + already_AddRefed<nsITransferable> GetTransferable(uint32_t aIndex, 1.194 + nsILoadContext* aLoadContext); 1.195 + 1.196 + // converts the data in the variant to an nsISupportString if possible or 1.197 + // an nsISupports or null otherwise. 1.198 + bool ConvertFromVariant(nsIVariant* aVariant, 1.199 + nsISupports** aSupports, 1.200 + uint32_t* aLength); 1.201 + 1.202 + // clears all of the data 1.203 + void ClearAll(); 1.204 + 1.205 + // Similar to SetData except also specifies the principal to store. 1.206 + // aData may be null when called from CacheExternalDragFormats or 1.207 + // CacheExternalClipboardFormats. 1.208 + nsresult SetDataWithPrincipal(const nsAString& aFormat, 1.209 + nsIVariant* aData, 1.210 + uint32_t aIndex, 1.211 + nsIPrincipal* aPrincipal); 1.212 + 1.213 + // returns a weak reference to the drag image 1.214 + Element* GetDragImage(int32_t* aX, int32_t* aY) 1.215 + { 1.216 + *aX = mDragImageX; 1.217 + *aY = mDragImageY; 1.218 + return mDragImage; 1.219 + } 1.220 + 1.221 + nsresult Clone(nsISupports* aParent, uint32_t aEventType, bool aUserCancelled, 1.222 + bool aIsCrossDomainSubFrameDrop, DataTransfer** aResult); 1.223 + 1.224 +protected: 1.225 + 1.226 + // returns a weak reference to the current principal 1.227 + nsIPrincipal* GetCurrentPrincipal(nsresult* rv); 1.228 + 1.229 + // converts some formats used for compatibility in aInFormat into aOutFormat. 1.230 + // Text and text/unicode become text/plain, and URL becomes text/uri-list 1.231 + void GetRealFormat(const nsAString& aInFormat, nsAString& aOutFormat); 1.232 + 1.233 + // caches text and uri-list data formats that exist in the drag service or 1.234 + // clipboard for retrieval later. 1.235 + void CacheExternalData(const char* aFormat, uint32_t aIndex, nsIPrincipal* aPrincipal); 1.236 + 1.237 + // caches the formats that exist in the drag service that were added by an 1.238 + // external drag 1.239 + void CacheExternalDragFormats(); 1.240 + 1.241 + // caches the formats that exist in the clipboard 1.242 + void CacheExternalClipboardFormats(); 1.243 + 1.244 + // fills in the data field of aItem with the data from the drag service or 1.245 + // clipboard for a given index. 1.246 + void FillInExternalData(TransferItem& aItem, uint32_t aIndex); 1.247 + 1.248 + void MozClearDataAtHelper(const nsAString& aFormat, uint32_t aIndex, 1.249 + mozilla::ErrorResult& aRv); 1.250 + 1.251 + nsCOMPtr<nsISupports> mParent; 1.252 + 1.253 + // the event type this data transfer is for. This will correspond to an 1.254 + // event->message value. 1.255 + uint32_t mEventType; 1.256 + 1.257 + // the drop effect and effect allowed 1.258 + uint32_t mDropEffect; 1.259 + uint32_t mEffectAllowed; 1.260 + 1.261 + // Indicates the behavior of the cursor during drag operations 1.262 + bool mCursorState; 1.263 + 1.264 + // readonly data transfers may not be modified except the drop effect and 1.265 + // effect allowed. 1.266 + bool mReadOnly; 1.267 + 1.268 + // true for drags started without a data transfer, for example, those from 1.269 + // another application. 1.270 + bool mIsExternal; 1.271 + 1.272 + // true if the user cancelled the drag. Used only for the dragend event. 1.273 + bool mUserCancelled; 1.274 + 1.275 + // true if this is a cross-domain drop from a subframe where access to the 1.276 + // data should be prevented 1.277 + bool mIsCrossDomainSubFrameDrop; 1.278 + 1.279 + // Indicates which clipboard type to use for clipboard operations. Ignored for 1.280 + // drag and drop. 1.281 + int32_t mClipboardType; 1.282 + 1.283 + // array of items, each containing an array of format->data pairs 1.284 + nsTArray<nsTArray<TransferItem> > mItems; 1.285 + 1.286 + // array of files, containing only the files present in the dataTransfer 1.287 + nsRefPtr<nsDOMFileList> mFiles; 1.288 + 1.289 + // the target of the drag. The drag and dragend events will fire at this. 1.290 + nsCOMPtr<mozilla::dom::Element> mDragTarget; 1.291 + 1.292 + // the custom drag image and coordinates within the image. If mDragImage is 1.293 + // null, the default image is created from the drag target. 1.294 + nsCOMPtr<mozilla::dom::Element> mDragImage; 1.295 + uint32_t mDragImageX; 1.296 + uint32_t mDragImageY; 1.297 +}; 1.298 + 1.299 +NS_DEFINE_STATIC_IID_ACCESSOR(DataTransfer, NS_DATATRANSFER_IID) 1.300 + 1.301 +} // namespace dom 1.302 +} // namespace mozilla 1.303 + 1.304 +#endif /* mozilla_dom_DataTransfer_h */ 1.305 +