1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is the Mozilla Corporation.
18 : * Portions created by the Initial Developer are Copyright (C) 2008
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Neil Deakin <enndeakin@gmail.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsDOMDataTransfer_h__
39 : #define nsDOMDataTransfer_h__
40 :
41 : #include "nsString.h"
42 : #include "nsTArray.h"
43 : #include "nsIVariant.h"
44 : #include "nsIPrincipal.h"
45 : #include "nsIDOMDataTransfer.h"
46 : #include "nsIDragService.h"
47 : #include "nsIDOMElement.h"
48 : #include "nsCycleCollectionParticipant.h"
49 :
50 : #include "nsAutoPtr.h"
51 : #include "nsIFile.h"
52 : #include "nsILocalFile.h"
53 : #include "nsDOMFile.h"
54 :
55 : class nsITransferable;
56 :
57 : /**
58 : * TransferItem is used to hold data for a particular format. Each piece of
59 : * data has a principal set from the caller which added it. This allows a
60 : * caller that wishes to retrieve the data to only be able to access the data
61 : * it is allowed to, yet still allow a chrome caller to retrieve any of the
62 : * data.
63 : */
64 0 : struct TransferItem {
65 : nsString mFormat;
66 : nsCOMPtr<nsIPrincipal> mPrincipal;
67 : nsCOMPtr<nsIVariant> mData;
68 : };
69 :
70 : class nsDOMDataTransfer : public nsIDOMDataTransfer
71 : {
72 : public:
73 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
74 : NS_DECL_NSIDOMDATATRANSFER
75 :
76 1464 : NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsDOMDataTransfer, nsIDOMDataTransfer)
77 :
78 : friend class nsDOMDragEvent;
79 : friend class nsEventStateManager;
80 : friend class nsContentUtils;
81 :
82 : protected:
83 :
84 : // the constructors are protected so only our friends can call them
85 :
86 : // default constructor used for the dragstart/draggesture event and
87 : // synthetic events
88 : nsDOMDataTransfer();
89 :
90 : // this constructor must only be used to create a dataTransfer for a drag
91 : // that was started without using a data transfer, either an external drag,
92 : // that is, a drag where the source is another application, or a drag
93 : // started by calling the drag service directly.
94 : nsDOMDataTransfer(PRUint32 aEventType);
95 :
96 : // this constructor is used only by the Clone method to copy the fields as
97 : // needed to a new data transfer.
98 : nsDOMDataTransfer(PRUint32 aEventType,
99 : const PRUint32 aEffectAllowed,
100 : bool aCursorState,
101 : bool aIsExternal,
102 : bool aUserCancelled,
103 : nsTArray<nsTArray<TransferItem> >& aItems,
104 : nsIDOMElement* aDragImage,
105 : PRUint32 aDragImageX,
106 : PRUint32 aDragImageY);
107 :
108 0 : ~nsDOMDataTransfer()
109 0 : {
110 0 : if (mFiles) {
111 0 : mFiles->Disconnect();
112 : }
113 0 : }
114 :
115 : static const char sEffects[8][9];
116 :
117 : public:
118 :
119 0 : void GetDragTarget(nsIDOMElement** aDragTarget)
120 : {
121 0 : *aDragTarget = mDragTarget;
122 0 : NS_IF_ADDREF(*aDragTarget);
123 0 : }
124 :
125 : // a readonly dataTransfer cannot have new data added or existing data removed.
126 : // Only the dropEffect and effectAllowed may be modified.
127 0 : void SetReadOnly() { mReadOnly = true; }
128 :
129 : // converts the data into an array of nsITransferable objects to be used for
130 : // drag and drop or clipboard operations.
131 : void GetTransferables(nsISupportsArray** transferables);
132 :
133 : // converts the data in the variant to an nsISupportString if possible or
134 : // an nsISupports or null otherwise.
135 : bool ConvertFromVariant(nsIVariant* aVariant,
136 : nsISupports** aSupports,
137 : PRUint32* aLength);
138 :
139 : // clears all of the data
140 : void ClearAll();
141 :
142 : // Similar to SetData except also specifies the principal to store.
143 : // aData may be null when called from CacheExternalFormats.
144 : nsresult SetDataWithPrincipal(const nsAString& aFormat,
145 : nsIVariant* aData,
146 : PRUint32 aIndex,
147 : nsIPrincipal* aPrincipal);
148 :
149 : protected:
150 :
151 : // returns a weak reference to the drag image
152 0 : nsIDOMElement* GetDragImage(PRInt32* aX, PRInt32* aY)
153 : {
154 0 : *aX = mDragImageX;
155 0 : *aY = mDragImageY;
156 0 : return mDragImage;
157 : }
158 :
159 : // returns a weak reference to the current principal
160 : nsIPrincipal* GetCurrentPrincipal(nsresult* rv);
161 :
162 : // converts some formats used for compatibility in aInFormat into aOutFormat.
163 : // Text and text/unicode become text/plain, and URL becomes text/uri-list
164 : void GetRealFormat(const nsAString& aInFormat, nsAString& aOutFormat);
165 :
166 : // caches the formats that exist in the drag service that were added by an
167 : // external drag
168 : void CacheExternalFormats();
169 :
170 : // fills in the data field of aItem with the data from the drag service for
171 : // a given index.
172 : void FillInExternalDragData(TransferItem& aItem, PRUint32 aIndex);
173 :
174 : // the event type this data transfer is for. This will correspond to an
175 : // event->message value.
176 : PRUint32 mEventType;
177 :
178 : // the drop effect and effect allowed
179 : PRUint32 mDropEffect;
180 : PRUint32 mEffectAllowed;
181 :
182 : // Indicates the behavior of the cursor during drag operations
183 : bool mCursorState;
184 :
185 : // readonly data transfers may not be modified except the drop effect and
186 : // effect allowed.
187 : bool mReadOnly;
188 :
189 : // true for drags started without a data transfer, for example, those from
190 : // another application.
191 : bool mIsExternal;
192 :
193 : // true if the user cancelled the drag. Used only for the dragend event.
194 : bool mUserCancelled;
195 :
196 : // array of items, each containing an array of format->data pairs
197 : nsTArray<nsTArray<TransferItem> > mItems;
198 :
199 : // array of files, containing only the files present in the dataTransfer
200 : nsRefPtr<nsDOMFileList> mFiles;
201 :
202 : // the target of the drag. The drag and dragend events will fire at this.
203 : nsCOMPtr<nsIDOMElement> mDragTarget;
204 :
205 : // the custom drag image and coordinates within the image. If mDragImage is
206 : // null, the default image is created from the drag target.
207 : nsCOMPtr<nsIDOMElement> mDragImage;
208 : PRUint32 mDragImageX;
209 : PRUint32 mDragImageY;
210 : };
211 :
212 : #endif // nsDOMDataTransfer_h__
213 :
|