2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include <ole2.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
|
|
|
|
#include "nsDataObj.h"
|
|
|
|
#include "nsClipboard.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
2008-08-11 12:54:41 -07:00
|
|
|
#include "IEnumFE.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsPrimitiveHelpers.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsImageClipboard.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsXPCOMStrings.h"
|
2008-10-12 09:34:26 -07:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "prtypes.h"
|
2008-11-14 14:23:02 -08:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2009-12-17 20:06:19 -08:00
|
|
|
#include "nsITimer.h"
|
2011-08-10 13:15:16 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2012-07-21 13:07:26 -07:00
|
|
|
#include "WinUtils.h"
|
|
|
|
#include "mozilla/LazyIdleThread.h"
|
|
|
|
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2012-07-21 13:07:26 -07:00
|
|
|
#define DEFAULT_THREAD_TIMEOUT_MS 30000
|
|
|
|
|
2011-08-10 13:15:16 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(nsDataObj::CStream, nsIStreamListener)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// CStream implementation
|
2011-08-10 13:15:16 -07:00
|
|
|
nsDataObj::CStream::CStream() :
|
|
|
|
mChannelRead(false),
|
|
|
|
mStreamRead(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
nsDataObj::CStream::~CStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// helper - initializes the stream
|
|
|
|
nsresult nsDataObj::CStream::Init(nsIURI *pSourceURI)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2011-08-10 13:15:16 -07:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(mChannel), pSourceURI,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, nullptr, nullptr,
|
2011-08-10 13:15:16 -07:00
|
|
|
nsIRequest::LOAD_FROM_CACHE);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = mChannel->AsyncOpen(this, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-08-10 13:15:16 -07:00
|
|
|
// IUnknown's QueryInterface, nsISupport's AddRef and Release are shared by
|
|
|
|
// IUnknown and nsIStreamListener.
|
2007-03-22 10:30:00 -07:00
|
|
|
STDMETHODIMP nsDataObj::CStream::QueryInterface(REFIID refiid, void** ppvResult)
|
|
|
|
{
|
|
|
|
*ppvResult = NULL;
|
|
|
|
if (IID_IUnknown == refiid ||
|
|
|
|
refiid == IID_IStream)
|
|
|
|
|
|
|
|
{
|
|
|
|
*ppvResult = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != *ppvResult)
|
|
|
|
{
|
|
|
|
((LPUNKNOWN)*ppvResult)->AddRef();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_NOINTERFACE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-10 13:15:16 -07:00
|
|
|
// nsIStreamListener implementation
|
2012-09-05 19:41:02 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDataObj::CStream::OnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aInputStream,
|
|
|
|
uint64_t aOffset, // offset within the stream
|
|
|
|
uint32_t aCount) // bytes available on this call
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-08-10 13:15:16 -07:00
|
|
|
// Extend the write buffer for the incoming data.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t* buffer = mChannelData.AppendElements(aCount);
|
2011-08-10 13:15:16 -07:00
|
|
|
if (buffer == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ASSERTION((mChannelData.Length() == (aOffset + aCount)),
|
|
|
|
"stream length mismatch w/write buffer");
|
|
|
|
|
|
|
|
// Read() may not return aCount on a single call, so loop until we've
|
|
|
|
// accumulated all the data OnDataAvailable has promised.
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t odaBytesReadTotal = 0;
|
2011-08-10 13:15:16 -07:00
|
|
|
do {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytesReadByCall = 0;
|
2011-08-10 13:15:16 -07:00
|
|
|
rv = aInputStream->Read((char*)(buffer + odaBytesReadTotal),
|
|
|
|
aCount, &bytesReadByCall);
|
|
|
|
odaBytesReadTotal += bytesReadByCall;
|
|
|
|
} while (aCount < odaBytesReadTotal && NS_SUCCEEDED(rv));
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-10 13:15:16 -07:00
|
|
|
NS_IMETHODIMP nsDataObj::CStream::OnStartRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-08-10 13:15:16 -07:00
|
|
|
mChannelResult = NS_OK;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsDataObj::CStream::OnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatusCode)
|
|
|
|
{
|
|
|
|
mChannelRead = true;
|
|
|
|
mChannelResult = aStatusCode;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pumps thread messages while waiting for the async listener operation to
|
|
|
|
// complete. Failing this call will fail the stream incall from Windows
|
|
|
|
// and cancel the operation.
|
|
|
|
nsresult nsDataObj::CStream::WaitForCompletion()
|
|
|
|
{
|
|
|
|
// We are guaranteed OnStopRequest will get called, so this should be ok.
|
|
|
|
while (!mChannelRead) {
|
|
|
|
// Pump messages
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ProcessNextEvent(nullptr, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-10 13:15:16 -07:00
|
|
|
if (!mChannelData.Length())
|
|
|
|
mChannelResult = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return mChannelResult;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// IStream
|
|
|
|
STDMETHODIMP nsDataObj::CStream::Clone(IStream** ppStream)
|
|
|
|
{
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::CStream::Commit(DWORD dwFrags)
|
|
|
|
{
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::CStream::CopyTo(IStream* pDestStream,
|
|
|
|
ULARGE_INTEGER nBytesToCopy,
|
|
|
|
ULARGE_INTEGER* nBytesRead,
|
|
|
|
ULARGE_INTEGER* nBytesWritten)
|
|
|
|
{
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::CStream::LockRegion(ULARGE_INTEGER nStart,
|
|
|
|
ULARGE_INTEGER nBytes,
|
|
|
|
DWORD dwFlags)
|
|
|
|
{
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::CStream::Read(void* pvBuffer,
|
|
|
|
ULONG nBytesToRead,
|
|
|
|
ULONG* nBytesRead)
|
|
|
|
{
|
2011-08-10 13:15:16 -07:00
|
|
|
// Wait for the write into our buffer to complete via the stream listener.
|
|
|
|
// We can't respond to this by saying "call us back later".
|
|
|
|
if (NS_FAILED(WaitForCompletion()))
|
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-08-10 13:15:16 -07:00
|
|
|
// Bytes left for Windows to read out of our buffer
|
|
|
|
ULONG bytesLeft = mChannelData.Length() - mStreamRead;
|
|
|
|
// Let Windows know what we will hand back, usually this is the entire buffer
|
|
|
|
*nBytesRead = NS_MIN(bytesLeft, nBytesToRead);
|
|
|
|
// Copy the buffer data over
|
|
|
|
memcpy(pvBuffer, ((char*)mChannelData.Elements() + mStreamRead), *nBytesRead);
|
|
|
|
// Update our bytes read tracking
|
|
|
|
mStreamRead += *nBytesRead;
|
2007-03-22 10:30:00 -07:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::CStream::Revert(void)
|
|
|
|
{
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::CStream::Seek(LARGE_INTEGER nMove,
|
|
|
|
DWORD dwOrigin,
|
|
|
|
ULARGE_INTEGER* nNewPos)
|
|
|
|
{
|
2007-10-18 21:30:03 -07:00
|
|
|
if (nNewPos == NULL)
|
|
|
|
return STG_E_INVALIDPOINTER;
|
|
|
|
|
|
|
|
if (nMove.LowPart == 0 && nMove.HighPart == 0 &&
|
|
|
|
(dwOrigin == STREAM_SEEK_SET || dwOrigin == STREAM_SEEK_CUR)) {
|
|
|
|
nNewPos->LowPart = 0;
|
|
|
|
nNewPos->HighPart = 0;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::CStream::SetSize(ULARGE_INTEGER nNewSize)
|
|
|
|
{
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::CStream::Stat(STATSTG* statstg, DWORD dwFlags)
|
|
|
|
{
|
2007-10-18 21:30:03 -07:00
|
|
|
if (statstg == NULL)
|
|
|
|
return STG_E_INVALIDPOINTER;
|
|
|
|
|
2011-08-10 13:15:16 -07:00
|
|
|
if (!mChannel || NS_FAILED(WaitForCompletion()))
|
2007-10-18 21:30:03 -07:00
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
memset((void*)statstg, 0, sizeof(STATSTG));
|
|
|
|
|
|
|
|
if (dwFlags != STATFLAG_NONAME)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
|
|
|
if (NS_FAILED(mChannel->GetURI(getter_AddRefs(sourceURI)))) {
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString strFileName;
|
2007-10-18 21:30:03 -07:00
|
|
|
nsCOMPtr<nsIURL> sourceURL = do_QueryInterface(sourceURI);
|
|
|
|
sourceURL->GetFileName(strFileName);
|
|
|
|
|
|
|
|
if (strFileName.IsEmpty())
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
NS_UnescapeURL(strFileName);
|
|
|
|
NS_ConvertUTF8toUTF16 wideFileName(strFileName);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t nMaxNameLength = (wideFileName.Length()*2) + 2;
|
2007-10-18 21:30:03 -07:00
|
|
|
void * retBuf = CoTaskMemAlloc(nMaxNameLength); // freed by caller
|
|
|
|
if (!retBuf)
|
|
|
|
return STG_E_INSUFFICIENTMEMORY;
|
|
|
|
|
|
|
|
ZeroMemory(retBuf, nMaxNameLength);
|
|
|
|
memcpy(retBuf, wideFileName.get(), wideFileName.Length()*2);
|
|
|
|
statstg->pwcsName = (LPOLESTR)retBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSTEMTIME st;
|
|
|
|
|
|
|
|
statstg->type = STGTY_STREAM;
|
|
|
|
|
|
|
|
GetSystemTime(&st);
|
|
|
|
SystemTimeToFileTime((const SYSTEMTIME*)&st, (LPFILETIME)&statstg->mtime);
|
|
|
|
statstg->ctime = statstg->atime = statstg->mtime;
|
|
|
|
|
2011-08-10 13:15:16 -07:00
|
|
|
statstg->cbSize.LowPart = (DWORD)mChannelData.Length();
|
2007-10-18 21:30:03 -07:00
|
|
|
statstg->grfMode = STGM_READ;
|
|
|
|
statstg->grfLocksSupported = LOCK_ONLYONCE;
|
|
|
|
statstg->clsid = CLSID_NULL;
|
|
|
|
|
|
|
|
return S_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::CStream::UnlockRegion(ULARGE_INTEGER nStart,
|
|
|
|
ULARGE_INTEGER nBytes,
|
|
|
|
DWORD dwFlags)
|
|
|
|
{
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::CStream::Write(const void* pvBuffer,
|
|
|
|
ULONG nBytesToRead,
|
|
|
|
ULONG* nBytesRead)
|
|
|
|
{
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
HRESULT nsDataObj::CreateStream(IStream **outStream)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(outStream, E_INVALIDARG);
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
nsAutoString wideFileName;
|
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
|
|
|
|
|
|
|
rv = GetDownloadDetails(getter_AddRefs(sourceURI),
|
|
|
|
wideFileName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsDataObj::CStream *pStream = new nsDataObj::CStream();
|
|
|
|
NS_ENSURE_TRUE(pStream, E_OUTOFMEMORY);
|
|
|
|
|
2011-08-10 13:15:16 -07:00
|
|
|
pStream->AddRef();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = pStream->Init(sourceURI);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
pStream->Release();
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
*outStream = pStream;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-20 11:19:38 -07:00
|
|
|
static GUID CLSID_nsDataObj =
|
2007-03-22 10:30:00 -07:00
|
|
|
{ 0x1bba7640, 0xdf52, 0x11cf, { 0x82, 0x7b, 0, 0xa0, 0x24, 0x3a, 0xe5, 0x05 } };
|
|
|
|
|
|
|
|
/*
|
|
|
|
* deliberately not using MAX_PATH. This is because on platforms < XP
|
|
|
|
* a file created with a long filename may be mishandled by the shell
|
|
|
|
* resulting in it not being able to be deleted or moved.
|
|
|
|
* See bug 250392 for more details.
|
|
|
|
*/
|
|
|
|
#define NS_MAX_FILEDESCRIPTOR 128 + 1
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class nsDataObj
|
|
|
|
*/
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// construction
|
|
|
|
//-----------------------------------------------------
|
|
|
|
nsDataObj::nsDataObj(nsIURI * uri)
|
2012-07-30 07:20:58 -07:00
|
|
|
: m_cRef(0), mTransferable(nullptr),
|
2008-10-12 09:34:26 -07:00
|
|
|
mIsAsyncMode(FALSE), mIsInOperation(FALSE)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-21 13:07:26 -07:00
|
|
|
mIOThread = new LazyIdleThread(DEFAULT_THREAD_TIMEOUT_MS,
|
|
|
|
NS_LITERAL_CSTRING("nsDataObj"),
|
|
|
|
LazyIdleThread::ManualShutdown);
|
2009-12-20 07:56:06 -08:00
|
|
|
m_enumFE = new CEnumFormatEtc();
|
2007-03-22 10:30:00 -07:00
|
|
|
m_enumFE->AddRef();
|
|
|
|
|
|
|
|
if (uri) {
|
|
|
|
// A URI was obtained, so pass this through to the DataObject
|
|
|
|
// so it can create a SourceURL for CF_HTML flavour
|
|
|
|
uri->GetSpec(mSourceURL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// destruction
|
|
|
|
//-----------------------------------------------------
|
|
|
|
nsDataObj::~nsDataObj()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mTransferable);
|
2008-09-06 09:24:45 -07:00
|
|
|
|
2009-03-10 19:26:03 -07:00
|
|
|
mDataFlavors.Clear();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
m_enumFE->Release();
|
|
|
|
|
2007-11-16 20:31:32 -08:00
|
|
|
// Free arbitrary system formats
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t idx = 0; idx < mDataEntryList.Length(); idx++) {
|
2007-11-16 20:31:32 -08:00
|
|
|
CoTaskMemFree(mDataEntryList[idx]->fe.ptd);
|
|
|
|
ReleaseStgMedium(&mDataEntryList[idx]->stgm);
|
|
|
|
CoTaskMemFree(mDataEntryList[idx]);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// IUnknown interface methods - see inknown.h for documentation
|
|
|
|
//-----------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::QueryInterface(REFIID riid, void** ppv)
|
|
|
|
{
|
|
|
|
*ppv=NULL;
|
|
|
|
|
|
|
|
if ( (IID_IUnknown == riid) || (IID_IDataObject == riid) ) {
|
|
|
|
*ppv = this;
|
|
|
|
AddRef();
|
|
|
|
return S_OK;
|
|
|
|
} else if (IID_IAsyncOperation == riid) {
|
|
|
|
*ppv = static_cast<IAsyncOperation*>(this);
|
|
|
|
AddRef();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_NOINTERFACE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
STDMETHODIMP_(ULONG) nsDataObj::AddRef()
|
|
|
|
{
|
|
|
|
++m_cRef;
|
|
|
|
NS_LOG_ADDREF(this, m_cRef, "nsDataObj", sizeof(*this));
|
|
|
|
return m_cRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
STDMETHODIMP_(ULONG) nsDataObj::Release()
|
|
|
|
{
|
2008-02-06 20:07:15 -08:00
|
|
|
--m_cRef;
|
2009-12-21 12:48:12 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_LOG_RELEASE(this, m_cRef, "nsDataObj");
|
2008-02-06 20:07:15 -08:00
|
|
|
if (0 != m_cRef)
|
2007-03-22 10:30:00 -07:00
|
|
|
return m_cRef;
|
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
// We have released our last ref on this object and need to delete the
|
|
|
|
// temp file. External app acting as drop target may still need to open the
|
|
|
|
// temp file. Addref a timer so it can delay deleting file and destroying
|
|
|
|
// this object. Delete file anyway and destroy this obj if there's a problem.
|
|
|
|
if (mCachedTempFile) {
|
|
|
|
nsresult rv;
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mTimer->InitWithFuncCallback(nsDataObj::RemoveTempFile, this,
|
|
|
|
500, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
return AddRef();
|
|
|
|
}
|
2011-10-01 19:16:19 -07:00
|
|
|
mCachedTempFile->Remove(false);
|
2009-12-17 20:06:19 -08:00
|
|
|
mCachedTempFile = NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
delete this;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
BOOL nsDataObj::FormatsMatch(const FORMATETC& source, const FORMATETC& target) const
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
if ((source.cfFormat == target.cfFormat) &&
|
|
|
|
(source.dwAspect & target.dwAspect) &&
|
|
|
|
(source.tymed & target.tymed)) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// IDataObject methods
|
|
|
|
//-----------------------------------------------------
|
2009-12-21 12:48:12 -08:00
|
|
|
STDMETHODIMP nsDataObj::GetData(LPFORMATETC aFormat, LPSTGMEDIUM pSTM)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
if (!mTransferable)
|
|
|
|
return DV_E_FORMATETC;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t dfInx = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static CLIPFORMAT fileDescriptorFlavorA = ::RegisterClipboardFormat( CFSTR_FILEDESCRIPTORA );
|
|
|
|
static CLIPFORMAT fileDescriptorFlavorW = ::RegisterClipboardFormat( CFSTR_FILEDESCRIPTORW );
|
|
|
|
static CLIPFORMAT uniformResourceLocatorA = ::RegisterClipboardFormat( CFSTR_INETURLA );
|
|
|
|
static CLIPFORMAT uniformResourceLocatorW = ::RegisterClipboardFormat( CFSTR_INETURLW );
|
|
|
|
static CLIPFORMAT fileFlavor = ::RegisterClipboardFormat( CFSTR_FILECONTENTS );
|
|
|
|
static CLIPFORMAT PreferredDropEffect = ::RegisterClipboardFormat( CFSTR_PREFERREDDROPEFFECT );
|
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
// Arbitrary system formats are used for image feedback during drag
|
|
|
|
// and drop. We are responsible for storing these internally during
|
|
|
|
// drag operations.
|
2007-11-16 20:31:32 -08:00
|
|
|
LPDATAENTRY pde;
|
2009-12-21 12:48:12 -08:00
|
|
|
if (LookupArbitraryFormat(aFormat, &pde, FALSE)) {
|
2009-12-27 09:16:38 -08:00
|
|
|
return CopyMediumData(pSTM, &pde->stgm, aFormat, FALSE)
|
|
|
|
? S_OK : E_UNEXPECTED;
|
2007-11-16 20:31:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Firefox internal formats
|
2007-03-22 10:30:00 -07:00
|
|
|
ULONG count;
|
|
|
|
FORMATETC fe;
|
|
|
|
m_enumFE->Reset();
|
2009-03-10 19:26:03 -07:00
|
|
|
while (NOERROR == m_enumFE->Next(1, &fe, &count)
|
2009-03-10 19:58:19 -07:00
|
|
|
&& dfInx < mDataFlavors.Length()) {
|
2009-03-10 19:26:03 -07:00
|
|
|
nsCString& df = mDataFlavors.ElementAt(dfInx);
|
2009-12-21 12:48:12 -08:00
|
|
|
if (FormatsMatch(fe, *aFormat)) {
|
2009-03-10 19:26:03 -07:00
|
|
|
pSTM->pUnkForRelease = NULL; // caller is responsible for deleting this data
|
2009-12-21 12:48:12 -08:00
|
|
|
CLIPFORMAT format = aFormat->cfFormat;
|
2009-03-10 19:26:03 -07:00
|
|
|
switch(format) {
|
|
|
|
|
|
|
|
// Someone is asking for plain or unicode text
|
|
|
|
case CF_TEXT:
|
|
|
|
case CF_UNICODETEXT:
|
2009-12-21 12:48:12 -08:00
|
|
|
return GetText(df, *aFormat, *pSTM);
|
2009-03-10 19:26:03 -07:00
|
|
|
|
|
|
|
// Some 3rd party apps that receive drag and drop files from the browser
|
|
|
|
// window require support for this.
|
|
|
|
case CF_HDROP:
|
2009-12-21 12:48:12 -08:00
|
|
|
return GetFile(*aFormat, *pSTM);
|
2009-03-10 19:26:03 -07:00
|
|
|
|
|
|
|
// Someone is asking for an image
|
2012-07-31 05:11:55 -07:00
|
|
|
case CF_DIBV5:
|
2012-09-06 19:48:16 -07:00
|
|
|
case CF_DIB:
|
2009-12-21 12:48:12 -08:00
|
|
|
return GetDib(df, *aFormat, *pSTM);
|
2009-03-10 19:26:03 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
if ( format == fileDescriptorFlavorA )
|
2011-10-01 19:16:19 -07:00
|
|
|
return GetFileDescriptor ( *aFormat, *pSTM, false );
|
2009-03-10 19:26:03 -07:00
|
|
|
if ( format == fileDescriptorFlavorW )
|
2011-10-01 19:16:19 -07:00
|
|
|
return GetFileDescriptor ( *aFormat, *pSTM, true);
|
2009-03-10 19:26:03 -07:00
|
|
|
if ( format == uniformResourceLocatorA )
|
2011-10-01 19:16:19 -07:00
|
|
|
return GetUniformResourceLocator( *aFormat, *pSTM, false);
|
2009-03-10 19:26:03 -07:00
|
|
|
if ( format == uniformResourceLocatorW )
|
2011-10-01 19:16:19 -07:00
|
|
|
return GetUniformResourceLocator( *aFormat, *pSTM, true);
|
2009-03-10 19:26:03 -07:00
|
|
|
if ( format == fileFlavor )
|
2009-12-21 12:48:12 -08:00
|
|
|
return GetFileContents ( *aFormat, *pSTM );
|
2009-03-10 19:26:03 -07:00
|
|
|
if ( format == PreferredDropEffect )
|
2009-12-21 12:48:12 -08:00
|
|
|
return GetPreferredDropEffect( *aFormat, *pSTM );
|
2011-08-16 12:30:44 -07:00
|
|
|
//PR_LOG(gWindowsLog, PR_LOG_ALWAYS,
|
|
|
|
// ("***** nsDataObj::GetData - Unknown format %u\n", format));
|
2009-12-21 12:48:12 -08:00
|
|
|
return GetText(df, *aFormat, *pSTM);
|
2009-03-10 19:26:03 -07:00
|
|
|
} //switch
|
|
|
|
} // if
|
2007-03-22 10:30:00 -07:00
|
|
|
dfInx++;
|
|
|
|
} // while
|
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
return DATA_E_FORMATETC;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::GetDataHere(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// Other objects querying to see if we support a
|
|
|
|
// particular format
|
|
|
|
//-----------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::QueryGetData(LPFORMATETC pFE)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
// Arbitrary system formats are used for image feedback during drag
|
|
|
|
// and drop. We are responsible for storing these internally during
|
|
|
|
// drag operations.
|
2007-11-16 20:31:32 -08:00
|
|
|
LPDATAENTRY pde;
|
2009-12-21 12:48:12 -08:00
|
|
|
if (LookupArbitraryFormat(pFE, &pde, FALSE))
|
2007-11-16 20:31:32 -08:00
|
|
|
return S_OK;
|
|
|
|
|
|
|
|
// Firefox internal formats
|
2007-03-22 10:30:00 -07:00
|
|
|
ULONG count;
|
|
|
|
FORMATETC fe;
|
|
|
|
m_enumFE->Reset();
|
|
|
|
while (NOERROR == m_enumFE->Next(1, &fe, &count)) {
|
|
|
|
if (fe.cfFormat == pFE->cfFormat) {
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::GetCanonicalFormatEtc
|
|
|
|
(LPFORMATETC pFEIn, LPFORMATETC pFEOut)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_FAIL;
|
2007-11-16 20:31:32 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------
|
2009-12-21 12:48:12 -08:00
|
|
|
STDMETHODIMP nsDataObj::SetData(LPFORMATETC aFormat, LPSTGMEDIUM aMedium, BOOL shouldRel)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
// Arbitrary system formats are used for image feedback during drag
|
|
|
|
// and drop. We are responsible for storing these internally during
|
|
|
|
// drag operations.
|
2007-11-16 20:31:32 -08:00
|
|
|
LPDATAENTRY pde;
|
2009-12-21 12:48:12 -08:00
|
|
|
if (LookupArbitraryFormat(aFormat, &pde, TRUE)) {
|
|
|
|
// Release the old data the lookup handed us for this format. This
|
|
|
|
// may have been set in CopyMediumData when we originally stored the
|
|
|
|
// data.
|
2007-11-16 20:31:32 -08:00
|
|
|
if (pde->stgm.tymed) {
|
|
|
|
ReleaseStgMedium(&pde->stgm);
|
2009-12-21 12:48:12 -08:00
|
|
|
memset(&pde->stgm, 0, sizeof(STGMEDIUM));
|
2007-11-16 20:31:32 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool result = true;
|
2009-12-21 12:48:12 -08:00
|
|
|
if (shouldRel) {
|
|
|
|
// If shouldRel is TRUE, the data object called owns the storage medium
|
|
|
|
// after the call returns. Store the incoming data in our data array for
|
|
|
|
// release when we are destroyed. This is the common case with arbitrary
|
|
|
|
// data from explorer.
|
|
|
|
pde->stgm = *aMedium;
|
2007-11-16 20:31:32 -08:00
|
|
|
} else {
|
2009-12-21 12:48:12 -08:00
|
|
|
// Copy the incoming data into our data array. (AFAICT, this never gets
|
|
|
|
// called with arbitrary formats for drag images.)
|
|
|
|
result = CopyMediumData(&pde->stgm, aMedium, aFormat, TRUE);
|
2007-11-16 20:31:32 -08:00
|
|
|
}
|
|
|
|
pde->fe.tymed = pde->stgm.tymed;
|
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
return result ? S_OK : DV_E_TYMED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
if (shouldRel)
|
|
|
|
ReleaseStgMedium(aMedium);
|
2007-11-16 20:31:32 -08:00
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
return S_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-12-21 12:48:12 -08:00
|
|
|
nsDataObj::LookupArbitraryFormat(FORMATETC *aFormat, LPDATAENTRY *aDataEntry, BOOL aAddorUpdate)
|
2007-11-16 20:31:32 -08:00
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
*aDataEntry = NULL;
|
2007-11-16 20:31:32 -08:00
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
if (aFormat->ptd != NULL)
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2007-11-16 20:31:32 -08:00
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
// See if it's already in our list. If so return the data entry.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t idx = 0; idx < mDataEntryList.Length(); idx++) {
|
2009-12-21 12:48:12 -08:00
|
|
|
if (mDataEntryList[idx]->fe.cfFormat == aFormat->cfFormat &&
|
|
|
|
mDataEntryList[idx]->fe.dwAspect == aFormat->dwAspect &&
|
|
|
|
mDataEntryList[idx]->fe.lindex == aFormat->lindex) {
|
|
|
|
if (aAddorUpdate || (mDataEntryList[idx]->fe.tymed & aFormat->tymed)) {
|
|
|
|
// If the caller requests we update, or if the
|
|
|
|
// medium type matches, return the entry.
|
|
|
|
*aDataEntry = mDataEntryList[idx];
|
2011-10-01 19:16:19 -07:00
|
|
|
return true;
|
2007-11-16 20:31:32 -08:00
|
|
|
} else {
|
2009-12-21 12:48:12 -08:00
|
|
|
// Medium does not match, not found.
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2007-11-16 20:31:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
if (!aAddorUpdate)
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2007-11-16 20:31:32 -08:00
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
// Add another entry to mDataEntryList
|
|
|
|
LPDATAENTRY dataEntry = (LPDATAENTRY)CoTaskMemAlloc(sizeof(DATAENTRY));
|
|
|
|
if (!dataEntry)
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2009-12-21 12:48:12 -08:00
|
|
|
|
|
|
|
dataEntry->fe = *aFormat;
|
|
|
|
*aDataEntry = dataEntry;
|
|
|
|
memset(&dataEntry->stgm, 0, sizeof(STGMEDIUM));
|
2007-11-16 20:31:32 -08:00
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
// Add this to our IEnumFORMATETC impl. so we can return it when
|
|
|
|
// it's requested.
|
|
|
|
m_enumFE->AddFormatEtc(aFormat);
|
2007-11-16 20:31:32 -08:00
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
// Store a copy internally in the arbitrary formats array.
|
|
|
|
mDataEntryList.AppendElement(dataEntry);
|
2007-11-16 20:31:32 -08:00
|
|
|
|
2011-10-01 19:16:19 -07:00
|
|
|
return true;
|
2009-12-21 12:48:12 -08:00
|
|
|
}
|
2007-11-16 20:31:32 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-12-21 12:48:12 -08:00
|
|
|
nsDataObj::CopyMediumData(STGMEDIUM *aMediumDst, STGMEDIUM *aMediumSrc, LPFORMATETC aFormat, BOOL aSetData)
|
|
|
|
{
|
|
|
|
STGMEDIUM stgmOut = *aMediumSrc;
|
|
|
|
|
|
|
|
switch (stgmOut.tymed) {
|
2007-11-16 20:31:32 -08:00
|
|
|
case TYMED_ISTREAM:
|
|
|
|
stgmOut.pstm->AddRef();
|
2009-12-21 12:48:12 -08:00
|
|
|
break;
|
2007-11-16 20:31:32 -08:00
|
|
|
case TYMED_ISTORAGE:
|
|
|
|
stgmOut.pstg->AddRef();
|
2009-12-21 12:48:12 -08:00
|
|
|
break;
|
|
|
|
case TYMED_HGLOBAL:
|
|
|
|
if (!aMediumSrc->pUnkForRelease) {
|
|
|
|
if (aSetData) {
|
|
|
|
if (aMediumSrc->tymed != TYMED_HGLOBAL)
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2009-12-21 12:48:12 -08:00
|
|
|
stgmOut.hGlobal = OleDuplicateData(aMediumSrc->hGlobal, aFormat->cfFormat, 0);
|
|
|
|
if (!stgmOut.hGlobal)
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2009-12-21 12:48:12 -08:00
|
|
|
} else {
|
|
|
|
// We are returning this data from LookupArbitraryFormat, indicate to the
|
|
|
|
// shell we hold it and will free it.
|
|
|
|
stgmOut.pUnkForRelease = static_cast<IDataObject*>(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2007-11-16 20:31:32 -08:00
|
|
|
}
|
|
|
|
|
2009-12-21 12:48:12 -08:00
|
|
|
if (stgmOut.pUnkForRelease)
|
|
|
|
stgmOut.pUnkForRelease->AddRef();
|
|
|
|
|
|
|
|
*aMediumDst = stgmOut;
|
|
|
|
|
2011-10-01 19:16:19 -07:00
|
|
|
return true;
|
2007-11-16 20:31:32 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::EnumFormatEtc(DWORD dwDir, LPENUMFORMATETC *ppEnum)
|
|
|
|
{
|
|
|
|
switch (dwDir) {
|
2008-10-12 09:34:26 -07:00
|
|
|
case DATADIR_GET:
|
|
|
|
m_enumFE->Clone(ppEnum);
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
case DATADIR_SET:
|
2008-10-12 09:34:26 -07:00
|
|
|
// fall through
|
2007-03-22 10:30:00 -07:00
|
|
|
default:
|
2008-10-12 09:34:26 -07:00
|
|
|
*ppEnum = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
} // switch
|
|
|
|
|
|
|
|
if (NULL == *ppEnum)
|
2010-02-20 05:45:19 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-20 05:44:02 -08:00
|
|
|
(*ppEnum)->Reset();
|
2008-02-06 20:07:15 -08:00
|
|
|
// Clone already AddRefed the result so don't addref it again.
|
2007-03-22 10:30:00 -07:00
|
|
|
return NOERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::DAdvise(LPFORMATETC pFE, DWORD dwFlags,
|
|
|
|
LPADVISESINK pIAdviseSink, DWORD* pdwConn)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::DUnadvise(DWORD dwConn)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
STDMETHODIMP nsDataObj::EnumDAdvise(LPENUMSTATDATA *ppEnum)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// IAsyncOperation methods
|
|
|
|
STDMETHODIMP nsDataObj::EndOperation(HRESULT hResult,
|
|
|
|
IBindCtx *pbcReserved,
|
|
|
|
DWORD dwEffects)
|
|
|
|
{
|
|
|
|
mIsInOperation = FALSE;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
STDMETHODIMP nsDataObj::GetAsyncMode(BOOL *pfIsOpAsync)
|
|
|
|
{
|
|
|
|
*pfIsOpAsync = mIsAsyncMode;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
STDMETHODIMP nsDataObj::InOperation(BOOL *pfInAsyncOp)
|
|
|
|
{
|
|
|
|
*pfInAsyncOp = mIsInOperation;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
STDMETHODIMP nsDataObj::SetAsyncMode(BOOL fDoOpAsync)
|
|
|
|
{
|
|
|
|
mIsAsyncMode = fDoOpAsync;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
STDMETHODIMP nsDataObj::StartOperation(IBindCtx *pbcReserved)
|
|
|
|
{
|
|
|
|
mIsInOperation = TRUE;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// GetData and SetData helper functions
|
|
|
|
//-----------------------------------------------------
|
|
|
|
HRESULT nsDataObj::AddSetFormat(FORMATETC& aFE)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return S_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
HRESULT nsDataObj::AddGetFormat(FORMATETC& aFE)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return S_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetDIB
|
|
|
|
//
|
|
|
|
// Someone is asking for a bitmap. The data in the transferable will be a straight
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-20 18:50:15 -07:00
|
|
|
// imgIContainer, so just QI it.
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
HRESULT
|
2012-09-06 19:48:16 -07:00
|
|
|
nsDataObj::GetDib(const nsACString& inFlavor,
|
|
|
|
FORMATETC &aFormat,
|
|
|
|
STGMEDIUM & aSTG)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
ULONG result = E_FAIL;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> genericDataWrapper;
|
|
|
|
mTransferable->GetTransferData(PromiseFlatCString(inFlavor).get(), getter_AddRefs(genericDataWrapper), &len);
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-20 18:50:15 -07:00
|
|
|
nsCOMPtr<imgIContainer> image ( do_QueryInterface(genericDataWrapper) );
|
2007-03-22 10:30:00 -07:00
|
|
|
if ( !image ) {
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-20 18:50:15 -07:00
|
|
|
// Check if the image was put in an nsISupportsInterfacePointer wrapper.
|
|
|
|
// This might not be necessary any more, but could be useful for backwards
|
|
|
|
// compatibility.
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupportsInterfacePointer> ptr(do_QueryInterface(genericDataWrapper));
|
|
|
|
if ( ptr )
|
|
|
|
ptr->GetData(getter_AddRefs(image));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( image ) {
|
2010-08-15 15:50:02 -07:00
|
|
|
// use the |nsImageToClipboard| helper class to build up a bitmap. We now own
|
|
|
|
// the bits, and pass them back to the OS in |aSTG|.
|
2012-09-06 19:48:16 -07:00
|
|
|
nsImageToClipboard converter(image, aFormat.cfFormat == CF_DIBV5);
|
2012-07-30 07:20:58 -07:00
|
|
|
HANDLE bits = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = converter.GetPicture ( &bits );
|
|
|
|
if ( NS_SUCCEEDED(rv) && bits ) {
|
|
|
|
aSTG.hGlobal = bits;
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
result = S_OK;
|
|
|
|
}
|
|
|
|
} // if we have an image
|
|
|
|
else
|
|
|
|
NS_WARNING ( "Definitely not an image on clipboard" );
|
2010-02-20 05:45:19 -08:00
|
|
|
return result;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetFileDescriptor
|
|
|
|
//
|
|
|
|
|
|
|
|
HRESULT
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDataObj :: GetFileDescriptor ( FORMATETC& aFE, STGMEDIUM& aSTG, bool aIsUnicode )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
HRESULT res = S_OK;
|
|
|
|
|
|
|
|
// How we handle this depends on if we're dealing with an internet
|
|
|
|
// shortcut, since those are done under the covers.
|
|
|
|
if (IsFlavourPresent(kFilePromiseMime) ||
|
|
|
|
IsFlavourPresent(kFileMime))
|
|
|
|
{
|
|
|
|
if (aIsUnicode)
|
|
|
|
return GetFileDescriptor_IStreamW(aFE, aSTG);
|
|
|
|
else
|
|
|
|
return GetFileDescriptor_IStreamA(aFE, aSTG);
|
|
|
|
}
|
|
|
|
else if (IsFlavourPresent(kURLMime))
|
|
|
|
{
|
|
|
|
if ( aIsUnicode )
|
|
|
|
res = GetFileDescriptorInternetShortcutW ( aFE, aSTG );
|
|
|
|
else
|
|
|
|
res = GetFileDescriptorInternetShortcutA ( aFE, aSTG );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NS_WARNING ( "Not yet implemented\n" );
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} // GetFileDescriptor
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
HRESULT
|
|
|
|
nsDataObj :: GetFileContents ( FORMATETC& aFE, STGMEDIUM& aSTG )
|
|
|
|
{
|
|
|
|
HRESULT res = S_OK;
|
|
|
|
|
|
|
|
// How we handle this depends on if we're dealing with an internet
|
|
|
|
// shortcut, since those are done under the covers.
|
|
|
|
if (IsFlavourPresent(kFilePromiseMime) ||
|
|
|
|
IsFlavourPresent(kFileMime))
|
|
|
|
return GetFileContents_IStream(aFE, aSTG);
|
|
|
|
else if (IsFlavourPresent(kURLMime))
|
|
|
|
return GetFileContentsInternetShortcut ( aFE, aSTG );
|
|
|
|
else
|
|
|
|
NS_WARNING ( "Not yet implemented\n" );
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
} // GetFileContents
|
|
|
|
|
|
|
|
//
|
|
|
|
// Given a unicode string, we ensure that it contains only characters which are valid within
|
|
|
|
// the file system. Remove all forbidden characters from the name, and completely disallow
|
|
|
|
// any title that starts with a forbidden name and extension (e.g. "nul" is invalid, but
|
|
|
|
// "nul." and "nul.txt" are also invalid and will cause problems).
|
|
|
|
//
|
2012-06-05 19:08:30 -07:00
|
|
|
// It would seem that this is more functionality suited to being in nsIFile.
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
static void
|
|
|
|
MangleTextToValidFilename(nsString & aText)
|
|
|
|
{
|
|
|
|
static const char* forbiddenNames[] = {
|
|
|
|
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
|
|
|
|
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
|
|
|
|
"CON", "PRN", "AUX", "NUL", "CLOCK$"
|
|
|
|
};
|
|
|
|
|
|
|
|
aText.StripChars(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS);
|
2011-10-01 19:16:19 -07:00
|
|
|
aText.CompressWhitespace(true, true);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t nameLen;
|
2011-10-10 22:50:08 -07:00
|
|
|
for (size_t n = 0; n < ArrayLength(forbiddenNames); ++n) {
|
2012-08-22 08:56:38 -07:00
|
|
|
nameLen = (uint32_t) strlen(forbiddenNames[n]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aText.EqualsIgnoreCase(forbiddenNames[n], nameLen)) {
|
|
|
|
// invalid name is either the entire string, or a prefix with a period
|
|
|
|
if (aText.Length() == nameLen || aText.CharAt(nameLen) == PRUnichar('.')) {
|
|
|
|
aText.Truncate();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Given a unicode string, convert it down to a valid local charset filename
|
|
|
|
// with the supplied extension. This ensures that we do not cut MBCS characters
|
|
|
|
// in the middle.
|
|
|
|
//
|
2012-06-05 19:08:30 -07:00
|
|
|
// It would seem that this is more functionality suited to being in nsIFile.
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
CreateFilenameFromTextA(nsString & aText, const char * aExtension,
|
2012-08-22 08:56:38 -07:00
|
|
|
char * aFilename, uint32_t aFilenameLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// ensure that the supplied name doesn't have invalid characters. If
|
|
|
|
// a valid mangled filename couldn't be created then it will leave the
|
|
|
|
// text empty.
|
|
|
|
MangleTextToValidFilename(aText);
|
|
|
|
if (aText.IsEmpty())
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// repeatably call WideCharToMultiByte as long as the title doesn't fit in the buffer
|
|
|
|
// available to us. Continually reduce the length of the source title until the MBCS
|
|
|
|
// version will fit in the buffer with room for the supplied extension. Doing it this
|
|
|
|
// way ensures that even in MBCS environments there will be a valid MBCS filename of
|
|
|
|
// the correct length.
|
|
|
|
int maxUsableFilenameLen = aFilenameLen - strlen(aExtension) - 1; // space for ext + null byte
|
|
|
|
int currLen, textLen = (int) NS_MIN(aText.Length(), aFilenameLen);
|
|
|
|
char defaultChar = '_';
|
|
|
|
do {
|
|
|
|
currLen = WideCharToMultiByte(CP_ACP,
|
|
|
|
WC_COMPOSITECHECK|WC_DEFAULTCHAR,
|
|
|
|
aText.get(), textLen--, aFilename, maxUsableFilenameLen, &defaultChar, NULL);
|
|
|
|
}
|
|
|
|
while (currLen == 0 && textLen > 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
if (currLen > 0 && textLen > 0) {
|
|
|
|
strcpy(&aFilename[currLen], aExtension);
|
2011-10-01 19:16:19 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// empty names aren't permitted
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
CreateFilenameFromTextW(nsString & aText, const wchar_t * aExtension,
|
2012-08-22 08:56:38 -07:00
|
|
|
wchar_t * aFilename, uint32_t aFilenameLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// ensure that the supplied name doesn't have invalid characters. If
|
|
|
|
// a valid mangled filename couldn't be created then it will leave the
|
|
|
|
// text empty.
|
|
|
|
MangleTextToValidFilename(aText);
|
|
|
|
if (aText.IsEmpty())
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
const int extensionLen = wcslen(aExtension);
|
|
|
|
if (aText.Length() + extensionLen + 1 > aFilenameLen)
|
|
|
|
aText.Truncate(aFilenameLen - extensionLen - 1);
|
|
|
|
wcscpy(&aFilename[0], aText.get());
|
|
|
|
wcscpy(&aFilename[aText.Length()], aExtension);
|
2011-10-01 19:16:19 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PAGEINFO_PROPERTIES "chrome://navigator/locale/pageInfo.properties"
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
GetLocalizedString(const PRUnichar * aName, nsXPIDLString & aString)
|
|
|
|
{
|
2010-05-14 02:24:41 -07:00
|
|
|
nsCOMPtr<nsIStringBundleService> stringService =
|
|
|
|
mozilla::services::GetStringBundleService();
|
|
|
|
if (!stringService)
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> stringBundle;
|
2010-05-14 02:24:41 -07:00
|
|
|
nsresult rv = stringService->CreateBundle(PAGEINFO_PROPERTIES,
|
|
|
|
getter_AddRefs(stringBundle));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = stringBundle->GetStringFromName(aName, getter_Copies(aString));
|
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetFileDescriptorInternetShortcut
|
|
|
|
//
|
|
|
|
// Create the special format for an internet shortcut and build up the data
|
|
|
|
// structures the shell is expecting.
|
|
|
|
//
|
|
|
|
HRESULT
|
|
|
|
nsDataObj :: GetFileDescriptorInternetShortcutA ( FORMATETC& aFE, STGMEDIUM& aSTG )
|
|
|
|
{
|
|
|
|
// get the title of the shortcut
|
|
|
|
nsAutoString title;
|
|
|
|
if ( NS_FAILED(ExtractShortcutTitle(title)) )
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
HGLOBAL fileGroupDescHandle = ::GlobalAlloc(GMEM_ZEROINIT|GMEM_SHARE,sizeof(FILEGROUPDESCRIPTORA));
|
|
|
|
if (!fileGroupDescHandle)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
LPFILEGROUPDESCRIPTORA fileGroupDescA = reinterpret_cast<LPFILEGROUPDESCRIPTORA>(::GlobalLock(fileGroupDescHandle));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fileGroupDescA) {
|
|
|
|
::GlobalFree(fileGroupDescHandle);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a valid filename in the following order: 1) from the page title,
|
|
|
|
// 2) localized string for an untitled page, 3) just use "Untitled.URL"
|
|
|
|
if (!CreateFilenameFromTextA(title, ".URL",
|
|
|
|
fileGroupDescA->fgd[0].cFileName, NS_MAX_FILEDESCRIPTOR)) {
|
|
|
|
nsXPIDLString untitled;
|
|
|
|
if (!GetLocalizedString(NS_LITERAL_STRING("noPageTitle").get(), untitled) ||
|
|
|
|
!CreateFilenameFromTextA(untitled, ".URL",
|
|
|
|
fileGroupDescA->fgd[0].cFileName, NS_MAX_FILEDESCRIPTOR)) {
|
|
|
|
strcpy(fileGroupDescA->fgd[0].cFileName, "Untitled.URL");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// one file in the file block
|
|
|
|
fileGroupDescA->cItems = 1;
|
|
|
|
fileGroupDescA->fgd[0].dwFlags = FD_LINKUI;
|
|
|
|
|
|
|
|
::GlobalUnlock( fileGroupDescHandle );
|
|
|
|
aSTG.hGlobal = fileGroupDescHandle;
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
} // GetFileDescriptorInternetShortcutA
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
nsDataObj :: GetFileDescriptorInternetShortcutW ( FORMATETC& aFE, STGMEDIUM& aSTG )
|
|
|
|
{
|
|
|
|
// get the title of the shortcut
|
|
|
|
nsAutoString title;
|
|
|
|
if ( NS_FAILED(ExtractShortcutTitle(title)) )
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
HGLOBAL fileGroupDescHandle = ::GlobalAlloc(GMEM_ZEROINIT|GMEM_SHARE,sizeof(FILEGROUPDESCRIPTORW));
|
|
|
|
if (!fileGroupDescHandle)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
LPFILEGROUPDESCRIPTORW fileGroupDescW = reinterpret_cast<LPFILEGROUPDESCRIPTORW>(::GlobalLock(fileGroupDescHandle));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fileGroupDescW) {
|
|
|
|
::GlobalFree(fileGroupDescHandle);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a valid filename in the following order: 1) from the page title,
|
|
|
|
// 2) localized string for an untitled page, 3) just use "Untitled.URL"
|
|
|
|
if (!CreateFilenameFromTextW(title, NS_LITERAL_STRING(".URL").get(),
|
|
|
|
fileGroupDescW->fgd[0].cFileName, NS_MAX_FILEDESCRIPTOR)) {
|
|
|
|
nsXPIDLString untitled;
|
|
|
|
if (!GetLocalizedString(NS_LITERAL_STRING("noPageTitle").get(), untitled) ||
|
|
|
|
!CreateFilenameFromTextW(untitled, NS_LITERAL_STRING(".URL").get(),
|
|
|
|
fileGroupDescW->fgd[0].cFileName, NS_MAX_FILEDESCRIPTOR)) {
|
|
|
|
wcscpy(fileGroupDescW->fgd[0].cFileName, NS_LITERAL_STRING("Untitled.URL").get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// one file in the file block
|
|
|
|
fileGroupDescW->cItems = 1;
|
|
|
|
fileGroupDescW->fgd[0].dwFlags = FD_LINKUI;
|
|
|
|
|
|
|
|
::GlobalUnlock( fileGroupDescHandle );
|
|
|
|
aSTG.hGlobal = fileGroupDescHandle;
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
} // GetFileDescriptorInternetShortcutW
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetFileContentsInternetShortcut
|
|
|
|
//
|
|
|
|
// Create the special format for an internet shortcut and build up the data
|
|
|
|
// structures the shell is expecting.
|
|
|
|
//
|
|
|
|
HRESULT
|
|
|
|
nsDataObj :: GetFileContentsInternetShortcut ( FORMATETC& aFE, STGMEDIUM& aSTG )
|
|
|
|
{
|
|
|
|
nsAutoString url;
|
|
|
|
if ( NS_FAILED(ExtractShortcutURL(url)) )
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
// will need to change if we ever support iDNS
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString asciiUrl;
|
2007-03-22 10:30:00 -07:00
|
|
|
LossyCopyUTF16toASCII(url, asciiUrl);
|
2012-07-21 13:07:26 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> icoFile;
|
|
|
|
nsCOMPtr<nsIURI> aUri;
|
|
|
|
NS_NewURI(getter_AddRefs(aUri), url);
|
|
|
|
|
|
|
|
nsAutoString aUriHash;
|
|
|
|
|
|
|
|
mozilla::widget::FaviconHelper::ObtainCachedIconFile(aUri, aUriHash, mIOThread, true);
|
|
|
|
|
|
|
|
nsresult rv = mozilla::widget::FaviconHelper::GetOutputIconPath(aUri, icoFile, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCString path;
|
|
|
|
rv = icoFile->GetNativePath(path);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
static char* shortcutFormatStr = "[InternetShortcut]\r\nURL=%s\r\n"
|
|
|
|
"IDList=\r\nHotKey=0\r\nIconFile=%s\r\n"
|
|
|
|
"IconIndex=0\r\n";
|
|
|
|
static const int formatLen = strlen(shortcutFormatStr) - 2*2; // don't include %s (2 times) in the len
|
|
|
|
const int totalLen = formatLen + asciiUrl.Length()
|
|
|
|
+ path.Length(); // we don't want a null character on the end
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// create a global memory area and build up the file contents w/in it
|
|
|
|
HGLOBAL hGlobalMemory = ::GlobalAlloc(GMEM_SHARE, totalLen);
|
|
|
|
if ( !hGlobalMemory )
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
char* contents = reinterpret_cast<char*>(::GlobalLock(hGlobalMemory));
|
2007-03-22 10:30:00 -07:00
|
|
|
if ( !contents ) {
|
|
|
|
::GlobalFree( hGlobalMemory );
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
//NOTE: we intentionally use the Microsoft version of snprintf here because it does NOT null
|
|
|
|
// terminate strings which reach the maximum size of the buffer. Since we know that the
|
|
|
|
// formatted length here is totalLen, this call to _snprintf will format the string into
|
|
|
|
// the buffer without appending the null character.
|
2012-07-21 13:07:26 -07:00
|
|
|
_snprintf( contents, totalLen, shortcutFormatStr, asciiUrl.get(), path.get() );
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
::GlobalUnlock(hGlobalMemory);
|
|
|
|
aSTG.hGlobal = hGlobalMemory;
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
} // GetFileContentsInternetShortcut
|
|
|
|
|
|
|
|
// check if specified flavour is present in the transferable
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsDataObj :: IsFlavourPresent(const char *inFlavour)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool retval = false;
|
2011-10-01 19:16:19 -07:00
|
|
|
NS_ENSURE_TRUE(mTransferable, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// get the list of flavors available in the transferable
|
|
|
|
nsCOMPtr<nsISupportsArray> flavorList;
|
|
|
|
mTransferable->FlavorsTransferableCanExport(getter_AddRefs(flavorList));
|
2011-10-01 19:16:19 -07:00
|
|
|
NS_ENSURE_TRUE(flavorList, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// try to find requested flavour
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t cnt;
|
2007-03-22 10:30:00 -07:00
|
|
|
flavorList->Count(&cnt);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < cnt; ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
flavorList->GetElementAt (i, getter_AddRefs(genericFlavor));
|
|
|
|
nsCOMPtr<nsISupportsCString> currentFlavor (do_QueryInterface(genericFlavor));
|
|
|
|
if (currentFlavor) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString flavorStr;
|
2007-03-22 10:30:00 -07:00
|
|
|
currentFlavor->GetData(flavorStr);
|
|
|
|
if (flavorStr.Equals(inFlavour)) {
|
2011-10-01 19:16:19 -07:00
|
|
|
retval = true; // found it!
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // for each flavor
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT nsDataObj::GetPreferredDropEffect ( FORMATETC& aFE, STGMEDIUM& aSTG )
|
|
|
|
{
|
|
|
|
HRESULT res = S_OK;
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
aSTG.pUnkForRelease = NULL;
|
|
|
|
HGLOBAL hGlobalMemory = NULL;
|
|
|
|
hGlobalMemory = ::GlobalAlloc(GMEM_MOVEABLE, sizeof(DWORD));
|
|
|
|
if (hGlobalMemory) {
|
|
|
|
DWORD* pdw = (DWORD*) GlobalLock(hGlobalMemory);
|
|
|
|
// The PreferredDropEffect clipboard format is only registered if a drag/drop
|
|
|
|
// of an image happens from Mozilla to the desktop. We want its value
|
|
|
|
// to be DROPEFFECT_MOVE in that case so that the file is moved from the
|
|
|
|
// temporary location, not copied.
|
|
|
|
// This value should, ideally, be set on the data object via SetData() but
|
|
|
|
// our IDataObject implementation doesn't implement SetData. It adds data
|
|
|
|
// to the data object lazily only when the drop target asks for it.
|
|
|
|
*pdw = (DWORD) DROPEFFECT_MOVE;
|
|
|
|
GlobalUnlock(hGlobalMemory);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res = E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
aSTG.hGlobal = hGlobalMemory;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
HRESULT nsDataObj::GetText(const nsACString & aDataFlavor, FORMATETC& aFE, STGMEDIUM& aSTG)
|
|
|
|
{
|
|
|
|
void* data = NULL;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if someone asks for text/plain, look up text/unicode instead in the transferable.
|
|
|
|
const char* flavorStr;
|
|
|
|
const nsPromiseFlatCString& flat = PromiseFlatCString(aDataFlavor);
|
|
|
|
if ( aDataFlavor.Equals("text/plain") )
|
|
|
|
flavorStr = kUnicodeMime;
|
|
|
|
else
|
|
|
|
flavorStr = flat.get();
|
|
|
|
|
|
|
|
// NOTE: CreateDataFromPrimitive creates new memory, that needs to be deleted
|
|
|
|
nsCOMPtr<nsISupports> genericDataWrapper;
|
|
|
|
mTransferable->GetTransferData(flavorStr, getter_AddRefs(genericDataWrapper), &len);
|
|
|
|
if ( !len )
|
2010-02-20 05:45:19 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPrimitiveHelpers::CreateDataFromPrimitive ( flavorStr, genericDataWrapper, &data, len );
|
|
|
|
if ( !data )
|
2010-02-20 05:45:19 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
HGLOBAL hGlobalMemory = NULL;
|
|
|
|
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
aSTG.pUnkForRelease = NULL;
|
|
|
|
|
|
|
|
// We play games under the hood and advertise flavors that we know we
|
|
|
|
// can support, only they require a bit of conversion or munging of the data.
|
|
|
|
// Do that here.
|
|
|
|
//
|
|
|
|
// The transferable gives us data that is null-terminated, but this isn't reflected in
|
|
|
|
// the |len| parameter. Windoze apps expect this null to be there so bump our data buffer
|
|
|
|
// by the appropriate size to account for the null (one char for CF_TEXT, one PRUnichar for
|
|
|
|
// CF_UNICODETEXT).
|
|
|
|
DWORD allocLen = (DWORD)len;
|
|
|
|
if ( aFE.cfFormat == CF_TEXT ) {
|
|
|
|
// Someone is asking for text/plain; convert the unicode (assuming it's present)
|
|
|
|
// to text with the correct platform encoding.
|
2012-07-30 07:20:58 -07:00
|
|
|
char* plainTextData = nullptr;
|
2007-07-08 00:08:04 -07:00
|
|
|
PRUnichar* castedUnicode = reinterpret_cast<PRUnichar*>(data);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t plainTextLen = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPrimitiveHelpers::ConvertUnicodeToPlatformPlainText ( castedUnicode, len / 2, &plainTextData, &plainTextLen );
|
|
|
|
|
|
|
|
// replace the unicode data with our plaintext data. Recall that |plainTextLen| doesn't include
|
|
|
|
// the null in the length.
|
|
|
|
nsMemory::Free(data);
|
|
|
|
if ( plainTextData ) {
|
|
|
|
data = plainTextData;
|
|
|
|
allocLen = plainTextLen + sizeof(char);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING ( "Oh no, couldn't convert unicode to plain text" );
|
2010-02-20 05:45:19 -08:00
|
|
|
return S_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( aFE.cfFormat == nsClipboard::CF_HTML ) {
|
|
|
|
// Someone is asking for win32's HTML flavor. Convert our html fragment
|
|
|
|
// from unicode to UTF-8 then put it into a format specified by msft.
|
2007-07-08 00:08:04 -07:00
|
|
|
NS_ConvertUTF16toUTF8 converter ( reinterpret_cast<PRUnichar*>(data) );
|
2012-07-30 07:20:58 -07:00
|
|
|
char* utf8HTML = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = BuildPlatformHTML ( converter.get(), &utf8HTML ); // null terminates
|
|
|
|
|
|
|
|
nsMemory::Free(data);
|
|
|
|
if ( NS_SUCCEEDED(rv) && utf8HTML ) {
|
|
|
|
// replace the unicode data with our HTML data. Don't forget the null.
|
|
|
|
data = utf8HTML;
|
|
|
|
allocLen = strlen(utf8HTML) + sizeof(char);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING ( "Oh no, couldn't convert to HTML" );
|
2010-02-20 05:45:19 -08:00
|
|
|
return S_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// we assume that any data that isn't caught above is unicode. This may
|
|
|
|
// be an erroneous assumption, but is true so far.
|
|
|
|
allocLen += sizeof(PRUnichar);
|
|
|
|
}
|
|
|
|
|
|
|
|
hGlobalMemory = (HGLOBAL)GlobalAlloc(GMEM_MOVEABLE, allocLen);
|
|
|
|
|
|
|
|
// Copy text to Global Memory Area
|
|
|
|
if ( hGlobalMemory ) {
|
2007-07-08 00:08:04 -07:00
|
|
|
char* dest = reinterpret_cast<char*>(GlobalLock(hGlobalMemory));
|
|
|
|
char* source = reinterpret_cast<char*>(data);
|
2007-03-22 10:30:00 -07:00
|
|
|
memcpy ( dest, source, allocLen ); // copies the null as well
|
|
|
|
GlobalUnlock(hGlobalMemory);
|
|
|
|
}
|
|
|
|
aSTG.hGlobal = hGlobalMemory;
|
|
|
|
|
|
|
|
// Now, delete the memory that was created by CreateDataFromPrimitive (or our text/plain data)
|
|
|
|
nsMemory::Free(data);
|
|
|
|
|
2010-02-20 05:45:19 -08:00
|
|
|
return S_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-11-14 14:23:02 -08:00
|
|
|
//-----------------------------------------------------
|
|
|
|
HRESULT nsDataObj::GetFile(FORMATETC& aFE, STGMEDIUM& aSTG)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t dfInx = 0;
|
2008-11-14 14:23:02 -08:00
|
|
|
ULONG count;
|
|
|
|
FORMATETC fe;
|
|
|
|
m_enumFE->Reset();
|
2009-03-10 19:26:03 -07:00
|
|
|
while (NOERROR == m_enumFE->Next(1, &fe, &count)
|
2009-12-20 09:03:18 -08:00
|
|
|
&& dfInx < mDataFlavors.Length()) {
|
|
|
|
if (mDataFlavors[dfInx].EqualsLiteral(kNativeImageMime))
|
|
|
|
return DropImage(aFE, aSTG);
|
|
|
|
if (mDataFlavors[dfInx].EqualsLiteral(kFileMime))
|
|
|
|
return DropFile(aFE, aSTG);
|
|
|
|
if (mDataFlavors[dfInx].EqualsLiteral(kFilePromiseMime))
|
|
|
|
return DropTempFile(aFE, aSTG);
|
|
|
|
dfInx++;
|
2008-11-14 14:23:02 -08:00
|
|
|
}
|
2009-12-20 09:03:18 -08:00
|
|
|
return E_FAIL;
|
2009-12-09 21:15:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT nsDataObj::DropFile(FORMATETC& aFE, STGMEDIUM& aSTG)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = 0;
|
2009-12-09 21:15:56 -08:00
|
|
|
nsCOMPtr<nsISupports> genericDataWrapper;
|
|
|
|
|
|
|
|
mTransferable->GetTransferData(kFileMime, getter_AddRefs(genericDataWrapper),
|
|
|
|
&len);
|
|
|
|
nsCOMPtr<nsIFile> file ( do_QueryInterface(genericDataWrapper) );
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupportsInterfacePointer> ptr(do_QueryInterface(genericDataWrapper));
|
|
|
|
if (ptr)
|
|
|
|
ptr->GetData(getter_AddRefs(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
aSTG.pUnkForRelease = NULL;
|
|
|
|
|
|
|
|
nsAutoString path;
|
|
|
|
rv = file->GetPath(path);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return E_FAIL;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t allocLen = path.Length() + 2;
|
2009-12-09 21:15:56 -08:00
|
|
|
HGLOBAL hGlobalMemory = NULL;
|
2009-12-21 12:48:12 -08:00
|
|
|
PRUnichar *dest;
|
2009-12-09 21:15:56 -08:00
|
|
|
|
|
|
|
hGlobalMemory = GlobalAlloc(GMEM_MOVEABLE, sizeof(DROPFILES) +
|
|
|
|
allocLen * sizeof(PRUnichar));
|
|
|
|
if (!hGlobalMemory)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
DROPFILES* pDropFile = (DROPFILES*)GlobalLock(hGlobalMemory);
|
|
|
|
|
|
|
|
// First, populate the drop file structure
|
|
|
|
pDropFile->pFiles = sizeof(DROPFILES); //Offset to start of file name string
|
|
|
|
pDropFile->fNC = 0;
|
|
|
|
pDropFile->pt.x = 0;
|
|
|
|
pDropFile->pt.y = 0;
|
|
|
|
pDropFile->fWide = TRUE;
|
|
|
|
|
|
|
|
// Copy the filename right after the DROPFILES structure
|
|
|
|
dest = (PRUnichar*)(((char*)pDropFile) + pDropFile->pFiles);
|
|
|
|
memcpy(dest, path.get(), (allocLen - 1) * sizeof(PRUnichar));
|
|
|
|
|
|
|
|
// Two null characters are needed at the end of the file name.
|
|
|
|
// Lookup the CF_HDROP shell clipboard format for more info.
|
|
|
|
// Add the second null character right after the first one.
|
|
|
|
dest[allocLen - 1] = L'\0';
|
|
|
|
|
|
|
|
GlobalUnlock(hGlobalMemory);
|
|
|
|
|
|
|
|
aSTG.hGlobal = hGlobalMemory;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT nsDataObj::DropImage(FORMATETC& aFE, STGMEDIUM& aSTG)
|
|
|
|
{
|
2008-11-14 14:23:02 -08:00
|
|
|
nsresult rv;
|
2009-12-17 20:06:19 -08:00
|
|
|
if (!mCachedTempFile) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = 0;
|
2009-12-17 20:06:19 -08:00
|
|
|
nsCOMPtr<nsISupports> genericDataWrapper;
|
|
|
|
|
|
|
|
mTransferable->GetTransferData(kNativeImageMime, getter_AddRefs(genericDataWrapper), &len);
|
|
|
|
nsCOMPtr<imgIContainer> image(do_QueryInterface(genericDataWrapper));
|
|
|
|
|
|
|
|
if (!image) {
|
|
|
|
// Check if the image was put in an nsISupportsInterfacePointer wrapper.
|
|
|
|
// This might not be necessary any more, but could be useful for backwards
|
|
|
|
// compatibility.
|
|
|
|
nsCOMPtr<nsISupportsInterfacePointer> ptr(do_QueryInterface(genericDataWrapper));
|
|
|
|
if (ptr)
|
|
|
|
ptr->GetData(getter_AddRefs(image));
|
|
|
|
}
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
if (!image)
|
|
|
|
return E_FAIL;
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
// Use the clipboard helper class to build up a memory bitmap.
|
|
|
|
nsImageToClipboard converter(image);
|
2012-07-30 07:20:58 -07:00
|
|
|
HANDLE bits = nullptr;
|
2009-12-17 20:06:19 -08:00
|
|
|
rv = converter.GetPicture(&bits); // Clipboard routines return a global handle we own.
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
if (NS_FAILED(rv) || !bits)
|
|
|
|
return E_FAIL;
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
// We now own these bits!
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bitmapSize = GlobalSize(bits);
|
2009-12-17 20:06:19 -08:00
|
|
|
if (!bitmapSize) {
|
|
|
|
GlobalFree(bits);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
// Save the bitmap to a temporary location.
|
|
|
|
nsCOMPtr<nsIFile> dropFile;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(dropFile));
|
|
|
|
if (!dropFile) {
|
|
|
|
GlobalFree(bits);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
// Filename must be random so as not to confuse apps like
|
|
|
|
// Photoshop which handle multiple drags into a single window.
|
|
|
|
char buf[13];
|
|
|
|
nsCString filename;
|
2012-04-05 09:50:55 -07:00
|
|
|
NS_MakeRandomString(buf, 8);
|
2009-12-17 20:06:19 -08:00
|
|
|
memcpy(buf+8, ".bmp", 5);
|
|
|
|
filename.Append(nsDependentCString(buf, 12));
|
|
|
|
dropFile->AppendNative(filename);
|
|
|
|
rv = dropFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0660);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
GlobalFree(bits);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
// Cache the temp file so we can delete it later and so
|
|
|
|
// it doesn't get recreated over and over on multiple calls
|
|
|
|
// which does occur from windows shell.
|
|
|
|
dropFile->Clone(getter_AddRefs(mCachedTempFile));
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
// Write the data to disk.
|
|
|
|
nsCOMPtr<nsIOutputStream> outStream;
|
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(outStream), dropFile);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
GlobalFree(bits);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
char * bm = (char *)GlobalLock(bits);
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
BITMAPFILEHEADER fileHdr;
|
|
|
|
BITMAPINFOHEADER *bmpHdr = (BITMAPINFOHEADER*)bm;
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
fileHdr.bfType = ((WORD) ('M' << 8) | 'B');
|
|
|
|
fileHdr.bfSize = GlobalSize (bits) + sizeof(fileHdr);
|
|
|
|
fileHdr.bfReserved1 = 0;
|
|
|
|
fileHdr.bfReserved2 = 0;
|
|
|
|
fileHdr.bfOffBits = (DWORD) (sizeof(fileHdr) + bmpHdr->biSize);
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t writeCount = 0;
|
2009-12-17 20:06:19 -08:00
|
|
|
if (NS_FAILED(outStream->Write((const char *)&fileHdr, sizeof(fileHdr), &writeCount)) ||
|
|
|
|
NS_FAILED(outStream->Write((const char *)bm, bitmapSize, &writeCount)))
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
outStream->Close();
|
2008-11-14 14:23:02 -08:00
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
GlobalUnlock(bits);
|
2008-11-14 14:23:02 -08:00
|
|
|
GlobalFree(bits);
|
|
|
|
|
2009-12-17 20:06:19 -08:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2008-11-14 14:23:02 -08:00
|
|
|
|
|
|
|
// Pass the file name back to the drop target so that it can access the file.
|
|
|
|
nsAutoString path;
|
|
|
|
rv = mCachedTempFile->GetPath(path);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
// Two null characters are needed to terminate the file name list.
|
|
|
|
HGLOBAL hGlobalMemory = NULL;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t allocLen = path.Length() + 2;
|
2008-11-14 14:23:02 -08:00
|
|
|
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
aSTG.pUnkForRelease = NULL;
|
|
|
|
|
|
|
|
hGlobalMemory = GlobalAlloc(GMEM_MOVEABLE, sizeof(DROPFILES) + allocLen * sizeof(PRUnichar));
|
|
|
|
if (!hGlobalMemory)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
DROPFILES* pDropFile = (DROPFILES*)GlobalLock(hGlobalMemory);
|
|
|
|
|
|
|
|
// First, populate the drop file structure.
|
|
|
|
pDropFile->pFiles = sizeof(DROPFILES); // Offset to start of file name char array.
|
|
|
|
pDropFile->fNC = 0;
|
|
|
|
pDropFile->pt.x = 0;
|
|
|
|
pDropFile->pt.y = 0;
|
|
|
|
pDropFile->fWide = TRUE;
|
|
|
|
|
|
|
|
// Copy the filename right after the DROPFILES structure.
|
|
|
|
PRUnichar* dest = (PRUnichar*)(((char*)pDropFile) + pDropFile->pFiles);
|
|
|
|
memcpy(dest, path.get(), (allocLen - 1) * sizeof(PRUnichar)); // Copies the null character in path as well.
|
|
|
|
|
|
|
|
// Two null characters are needed at the end of the file name.
|
|
|
|
// Lookup the CF_HDROP shell clipboard format for more info.
|
2009-12-20 09:03:18 -08:00
|
|
|
// Add the second null character right after the first one.
|
|
|
|
dest[allocLen - 1] = L'\0';
|
|
|
|
|
|
|
|
GlobalUnlock(hGlobalMemory);
|
|
|
|
|
|
|
|
aSTG.hGlobal = hGlobalMemory;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT nsDataObj::DropTempFile(FORMATETC& aFE, STGMEDIUM& aSTG)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (!mCachedTempFile) {
|
|
|
|
// Tempfile will need a temporary location.
|
|
|
|
nsCOMPtr<nsIFile> dropFile;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(dropFile));
|
|
|
|
if (!dropFile)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
// Filename must be random
|
|
|
|
nsCString filename;
|
|
|
|
nsAutoString wideFileName;
|
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
|
|
|
rv = GetDownloadDetails(getter_AddRefs(sourceURI),
|
|
|
|
wideFileName);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return E_FAIL;
|
|
|
|
NS_UTF16ToCString(wideFileName, NS_CSTRING_ENCODING_NATIVE_FILESYSTEM, filename);
|
|
|
|
|
|
|
|
dropFile->AppendNative(filename);
|
|
|
|
rv = dropFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0660);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
// Cache the temp file so we can delete it later and so
|
|
|
|
// it doesn't get recreated over and over on multiple calls
|
|
|
|
// which does occur from windows shell.
|
|
|
|
dropFile->Clone(getter_AddRefs(mCachedTempFile));
|
|
|
|
|
|
|
|
// Write the data to disk.
|
|
|
|
nsCOMPtr<nsIOutputStream> outStream;
|
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(outStream), dropFile);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
IStream *pStream = NULL;
|
|
|
|
nsDataObj::CreateStream(&pStream);
|
|
|
|
NS_ENSURE_TRUE(pStream, E_FAIL);
|
|
|
|
|
|
|
|
char buffer[512];
|
|
|
|
ULONG readCount = 0;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t writeCount = 0;
|
2009-12-20 09:03:18 -08:00
|
|
|
while (1) {
|
2012-08-16 04:58:11 -07:00
|
|
|
HRESULT hres = pStream->Read(buffer, sizeof(buffer), &readCount);
|
|
|
|
if (FAILED(hres))
|
2009-12-20 09:03:18 -08:00
|
|
|
return E_FAIL;
|
|
|
|
if (readCount == 0)
|
|
|
|
break;
|
|
|
|
rv = outStream->Write(buffer, readCount, &writeCount);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
outStream->Close();
|
|
|
|
pStream->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the file name back to the drop target so that it can access the file.
|
|
|
|
nsAutoString path;
|
|
|
|
rv = mCachedTempFile->GetPath(path);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return E_FAIL;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t allocLen = path.Length() + 2;
|
2009-12-20 09:03:18 -08:00
|
|
|
|
|
|
|
// Two null characters are needed to terminate the file name list.
|
|
|
|
HGLOBAL hGlobalMemory = NULL;
|
|
|
|
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
aSTG.pUnkForRelease = NULL;
|
|
|
|
|
|
|
|
hGlobalMemory = GlobalAlloc(GMEM_MOVEABLE, sizeof(DROPFILES) + allocLen * sizeof(PRUnichar));
|
|
|
|
if (!hGlobalMemory)
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
DROPFILES* pDropFile = (DROPFILES*)GlobalLock(hGlobalMemory);
|
|
|
|
|
|
|
|
// First, populate the drop file structure.
|
|
|
|
pDropFile->pFiles = sizeof(DROPFILES); // Offset to start of file name char array.
|
|
|
|
pDropFile->fNC = 0;
|
|
|
|
pDropFile->pt.x = 0;
|
|
|
|
pDropFile->pt.y = 0;
|
|
|
|
pDropFile->fWide = TRUE;
|
|
|
|
|
|
|
|
// Copy the filename right after the DROPFILES structure.
|
|
|
|
PRUnichar* dest = (PRUnichar*)(((char*)pDropFile) + pDropFile->pFiles);
|
|
|
|
memcpy(dest, path.get(), (allocLen - 1) * sizeof(PRUnichar)); // Copies the null character in path as well.
|
|
|
|
|
|
|
|
// Two null characters are needed at the end of the file name.
|
|
|
|
// Lookup the CF_HDROP shell clipboard format for more info.
|
2008-11-14 14:23:02 -08:00
|
|
|
// Add the second null character right after the first one.
|
|
|
|
dest[allocLen - 1] = L'\0';
|
|
|
|
|
|
|
|
GlobalUnlock(hGlobalMemory);
|
|
|
|
|
|
|
|
aSTG.hGlobal = hGlobalMemory;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------
|
|
|
|
HRESULT nsDataObj::GetMetafilePict(FORMATETC&, STGMEDIUM&)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_NOTIMPL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
HRESULT nsDataObj::SetBitmap(FORMATETC&, STGMEDIUM&)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_NOTIMPL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
2007-11-16 20:31:32 -08:00
|
|
|
HRESULT nsDataObj::SetDib(FORMATETC&, STGMEDIUM&)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
HRESULT nsDataObj::SetText (FORMATETC& aFE, STGMEDIUM& aSTG)
|
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
2007-11-16 20:31:32 -08:00
|
|
|
HRESULT nsDataObj::SetMetafilePict(FORMATETC&, STGMEDIUM&)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-12-21 12:48:12 -08:00
|
|
|
return E_FAIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
//-----------------------------------------------------
|
|
|
|
CLSID nsDataObj::GetClassID() const
|
|
|
|
{
|
|
|
|
return CLSID_nsDataObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
2008-09-06 09:24:45 -07:00
|
|
|
// Registers the DataFlavor/FE pair.
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------
|
|
|
|
void nsDataObj::AddDataFlavor(const char* aDataFlavor, LPFORMATETC aFE)
|
|
|
|
{
|
2008-09-06 09:24:45 -07:00
|
|
|
// These two lists are the mapping to and from data flavors and FEs.
|
2008-11-14 14:23:02 -08:00
|
|
|
// Later, OLE will tell us it needs a certain type of FORMATETC (text, unicode, etc)
|
2008-09-06 09:24:45 -07:00
|
|
|
// unicode, etc), so we will look up the data flavor that corresponds to
|
|
|
|
// the FE and then ask the transferable for that type of data.
|
2009-03-10 19:26:03 -07:00
|
|
|
mDataFlavors.AppendElement(aDataFlavor);
|
2009-12-20 07:56:06 -08:00
|
|
|
m_enumFE->AddFormatEtc(aFE);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// Sets the transferable object
|
|
|
|
//-----------------------------------------------------
|
|
|
|
void nsDataObj::SetTransferable(nsITransferable * aTransferable)
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mTransferable);
|
|
|
|
|
|
|
|
mTransferable = aTransferable;
|
2012-07-30 07:20:58 -07:00
|
|
|
if (nullptr == mTransferable) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(mTransferable);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// ExtractURL
|
|
|
|
//
|
|
|
|
// Roots around in the transferable for the appropriate flavor that indicates
|
|
|
|
// a url and pulls out the url portion of the data. Used mostly for creating
|
|
|
|
// internet shortcuts on the desktop. The url flavor is of the format:
|
|
|
|
//
|
|
|
|
// <url> <linefeed> <page title>
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsDataObj :: ExtractShortcutURL ( nsString & outURL )
|
|
|
|
{
|
|
|
|
NS_ASSERTION ( mTransferable, "We don't have a good transferable" );
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> genericURL;
|
|
|
|
if ( NS_SUCCEEDED(mTransferable->GetTransferData(kURLMime, getter_AddRefs(genericURL), &len)) ) {
|
|
|
|
nsCOMPtr<nsISupportsString> urlObject ( do_QueryInterface(genericURL) );
|
|
|
|
if ( urlObject ) {
|
|
|
|
nsAutoString url;
|
|
|
|
urlObject->GetData ( url );
|
|
|
|
outURL = url;
|
|
|
|
|
|
|
|
// find the first linefeed in the data, that's where the url ends. trunc the
|
|
|
|
// result string at that point.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t lineIndex = outURL.FindChar ( '\n' );
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION ( lineIndex > 0, "Format for url flavor is <url> <linefeed> <page title>" );
|
|
|
|
if ( lineIndex > 0 ) {
|
|
|
|
outURL.Truncate ( lineIndex );
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ( NS_SUCCEEDED(mTransferable->GetTransferData(kURLDataMime, getter_AddRefs(genericURL), &len)) ||
|
|
|
|
NS_SUCCEEDED(mTransferable->GetTransferData(kURLPrivateMime, getter_AddRefs(genericURL), &len)) ) {
|
|
|
|
nsCOMPtr<nsISupportsString> urlObject ( do_QueryInterface(genericURL) );
|
|
|
|
if ( urlObject ) {
|
|
|
|
nsAutoString url;
|
|
|
|
urlObject->GetData ( url );
|
|
|
|
outURL = url;
|
|
|
|
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // if found flavor
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
} // ExtractShortcutURL
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// ExtractShortcutTitle
|
|
|
|
//
|
|
|
|
// Roots around in the transferable for the appropriate flavor that indicates
|
|
|
|
// a url and pulls out the title portion of the data. Used mostly for creating
|
|
|
|
// internet shortcuts on the desktop. The url flavor is of the format:
|
|
|
|
//
|
|
|
|
// <url> <linefeed> <page title>
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsDataObj :: ExtractShortcutTitle ( nsString & outTitle )
|
|
|
|
{
|
|
|
|
NS_ASSERTION ( mTransferable, "We'd don't have a good transferable" );
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> genericURL;
|
|
|
|
if ( NS_SUCCEEDED(mTransferable->GetTransferData(kURLMime, getter_AddRefs(genericURL), &len)) ) {
|
|
|
|
nsCOMPtr<nsISupportsString> urlObject ( do_QueryInterface(genericURL) );
|
|
|
|
if ( urlObject ) {
|
|
|
|
nsAutoString url;
|
|
|
|
urlObject->GetData ( url );
|
|
|
|
|
|
|
|
// find the first linefeed in the data, that's where the url ends. we want
|
|
|
|
// everything after that linefeed. FindChar() returns -1 if we can't find
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t lineIndex = url.FindChar ( '\n' );
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION ( lineIndex != -1, "Format for url flavor is <url> <linefeed> <page title>" );
|
|
|
|
if ( lineIndex != -1 ) {
|
|
|
|
url.Mid ( outTitle, lineIndex + 1, (len/2) - (lineIndex + 1) );
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // if found flavor
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
} // ExtractShortcutTitle
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// BuildPlatformHTML
|
|
|
|
//
|
|
|
|
// Munge our HTML data to win32's CF_HTML spec. Basically, put the requisite
|
|
|
|
// header information on it. This will null terminate |outPlatformHTML|. See
|
|
|
|
// http://msdn.microsoft.com/workshop/networking/clipboard/htmlclipboard.asp
|
|
|
|
// for details.
|
|
|
|
//
|
|
|
|
// We assume that |inOurHTML| is already a fragment (ie, doesn't have <HTML>
|
|
|
|
// or <BODY> tags). We'll wrap the fragment with them to make other apps
|
|
|
|
// happy.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsDataObj :: BuildPlatformHTML ( const char* inOurHTML, char** outPlatformHTML )
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*outPlatformHTML = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsDependentCString inHTMLString(inOurHTML);
|
|
|
|
const char* const numPlaceholder = "00000000";
|
|
|
|
const char* const startHTMLPrefix = "Version:0.9\r\nStartHTML:";
|
|
|
|
const char* const endHTMLPrefix = "\r\nEndHTML:";
|
|
|
|
const char* const startFragPrefix = "\r\nStartFragment:";
|
|
|
|
const char* const endFragPrefix = "\r\nEndFragment:";
|
|
|
|
const char* const startSourceURLPrefix = "\r\nSourceURL:";
|
|
|
|
const char* const endFragTrailer = "\r\n";
|
|
|
|
|
|
|
|
// Do we already have mSourceURL from a drag?
|
|
|
|
if (mSourceURL.IsEmpty()) {
|
|
|
|
nsAutoString url;
|
|
|
|
ExtractShortcutURL(url);
|
|
|
|
|
|
|
|
AppendUTF16toUTF8(url, mSourceURL);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const int32_t kSourceURLLength = mSourceURL.Length();
|
|
|
|
const int32_t kNumberLength = strlen(numPlaceholder);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const int32_t kTotalHeaderLen = strlen(startHTMLPrefix) +
|
2007-03-22 10:30:00 -07:00
|
|
|
strlen(endHTMLPrefix) +
|
|
|
|
strlen(startFragPrefix) +
|
|
|
|
strlen(endFragPrefix) +
|
|
|
|
strlen(endFragTrailer) +
|
|
|
|
(kSourceURLLength > 0 ? strlen(startSourceURLPrefix) : 0) +
|
|
|
|
kSourceURLLength +
|
|
|
|
(4 * kNumberLength);
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(htmlHeaderString, "<html><body>\r\n");
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(fragmentHeaderString, "<!--StartFragment-->");
|
|
|
|
|
|
|
|
nsDependentCString trailingString(
|
|
|
|
"<!--EndFragment-->\r\n"
|
|
|
|
"</body>\r\n"
|
|
|
|
"</html>");
|
|
|
|
|
|
|
|
// calculate the offsets
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startHTMLOffset = kTotalHeaderLen;
|
|
|
|
int32_t startFragOffset = startHTMLOffset
|
2007-03-22 10:30:00 -07:00
|
|
|
+ htmlHeaderString.Length()
|
|
|
|
+ fragmentHeaderString.Length();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t endFragOffset = startFragOffset
|
2007-03-22 10:30:00 -07:00
|
|
|
+ inHTMLString.Length();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t endHTMLOffset = endFragOffset
|
2007-03-22 10:30:00 -07:00
|
|
|
+ trailingString.Length();
|
|
|
|
|
|
|
|
// now build the final version
|
|
|
|
nsCString clipboardString;
|
|
|
|
clipboardString.SetCapacity(endHTMLOffset);
|
|
|
|
|
|
|
|
clipboardString.Append(startHTMLPrefix);
|
|
|
|
clipboardString.Append(nsPrintfCString("%08u", startHTMLOffset));
|
|
|
|
|
|
|
|
clipboardString.Append(endHTMLPrefix);
|
|
|
|
clipboardString.Append(nsPrintfCString("%08u", endHTMLOffset));
|
|
|
|
|
|
|
|
clipboardString.Append(startFragPrefix);
|
|
|
|
clipboardString.Append(nsPrintfCString("%08u", startFragOffset));
|
|
|
|
|
|
|
|
clipboardString.Append(endFragPrefix);
|
|
|
|
clipboardString.Append(nsPrintfCString("%08u", endFragOffset));
|
|
|
|
|
|
|
|
if (kSourceURLLength > 0) {
|
|
|
|
clipboardString.Append(startSourceURLPrefix);
|
|
|
|
clipboardString.Append(mSourceURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
clipboardString.Append(endFragTrailer);
|
|
|
|
|
|
|
|
clipboardString.Append(htmlHeaderString);
|
|
|
|
clipboardString.Append(fragmentHeaderString);
|
|
|
|
clipboardString.Append(inHTMLString);
|
|
|
|
clipboardString.Append(trailingString);
|
|
|
|
|
|
|
|
*outPlatformHTML = ToNewCString(clipboardString);
|
|
|
|
if (!*outPlatformHTML)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDataObj :: GetUniformResourceLocator( FORMATETC& aFE, STGMEDIUM& aSTG, bool aIsUnicode )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
HRESULT res = S_OK;
|
|
|
|
if (IsFlavourPresent(kURLMime)) {
|
|
|
|
if ( aIsUnicode )
|
|
|
|
res = ExtractUniformResourceLocatorW( aFE, aSTG );
|
|
|
|
else
|
|
|
|
res = ExtractUniformResourceLocatorA( aFE, aSTG );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NS_WARNING ("Not yet implemented\n");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
nsDataObj::ExtractUniformResourceLocatorA(FORMATETC& aFE, STGMEDIUM& aSTG )
|
|
|
|
{
|
|
|
|
HRESULT result = S_OK;
|
|
|
|
|
|
|
|
nsAutoString url;
|
|
|
|
if (NS_FAILED(ExtractShortcutURL(url)))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
NS_LossyConvertUTF16toASCII asciiUrl(url);
|
|
|
|
const int totalLen = asciiUrl.Length() + 1;
|
|
|
|
HGLOBAL hGlobalMemory = GlobalAlloc(GMEM_ZEROINIT|GMEM_SHARE, totalLen);
|
|
|
|
if (!hGlobalMemory)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
char* contents = reinterpret_cast<char*>(GlobalLock(hGlobalMemory));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!contents) {
|
|
|
|
GlobalFree(hGlobalMemory);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(contents, asciiUrl.get());
|
|
|
|
GlobalUnlock(hGlobalMemory);
|
|
|
|
aSTG.hGlobal = hGlobalMemory;
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
nsDataObj::ExtractUniformResourceLocatorW(FORMATETC& aFE, STGMEDIUM& aSTG )
|
|
|
|
{
|
|
|
|
HRESULT result = S_OK;
|
|
|
|
|
|
|
|
nsAutoString url;
|
|
|
|
if (NS_FAILED(ExtractShortcutURL(url)))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
const int totalLen = (url.Length() + 1) * sizeof(PRUnichar);
|
|
|
|
HGLOBAL hGlobalMemory = GlobalAlloc(GMEM_ZEROINIT|GMEM_SHARE, totalLen);
|
|
|
|
if (!hGlobalMemory)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
wchar_t* contents = reinterpret_cast<wchar_t*>(GlobalLock(hGlobalMemory));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!contents) {
|
|
|
|
GlobalFree(hGlobalMemory);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
wcscpy(contents, url.get());
|
|
|
|
GlobalUnlock(hGlobalMemory);
|
|
|
|
aSTG.hGlobal = hGlobalMemory;
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Gets the filename from the kFilePromiseURLMime flavour
|
|
|
|
nsresult nsDataObj::GetDownloadDetails(nsIURI **aSourceURI,
|
|
|
|
nsAString &aFilename)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aSourceURI = nullptr;
|
2008-04-03 04:33:06 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(mTransferable, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// get the URI from the kFilePromiseURLMime flavor
|
|
|
|
nsCOMPtr<nsISupports> urlPrimitive;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t dataSize = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
mTransferable->GetTransferData(kFilePromiseURLMime, getter_AddRefs(urlPrimitive), &dataSize);
|
|
|
|
nsCOMPtr<nsISupportsString> srcUrlPrimitive = do_QueryInterface(urlPrimitive);
|
|
|
|
NS_ENSURE_TRUE(srcUrlPrimitive, NS_ERROR_FAILURE);
|
|
|
|
|
2008-04-03 04:33:06 -07:00
|
|
|
nsAutoString srcUri;
|
|
|
|
srcUrlPrimitive->GetData(srcUri);
|
|
|
|
if (srcUri.IsEmpty())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
2008-04-03 04:33:06 -07:00
|
|
|
NS_NewURI(getter_AddRefs(sourceURI), srcUri);
|
|
|
|
|
|
|
|
nsAutoString srcFileName;
|
|
|
|
nsCOMPtr<nsISupports> fileNamePrimitive;
|
|
|
|
mTransferable->GetTransferData(kFilePromiseDestFilename, getter_AddRefs(fileNamePrimitive), &dataSize);
|
|
|
|
nsCOMPtr<nsISupportsString> srcFileNamePrimitive = do_QueryInterface(fileNamePrimitive);
|
|
|
|
if (srcFileNamePrimitive) {
|
|
|
|
srcFileNamePrimitive->GetData(srcFileName);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2008-04-03 04:33:06 -07:00
|
|
|
nsCOMPtr<nsIURL> sourceURL = do_QueryInterface(sourceURI);
|
|
|
|
if (!sourceURL)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString urlFileName;
|
2008-04-03 04:33:06 -07:00
|
|
|
sourceURL->GetFileName(urlFileName);
|
|
|
|
NS_UnescapeURL(urlFileName);
|
|
|
|
CopyUTF8toUTF16(urlFileName, srcFileName);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-04-03 04:33:06 -07:00
|
|
|
if (srcFileName.IsEmpty())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// make the name safe for the filesystem
|
2008-04-03 04:33:06 -07:00
|
|
|
MangleTextToValidFilename(srcFileName);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-04-03 04:33:06 -07:00
|
|
|
sourceURI.swap(*aSourceURI);
|
|
|
|
aFilename = srcFileName;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT nsDataObj::GetFileDescriptor_IStreamA(FORMATETC& aFE, STGMEDIUM& aSTG)
|
|
|
|
{
|
|
|
|
HGLOBAL fileGroupDescHandle = ::GlobalAlloc(GMEM_ZEROINIT|GMEM_SHARE,sizeof(FILEGROUPDESCRIPTORW));
|
|
|
|
NS_ENSURE_TRUE(fileGroupDescHandle, E_OUTOFMEMORY);
|
|
|
|
|
2008-02-21 12:57:22 -08:00
|
|
|
LPFILEGROUPDESCRIPTORA fileGroupDescA = reinterpret_cast<LPFILEGROUPDESCRIPTORA>(GlobalLock(fileGroupDescHandle));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fileGroupDescA) {
|
|
|
|
::GlobalFree(fileGroupDescHandle);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString wideFileName;
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
|
|
|
rv = GetDownloadDetails(getter_AddRefs(sourceURI),
|
|
|
|
wideFileName);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
::GlobalFree(fileGroupDescHandle);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString nativeFileName;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_UTF16ToCString(wideFileName, NS_CSTRING_ENCODING_NATIVE_FILESYSTEM, nativeFileName);
|
|
|
|
|
|
|
|
strncpy(fileGroupDescA->fgd[0].cFileName, nativeFileName.get(), NS_MAX_FILEDESCRIPTOR - 1);
|
|
|
|
fileGroupDescA->fgd[0].cFileName[NS_MAX_FILEDESCRIPTOR - 1] = '\0';
|
|
|
|
|
|
|
|
// one file in the file block
|
|
|
|
fileGroupDescA->cItems = 1;
|
|
|
|
fileGroupDescA->fgd[0].dwFlags = FD_PROGRESSUI;
|
|
|
|
|
2008-02-21 12:57:22 -08:00
|
|
|
GlobalUnlock( fileGroupDescHandle );
|
2007-03-22 10:30:00 -07:00
|
|
|
aSTG.hGlobal = fileGroupDescHandle;
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT nsDataObj::GetFileDescriptor_IStreamW(FORMATETC& aFE, STGMEDIUM& aSTG)
|
|
|
|
{
|
|
|
|
HGLOBAL fileGroupDescHandle = ::GlobalAlloc(GMEM_ZEROINIT|GMEM_SHARE,sizeof(FILEGROUPDESCRIPTORW));
|
|
|
|
NS_ENSURE_TRUE(fileGroupDescHandle, E_OUTOFMEMORY);
|
|
|
|
|
2008-02-21 12:57:22 -08:00
|
|
|
LPFILEGROUPDESCRIPTORW fileGroupDescW = reinterpret_cast<LPFILEGROUPDESCRIPTORW>(GlobalLock(fileGroupDescHandle));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fileGroupDescW) {
|
|
|
|
::GlobalFree(fileGroupDescHandle);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString wideFileName;
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
|
|
|
rv = GetDownloadDetails(getter_AddRefs(sourceURI),
|
|
|
|
wideFileName);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
::GlobalFree(fileGroupDescHandle);
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wcsncpy(fileGroupDescW->fgd[0].cFileName, wideFileName.get(), NS_MAX_FILEDESCRIPTOR - 1);
|
|
|
|
fileGroupDescW->fgd[0].cFileName[NS_MAX_FILEDESCRIPTOR - 1] = '\0';
|
|
|
|
// one file in the file block
|
|
|
|
fileGroupDescW->cItems = 1;
|
|
|
|
fileGroupDescW->fgd[0].dwFlags = FD_PROGRESSUI;
|
|
|
|
|
2008-02-21 12:57:22 -08:00
|
|
|
GlobalUnlock(fileGroupDescHandle);
|
2007-03-22 10:30:00 -07:00
|
|
|
aSTG.hGlobal = fileGroupDescHandle;
|
|
|
|
aSTG.tymed = TYMED_HGLOBAL;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT nsDataObj::GetFileContents_IStream(FORMATETC& aFE, STGMEDIUM& aSTG)
|
|
|
|
{
|
|
|
|
IStream *pStream = NULL;
|
|
|
|
|
|
|
|
nsDataObj::CreateStream(&pStream);
|
|
|
|
NS_ENSURE_TRUE(pStream, E_FAIL);
|
|
|
|
|
|
|
|
aSTG.tymed = TYMED_ISTREAM;
|
|
|
|
aSTG.pstm = pStream;
|
2009-05-27 08:22:18 -07:00
|
|
|
aSTG.pUnkForRelease = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
2009-12-17 20:06:19 -08:00
|
|
|
|
|
|
|
void nsDataObj::RemoveTempFile(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
nsDataObj *timedDataObj = static_cast<nsDataObj *>(aClosure);
|
|
|
|
if (timedDataObj->mCachedTempFile) {
|
2011-10-01 19:16:19 -07:00
|
|
|
timedDataObj->mCachedTempFile->Remove(false);
|
2009-12-17 20:06:19 -08:00
|
|
|
timedDataObj->mCachedTempFile = NULL;
|
|
|
|
}
|
|
|
|
timedDataObj->Release();
|
|
|
|
}
|