widget/windows/nsDataObjCollection.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/windows/nsDataObjCollection.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,466 @@
     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 +#include <shlobj.h>
    1.10 +
    1.11 +#include "nsDataObjCollection.h"
    1.12 +#include "nsClipboard.h"
    1.13 +#include "IEnumFE.h"
    1.14 +
    1.15 +#include <ole2.h>
    1.16 +
    1.17 +// {25589C3E-1FAC-47b9-BF43-CAEA89B79533}
    1.18 +const IID IID_IDataObjCollection =
    1.19 +  {0x25589c3e, 0x1fac, 0x47b9, {0xbf, 0x43, 0xca, 0xea, 0x89, 0xb7, 0x95, 0x33}};
    1.20 +
    1.21 +/*
    1.22 + * Class nsDataObjCollection
    1.23 + */
    1.24 +
    1.25 +nsDataObjCollection::nsDataObjCollection()
    1.26 +  : m_cRef(0), mIsAsyncMode(FALSE), mIsInOperation(FALSE)
    1.27 +{
    1.28 +  m_enumFE = new CEnumFormatEtc();
    1.29 +  m_enumFE->AddRef();
    1.30 +}
    1.31 +
    1.32 +nsDataObjCollection::~nsDataObjCollection()
    1.33 +{
    1.34 +  mDataFlavors.Clear();
    1.35 +  mDataObjects.Clear();
    1.36 +
    1.37 +  m_enumFE->Release();
    1.38 +}
    1.39 +
    1.40 +
    1.41 +// IUnknown interface methods - see iunknown.h for documentation
    1.42 +STDMETHODIMP nsDataObjCollection::QueryInterface(REFIID riid, void** ppv)
    1.43 +{
    1.44 +  *ppv=nullptr;
    1.45 +
    1.46 +  if ( (IID_IUnknown == riid) || (IID_IDataObject  == riid) ) {
    1.47 +    *ppv = static_cast<IDataObject*>(this); 
    1.48 +    AddRef();
    1.49 +    return NOERROR;
    1.50 +  }
    1.51 +
    1.52 +  if ( IID_IDataObjCollection  == riid ) {
    1.53 +    *ppv = static_cast<nsIDataObjCollection*>(this); 
    1.54 +    AddRef();
    1.55 +    return NOERROR;
    1.56 +  }
    1.57 +
    1.58 +  return E_NOINTERFACE;
    1.59 +}
    1.60 +
    1.61 +STDMETHODIMP_(ULONG) nsDataObjCollection::AddRef()
    1.62 +{
    1.63 +  return ++m_cRef;
    1.64 +}
    1.65 +
    1.66 +STDMETHODIMP_(ULONG) nsDataObjCollection::Release()
    1.67 +{
    1.68 +  if (0 != --m_cRef)
    1.69 +    return m_cRef;
    1.70 +
    1.71 +  delete this;
    1.72 +
    1.73 +  return 0;
    1.74 +}
    1.75 +
    1.76 +BOOL nsDataObjCollection::FormatsMatch(const FORMATETC& source,
    1.77 +                                       const FORMATETC& target) const
    1.78 +{
    1.79 +  if ((source.cfFormat == target.cfFormat) &&
    1.80 +      (source.dwAspect & target.dwAspect)  &&
    1.81 +      (source.tymed    & target.tymed)) {
    1.82 +    return TRUE;
    1.83 +  } else {
    1.84 +    return FALSE;
    1.85 +  }
    1.86 +}
    1.87 +
    1.88 +// IDataObject methods
    1.89 +STDMETHODIMP nsDataObjCollection::GetData(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
    1.90 +{
    1.91 +  static CLIPFORMAT fileDescriptorFlavorA =
    1.92 +                               ::RegisterClipboardFormat(CFSTR_FILEDESCRIPTORA);
    1.93 +  static CLIPFORMAT fileDescriptorFlavorW =
    1.94 +                               ::RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW);
    1.95 +  static CLIPFORMAT fileFlavor = ::RegisterClipboardFormat(CFSTR_FILECONTENTS);
    1.96 +
    1.97 +  switch (pFE->cfFormat) {
    1.98 +  case CF_TEXT:
    1.99 +  case CF_UNICODETEXT:
   1.100 +    return GetText(pFE, pSTM);
   1.101 +  case CF_HDROP:
   1.102 +    return GetFile(pFE, pSTM);
   1.103 +  default:
   1.104 +    if (pFE->cfFormat == fileDescriptorFlavorA ||
   1.105 +        pFE->cfFormat == fileDescriptorFlavorW) {
   1.106 +      return GetFileDescriptors(pFE, pSTM);
   1.107 +    }
   1.108 +    if (pFE->cfFormat == fileFlavor) {
   1.109 +      return GetFileContents(pFE, pSTM);
   1.110 +    }
   1.111 +  }
   1.112 +  return GetFirstSupporting(pFE, pSTM);
   1.113 +}
   1.114 +
   1.115 +STDMETHODIMP nsDataObjCollection::GetDataHere(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
   1.116 +{
   1.117 +  return E_FAIL;
   1.118 +}
   1.119 +
   1.120 +// Other objects querying to see if we support a particular format
   1.121 +STDMETHODIMP nsDataObjCollection::QueryGetData(LPFORMATETC pFE)
   1.122 +{
   1.123 +  UINT format = nsClipboard::GetFormat(MULTI_MIME);
   1.124 +
   1.125 +  if (format == pFE->cfFormat) {
   1.126 +    return S_OK;
   1.127 +  }
   1.128 +
   1.129 +  for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
   1.130 +    IDataObject * dataObj = mDataObjects.ElementAt(i);
   1.131 +    if (S_OK == dataObj->QueryGetData(pFE)) {
   1.132 +      return S_OK;
   1.133 +    }
   1.134 +  }
   1.135 +
   1.136 +  return DV_E_FORMATETC;
   1.137 +}
   1.138 +
   1.139 +STDMETHODIMP nsDataObjCollection::GetCanonicalFormatEtc(LPFORMATETC pFEIn,
   1.140 +                                                        LPFORMATETC pFEOut)
   1.141 +{
   1.142 +  return E_NOTIMPL;
   1.143 +}
   1.144 +
   1.145 +STDMETHODIMP nsDataObjCollection::SetData(LPFORMATETC pFE,
   1.146 +                                          LPSTGMEDIUM pSTM,
   1.147 +                                          BOOL fRelease)
   1.148 +{
   1.149 +  // Set arbitrary data formats on the first object in the collection and let
   1.150 +  // it handle the heavy lifting
   1.151 +  if (mDataObjects.Length() == 0)
   1.152 +    return E_FAIL;
   1.153 +  return mDataObjects.ElementAt(0)->SetData(pFE, pSTM, fRelease);
   1.154 +}
   1.155 +
   1.156 +STDMETHODIMP nsDataObjCollection::EnumFormatEtc(DWORD dwDir,
   1.157 +                                                LPENUMFORMATETC *ppEnum)
   1.158 +{
   1.159 +  if (dwDir == DATADIR_GET) {
   1.160 +    // Clone addref's the new enumerator.
   1.161 +    m_enumFE->Clone(ppEnum);
   1.162 +    if (!(*ppEnum))
   1.163 +      return E_FAIL;
   1.164 +    (*ppEnum)->Reset();
   1.165 +    return S_OK;
   1.166 +  }
   1.167 +
   1.168 +  return E_NOTIMPL;
   1.169 +}
   1.170 +
   1.171 +STDMETHODIMP nsDataObjCollection::DAdvise(LPFORMATETC pFE,
   1.172 +                                          DWORD dwFlags,
   1.173 +                                          LPADVISESINK pIAdviseSink,
   1.174 +                                          DWORD* pdwConn)
   1.175 +{
   1.176 +  return OLE_E_ADVISENOTSUPPORTED;
   1.177 +}
   1.178 +
   1.179 +STDMETHODIMP nsDataObjCollection::DUnadvise(DWORD dwConn)
   1.180 +{
   1.181 +  return OLE_E_ADVISENOTSUPPORTED;
   1.182 +}
   1.183 +
   1.184 +STDMETHODIMP nsDataObjCollection::EnumDAdvise(LPENUMSTATDATA *ppEnum)
   1.185 +{
   1.186 +  return OLE_E_ADVISENOTSUPPORTED;
   1.187 +}
   1.188 +
   1.189 +// GetData and SetData helper functions
   1.190 +HRESULT nsDataObjCollection::AddSetFormat(FORMATETC& aFE)
   1.191 +{
   1.192 +  return S_OK;
   1.193 +}
   1.194 +
   1.195 +HRESULT nsDataObjCollection::AddGetFormat(FORMATETC& aFE)
   1.196 +{
   1.197 +  return S_OK;
   1.198 +}
   1.199 +
   1.200 +// Registers a DataFlavor/FE pair
   1.201 +void nsDataObjCollection::AddDataFlavor(const char * aDataFlavor,
   1.202 +                                        LPFORMATETC aFE)
   1.203 +{
   1.204 +  // Add the FormatEtc to our list if it's not already there.  We don't care
   1.205 +  // about the internal aDataFlavor because nsDataObj handles that.
   1.206 +  IEnumFORMATETC * ifEtc;
   1.207 +  FORMATETC fEtc;
   1.208 +  ULONG num;
   1.209 +  if (S_OK != this->EnumFormatEtc(DATADIR_GET, &ifEtc))
   1.210 +    return;
   1.211 +  while (S_OK == ifEtc->Next(1, &fEtc, &num)) {
   1.212 +    NS_ASSERTION(1 == num,
   1.213 +         "Bit off more than we can chew in nsDataObjCollection::AddDataFlavor");
   1.214 +    if (FormatsMatch(fEtc, *aFE)) {
   1.215 +      ifEtc->Release();
   1.216 +      return;
   1.217 +    }
   1.218 +  } // If we didn't find a matching format, add this one
   1.219 +  ifEtc->Release();
   1.220 +  m_enumFE->AddFormatEtc(aFE);
   1.221 +}
   1.222 +
   1.223 +// We accept ownership of the nsDataObj which we free on destruction
   1.224 +void nsDataObjCollection::AddDataObject(IDataObject * aDataObj)
   1.225 +{
   1.226 +  nsDataObj* dataObj = reinterpret_cast<nsDataObj*>(aDataObj);
   1.227 +  mDataObjects.AppendElement(dataObj);
   1.228 +}
   1.229 +
   1.230 +// Methods for getting data
   1.231 +HRESULT nsDataObjCollection::GetFile(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
   1.232 +{
   1.233 +  STGMEDIUM workingmedium;
   1.234 +  FORMATETC fe = *pFE;
   1.235 +  HGLOBAL hGlobalMemory;
   1.236 +  HRESULT hr;
   1.237 +  // Make enough space for the header and the trailing null
   1.238 +  uint32_t buffersize = sizeof(DROPFILES) + sizeof(char16_t);
   1.239 +  uint32_t alloclen = 0;
   1.240 +  char16_t* realbuffer;
   1.241 +  nsAutoString filename;
   1.242 +  
   1.243 +  hGlobalMemory = GlobalAlloc(GHND, buffersize);
   1.244 +
   1.245 +  for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
   1.246 +    nsDataObj* dataObj = mDataObjects.ElementAt(i);
   1.247 +    hr = dataObj->GetData(&fe, &workingmedium);
   1.248 +    if (hr != S_OK) {
   1.249 +      switch (hr) {
   1.250 +      case DV_E_FORMATETC:
   1.251 +        continue;
   1.252 +      default:
   1.253 +        return hr;
   1.254 +      }
   1.255 +    }
   1.256 +    // Now we need to pull out the filename
   1.257 +    char16_t* buffer = (char16_t*)GlobalLock(workingmedium.hGlobal);
   1.258 +    if (buffer == nullptr)
   1.259 +      return E_FAIL;
   1.260 +    buffer += sizeof(DROPFILES)/sizeof(char16_t);
   1.261 +    filename = buffer;
   1.262 +    GlobalUnlock(workingmedium.hGlobal);
   1.263 +    ReleaseStgMedium(&workingmedium);
   1.264 +    // Now put the filename into our buffer
   1.265 +    alloclen = (filename.Length() + 1) * sizeof(char16_t);
   1.266 +    hGlobalMemory = ::GlobalReAlloc(hGlobalMemory, buffersize + alloclen, GHND);
   1.267 +    if (hGlobalMemory == nullptr)
   1.268 +      return E_FAIL;
   1.269 +    realbuffer = (char16_t*)((char*)GlobalLock(hGlobalMemory) + buffersize);
   1.270 +    if (!realbuffer)
   1.271 +      return E_FAIL;
   1.272 +    realbuffer--; // Overwrite the preceding null
   1.273 +    memcpy(realbuffer, filename.get(), alloclen);
   1.274 +    GlobalUnlock(hGlobalMemory);
   1.275 +    buffersize += alloclen;
   1.276 +  }
   1.277 +  // We get the last null (on the double null terminator) for free since we used
   1.278 +  // the zero memory flag when we allocated.  All we need to do is fill the
   1.279 +  // DROPFILES structure
   1.280 +  DROPFILES* df = (DROPFILES*)GlobalLock(hGlobalMemory);
   1.281 +  if (!df)
   1.282 +    return E_FAIL;
   1.283 +  df->pFiles = sizeof(DROPFILES); //Offset to start of file name string
   1.284 +  df->fNC    = 0;
   1.285 +  df->pt.x   = 0;
   1.286 +  df->pt.y   = 0;
   1.287 +  df->fWide  = TRUE; // utf-16 chars
   1.288 +  GlobalUnlock(hGlobalMemory);
   1.289 +  // Finally fill out the STGMEDIUM struct
   1.290 +  pSTM->tymed = TYMED_HGLOBAL;
   1.291 +  pSTM->pUnkForRelease = nullptr; // Caller gets to free the data
   1.292 +  pSTM->hGlobal = hGlobalMemory;
   1.293 +  return S_OK;
   1.294 +}
   1.295 +
   1.296 +HRESULT nsDataObjCollection::GetText(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
   1.297 +{
   1.298 +  STGMEDIUM workingmedium;
   1.299 +  FORMATETC fe = *pFE;
   1.300 +  HGLOBAL hGlobalMemory;
   1.301 +  HRESULT hr;
   1.302 +  uint32_t buffersize = 1;
   1.303 +  uint32_t alloclen = 0;
   1.304 +
   1.305 +  hGlobalMemory = GlobalAlloc(GHND, buffersize);
   1.306 +
   1.307 +  if (pFE->cfFormat == CF_TEXT) {
   1.308 +    nsAutoCString text;
   1.309 +    for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
   1.310 +      nsDataObj* dataObj = mDataObjects.ElementAt(i);
   1.311 +      hr = dataObj->GetData(&fe, &workingmedium);
   1.312 +      if (hr != S_OK) {
   1.313 +        switch (hr) {
   1.314 +        case DV_E_FORMATETC:
   1.315 +          continue;
   1.316 +        default:
   1.317 +          return hr;
   1.318 +        }
   1.319 +      }
   1.320 +      // Now we need to pull out the text
   1.321 +      char* buffer = (char*)GlobalLock(workingmedium.hGlobal);
   1.322 +      if (buffer == nullptr)
   1.323 +        return E_FAIL;
   1.324 +      text = buffer;
   1.325 +      GlobalUnlock(workingmedium.hGlobal);
   1.326 +      ReleaseStgMedium(&workingmedium);
   1.327 +      // Now put the text into our buffer
   1.328 +      alloclen = text.Length();
   1.329 +      hGlobalMemory = ::GlobalReAlloc(hGlobalMemory, buffersize + alloclen,
   1.330 +                                      GHND);
   1.331 +      if (hGlobalMemory == nullptr)
   1.332 +        return E_FAIL;
   1.333 +      buffer = ((char*)GlobalLock(hGlobalMemory) + buffersize);
   1.334 +      if (!buffer)
   1.335 +        return E_FAIL;
   1.336 +      buffer--; // Overwrite the preceding null
   1.337 +      memcpy(buffer, text.get(), alloclen);
   1.338 +      GlobalUnlock(hGlobalMemory);
   1.339 +      buffersize += alloclen;
   1.340 +    }
   1.341 +    pSTM->tymed = TYMED_HGLOBAL;
   1.342 +    pSTM->pUnkForRelease = nullptr; // Caller gets to free the data
   1.343 +    pSTM->hGlobal = hGlobalMemory;
   1.344 +    return S_OK;
   1.345 +  }
   1.346 +  if (pFE->cfFormat == CF_UNICODETEXT) {
   1.347 +    buffersize = sizeof(char16_t);
   1.348 +    nsAutoString text;
   1.349 +    for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
   1.350 +      nsDataObj* dataObj = mDataObjects.ElementAt(i);
   1.351 +      hr = dataObj->GetData(&fe, &workingmedium);
   1.352 +      if (hr != S_OK) {
   1.353 +        switch (hr) {
   1.354 +        case DV_E_FORMATETC:
   1.355 +          continue;
   1.356 +        default:
   1.357 +          return hr;
   1.358 +        }
   1.359 +      }
   1.360 +      // Now we need to pull out the text
   1.361 +      char16_t* buffer = (char16_t*)GlobalLock(workingmedium.hGlobal);
   1.362 +      if (buffer == nullptr)
   1.363 +        return E_FAIL;
   1.364 +      text = buffer;
   1.365 +      GlobalUnlock(workingmedium.hGlobal);
   1.366 +      ReleaseStgMedium(&workingmedium);
   1.367 +      // Now put the text into our buffer
   1.368 +      alloclen = text.Length() * sizeof(char16_t);
   1.369 +      hGlobalMemory = ::GlobalReAlloc(hGlobalMemory, buffersize + alloclen,
   1.370 +                                      GHND);
   1.371 +      if (hGlobalMemory == nullptr)
   1.372 +        return E_FAIL;
   1.373 +      buffer = (char16_t*)((char*)GlobalLock(hGlobalMemory) + buffersize);
   1.374 +      if (!buffer)
   1.375 +        return E_FAIL;
   1.376 +      buffer--; // Overwrite the preceding null
   1.377 +      memcpy(buffer, text.get(), alloclen);
   1.378 +      GlobalUnlock(hGlobalMemory);
   1.379 +      buffersize += alloclen;
   1.380 +    }
   1.381 +    pSTM->tymed = TYMED_HGLOBAL;
   1.382 +    pSTM->pUnkForRelease = nullptr; // Caller gets to free the data
   1.383 +    pSTM->hGlobal = hGlobalMemory;
   1.384 +    return S_OK;
   1.385 +  }
   1.386 +
   1.387 +  return E_FAIL;
   1.388 +}
   1.389 +
   1.390 +HRESULT nsDataObjCollection::GetFileDescriptors(LPFORMATETC pFE,
   1.391 +                                                LPSTGMEDIUM pSTM)
   1.392 +{
   1.393 +  STGMEDIUM workingmedium;
   1.394 +  FORMATETC fe = *pFE;
   1.395 +  HGLOBAL hGlobalMemory;
   1.396 +  HRESULT hr;
   1.397 +  uint32_t buffersize = sizeof(FILEGROUPDESCRIPTOR);
   1.398 +  uint32_t alloclen = sizeof(FILEDESCRIPTOR);
   1.399 +
   1.400 +  hGlobalMemory = GlobalAlloc(GHND, buffersize);
   1.401 +
   1.402 +  for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
   1.403 +    nsDataObj* dataObj = mDataObjects.ElementAt(i);
   1.404 +    hr = dataObj->GetData(&fe, &workingmedium);
   1.405 +    if (hr != S_OK) {
   1.406 +      switch (hr) {
   1.407 +      case DV_E_FORMATETC:
   1.408 +        continue;
   1.409 +      default:
   1.410 +        return hr;
   1.411 +      }
   1.412 +    }
   1.413 +    // Now we need to pull out the filedescriptor
   1.414 +    FILEDESCRIPTOR* buffer =
   1.415 +     (FILEDESCRIPTOR*)((char*)GlobalLock(workingmedium.hGlobal) + sizeof(UINT));
   1.416 +    if (buffer == nullptr)
   1.417 +      return E_FAIL;
   1.418 +    hGlobalMemory = ::GlobalReAlloc(hGlobalMemory, buffersize + alloclen, GHND);
   1.419 +    if (hGlobalMemory == nullptr)
   1.420 +      return E_FAIL;
   1.421 +    FILEGROUPDESCRIPTOR* realbuffer =
   1.422 +                                (FILEGROUPDESCRIPTOR*)GlobalLock(hGlobalMemory);
   1.423 +    if (!realbuffer)
   1.424 +      return E_FAIL;
   1.425 +    FILEDESCRIPTOR* copyloc = (FILEDESCRIPTOR*)((char*)realbuffer + buffersize);
   1.426 +    memcpy(copyloc, buffer, sizeof(FILEDESCRIPTOR));
   1.427 +    realbuffer->cItems++;
   1.428 +    GlobalUnlock(hGlobalMemory);
   1.429 +    GlobalUnlock(workingmedium.hGlobal);
   1.430 +    ReleaseStgMedium(&workingmedium);
   1.431 +    buffersize += alloclen;
   1.432 +  }
   1.433 +  pSTM->tymed = TYMED_HGLOBAL;
   1.434 +  pSTM->pUnkForRelease = nullptr; // Caller gets to free the data
   1.435 +  pSTM->hGlobal = hGlobalMemory;
   1.436 +  return S_OK;
   1.437 +}
   1.438 +
   1.439 +HRESULT nsDataObjCollection::GetFileContents(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
   1.440 +{
   1.441 +  ULONG num = 0;
   1.442 +  ULONG numwanted = (pFE->lindex == -1) ? 0 : pFE->lindex;
   1.443 +  FORMATETC fEtc = *pFE;
   1.444 +  fEtc.lindex = -1;  // We're lying to the data object so it thinks it's alone
   1.445 +
   1.446 +  // The key for this data type is to figure out which data object the index
   1.447 +  // corresponds to and then just pass it along
   1.448 +  for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
   1.449 +    nsDataObj* dataObj = mDataObjects.ElementAt(i);
   1.450 +    if (dataObj->QueryGetData(&fEtc) != S_OK)
   1.451 +      continue;
   1.452 +    if (num == numwanted)
   1.453 +      return dataObj->GetData(pFE, pSTM);
   1.454 +    numwanted++;
   1.455 +  }
   1.456 +  return DV_E_LINDEX;
   1.457 +}
   1.458 +
   1.459 +HRESULT nsDataObjCollection::GetFirstSupporting(LPFORMATETC pFE,
   1.460 +                                                LPSTGMEDIUM pSTM)
   1.461 +{
   1.462 +  // There is no way to pass more than one of this, so just find the first data
   1.463 +  // object that supports it and pass it along
   1.464 +  for (uint32_t i = 0; i < mDataObjects.Length(); ++i) {
   1.465 +    if (mDataObjects.ElementAt(i)->QueryGetData(pFE) == S_OK)
   1.466 +      return mDataObjects.ElementAt(i)->GetData(pFE, pSTM);
   1.467 +  }
   1.468 +  return DV_E_FORMATETC;
   1.469 +}

mercurial