Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
6 #ifndef _NSDATAOBJ_H_
7 #define _NSDATAOBJ_H_
9 #include <oleidl.h>
10 #include <shldisp.h>
12 #include "nsCOMPtr.h"
13 #include "nsString.h"
14 #include "nsIFile.h"
15 #include "nsIURI.h"
16 #include "nsIInputStream.h"
17 #include "nsIStreamListener.h"
18 #include "nsIChannel.h"
19 #include "nsCOMArray.h"
20 #include "nsITimer.h"
22 class nsIThread;
24 // The SDK shipping with VC11 has renamed IAsyncOperation to
25 // IDataObjectAsyncCapability. We try to detect this, and rename this in our
26 // code too to make sure that we pick the correct name when building.
27 #ifdef __IDataObjectAsyncCapability_INTERFACE_DEFINED__
28 #define IAsyncOperation IDataObjectAsyncCapability
29 #define IID_IAsyncOperation IID_IDataObjectAsyncCapability
30 #else
31 // XXX for older version of PSDK where IAsyncOperation and related stuff is not available
32 // but thisdefine should be removed when parocles config is updated
33 #ifndef __IAsyncOperation_INTERFACE_DEFINED__
34 // IAsyncOperation interface definition
35 EXTERN_C const IID IID_IAsyncOperation;
37 MIDL_INTERFACE("3D8B0590-F691-11d2-8EA9-006097DF5BD4")
38 IAsyncOperation : public IUnknown
39 {
40 virtual HRESULT STDMETHODCALLTYPE SetAsyncMode(BOOL fDoOpAsync) = 0;
41 virtual HRESULT STDMETHODCALLTYPE GetAsyncMode(BOOL *pfIsOpAsync) = 0;
42 virtual HRESULT STDMETHODCALLTYPE StartOperation(IBindCtx *pbcReserved) = 0;
43 virtual HRESULT STDMETHODCALLTYPE InOperation(BOOL *pfInAsyncOp) = 0;
44 virtual HRESULT STDMETHODCALLTYPE EndOperation(HRESULT hResult,
45 IBindCtx *pbcReserved,
46 DWORD dwEffects) = 0;
47 };
48 // this is not defined in the old headers for some reason
49 #ifndef FD_PROGRESSUI
50 #define FD_PROGRESSUI 0x4000
51 #endif
53 #endif // __IAsyncOperation_INTERFACE_DEFINED__
54 #endif // __IDataObjectAsyncCapability_INTERFACE_DEFINED__
56 /*
57 * CFSTR_SHELLURL is deprecated and doesn't have a Unicode version.
58 * Therefore we are using CFSTR_INETURL instead of CFSTR_SHELLURL.
59 * See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_basics/shell_basics_programming/transferring/clipboard.asp
60 */
61 #ifndef CFSTR_INETURLA
62 #define CFSTR_INETURLA L"UniformResourceLocator"
63 #endif
64 #ifndef CFSTR_INETURLW
65 #define CFSTR_INETURLW L"UniformResourceLocatorW"
66 #endif
68 // For support of MinGW w32api v2.4.
69 // When the next version of w32api is released with shlobj.h rev 1.35
70 // http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/winsup/w32api/include/shlobj.h?cvsroot=src
71 // then that can be made the base required version and this code should be removed.
72 #ifndef CFSTR_FILEDESCRIPTORA
73 # define CFSTR_FILEDESCRIPTORA L"FileGroupDescriptor"
74 #endif
75 #ifndef CFSTR_FILEDESCRIPTORW
76 # define CFSTR_FILEDESCRIPTORW L"FileGroupDescriptorW"
77 #endif
79 class CEnumFormatEtc;
80 class nsITransferable;
82 /*
83 * This ole registered class is used to facilitate drag-drop of objects which
84 * can be adapted by an object derived from CfDragDrop. The CfDragDrop is
85 * associated with instances via SetDragDrop().
86 */
87 class nsDataObj : public IDataObject,
88 public IAsyncOperation
89 {
91 protected:
92 nsCOMPtr<nsIThread> mIOThread;
94 public: // construction, destruction
95 nsDataObj(nsIURI *uri = nullptr);
96 virtual ~nsDataObj();
98 public: // IUnknown methods - see iunknown.h for documentation
99 STDMETHODIMP_(ULONG) AddRef ();
100 STDMETHODIMP QueryInterface(REFIID, void**);
101 STDMETHODIMP_(ULONG) Release ();
103 // support for clipboard
104 void AddDataFlavor(const char* aDataFlavor, LPFORMATETC aFE);
105 void SetTransferable(nsITransferable * aTransferable);
107 // Return the registered OLE class ID of this object's CfDataObj.
108 CLSID GetClassID() const;
110 public: // IDataObject methods - these are general comments. see CfDragDrop
111 // for overriding behavior
113 // Store data in pSTM according to the format specified by pFE, if the
114 // format is supported (supported formats are specified in CfDragDrop::
115 // GetFormats) and return NOERROR; otherwise return DATA_E_FORMATETC. It
116 // is the callers responsibility to free pSTM if NOERROR is returned.
117 STDMETHODIMP GetData (LPFORMATETC pFE, LPSTGMEDIUM pSTM);
119 // Similar to GetData except that the caller allocates the structure
120 // referenced by pSTM.
121 STDMETHODIMP GetDataHere (LPFORMATETC pFE, LPSTGMEDIUM pSTM);
123 // Returns S_TRUE if this object supports the format specified by pSTM,
124 // S_FALSE otherwise.
125 STDMETHODIMP QueryGetData (LPFORMATETC pFE);
127 // Set pCanonFE to the canonical format of pFE if one exists and return
128 // NOERROR, otherwise return DATA_S_SAMEFORMATETC. A canonical format
129 // implies an identical rendering.
130 STDMETHODIMP GetCanonicalFormatEtc (LPFORMATETC pFE, LPFORMATETC pCanonFE);
132 // Set this objects data according to the format specified by pFE and
133 // the storage medium specified by pSTM and return NOERROR, if the format
134 // is supported. If release is TRUE this object must release the storage
135 // associated with pSTM.
136 STDMETHODIMP SetData (LPFORMATETC pFE, LPSTGMEDIUM pSTM, BOOL release);
138 // Set ppEnum to an IEnumFORMATETC object which will iterate all of the
139 // data formats that this object supports. direction is either DATADIR_GET
140 // or DATADIR_SET.
141 STDMETHODIMP EnumFormatEtc (DWORD direction, LPENUMFORMATETC* ppEnum);
143 // Set up an advisory connection to this object based on the format specified
144 // by pFE, flags, and the pAdvise. Set pConn to the established advise
145 // connection.
146 STDMETHODIMP DAdvise (LPFORMATETC pFE, DWORD flags, LPADVISESINK pAdvise,
147 DWORD* pConn);
149 // Turn off advising of a previous call to DAdvise which set pConn.
150 STDMETHODIMP DUnadvise (DWORD pConn);
152 // Set ppEnum to an IEnumSTATDATA object which will iterate over the
153 // existing objects which have established advisory connections to this
154 // object.
155 STDMETHODIMP EnumDAdvise (LPENUMSTATDATA *ppEnum);
157 // IAsyncOperation methods
158 STDMETHOD(EndOperation)(HRESULT hResult, IBindCtx *pbcReserved, DWORD dwEffects);
159 STDMETHOD(GetAsyncMode)(BOOL *pfIsOpAsync);
160 STDMETHOD(InOperation)(BOOL *pfInAsyncOp);
161 STDMETHOD(SetAsyncMode)(BOOL fDoOpAsync);
162 STDMETHOD(StartOperation)(IBindCtx *pbcReserved);
164 public: // other methods
166 // Gets the filename from the kFilePromiseURLMime flavour
167 HRESULT GetDownloadDetails(nsIURI **aSourceURI,
168 nsAString &aFilename);
170 protected:
171 // help determine the kind of drag
172 bool IsFlavourPresent(const char *inFlavour);
174 virtual HRESULT AddSetFormat(FORMATETC& FE);
175 virtual HRESULT AddGetFormat(FORMATETC& FE);
177 virtual HRESULT GetFile ( FORMATETC& aFE, STGMEDIUM& aSTG );
178 virtual HRESULT GetText ( const nsACString& aDF, FORMATETC& aFE, STGMEDIUM & aSTG );
179 virtual HRESULT GetDib ( const nsACString& inFlavor, FORMATETC &, STGMEDIUM & aSTG );
180 virtual HRESULT GetMetafilePict(FORMATETC& FE, STGMEDIUM& STM);
182 virtual HRESULT DropImage( FORMATETC& aFE, STGMEDIUM& aSTG );
183 virtual HRESULT DropFile( FORMATETC& aFE, STGMEDIUM& aSTG );
184 virtual HRESULT DropTempFile( FORMATETC& aFE, STGMEDIUM& aSTG );
186 virtual HRESULT GetUniformResourceLocator ( FORMATETC& aFE, STGMEDIUM& aSTG, bool aIsUnicode ) ;
187 virtual HRESULT ExtractUniformResourceLocatorA ( FORMATETC& aFE, STGMEDIUM& aSTG ) ;
188 virtual HRESULT ExtractUniformResourceLocatorW ( FORMATETC& aFE, STGMEDIUM& aSTG ) ;
189 virtual HRESULT GetFileDescriptor ( FORMATETC& aFE, STGMEDIUM& aSTG, bool aIsUnicode ) ;
190 virtual HRESULT GetFileContents ( FORMATETC& aFE, STGMEDIUM& aSTG ) ;
191 virtual HRESULT GetPreferredDropEffect ( FORMATETC& aFE, STGMEDIUM& aSTG );
193 virtual HRESULT SetBitmap(FORMATETC& FE, STGMEDIUM& STM);
194 virtual HRESULT SetDib (FORMATETC& FE, STGMEDIUM& STM);
195 virtual HRESULT SetText (FORMATETC& FE, STGMEDIUM& STM);
196 virtual HRESULT SetMetafilePict(FORMATETC& FE, STGMEDIUM& STM);
198 // Provide the structures needed for an internet shortcut by the shell
199 virtual HRESULT GetFileDescriptorInternetShortcutA ( FORMATETC& aFE, STGMEDIUM& aSTG ) ;
200 virtual HRESULT GetFileDescriptorInternetShortcutW ( FORMATETC& aFE, STGMEDIUM& aSTG ) ;
201 virtual HRESULT GetFileContentsInternetShortcut ( FORMATETC& aFE, STGMEDIUM& aSTG ) ;
203 // IStream implementation
204 virtual HRESULT GetFileDescriptor_IStreamA ( FORMATETC& aFE, STGMEDIUM& aSTG ) ;
205 virtual HRESULT GetFileDescriptor_IStreamW ( FORMATETC& aFE, STGMEDIUM& aSTG ) ;
206 virtual HRESULT GetFileContents_IStream ( FORMATETC& aFE, STGMEDIUM& aSTG ) ;
208 nsresult ExtractShortcutURL ( nsString & outURL ) ;
209 nsresult ExtractShortcutTitle ( nsString & outTitle ) ;
211 // munge our HTML data to win32's CF_HTML spec. Will null terminate
212 nsresult BuildPlatformHTML ( const char* inOurHTML, char** outPlatformHTML ) ;
214 // Used for the SourceURL part of CF_HTML
215 nsCString mSourceURL;
217 BOOL FormatsMatch(const FORMATETC& source, const FORMATETC& target) const;
219 ULONG m_cRef; // the reference count
221 nsTArray<nsCString> mDataFlavors;
223 nsITransferable * mTransferable; // nsDataObj owns and ref counts nsITransferable,
224 // the nsITransferable does know anything about the nsDataObj
226 CEnumFormatEtc * m_enumFE; // Ownership Rules:
227 // nsDataObj owns and ref counts CEnumFormatEtc,
229 nsCOMPtr<nsIFile> mCachedTempFile;
231 BOOL mIsAsyncMode;
232 BOOL mIsInOperation;
233 ///////////////////////////////////////////////////////////////////////////////
234 // CStream class implementation
235 // this class is used in Drag and drop with download sample
236 // called from IDataObject::GetData
237 class CStream : public IStream, public nsIStreamListener
238 {
239 nsCOMPtr<nsIChannel> mChannel;
240 nsTArray<uint8_t> mChannelData;
241 bool mChannelRead;
242 nsresult mChannelResult;
243 uint32_t mStreamRead;
245 protected:
246 virtual ~CStream();
247 nsresult WaitForCompletion();
249 public:
250 CStream();
251 nsresult Init(nsIURI *pSourceURI);
253 NS_DECL_ISUPPORTS
254 NS_DECL_NSIREQUESTOBSERVER
255 NS_DECL_NSISTREAMLISTENER
257 // IUnknown
258 STDMETHOD(QueryInterface)(REFIID refiid, void** ppvResult);
260 // IStream
261 STDMETHOD(Clone)(IStream** ppStream);
262 STDMETHOD(Commit)(DWORD dwFrags);
263 STDMETHOD(CopyTo)(IStream* pDestStream, ULARGE_INTEGER nBytesToCopy, ULARGE_INTEGER* nBytesRead, ULARGE_INTEGER* nBytesWritten);
264 STDMETHOD(LockRegion)(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes, DWORD dwFlags);
265 STDMETHOD(Read)(void* pvBuffer, ULONG nBytesToRead, ULONG* nBytesRead);
266 STDMETHOD(Revert)(void);
267 STDMETHOD(Seek)(LARGE_INTEGER nMove, DWORD dwOrigin, ULARGE_INTEGER* nNewPos);
268 STDMETHOD(SetSize)(ULARGE_INTEGER nNewSize);
269 STDMETHOD(Stat)(STATSTG* statstg, DWORD dwFlags);
270 STDMETHOD(UnlockRegion)(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes, DWORD dwFlags);
271 STDMETHOD(Write)(const void* pvBuffer, ULONG nBytesToRead, ULONG* nBytesRead);
272 };
274 HRESULT CreateStream(IStream **outStream);
276 private:
278 // Drag and drop helper data for implementing drag and drop image support
279 typedef struct {
280 FORMATETC fe;
281 STGMEDIUM stgm;
282 } DATAENTRY, *LPDATAENTRY;
284 nsTArray <LPDATAENTRY> mDataEntryList;
285 nsCOMPtr<nsITimer> mTimer;
287 bool LookupArbitraryFormat(FORMATETC *aFormat, LPDATAENTRY *aDataEntry, BOOL aAddorUpdate);
288 bool CopyMediumData(STGMEDIUM *aMediumDst, STGMEDIUM *aMediumSrc, LPFORMATETC aFormat, BOOL aSetData);
289 static void RemoveTempFile(nsITimer* aTimer, void* aClosure);
290 };
293 #endif // _NSDATAOBJ_H_