1 /* 2 * Copyright (C) 2007 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "WCDataObject.h" 28 29 #include "PlatformString.h" 30 31 namespace WebCore { 32 33 class WCEnumFormatEtc : public IEnumFORMATETC 34 { 35 public: 36 WCEnumFormatEtc(const Vector<FORMATETC>& formats); 37 WCEnumFormatEtc(const Vector<FORMATETC*>& formats); 38 39 //IUnknown members 40 STDMETHOD(QueryInterface)(REFIID, void**); 41 STDMETHOD_(ULONG, AddRef)(void); 42 STDMETHOD_(ULONG, Release)(void); 43 44 //IEnumFORMATETC members 45 STDMETHOD(Next)(ULONG, LPFORMATETC, ULONG*); 46 STDMETHOD(Skip)(ULONG); 47 STDMETHOD(Reset)(void); 48 STDMETHOD(Clone)(IEnumFORMATETC**); 49 50 private: 51 long m_ref; 52 Vector<FORMATETC> m_formats; 53 size_t m_current; 54 }; 55 56 57 58 WCEnumFormatEtc::WCEnumFormatEtc(const Vector<FORMATETC>& formats) 59 : m_ref(1) 60 , m_current(0) 61 { 62 for(size_t i = 0; i < formats.size(); ++i) 63 m_formats.append(formats[i]); 64 } 65 66 WCEnumFormatEtc::WCEnumFormatEtc(const Vector<FORMATETC*>& formats) 67 : m_ref(1) 68 , m_current(0) 69 { 70 for(size_t i = 0; i < formats.size(); ++i) 71 m_formats.append(*formats[i]); 72 } 73 74 STDMETHODIMP WCEnumFormatEtc::QueryInterface(REFIID riid, void** ppvObject) 75 { 76 *ppvObject = 0; 77 if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IEnumFORMATETC)) { 78 *ppvObject = this; 79 AddRef(); 80 return S_OK; 81 } 82 83 return E_NOINTERFACE; 84 } 85 86 STDMETHODIMP_(ULONG) WCEnumFormatEtc::AddRef(void) 87 { 88 return InterlockedIncrement(&m_ref); 89 } 90 91 STDMETHODIMP_(ULONG) WCEnumFormatEtc::Release(void) 92 { 93 long c = InterlockedDecrement(&m_ref); 94 if (c == 0) 95 delete this; 96 return c; 97 } 98 99 STDMETHODIMP WCEnumFormatEtc::Next(ULONG celt, LPFORMATETC lpFormatEtc, ULONG* pceltFetched) 100 { 101 if(pceltFetched != 0) 102 *pceltFetched=0; 103 104 ULONG cReturn = celt; 105 106 if(celt <= 0 || lpFormatEtc == 0 || m_current >= m_formats.size()) 107 return S_FALSE; 108 109 if(pceltFetched == 0 && celt != 1) // pceltFetched can be 0 only for 1 item request 110 return S_FALSE; 111 112 while (m_current < m_formats.size() && cReturn > 0) { 113 *lpFormatEtc++ = m_formats[m_current++]; 114 --cReturn; 115 } 116 if (pceltFetched != 0) 117 *pceltFetched = celt - cReturn; 118 119 return (cReturn == 0) ? S_OK : S_FALSE; 120 } 121 122 STDMETHODIMP WCEnumFormatEtc::Skip(ULONG celt) 123 { 124 if((m_current + int(celt)) >= m_formats.size()) 125 return S_FALSE; 126 m_current += celt; 127 return S_OK; 128 } 129 130 STDMETHODIMP WCEnumFormatEtc::Reset(void) 131 { 132 m_current = 0; 133 return S_OK; 134 } 135 136 STDMETHODIMP WCEnumFormatEtc::Clone(IEnumFORMATETC** ppCloneEnumFormatEtc) 137 { 138 if(!ppCloneEnumFormatEtc) 139 return E_POINTER; 140 141 WCEnumFormatEtc *newEnum = new WCEnumFormatEtc(m_formats); 142 if(!newEnum) 143 return E_OUTOFMEMORY; 144 145 newEnum->AddRef(); 146 newEnum->m_current = m_current; 147 *ppCloneEnumFormatEtc = newEnum; 148 return S_OK; 149 } 150 151 152 153 ////////////////////////////////////////////////////////////////////////// 154 155 HRESULT WCDataObject::createInstance(WCDataObject** result) 156 { 157 if (!result) 158 return E_POINTER; 159 *result = new WCDataObject(); 160 return S_OK; 161 } 162 163 WCDataObject::WCDataObject() 164 : m_ref(1) 165 { 166 } 167 168 WCDataObject::~WCDataObject() 169 { 170 for(size_t i = 0; i < m_medium.size(); ++i) { 171 ReleaseStgMedium(m_medium[i]); 172 delete m_medium[i]; 173 } 174 WTF::deleteAllValues(m_formats); 175 } 176 177 STDMETHODIMP WCDataObject::QueryInterface(REFIID riid,void** ppvObject) 178 { 179 *ppvObject = 0; 180 if (IsEqualIID(riid, IID_IUnknown) || 181 IsEqualIID(riid, IID_IDataObject)) { 182 *ppvObject=this; 183 } 184 if (*ppvObject) { 185 AddRef(); 186 return S_OK; 187 } 188 return E_NOINTERFACE; 189 } 190 191 STDMETHODIMP_(ULONG) WCDataObject::AddRef( void) 192 { 193 return InterlockedIncrement(&m_ref); 194 } 195 196 STDMETHODIMP_(ULONG) WCDataObject::Release( void) 197 { 198 long c = InterlockedDecrement(&m_ref); 199 if (c == 0) 200 delete this; 201 return c; 202 } 203 204 STDMETHODIMP WCDataObject::GetData(FORMATETC* pformatetcIn, STGMEDIUM* pmedium) 205 { 206 if(!pformatetcIn || !pmedium) 207 return E_POINTER; 208 pmedium->hGlobal = 0; 209 210 for(size_t i=0; i < m_formats.size(); ++i) { 211 if(/*pformatetcIn->tymed & m_formats[i]->tymed &&*/ // tymed can be 0 (TYMED_NULL) - but it can have a medium that contains an pUnkForRelease 212 pformatetcIn->lindex == m_formats[i]->lindex && 213 pformatetcIn->dwAspect == m_formats[i]->dwAspect && 214 pformatetcIn->cfFormat == m_formats[i]->cfFormat) { 215 CopyMedium(pmedium, m_medium[i], m_formats[i]); 216 return S_OK; 217 } 218 } 219 return DV_E_FORMATETC; 220 } 221 222 STDMETHODIMP WCDataObject::GetDataHere(FORMATETC*, STGMEDIUM*) 223 { 224 return E_NOTIMPL; 225 } 226 227 STDMETHODIMP WCDataObject::QueryGetData(FORMATETC* pformatetc) 228 { 229 if(!pformatetc) 230 return E_POINTER; 231 232 if (!(DVASPECT_CONTENT & pformatetc->dwAspect)) 233 return (DV_E_DVASPECT); 234 HRESULT hr = DV_E_TYMED; 235 for(size_t i = 0; i < m_formats.size(); ++i) { 236 if(pformatetc->tymed & m_formats[i]->tymed) { 237 if(pformatetc->cfFormat == m_formats[i]->cfFormat) 238 return S_OK; 239 else 240 hr = DV_E_CLIPFORMAT; 241 } 242 else 243 hr = DV_E_TYMED; 244 } 245 return hr; 246 } 247 248 STDMETHODIMP WCDataObject::GetCanonicalFormatEtc(FORMATETC*, FORMATETC*) 249 { 250 return DATA_S_SAMEFORMATETC; 251 } 252 253 STDMETHODIMP WCDataObject::SetData(FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease) 254 { 255 if(!pformatetc || !pmedium) 256 return E_POINTER; 257 258 FORMATETC* fetc=new FORMATETC; 259 if (!fetc) 260 return E_OUTOFMEMORY; 261 262 STGMEDIUM* pStgMed = new STGMEDIUM; 263 264 if(!pStgMed) { 265 delete fetc; 266 return E_OUTOFMEMORY; 267 } 268 269 ZeroMemory(fetc,sizeof(FORMATETC)); 270 ZeroMemory(pStgMed,sizeof(STGMEDIUM)); 271 272 *fetc = *pformatetc; 273 m_formats.append(fetc); 274 275 if(fRelease) 276 *pStgMed = *pmedium; 277 else 278 CopyMedium(pStgMed, pmedium, pformatetc); 279 m_medium.append(pStgMed); 280 281 return S_OK; 282 } 283 284 void WCDataObject::CopyMedium(STGMEDIUM* pMedDest, STGMEDIUM* pMedSrc, FORMATETC* pFmtSrc) 285 { 286 switch(pMedSrc->tymed) 287 { 288 case TYMED_HGLOBAL: 289 pMedDest->hGlobal = (HGLOBAL)OleDuplicateData(pMedSrc->hGlobal,pFmtSrc->cfFormat, 0); 290 break; 291 case TYMED_GDI: 292 pMedDest->hBitmap = (HBITMAP)OleDuplicateData(pMedSrc->hBitmap,pFmtSrc->cfFormat, 0); 293 break; 294 case TYMED_MFPICT: 295 pMedDest->hMetaFilePict = (HMETAFILEPICT)OleDuplicateData(pMedSrc->hMetaFilePict,pFmtSrc->cfFormat, 0); 296 break; 297 case TYMED_ENHMF: 298 pMedDest->hEnhMetaFile = (HENHMETAFILE)OleDuplicateData(pMedSrc->hEnhMetaFile,pFmtSrc->cfFormat, 0); 299 break; 300 case TYMED_FILE: 301 pMedSrc->lpszFileName = (LPOLESTR)OleDuplicateData(pMedSrc->lpszFileName,pFmtSrc->cfFormat, 0); 302 break; 303 case TYMED_ISTREAM: 304 pMedDest->pstm = pMedSrc->pstm; 305 pMedSrc->pstm->AddRef(); 306 break; 307 case TYMED_ISTORAGE: 308 pMedDest->pstg = pMedSrc->pstg; 309 pMedSrc->pstg->AddRef(); 310 break; 311 default: 312 break; 313 } 314 pMedDest->tymed = pMedSrc->tymed; 315 pMedDest->pUnkForRelease = 0; 316 if(pMedSrc->pUnkForRelease != 0) { 317 pMedDest->pUnkForRelease = pMedSrc->pUnkForRelease; 318 pMedSrc->pUnkForRelease->AddRef(); 319 } 320 } 321 STDMETHODIMP WCDataObject::EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc) 322 { 323 if(!ppenumFormatEtc) 324 return E_POINTER; 325 326 *ppenumFormatEtc=0; 327 switch (dwDirection) 328 { 329 case DATADIR_GET: 330 *ppenumFormatEtc= new WCEnumFormatEtc(m_formats); 331 if(!(*ppenumFormatEtc)) 332 return E_OUTOFMEMORY; 333 break; 334 335 case DATADIR_SET: 336 default: 337 return E_NOTIMPL; 338 break; 339 } 340 341 return S_OK; 342 } 343 344 STDMETHODIMP WCDataObject::DAdvise(FORMATETC*, DWORD, IAdviseSink*,DWORD*) 345 { 346 return OLE_E_ADVISENOTSUPPORTED; 347 } 348 349 STDMETHODIMP WCDataObject::DUnadvise(DWORD) 350 { 351 return E_NOTIMPL; 352 } 353 354 HRESULT STDMETHODCALLTYPE WCDataObject::EnumDAdvise(IEnumSTATDATA**) 355 { 356 return OLE_E_ADVISENOTSUPPORTED; 357 } 358 359 void WCDataObject::clearData(CLIPFORMAT format) 360 { 361 size_t ptr = 0; 362 while (ptr < m_formats.size()) { 363 if (m_formats[ptr]->cfFormat == format) { 364 FORMATETC* current = m_formats[ptr]; 365 m_formats[ptr] = m_formats[m_formats.size() - 1]; 366 m_formats[m_formats.size() - 1] = 0; 367 m_formats.removeLast(); 368 delete current; 369 STGMEDIUM* medium = m_medium[ptr]; 370 m_medium[ptr] = m_medium[m_medium.size() - 1]; 371 m_medium[m_medium.size() - 1] = 0; 372 m_medium.removeLast(); 373 ReleaseStgMedium(medium); 374 delete medium; 375 continue; 376 } 377 ptr++; 378 } 379 } 380 381 382 } 383