2015-08-28 22:21:08 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "DataTransferItem.h"
|
|
|
|
#include "DataTransferItemList.h"
|
|
|
|
|
|
|
|
#include "mozilla/ContentEvents.h"
|
|
|
|
#include "mozilla/EventForwards.h"
|
|
|
|
#include "mozilla/dom/DataTransferItemBinding.h"
|
|
|
|
#include "nsIClipboard.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsQueryObject.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsVariant.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct FileMimeNameData
|
|
|
|
{
|
|
|
|
const char* mMimeName;
|
|
|
|
const char* mFileName;
|
|
|
|
};
|
|
|
|
|
|
|
|
FileMimeNameData kFileMimeNameMap[] = {
|
|
|
|
{ kFileMime, "GenericFileName" },
|
2016-05-05 17:07:24 +03:00
|
|
|
{ kJPEGImageMime, "GenericImageNameJPEG" },
|
|
|
|
{ kGIFImageMime, "GenericImageNameGIF" },
|
|
|
|
{ kPNGImageMime, "GenericImageNamePNG" },
|
2015-08-28 22:21:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DataTransferItem, mData,
|
2016-07-28 00:37:12 +03:00
|
|
|
mPrincipal, mParent, mCachedFile)
|
2015-08-28 22:21:08 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(DataTransferItem)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(DataTransferItem)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DataTransferItem)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
DataTransferItem::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return DataTransferItemBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DataTransferItem>
|
|
|
|
DataTransferItem::Clone(DataTransferItemList* aParent) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
|
|
|
|
RefPtr<DataTransferItem> it = new DataTransferItem(aParent, mType);
|
|
|
|
|
|
|
|
// Copy over all of the fields
|
|
|
|
it->mKind = mKind;
|
|
|
|
it->mIndex = mIndex;
|
|
|
|
it->mData = mData;
|
|
|
|
it->mPrincipal = mPrincipal;
|
2016-06-21 20:07:17 +03:00
|
|
|
it->mChromeOnly = mChromeOnly;
|
2015-08-28 22:21:08 +03:00
|
|
|
|
|
|
|
return it.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataTransferItem::SetType(const nsAString& aType)
|
|
|
|
{
|
|
|
|
mType = aType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataTransferItem::SetData(nsIVariant* aData)
|
|
|
|
{
|
2016-06-21 20:07:17 +03:00
|
|
|
// Invalidate our file cache, we will regenerate it with the new data
|
|
|
|
mCachedFile = nullptr;
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
if (!aData) {
|
|
|
|
// We are holding a temporary null which will later be filled.
|
|
|
|
// These are provided by the system, and have guaranteed properties about
|
|
|
|
// their kind based on their type.
|
|
|
|
MOZ_ASSERT(!mType.IsEmpty());
|
|
|
|
|
|
|
|
mKind = KIND_STRING;
|
|
|
|
for (uint32_t i = 0; i < ArrayLength(kFileMimeNameMap); ++i) {
|
|
|
|
if (mType.EqualsASCII(kFileMimeNameMap[i].mMimeName)) {
|
|
|
|
mKind = KIND_FILE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mData = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mKind = KIND_OTHER;
|
|
|
|
mData = aData;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
nsresult rv = aData->GetAsISupports(getter_AddRefs(supports));
|
|
|
|
if (NS_SUCCEEDED(rv) && supports) {
|
2016-06-21 20:07:17 +03:00
|
|
|
// Check if we have one of the supported file data formats
|
|
|
|
if (nsCOMPtr<nsIDOMBlob>(do_QueryInterface(supports)) ||
|
|
|
|
nsCOMPtr<BlobImpl>(do_QueryInterface(supports)) ||
|
|
|
|
nsCOMPtr<nsIFile>(do_QueryInterface(supports))) {
|
2015-08-28 22:21:08 +03:00
|
|
|
mKind = KIND_FILE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString string;
|
|
|
|
// If we can't get the data type as a string, that means that the object
|
|
|
|
// should be considered to be of the "other" type. This is impossible
|
|
|
|
// through the APIs defined by the spec, but we provide extra Moz* APIs,
|
|
|
|
// which allow setting of non-string data. We determine whether we can
|
|
|
|
// consider it a string, by calling GetAsAString, and checking for success.
|
|
|
|
rv = aData->GetAsAString(string);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mKind = KIND_STRING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataTransferItem::FillInExternalData()
|
|
|
|
{
|
|
|
|
if (mData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 utf8format(mType);
|
|
|
|
const char* format = utf8format.get();
|
|
|
|
if (strcmp(format, "text/plain") == 0) {
|
|
|
|
format = kUnicodeMime;
|
|
|
|
} else if (strcmp(format, "text/uri-list") == 0) {
|
|
|
|
format = kURLDataMime;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransferable> trans =
|
|
|
|
do_CreateInstance("@mozilla.org/widget/transferable;1");
|
|
|
|
if (NS_WARN_IF(!trans)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
trans->Init(nullptr);
|
|
|
|
trans->AddDataFlavor(format);
|
|
|
|
|
|
|
|
if (mParent->GetEventMessage() == ePaste) {
|
|
|
|
MOZ_ASSERT(mIndex == 0, "index in clipboard must be 0");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIClipboard> clipboard =
|
|
|
|
do_GetService("@mozilla.org/widget/clipboard;1");
|
|
|
|
if (!clipboard || mParent->ClipboardType() < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = clipboard->GetData(trans, mParent->ClipboardType());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession();
|
|
|
|
if (!dragSession) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = dragSession->GetData(trans, mIndex);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t length = 0;
|
|
|
|
nsCOMPtr<nsISupports> data;
|
|
|
|
nsresult rv = trans->GetTransferData(format, getter_AddRefs(data), &length);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv) || !data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:07:17 +03:00
|
|
|
// Fill the variant
|
2015-08-28 22:21:08 +03:00
|
|
|
RefPtr<nsVariantCC> variant = new nsVariantCC();
|
|
|
|
|
2016-06-21 20:07:17 +03:00
|
|
|
eKind oldKind = Kind();
|
|
|
|
if (oldKind == KIND_FILE) {
|
|
|
|
// Because this is an external piece of data, mType is one of kFileMime,
|
|
|
|
// kPNGImageMime, kJPEGImageMime, or kGIFImageMime. Some of these types
|
|
|
|
// are passed in as a nsIInputStream which must be converted to a
|
|
|
|
// dom::File before storing.
|
|
|
|
if (nsCOMPtr<nsIInputStream> istream = do_QueryInterface(data)) {
|
|
|
|
RefPtr<File> file = CreateFileFromInputStream(istream);
|
|
|
|
if (NS_WARN_IF(!file)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data = do_QueryObject(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
variant->SetAsISupports(data);
|
2015-08-28 22:21:08 +03:00
|
|
|
} else {
|
2016-06-21 20:07:17 +03:00
|
|
|
// We have an external piece of string data. Extract it and store it in the variant
|
|
|
|
MOZ_ASSERT(oldKind == KIND_STRING);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsString> supportsstr = do_QueryInterface(data);
|
|
|
|
if (supportsstr) {
|
|
|
|
nsAutoString str;
|
|
|
|
supportsstr->GetData(str);
|
|
|
|
variant->SetAsAString(str);
|
2015-08-28 22:21:08 +03:00
|
|
|
} else {
|
2016-06-21 20:07:17 +03:00
|
|
|
nsCOMPtr<nsISupportsCString> supportscstr = do_QueryInterface(data);
|
|
|
|
if (supportscstr) {
|
|
|
|
nsAutoCString str;
|
|
|
|
supportscstr->GetData(str);
|
|
|
|
variant->SetAsACString(str);
|
|
|
|
}
|
2015-08-28 22:21:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetData(variant);
|
2016-06-21 20:07:17 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (oldKind != Kind()) {
|
|
|
|
NS_WARNING("Clipboard data provided by the OS does not match predicted kind");
|
|
|
|
}
|
|
|
|
#endif
|
2015-08-28 22:21:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<File>
|
|
|
|
DataTransferItem::GetAsFile(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mKind != KIND_FILE) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIVariant* data = Data();
|
|
|
|
if (NS_WARN_IF(!data)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
aRv = data->GetAsISupports(getter_AddRefs(supports));
|
|
|
|
MOZ_ASSERT(!aRv.Failed() && supports,
|
2016-06-21 20:07:17 +03:00
|
|
|
"File objects should be stored as nsISupports variants");
|
2015-08-28 22:21:08 +03:00
|
|
|
if (aRv.Failed() || !supports) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:07:17 +03:00
|
|
|
// Generate the dom::File from the stored data, caching it so that the
|
|
|
|
// same object is returned in the future.
|
|
|
|
if (!mCachedFile) {
|
|
|
|
if (nsCOMPtr<nsIDOMBlob> domBlob = do_QueryInterface(supports)) {
|
|
|
|
Blob* blob = static_cast<Blob*>(domBlob.get());
|
|
|
|
mCachedFile = blob->ToFile();
|
|
|
|
} else if (nsCOMPtr<BlobImpl> blobImpl = do_QueryInterface(supports)) {
|
|
|
|
MOZ_ASSERT(blobImpl->IsFile());
|
|
|
|
mCachedFile = File::Create(GetParentObject(), blobImpl);
|
|
|
|
} else if (nsCOMPtr<nsIFile> ifile = do_QueryInterface(supports)) {
|
|
|
|
mCachedFile = File::CreateFromFile(GetParentObject(), ifile);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "One of the above code paths should be taken");
|
2016-05-31 22:03:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:07:17 +03:00
|
|
|
RefPtr<File> file = mCachedFile;
|
2016-05-31 22:03:44 +03:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
already_AddRefed<File>
|
2016-06-21 20:07:17 +03:00
|
|
|
DataTransferItem::CreateFileFromInputStream(nsIInputStream* aStream)
|
2015-08-28 22:21:08 +03:00
|
|
|
{
|
|
|
|
const char* key = nullptr;
|
|
|
|
for (uint32_t i = 0; i < ArrayLength(kFileMimeNameMap); ++i) {
|
|
|
|
if (mType.EqualsASCII(kFileMimeNameMap[i].mMimeName)) {
|
|
|
|
key = kFileMimeNameMap[i].mFileName;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!key) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unsupported mime type");
|
|
|
|
key = "GenericFileName";
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXPIDLString fileName;
|
2016-06-21 20:07:17 +03:00
|
|
|
nsresult rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
key, fileName);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-08-28 22:21:08 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t available;
|
2016-06-21 20:07:17 +03:00
|
|
|
rv = aStream->Available(&available);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-08-28 22:21:08 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* data = nullptr;
|
2016-06-21 20:07:17 +03:00
|
|
|
rv = NS_ReadInputStreamToBuffer(aStream, &data, available);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-08-28 22:21:08 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:07:17 +03:00
|
|
|
return File::CreateMemoryFile(GetParentObject(), data, available, fileName,
|
2015-08-28 22:21:08 +03:00
|
|
|
mType, PR_Now());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-31 22:03:44 +03:00
|
|
|
DataTransferItem::GetAsString(FunctionStringCallback* aCallback,
|
2015-08-28 22:21:08 +03:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!aCallback || mKind != KIND_STRING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:07:17 +03:00
|
|
|
// Theoretically this should be done inside of the runnable, as it might be an
|
|
|
|
// expensive operation on some systems. However, it doesn't really matter as
|
|
|
|
// in order to get a DataTransferItem, we need to call
|
|
|
|
// DataTransferItemList::IndexedGetter which already calls the Data method,
|
|
|
|
// meaning that this operation is basically free.
|
2015-08-28 22:21:08 +03:00
|
|
|
nsIVariant* data = Data();
|
|
|
|
if (NS_WARN_IF(!data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString stringData;
|
|
|
|
data->GetAsAString(stringData);
|
|
|
|
|
|
|
|
// Dispatch the callback to the main thread
|
|
|
|
class GASRunnable final : public Runnable
|
|
|
|
{
|
|
|
|
public:
|
2016-05-31 22:03:44 +03:00
|
|
|
GASRunnable(FunctionStringCallback* aCallback,
|
2015-08-28 22:21:08 +03:00
|
|
|
const nsAString& aStringData)
|
|
|
|
: mCallback(aCallback), mStringData(aStringData)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
mCallback->Call(mStringData, rv);
|
|
|
|
NS_WARN_IF(rv.Failed());
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
RefPtr<FunctionStringCallback> mCallback;
|
|
|
|
nsString mStringData;
|
|
|
|
};
|
|
|
|
|
|
|
|
RefPtr<GASRunnable> runnable = new GASRunnable(aCallback, stringData);
|
|
|
|
nsresult rv = NS_DispatchToMainThread(runnable);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("NS_DispatchToMainThread Failed in "
|
|
|
|
"DataTransferItem::GetAsString!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|