2013-11-11 12:03:59 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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/. */
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-09-25 15:21:22 +04:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2013-08-12 18:45:33 +04:00
|
|
|
#include "DataTransfer.h"
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2013-08-22 10:30:55 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
2008-08-27 16:07:27 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2011-10-03 23:11:31 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2013-07-09 21:51:03 +04:00
|
|
|
#include "mozilla/dom/DOMStringList.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2008-08-27 16:07:27 +04:00
|
|
|
#include "nsIDragService.h"
|
2013-03-11 19:49:48 +04:00
|
|
|
#include "nsIClipboard.h"
|
2008-08-27 16:07:27 +04:00
|
|
|
#include "nsContentUtils.h"
|
2010-03-25 23:38:53 +03:00
|
|
|
#include "nsIContent.h"
|
2016-04-21 21:11:14 +03:00
|
|
|
#include "nsIBinaryInputStream.h"
|
|
|
|
#include "nsIBinaryOutputStream.h"
|
|
|
|
#include "nsIStorageStream.h"
|
|
|
|
#include "nsStringStream.h"
|
2010-08-18 22:34:14 +04:00
|
|
|
#include "nsCRT.h"
|
2011-02-24 20:58:20 +03:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2012-07-02 03:45:59 +04:00
|
|
|
#include "nsIScriptContext.h"
|
2013-10-02 15:40:07 +04:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
2015-10-07 18:17:42 +03:00
|
|
|
#include "nsVariant.h"
|
2015-04-08 21:48:11 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2014-02-27 07:23:31 +04:00
|
|
|
#include "mozilla/dom/DataTransferBinding.h"
|
2015-08-28 22:21:08 +03:00
|
|
|
#include "mozilla/dom/DataTransferItemList.h"
|
2015-07-10 20:57:23 +03:00
|
|
|
#include "mozilla/dom/Directory.h"
|
2014-02-27 07:23:31 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2015-07-22 19:03:07 +03:00
|
|
|
#include "mozilla/dom/FileList.h"
|
2014-02-27 07:23:31 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2015-07-10 20:57:23 +03:00
|
|
|
#include "mozilla/dom/OSFileSystem.h"
|
2015-04-29 23:29:34 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-08-28 22:21:08 +03:00
|
|
|
#include "nsNetUtil.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2013-08-12 18:45:33 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-08-02 05:29:05 +04:00
|
|
|
|
2013-08-12 18:45:33 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(DataTransfer)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DataTransfer)
|
2014-02-27 07:23:31 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
|
2015-10-09 20:24:23 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mItems)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDragTarget)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDragImage)
|
2014-02-27 07:23:31 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2011-08-22 13:14:13 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2013-08-12 18:45:33 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DataTransfer)
|
2014-02-27 07:23:31 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
|
2015-10-09 20:24:23 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mItems)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDragTarget)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDragImage)
|
2014-02-27 07:23:31 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2011-08-22 13:14:13 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2014-02-27 07:23:31 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(DataTransfer)
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2013-08-12 18:45:33 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(DataTransfer)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(DataTransfer)
|
2010-01-12 16:08:43 +03:00
|
|
|
|
2013-08-12 18:45:33 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DataTransfer)
|
2014-02-28 22:57:14 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2014-02-27 07:23:31 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(mozilla::dom::DataTransfer)
|
2008-08-27 16:07:27 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMDataTransfer)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMDataTransfer)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
// the size of the array
|
2013-08-12 18:45:33 +04:00
|
|
|
const char DataTransfer::sEffects[8][9] = {
|
2008-08-27 16:07:27 +04:00
|
|
|
"none", "copy", "move", "copyMove", "link", "copyLink", "linkMove", "all"
|
|
|
|
};
|
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
// Used for custom clipboard types.
|
|
|
|
enum CustomClipboardTypeId {
|
|
|
|
eCustomClipboardTypeId_None,
|
|
|
|
eCustomClipboardTypeId_String
|
|
|
|
};
|
|
|
|
|
2015-09-02 09:08:01 +03:00
|
|
|
DataTransfer::DataTransfer(nsISupports* aParent, EventMessage aEventMessage,
|
2014-02-27 07:23:31 +04:00
|
|
|
bool aIsExternal, int32_t aClipboardType)
|
2015-09-02 09:08:01 +03:00
|
|
|
: mParent(aParent)
|
|
|
|
, mDropEffect(nsIDragService::DRAGDROP_ACTION_NONE)
|
|
|
|
, mEffectAllowed(nsIDragService::DRAGDROP_ACTION_UNINITIALIZED)
|
|
|
|
, mEventMessage(aEventMessage)
|
|
|
|
, mCursorState(false)
|
|
|
|
, mReadOnly(true)
|
|
|
|
, mIsExternal(aIsExternal)
|
|
|
|
, mUserCancelled(false)
|
|
|
|
, mIsCrossDomainSubFrameDrop(false)
|
|
|
|
, mClipboardType(aClipboardType)
|
|
|
|
, mDragImageX(0)
|
|
|
|
, mDragImageY(0)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2015-08-28 22:21:08 +03:00
|
|
|
mItems = new DataTransferItemList(this, aIsExternal, false /* aIsCrossDomainSubFrameDrop */);
|
2013-03-11 19:49:48 +04:00
|
|
|
// For these events, we want to be able to add data to the data transfer, so
|
|
|
|
// clear the readonly state. Otherwise, the data is already present. For
|
|
|
|
// external usage, cache the data from the native clipboard or drag.
|
2015-09-08 17:33:35 +03:00
|
|
|
if (aEventMessage == eCut ||
|
2015-09-08 17:33:34 +03:00
|
|
|
aEventMessage == eCopy ||
|
2015-09-02 09:08:01 +03:00
|
|
|
aEventMessage == eDragStart ||
|
2015-09-02 09:08:03 +03:00
|
|
|
aEventMessage == eLegacyDragGesture) {
|
2013-03-11 19:49:48 +04:00
|
|
|
mReadOnly = false;
|
2014-02-27 07:23:31 +04:00
|
|
|
} else if (mIsExternal) {
|
2015-09-08 17:33:35 +03:00
|
|
|
if (aEventMessage == ePaste) {
|
2013-03-11 19:49:48 +04:00
|
|
|
CacheExternalClipboardFormats();
|
2015-09-02 09:08:01 +03:00
|
|
|
} else if (aEventMessage >= eDragDropEventFirst &&
|
|
|
|
aEventMessage <= eDragDropEventLast) {
|
2013-03-11 19:49:48 +04:00
|
|
|
CacheExternalDragFormats();
|
|
|
|
}
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
DataTransfer::DataTransfer(nsISupports* aParent,
|
2015-09-02 09:08:01 +03:00
|
|
|
EventMessage aEventMessage,
|
2013-08-12 18:45:33 +04:00
|
|
|
const uint32_t aEffectAllowed,
|
|
|
|
bool aCursorState,
|
|
|
|
bool aIsExternal,
|
|
|
|
bool aUserCancelled,
|
|
|
|
bool aIsCrossDomainSubFrameDrop,
|
|
|
|
int32_t aClipboardType,
|
2015-08-28 22:21:08 +03:00
|
|
|
DataTransferItemList* aItems,
|
2014-02-27 07:23:31 +04:00
|
|
|
Element* aDragImage,
|
2013-08-12 18:45:33 +04:00
|
|
|
uint32_t aDragImageX,
|
|
|
|
uint32_t aDragImageY)
|
2015-09-02 09:08:01 +03:00
|
|
|
: mParent(aParent)
|
|
|
|
, mDropEffect(nsIDragService::DRAGDROP_ACTION_NONE)
|
|
|
|
, mEffectAllowed(aEffectAllowed)
|
|
|
|
, mEventMessage(aEventMessage)
|
|
|
|
, mCursorState(aCursorState)
|
|
|
|
, mReadOnly(true)
|
|
|
|
, mIsExternal(aIsExternal)
|
|
|
|
, mUserCancelled(aUserCancelled)
|
|
|
|
, mIsCrossDomainSubFrameDrop(aIsCrossDomainSubFrameDrop)
|
|
|
|
, mClipboardType(aClipboardType)
|
|
|
|
, mDragImage(aDragImage)
|
|
|
|
, mDragImageX(aDragImageX)
|
|
|
|
, mDragImageY(aDragImageY)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
MOZ_ASSERT(mParent);
|
2015-08-28 22:21:08 +03:00
|
|
|
MOZ_ASSERT(aItems);
|
|
|
|
|
|
|
|
// We clone the items array after everything else, so that it has a valid
|
|
|
|
// mParent value
|
|
|
|
mItems = aItems->Clone(this);
|
2008-08-27 16:07:27 +04:00
|
|
|
// The items are copied from aItems into mItems. There is no need to copy
|
|
|
|
// the actual data in the items as the data transfer will be read only. The
|
|
|
|
// draggesture and dragstart events are the only times when items are
|
|
|
|
// modifiable, but those events should have been using the first constructor
|
|
|
|
// above.
|
2015-09-02 09:08:03 +03:00
|
|
|
NS_ASSERTION(aEventMessage != eLegacyDragGesture &&
|
2015-09-02 09:08:01 +03:00
|
|
|
aEventMessage != eDragStart,
|
2013-08-12 18:45:33 +04:00
|
|
|
"invalid event type for DataTransfer constructor");
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
DataTransfer::~DataTransfer()
|
2015-07-07 13:38:00 +03:00
|
|
|
{}
|
2014-02-27 07:23:31 +04:00
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<DataTransfer>
|
|
|
|
DataTransfer::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aEventType, bool aIsExternal,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsAutoCString onEventType("on");
|
|
|
|
AppendUTF16toUTF8(aEventType, onEventType);
|
2016-03-29 02:09:43 +03:00
|
|
|
nsCOMPtr<nsIAtom> eventTypeAtom = NS_Atomize(onEventType);
|
2014-02-27 07:23:31 +04:00
|
|
|
if (!eventTypeAtom) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:56:59 +03:00
|
|
|
EventMessage eventMessage = nsContentUtils::GetEventMessage(eventTypeAtom);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataTransfer> transfer = new DataTransfer(aGlobal.GetAsSupports(),
|
2015-08-26 15:56:59 +03:00
|
|
|
eventMessage, aIsExternal,
|
2014-02-27 07:23:31 +04:00
|
|
|
-1);
|
|
|
|
return transfer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
DataTransfer::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-02-27 07:23:31 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return DataTransferBinding::Wrap(aCx, this, aGivenProto);
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
|
|
|
|
2008-08-27 16:07:27 +04:00
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::GetDropEffect(nsAString& aDropEffect)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
nsString dropEffect;
|
|
|
|
GetDropEffect(dropEffect);
|
|
|
|
aDropEffect = dropEffect;
|
2008-08-27 16:07:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::SetDropEffect(const nsAString& aDropEffect)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
|
|
|
// the drop effect can only be 'none', 'copy', 'move' or 'link'.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t e = 0; e <= nsIDragService::DRAGDROP_ACTION_LINK; e++) {
|
2008-08-27 16:07:27 +04:00
|
|
|
if (aDropEffect.EqualsASCII(sEffects[e])) {
|
|
|
|
// don't allow copyMove
|
|
|
|
if (e != (nsIDragService::DRAGDROP_ACTION_COPY |
|
2016-04-22 16:53:20 +03:00
|
|
|
nsIDragService::DRAGDROP_ACTION_MOVE)) {
|
2008-08-27 16:07:27 +04:00
|
|
|
mDropEffect = e;
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::GetEffectAllowed(nsAString& aEffectAllowed)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
nsString effectAllowed;
|
|
|
|
GetEffectAllowed(effectAllowed);
|
|
|
|
aEffectAllowed = effectAllowed;
|
2008-08-27 16:07:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::SetEffectAllowed(const nsAString& aEffectAllowed)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
|
|
|
if (aEffectAllowed.EqualsLiteral("uninitialized")) {
|
|
|
|
mEffectAllowed = nsIDragService::DRAGDROP_ACTION_UNINITIALIZED;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-11 12:03:59 +04:00
|
|
|
static_assert(nsIDragService::DRAGDROP_ACTION_NONE == 0,
|
|
|
|
"DRAGDROP_ACTION_NONE constant is wrong");
|
|
|
|
static_assert(nsIDragService::DRAGDROP_ACTION_COPY == 1,
|
|
|
|
"DRAGDROP_ACTION_COPY constant is wrong");
|
|
|
|
static_assert(nsIDragService::DRAGDROP_ACTION_MOVE == 2,
|
|
|
|
"DRAGDROP_ACTION_MOVE constant is wrong");
|
|
|
|
static_assert(nsIDragService::DRAGDROP_ACTION_LINK == 4,
|
|
|
|
"DRAGDROP_ACTION_LINK constant is wrong");
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t e = 0; e < ArrayLength(sEffects); e++) {
|
2008-08-27 16:07:27 +04:00
|
|
|
if (aEffectAllowed.EqualsASCII(sEffects[e])) {
|
|
|
|
mEffectAllowed = e;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::GetDropEffectInt(uint32_t* aDropEffect)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
|
|
|
*aDropEffect = mDropEffect;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::SetDropEffectInt(uint32_t aDropEffect)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
|
|
|
mDropEffect = aDropEffect;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::GetEffectAllowedInt(uint32_t* aEffectAllowed)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
|
|
|
*aEffectAllowed = mEffectAllowed;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::SetEffectAllowedInt(uint32_t aEffectAllowed)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
|
|
|
mEffectAllowed = aEffectAllowed;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-02-17 18:51:12 +03:00
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::GetMozUserCancelled(bool* aUserCancelled)
|
2009-02-17 18:51:12 +03:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
*aUserCancelled = MozUserCancelled();
|
2009-02-17 18:51:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
FileList*
|
2014-02-27 07:23:31 +04:00
|
|
|
DataTransfer::GetFiles(ErrorResult& aRv)
|
2015-10-23 02:28:14 +03:00
|
|
|
{
|
2015-08-28 22:21:08 +03:00
|
|
|
return mItems->Files();
|
2015-10-23 02:28:14 +03:00
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DataTransfer::GetFiles(nsIDOMFileList** aFileList)
|
2009-07-31 01:02:08 +04:00
|
|
|
{
|
2015-08-28 22:21:08 +03:00
|
|
|
if (!aFileList) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
2009-07-31 01:02:08 +04:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
RefPtr<FileList> files = GetFiles(rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
2016-03-30 19:58:41 +03:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
files.forget(aFileList);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-03-30 19:58:41 +03:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
already_AddRefed<DOMStringList>
|
|
|
|
DataTransfer::GetTypes(ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
RefPtr<DOMStringList> types = new DOMStringList();
|
2016-03-30 19:58:41 +03:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
const nsTArray<RefPtr<DataTransferItem>>* items = mItems->MozItemsAt(0);
|
|
|
|
if (!items || items->IsEmpty()) {
|
|
|
|
return types.forget();
|
|
|
|
}
|
2015-05-12 15:09:51 +03:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
bool addFile = false;
|
|
|
|
for (uint32_t i = 0; i < items->Length(); i++) {
|
|
|
|
DataTransferItem* item = items->ElementAt(i);
|
|
|
|
MOZ_ASSERT(item);
|
2015-05-12 15:09:51 +03:00
|
|
|
|
2016-05-31 22:03:44 +03:00
|
|
|
if (item->ChromeOnly() && !nsContentUtils::LegacyIsCallerChromeOrNativeCode()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
nsAutoString type;
|
|
|
|
item->GetType(type);
|
|
|
|
if (NS_WARN_IF(!types->Add(type))) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2009-07-31 01:02:08 +04:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
if (!addFile) {
|
|
|
|
addFile = item->Kind() == DataTransferItem::KIND_FILE;
|
2009-07-31 01:02:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
// If we have any files, we need to also add the "Files" type!
|
|
|
|
if (addFile && NS_WARN_IF(!types->Add(NS_LITERAL_STRING("Files")))) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.forget();
|
2009-07-31 01:02:08 +04:00
|
|
|
}
|
|
|
|
|
2008-08-27 16:07:27 +04:00
|
|
|
NS_IMETHODIMP
|
2015-08-28 22:21:08 +03:00
|
|
|
DataTransfer::GetTypes(nsISupports** aTypes)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2015-08-28 22:21:08 +03:00
|
|
|
if (NS_WARN_IF(!aTypes)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-09-15 02:14:13 +04:00
|
|
|
|
2016-05-11 17:04:19 +03:00
|
|
|
ErrorResult rv;
|
2015-08-28 22:21:08 +03:00
|
|
|
RefPtr<DOMStringList> types = GetTypes(rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
2014-02-27 07:23:31 +04:00
|
|
|
|
2014-03-15 23:00:15 +04:00
|
|
|
types.forget(aTypes);
|
2009-09-15 02:14:13 +04:00
|
|
|
return NS_OK;
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
void
|
|
|
|
DataTransfer::GetData(const nsAString& aFormat, nsAString& aData,
|
|
|
|
ErrorResult& aRv)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
|
|
|
// return an empty string if data for the format was not found
|
|
|
|
aData.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIVariant> data;
|
2016-04-22 16:53:20 +03:00
|
|
|
nsresult rv =
|
|
|
|
GetDataAtInternal(aFormat, 0, nsContentUtils::SubjectPrincipal(),
|
|
|
|
getter_AddRefs(data));
|
2014-02-27 07:23:31 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv != NS_ERROR_DOM_INDEX_SIZE_ERR) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
|
|
|
return;
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
nsAutoString stringdata;
|
|
|
|
data->GetAsAString(stringdata);
|
|
|
|
|
|
|
|
// for the URL type, parse out the first URI from the list. The URIs are
|
|
|
|
// separated by newlines
|
2011-11-08 15:42:53 +04:00
|
|
|
nsAutoString lowercaseFormat;
|
2014-08-13 15:53:32 +04:00
|
|
|
nsContentUtils::ASCIIToLower(aFormat, lowercaseFormat);
|
2013-03-11 19:49:48 +04:00
|
|
|
|
2011-11-08 15:42:53 +04:00
|
|
|
if (lowercaseFormat.EqualsLiteral("url")) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t lastidx = 0, idx;
|
|
|
|
int32_t length = stringdata.Length();
|
2008-08-27 16:07:27 +04:00
|
|
|
while (lastidx < length) {
|
|
|
|
idx = stringdata.FindChar('\n', lastidx);
|
|
|
|
// lines beginning with # are comments
|
|
|
|
if (stringdata[lastidx] == '#') {
|
2016-04-22 16:53:20 +03:00
|
|
|
if (idx == -1) {
|
2008-08-27 16:07:27 +04:00
|
|
|
break;
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
else {
|
2016-04-22 16:53:20 +03:00
|
|
|
if (idx == -1) {
|
2008-08-27 16:07:27 +04:00
|
|
|
aData.Assign(Substring(stringdata, lastidx));
|
2016-04-22 16:53:20 +03:00
|
|
|
} else {
|
2008-08-27 16:07:27 +04:00
|
|
|
aData.Assign(Substring(stringdata, lastidx, idx - lastidx));
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
|
|
|
aData = nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(aData,
|
|
|
|
true);
|
2014-02-27 07:23:31 +04:00
|
|
|
return;
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
lastidx = idx + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aData = stringdata;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-27 07:23:31 +04:00
|
|
|
DataTransfer::GetData(const nsAString& aFormat, nsAString& aData)
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
GetData(aFormat, aData, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataTransfer::SetData(const nsAString& aFormat, const nsAString& aData,
|
|
|
|
ErrorResult& aRv)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsVariantCC> variant = new nsVariantCC();
|
2008-08-27 16:07:27 +04:00
|
|
|
variant->SetAsAString(aData);
|
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
aRv = SetDataAtInternal(aFormat, variant, 0,
|
|
|
|
nsContentUtils::SubjectPrincipal());
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataTransfer::ClearData(const Optional<nsAString>& aFormat, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mReadOnly) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
if (MozItemCount() == 0) {
|
2014-02-27 07:23:31 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aFormat.WasPassed()) {
|
|
|
|
MozClearDataAtHelper(aFormat.Value(), 0, aRv);
|
|
|
|
} else {
|
|
|
|
MozClearDataAtHelper(EmptyString(), 0, aRv);
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::ClearData(const nsAString& aFormat)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
Optional<nsAString> format;
|
|
|
|
format = &aFormat;
|
|
|
|
ErrorResult rv;
|
|
|
|
ClearData(format, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::GetMozItemCount(uint32_t* aCount)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
*aCount = MozItemCount();
|
2008-08-27 16:07:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-03-06 20:54:29 +03:00
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::GetMozCursor(nsAString& aCursorState)
|
2009-03-06 20:54:29 +03:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
nsString cursor;
|
|
|
|
GetMozCursor(cursor);
|
|
|
|
aCursorState = cursor;
|
2009-03-06 20:54:29 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::SetMozCursor(const nsAString& aCursorState)
|
2009-03-06 20:54:29 +03:00
|
|
|
{
|
|
|
|
// Lock the cursor to an arrow during the drag.
|
2009-03-10 03:01:55 +03:00
|
|
|
mCursorState = aCursorState.EqualsLiteral("default");
|
2009-03-06 20:54:29 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
already_AddRefed<nsINode>
|
|
|
|
DataTransfer::GetMozSourceNode()
|
2010-03-02 17:21:20 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession();
|
2014-02-27 07:23:31 +04:00
|
|
|
if (!dragSession) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2010-03-02 17:21:20 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> sourceNode;
|
|
|
|
dragSession->GetSourceNode(getter_AddRefs(sourceNode));
|
2014-02-27 07:23:31 +04:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(sourceNode);
|
2015-09-27 08:43:29 +03:00
|
|
|
if (node && !nsContentUtils::LegacyIsCallerNativeCode()
|
|
|
|
&& !nsContentUtils::CanCallerAccess(node)) {
|
2014-02-27 07:23:31 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2010-03-02 17:21:20 +03:00
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
return node.forget();
|
2010-03-02 17:21:20 +03:00
|
|
|
}
|
|
|
|
|
2008-08-27 16:07:27 +04:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 07:23:31 +04:00
|
|
|
DataTransfer::GetMozSourceNode(nsIDOMNode** aSourceNode)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
nsCOMPtr<nsINode> sourceNode = GetMozSourceNode();
|
|
|
|
if (!sourceNode) {
|
|
|
|
*aSourceNode = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
return CallQueryInterface(sourceNode, aSourceNode);
|
|
|
|
}
|
|
|
|
|
2013-07-09 21:54:21 +04:00
|
|
|
already_AddRefed<DOMStringList>
|
2016-05-11 17:04:19 +03:00
|
|
|
DataTransfer::MozTypesAt(uint32_t aIndex, ErrorResult& aRv) const
|
2014-02-27 07:23:31 +04:00
|
|
|
{
|
2013-03-11 19:49:48 +04:00
|
|
|
// Only the first item is valid for clipboard events
|
|
|
|
if (aIndex > 0 &&
|
2015-09-08 17:33:35 +03:00
|
|
|
(mEventMessage == eCut || mEventMessage == eCopy ||
|
2015-09-08 17:33:35 +03:00
|
|
|
mEventMessage == ePaste)) {
|
2014-02-27 07:23:31 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMStringList> types = new DOMStringList();
|
2015-08-28 22:21:08 +03:00
|
|
|
if (aIndex < MozItemCount()) {
|
2008-08-27 16:07:27 +04:00
|
|
|
// note that you can retrieve the types regardless of their principal
|
2015-08-28 22:21:08 +03:00
|
|
|
const nsTArray<RefPtr<DataTransferItem>>& items = *mItems->MozItemsAt(aIndex);
|
2016-05-11 17:04:19 +03:00
|
|
|
|
2016-05-31 22:03:44 +03:00
|
|
|
bool addFile = false;
|
2015-08-28 22:21:08 +03:00
|
|
|
for (uint32_t i = 0; i < items.Length(); i++) {
|
2016-05-31 22:03:44 +03:00
|
|
|
if (items[i]->ChromeOnly() && !nsContentUtils::LegacyIsCallerChromeOrNativeCode()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
nsAutoString type;
|
|
|
|
items[i]->GetType(type);
|
|
|
|
if (NS_WARN_IF(!types->Add(type))) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2016-05-11 17:04:19 +03:00
|
|
|
}
|
2016-05-31 22:03:44 +03:00
|
|
|
|
|
|
|
if (items[i]->Kind() == DataTransferItem::KIND_FILE) {
|
|
|
|
addFile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addFile) {
|
|
|
|
types->Add(NS_LITERAL_STRING("Files"));
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
return types.forget();
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
NS_IMETHODIMP
|
2013-07-09 21:54:21 +04:00
|
|
|
DataTransfer::MozTypesAt(uint32_t aIndex, nsISupports** aTypes)
|
2014-02-27 07:23:31 +04:00
|
|
|
{
|
|
|
|
ErrorResult rv;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMStringList> types = MozTypesAt(aIndex, rv);
|
2014-03-15 23:00:15 +04:00
|
|
|
types.forget(aTypes);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2015-10-23 02:28:14 +03:00
|
|
|
nsresult
|
2016-04-22 16:53:20 +03:00
|
|
|
DataTransfer::GetDataAtNoSecurityCheck(const nsAString& aFormat,
|
|
|
|
uint32_t aIndex,
|
2015-10-23 02:28:14 +03:00
|
|
|
nsIVariant** aData)
|
|
|
|
{
|
2016-04-22 16:53:20 +03:00
|
|
|
return GetDataAtInternal(aFormat, aIndex,
|
|
|
|
nsContentUtils::GetSystemPrincipal(), aData);
|
2015-10-23 02:28:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DataTransfer::GetDataAtInternal(const nsAString& aFormat, uint32_t aIndex,
|
2016-04-22 16:53:20 +03:00
|
|
|
nsIPrincipal* aSubjectPrincipal,
|
|
|
|
nsIVariant** aData)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aData = nullptr;
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
if (aFormat.IsEmpty()) {
|
2008-08-27 16:07:27 +04:00
|
|
|
return NS_OK;
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
if (aIndex >= MozItemCount()) {
|
2008-08-27 16:07:27 +04:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only the first item is valid for clipboard events
|
|
|
|
if (aIndex > 0 &&
|
2015-09-08 17:33:35 +03:00
|
|
|
(mEventMessage == eCut || mEventMessage == eCopy ||
|
2015-09-08 17:33:35 +03:00
|
|
|
mEventMessage == ePaste)) {
|
2013-03-11 19:49:48 +04:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
|
2008-08-27 16:07:27 +04:00
|
|
|
nsAutoString format;
|
|
|
|
GetRealFormat(aFormat, format);
|
|
|
|
|
2012-03-20 19:21:41 +04:00
|
|
|
// Check if the caller is allowed to access the drag data. Callers with
|
2012-10-26 03:10:53 +04:00
|
|
|
// chrome privileges can always read the data. During the
|
2012-03-20 19:21:41 +04:00
|
|
|
// drop event, allow retrieving the data except in the case where the
|
|
|
|
// source of the drag is in a child frame of the caller. In that case,
|
|
|
|
// we only allow access to data of the same principal. During other events,
|
|
|
|
// only allow access to the data with the same principal.
|
2015-10-23 02:28:14 +03:00
|
|
|
bool checkFormatItemPrincipal = mIsCrossDomainSubFrameDrop ||
|
2015-09-02 09:08:03 +03:00
|
|
|
(mEventMessage != eDrop && mEventMessage != eLegacyDragDrop &&
|
2015-10-23 02:28:14 +03:00
|
|
|
mEventMessage != ePaste);
|
2015-08-28 22:21:08 +03:00
|
|
|
MOZ_ASSERT(aSubjectPrincipal);
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
RefPtr<DataTransferItem> item = mItems->MozItemByTypeAt(format, aIndex);
|
|
|
|
if (!item) {
|
|
|
|
// The index exists but there's no data for the specified format, in this
|
|
|
|
// case we just return undefined
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2016-05-31 22:03:44 +03:00
|
|
|
// If we have chrome only content, and we aren't chrome, don't allow access
|
|
|
|
if (!nsContentUtils::IsSystemPrincipal(aSubjectPrincipal) && item->ChromeOnly()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
if (item->Principal() && checkFormatItemPrincipal &&
|
|
|
|
!aSubjectPrincipal->Subsumes(item->Principal())) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIVariant> data = item->Data();
|
|
|
|
MOZ_ASSERT(data);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> isupportsData;
|
|
|
|
nsresult rv = data->GetAsISupports(getter_AddRefs(isupportsData));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && isupportsData) {
|
|
|
|
// Make sure the code that is calling us is same-origin with the data.
|
|
|
|
nsCOMPtr<EventTarget> pt = do_QueryInterface(isupportsData);
|
|
|
|
if (pt) {
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsIScriptContext* c = pt->GetContextForEventHandlers(&rv);
|
|
|
|
NS_ENSURE_TRUE(c && NS_SUCCEEDED(rv), NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
nsIGlobalObject* go = c->GetGlobalObject();
|
|
|
|
NS_ENSURE_TRUE(go, NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sp = do_QueryInterface(go);
|
|
|
|
MOZ_ASSERT(sp, "This cannot fail on the main thread.");
|
|
|
|
nsIPrincipal* dataPrincipal = sp->GetPrincipal();
|
|
|
|
NS_ENSURE_TRUE(dataPrincipal, NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
NS_ENSURE_TRUE(aSubjectPrincipal->Subsumes(dataPrincipal), NS_ERROR_DOM_SECURITY_ERR);
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
data.forget(aData);
|
2008-08-27 16:07:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
void
|
2014-02-27 07:23:31 +04:00
|
|
|
DataTransfer::MozGetDataAt(JSContext* aCx, const nsAString& aFormat,
|
2014-06-12 00:26:52 +04:00
|
|
|
uint32_t aIndex,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval,
|
|
|
|
mozilla::ErrorResult& aRv)
|
2014-02-27 07:23:31 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIVariant> data;
|
2016-04-22 16:53:20 +03:00
|
|
|
aRv = GetDataAtInternal(aFormat, aIndex, nsContentUtils::SubjectPrincipal(),
|
|
|
|
getter_AddRefs(data));
|
2014-02-27 07:23:31 +04:00
|
|
|
if (aRv.Failed()) {
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
2015-04-18 05:01:01 +03:00
|
|
|
aRetval.setNull();
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> result(aCx);
|
2014-06-12 00:26:52 +04:00
|
|
|
if (!VariantToJsval(aCx, data, aRetval)) {
|
2014-02-27 07:23:31 +04:00
|
|
|
aRv = NS_ERROR_FAILURE;
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 01:30:04 +03:00
|
|
|
nsresult
|
|
|
|
DataTransfer::SetDataAtInternal(const nsAString& aFormat, nsIVariant* aData,
|
2016-04-22 16:53:20 +03:00
|
|
|
uint32_t aIndex,
|
|
|
|
nsIPrincipal* aSubjectPrincipal)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
if (aFormat.IsEmpty()) {
|
2008-08-27 16:07:27 +04:00
|
|
|
return NS_OK;
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
if (mReadOnly) {
|
2008-08-27 16:07:27 +04:00
|
|
|
return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
|
|
|
// Specifying an index less than the current length will replace an existing
|
|
|
|
// item. Specifying an index equal to the current length will add a new item.
|
2015-08-28 22:21:08 +03:00
|
|
|
if (aIndex > MozItemCount()) {
|
2013-03-11 19:49:48 +04:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only the first item is valid for clipboard events
|
|
|
|
if (aIndex > 0 &&
|
2015-09-08 17:33:35 +03:00
|
|
|
(mEventMessage == eCut || mEventMessage == eCopy ||
|
2015-09-08 17:33:35 +03:00
|
|
|
mEventMessage == ePaste)) {
|
2008-08-27 16:07:27 +04:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
// Don't allow the custom type to be assigned.
|
|
|
|
if (aFormat.EqualsLiteral(kCustomTypesMime)) {
|
|
|
|
return NS_ERROR_TYPE_ERR;
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:11:13 +03:00
|
|
|
// Don't allow non-chrome to add non-string or file data. We'll block file
|
|
|
|
// promises as well which are used internally for drags to the desktop.
|
|
|
|
if (!nsContentUtils::IsSystemPrincipal(aSubjectPrincipal)) {
|
2016-05-11 17:04:19 +03:00
|
|
|
if (aFormat.EqualsLiteral(kFilePromiseMime) ||
|
|
|
|
aFormat.EqualsLiteral(kFileMime)) {
|
2016-04-21 21:11:13 +03:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t type;
|
|
|
|
aData->GetDataType(&type);
|
|
|
|
if (type == nsIDataType::VTYPE_INTERFACE ||
|
|
|
|
type == nsIDataType::VTYPE_INTERFACE_IS) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2015-10-23 01:30:04 +03:00
|
|
|
return SetDataWithPrincipal(aFormat, aData, aIndex, aSubjectPrincipal);
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
void
|
|
|
|
DataTransfer::MozSetDataAt(JSContext* aCx, const nsAString& aFormat,
|
|
|
|
JS::Handle<JS::Value> aData,
|
|
|
|
uint32_t aIndex, ErrorResult& aRv)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
nsCOMPtr<nsIVariant> data;
|
|
|
|
aRv = nsContentUtils::XPConnect()->JSValToVariant(aCx, aData,
|
|
|
|
getter_AddRefs(data));
|
|
|
|
if (!aRv.Failed()) {
|
2016-04-22 16:53:20 +03:00
|
|
|
aRv = SetDataAtInternal(aFormat, data, aIndex,
|
|
|
|
nsContentUtils::SubjectPrincipal());
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataTransfer::MozClearDataAt(const nsAString& aFormat, uint32_t aIndex,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mReadOnly) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
if (aIndex >= MozItemCount()) {
|
2014-02-27 07:23:31 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only the first item is valid for clipboard events
|
|
|
|
if (aIndex > 0 &&
|
2015-09-08 17:33:35 +03:00
|
|
|
(mEventMessage == eCut || mEventMessage == eCopy ||
|
2015-09-08 17:33:35 +03:00
|
|
|
mEventMessage == ePaste)) {
|
2014-02-27 07:23:31 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
MozClearDataAtHelper(aFormat, aIndex, aRv);
|
2015-08-28 22:21:08 +03:00
|
|
|
|
|
|
|
// If we just cleared the 0-th index, and there are still more than 1 indexes
|
|
|
|
// remaining, MozClearDataAt should cause the 1st index to become the 0th
|
|
|
|
// index. This should _only_ happen when the MozClearDataAt function is
|
|
|
|
// explicitly called by script, as this behavior is inconsistent with spec.
|
|
|
|
// (however, so is the MozClearDataAt API)
|
|
|
|
|
|
|
|
if (aIndex == 0 && mItems->MozItemCount() > 1 &&
|
|
|
|
mItems->MozItemsAt(0)->Length() == 0) {
|
|
|
|
mItems->PopIndexZero();
|
|
|
|
}
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataTransfer::MozClearDataAtHelper(const nsAString& aFormat, uint32_t aIndex,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mReadOnly);
|
2015-08-28 22:21:08 +03:00
|
|
|
MOZ_ASSERT(aIndex < MozItemCount());
|
2014-02-27 07:23:31 +04:00
|
|
|
MOZ_ASSERT(aIndex == 0 ||
|
2015-09-08 17:33:35 +03:00
|
|
|
(mEventMessage != eCut && mEventMessage != eCopy &&
|
2015-09-08 17:33:35 +03:00
|
|
|
mEventMessage != ePaste));
|
2014-02-27 07:23:31 +04:00
|
|
|
|
2008-08-27 16:07:27 +04:00
|
|
|
nsAutoString format;
|
|
|
|
GetRealFormat(aFormat, format);
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
mItems->MozRemoveByTypeAt(format, aIndex, aRv);
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-27 07:23:31 +04:00
|
|
|
DataTransfer::MozClearDataAt(const nsAString& aFormat, uint32_t aIndex)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
MozClearDataAt(aFormat, aIndex, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
void
|
|
|
|
DataTransfer::SetDragImage(Element& aImage, int32_t aX, int32_t aY,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mReadOnly) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
|
|
|
|
return;
|
2010-03-25 23:38:53 +03:00
|
|
|
}
|
2014-02-27 07:23:31 +04:00
|
|
|
|
|
|
|
mDragImage = &aImage;
|
2008-08-27 16:07:27 +04:00
|
|
|
mDragImageX = aX;
|
|
|
|
mDragImageY = aY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-27 07:23:31 +04:00
|
|
|
DataTransfer::SetDragImage(nsIDOMElement* aImage, int32_t aX, int32_t aY)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
nsCOMPtr<Element> image = do_QueryInterface(aImage);
|
|
|
|
if (image) {
|
|
|
|
SetDragImage(*image, aX, aY, rv);
|
|
|
|
}
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2015-07-10 20:57:23 +03:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
DataTransfer::GetFilesAndDirectories(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> parentNode = do_QueryInterface(mParent);
|
|
|
|
if (!parentNode) {
|
2015-08-25 10:22:57 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2015-07-10 20:57:23 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = parentNode->OwnerDoc()->GetScopeObject();
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
if (!global) {
|
2015-08-25 10:22:57 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2015-07-10 20:57:23 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> p = Promise::Create(global, aRv);
|
2015-08-28 22:21:08 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-07-10 20:57:23 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
RefPtr<FileList> files = mItems->Files();
|
|
|
|
if (NS_WARN_IF(!files)) {
|
|
|
|
return nullptr;
|
2015-07-10 20:57:23 +03:00
|
|
|
}
|
|
|
|
|
2016-04-12 15:51:52 +03:00
|
|
|
Sequence<RefPtr<File>> filesSeq;
|
2015-08-28 22:21:08 +03:00
|
|
|
files->ToSequence(filesSeq, aRv);
|
2016-03-20 13:56:10 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2015-07-10 20:57:23 +03:00
|
|
|
}
|
|
|
|
|
2016-04-12 15:51:52 +03:00
|
|
|
p->MaybeResolve(filesSeq);
|
2015-07-10 20:57:23 +03:00
|
|
|
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
void
|
|
|
|
DataTransfer::AddElement(Element& aElement, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mReadOnly) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
|
|
|
|
return;
|
2012-03-20 19:21:41 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
mDragTarget = &aElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DataTransfer::AddElement(nsIDOMElement* aElement)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
nsCOMPtr<Element> element = do_QueryInterface(aElement);
|
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_INVALID_ARG);
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
AddElement(*element, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-09-02 09:08:01 +03:00
|
|
|
DataTransfer::Clone(nsISupports* aParent, EventMessage aEventMessage,
|
2014-02-27 07:23:31 +04:00
|
|
|
bool aUserCancelled, bool aIsCrossDomainSubFrameDrop,
|
|
|
|
DataTransfer** aNewDataTransfer)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2015-08-28 22:21:08 +03:00
|
|
|
RefPtr<DataTransfer> newDataTransfer =
|
2015-09-02 09:08:01 +03:00
|
|
|
new DataTransfer(aParent, aEventMessage, mEffectAllowed, mCursorState,
|
2014-02-27 07:23:31 +04:00
|
|
|
mIsExternal, aUserCancelled, aIsCrossDomainSubFrameDrop,
|
|
|
|
mClipboardType, mItems, mDragImage, mDragImageX,
|
|
|
|
mDragImageY);
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
newDataTransfer.forget(aNewDataTransfer);
|
2008-08-27 16:07:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
already_AddRefed<nsISupportsArray>
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::GetTransferables(nsIDOMNode* aDragTarget)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2012-04-17 06:14:01 +04:00
|
|
|
MOZ_ASSERT(aDragTarget);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> dragNode = do_QueryInterface(aDragTarget);
|
2013-03-11 19:49:48 +04:00
|
|
|
if (!dragNode) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-30 19:58:41 +03:00
|
|
|
|
2016-04-20 13:28:28 +03:00
|
|
|
nsIDocument* doc = dragNode->GetComposedDoc();
|
2013-03-11 19:49:48 +04:00
|
|
|
if (!doc) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-08 21:48:11 +03:00
|
|
|
|
|
|
|
return GetTransferables(doc->GetLoadContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsISupportsArray>
|
|
|
|
DataTransfer::GetTransferables(nsILoadContext* aLoadContext)
|
|
|
|
{
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsArray> transArray =
|
|
|
|
do_CreateInstance("@mozilla.org/supports-array;1");
|
|
|
|
if (!transArray) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-04-17 06:14:01 +04:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
uint32_t count = MozItemCount();
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2015-04-08 21:48:11 +03:00
|
|
|
nsCOMPtr<nsITransferable> transferable = GetTransferable(i, aLoadContext);
|
2013-03-11 19:49:48 +04:00
|
|
|
if (transferable) {
|
|
|
|
transArray->AppendElement(transferable);
|
|
|
|
}
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
return transArray.forget();
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
already_AddRefed<nsITransferable>
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::GetTransferable(uint32_t aIndex, nsILoadContext* aLoadContext)
|
2013-03-11 19:49:48 +04:00
|
|
|
{
|
2015-08-28 22:21:08 +03:00
|
|
|
if (aIndex >= MozItemCount()) {
|
2013-03-11 19:49:48 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
const nsTArray<RefPtr<DataTransferItem>>& item = *mItems->MozItemsAt(aIndex);
|
2013-03-11 19:49:48 +04:00
|
|
|
uint32_t count = item.Length();
|
|
|
|
if (!count) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
nsCOMPtr<nsITransferable> transferable =
|
|
|
|
do_CreateInstance("@mozilla.org/widget/transferable;1");
|
|
|
|
if (!transferable) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
transferable->Init(aLoadContext);
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
nsCOMPtr<nsIStorageStream> storageStream;
|
|
|
|
nsCOMPtr<nsIBinaryOutputStream> stream;
|
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
bool added = false;
|
2016-04-21 21:11:14 +03:00
|
|
|
bool handlingCustomFormats = true;
|
|
|
|
uint32_t totalCustomLength = 0;
|
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
const char* knownFormats[] = {
|
|
|
|
kTextMime, kHTMLMime, kNativeHTMLMime, kRTFMime,
|
|
|
|
kURLMime, kURLDataMime, kURLDescriptionMime, kURLPrivateMime,
|
|
|
|
kPNGImageMime, kJPEGImageMime, kGIFImageMime, kNativeImageMime,
|
2016-04-26 03:21:00 +03:00
|
|
|
kFileMime, kFilePromiseMime, kFilePromiseURLMime,
|
|
|
|
kFilePromiseDestFilename, kFilePromiseDirectoryMime,
|
2016-04-22 16:53:20 +03:00
|
|
|
kMozTextInternal, kHTMLContext, kHTMLInfo };
|
2016-04-21 21:11:14 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Two passes are made here to iterate over all of the types. First, look for
|
2016-04-22 16:53:20 +03:00
|
|
|
* any types that are not in the list of known types. For this pass,
|
|
|
|
* handlingCustomFormats will be true. Data that corresponds to unknown types
|
|
|
|
* will be pulled out and inserted into a single type (kCustomTypesMime) by
|
|
|
|
* writing the data into a stream.
|
2016-04-21 21:11:14 +03:00
|
|
|
*
|
2016-04-22 16:53:20 +03:00
|
|
|
* The second pass will iterate over the formats looking for known types.
|
|
|
|
* These are added as is. The unknown types are all then inserted as a single
|
|
|
|
* type (kCustomTypesMime) in the same position of the first custom type. This
|
|
|
|
* model is used to maintain the format order as best as possible.
|
2016-04-21 21:11:14 +03:00
|
|
|
*
|
2016-04-22 16:53:20 +03:00
|
|
|
* The format of the kCustomTypesMime type is one or more of the following
|
|
|
|
* stored sequentially:
|
2016-04-21 21:11:14 +03:00
|
|
|
* <32-bit> type (only none or string is supported)
|
|
|
|
* <32-bit> length of format
|
|
|
|
* <wide string> format
|
|
|
|
* <32-bit> length of data
|
|
|
|
* <wide string> data
|
2016-04-22 16:53:20 +03:00
|
|
|
* A type of eCustomClipboardTypeId_None ends the list, without any following
|
|
|
|
* data.
|
2016-04-21 21:11:14 +03:00
|
|
|
*/
|
|
|
|
do {
|
|
|
|
for (uint32_t f = 0; f < count; f++) {
|
2015-08-28 22:21:08 +03:00
|
|
|
RefPtr<DataTransferItem> formatitem = item[f];
|
|
|
|
if (!formatitem->Data()) { // skip empty items
|
2016-04-21 21:11:14 +03:00
|
|
|
continue;
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
nsAutoString type;
|
|
|
|
formatitem->GetType(type);
|
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
// If the data is of one of the well-known formats, use it directly.
|
|
|
|
bool isCustomFormat = true;
|
|
|
|
for (uint32_t f = 0; f < ArrayLength(knownFormats); f++) {
|
2015-08-28 22:21:08 +03:00
|
|
|
if (type.EqualsASCII(knownFormats[f])) {
|
2016-04-21 21:11:14 +03:00
|
|
|
isCustomFormat = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
uint32_t lengthInBytes;
|
|
|
|
nsCOMPtr<nsISupports> convertedData;
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
if (handlingCustomFormats) {
|
2015-08-28 22:21:08 +03:00
|
|
|
if (!ConvertFromVariant(formatitem->Data(), getter_AddRefs(convertedData),
|
2016-04-22 16:53:20 +03:00
|
|
|
&lengthInBytes)) {
|
2016-04-21 21:11:14 +03:00
|
|
|
continue;
|
|
|
|
}
|
2013-03-11 19:49:48 +04:00
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
// When handling custom types, add the data to the stream if this is a
|
|
|
|
// custom type.
|
|
|
|
if (isCustomFormat) {
|
2016-04-22 16:53:20 +03:00
|
|
|
// If it isn't a string, just ignore it. The dataTransfer is cached in
|
|
|
|
// the drag sesion during drag-and-drop, so non-strings will be
|
|
|
|
// available when dragging locally.
|
2016-04-21 21:11:14 +03:00
|
|
|
nsCOMPtr<nsISupportsString> str(do_QueryInterface(convertedData));
|
|
|
|
if (str) {
|
|
|
|
nsAutoString data;
|
|
|
|
str->GetData(data);
|
|
|
|
|
|
|
|
if (!stream) {
|
|
|
|
// Create a storage stream to write to.
|
|
|
|
NS_NewStorageStream(1024, UINT32_MAX, getter_AddRefs(storageStream));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> outputStream;
|
|
|
|
storageStream->GetOutputStream(0, getter_AddRefs(outputStream));
|
|
|
|
|
|
|
|
stream = do_CreateInstance("@mozilla.org/binaryoutputstream;1");
|
|
|
|
stream->SetOutputStream(outputStream);
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
int32_t formatLength = type.Length() * sizeof(nsString::char_type);
|
2016-04-21 21:11:14 +03:00
|
|
|
|
|
|
|
stream->Write32(eCustomClipboardTypeId_String);
|
|
|
|
stream->Write32(formatLength);
|
2015-08-28 22:21:08 +03:00
|
|
|
stream->WriteBytes((const char *)type.get(),
|
2016-04-22 16:53:20 +03:00
|
|
|
formatLength);
|
2016-04-21 21:11:14 +03:00
|
|
|
stream->Write32(lengthInBytes);
|
|
|
|
stream->WriteBytes((const char *)data.get(), lengthInBytes);
|
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
// The total size of the stream is the format length, the data
|
|
|
|
// length, two integers to hold the lengths and one integer for the
|
|
|
|
// string flag.
|
|
|
|
totalCustomLength +=
|
|
|
|
formatLength + lengthInBytes + (sizeof(uint32_t) * 3);
|
2016-04-21 21:11:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isCustomFormat && stream) {
|
|
|
|
// This is the second pass of the loop (handlingCustomFormats is false).
|
|
|
|
// When encountering the first custom format, append all of the stream
|
|
|
|
// at this position.
|
|
|
|
|
|
|
|
// Write out a terminator.
|
|
|
|
totalCustomLength += sizeof(uint32_t);
|
|
|
|
stream->Write32(eCustomClipboardTypeId_None);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
|
|
|
storageStream->NewInputStream(0, getter_AddRefs(inputStream));
|
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
RefPtr<nsStringBuffer> stringBuffer =
|
|
|
|
nsStringBuffer::Alloc(totalCustomLength + 1);
|
2016-04-21 21:11:14 +03:00
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
// Read the data from the string and add a null-terminator as ToString
|
|
|
|
// needs it.
|
2016-04-21 21:11:14 +03:00
|
|
|
uint32_t amountRead;
|
2016-04-22 16:53:20 +03:00
|
|
|
inputStream->Read(static_cast<char*>(stringBuffer->Data()),
|
|
|
|
totalCustomLength, &amountRead);
|
2016-04-21 21:11:14 +03:00
|
|
|
static_cast<char*>(stringBuffer->Data())[amountRead] = 0;
|
|
|
|
|
|
|
|
nsCString str;
|
|
|
|
stringBuffer->ToString(totalCustomLength, str);
|
2016-04-22 16:53:20 +03:00
|
|
|
nsCOMPtr<nsISupportsCString>
|
|
|
|
strSupports(do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID));
|
2016-04-21 21:11:14 +03:00
|
|
|
strSupports->SetData(str);
|
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
nsresult rv = transferable->SetTransferData(kCustomTypesMime,
|
|
|
|
strSupports,
|
|
|
|
totalCustomLength);
|
2016-04-21 21:11:14 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
added = true;
|
|
|
|
|
|
|
|
// Clear the stream so it doesn't get used again.
|
|
|
|
stream = nullptr;
|
|
|
|
} else {
|
|
|
|
// This is the second pass of the loop and a known type is encountered.
|
|
|
|
// Add it as is.
|
2015-08-28 22:21:08 +03:00
|
|
|
if (!ConvertFromVariant(formatitem->Data(), getter_AddRefs(convertedData),
|
2016-04-22 16:53:20 +03:00
|
|
|
&lengthInBytes)) {
|
2016-04-21 21:11:14 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
// The underlying drag code uses text/unicode, so use that instead of
|
|
|
|
// text/plain
|
2016-04-21 21:11:14 +03:00
|
|
|
const char* format;
|
2015-08-28 22:21:08 +03:00
|
|
|
NS_ConvertUTF16toUTF8 utf8format(type);
|
2016-04-21 21:11:14 +03:00
|
|
|
if (utf8format.EqualsLiteral(kTextMime)) {
|
|
|
|
format = kUnicodeMime;
|
|
|
|
} else {
|
|
|
|
format = utf8format.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a converter is set for a format, set the converter for the
|
|
|
|
// transferable and don't add the item
|
2016-04-22 16:53:20 +03:00
|
|
|
nsCOMPtr<nsIFormatConverter> converter =
|
|
|
|
do_QueryInterface(convertedData);
|
2016-04-21 21:11:14 +03:00
|
|
|
if (converter) {
|
|
|
|
transferable->AddDataFlavor(format);
|
|
|
|
transferable->SetConverter(converter);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
nsresult rv = transferable->SetTransferData(format, convertedData,
|
|
|
|
lengthInBytes);
|
2016-04-21 21:11:14 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
added = true;
|
|
|
|
}
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
handlingCustomFormats = !handlingCustomFormats;
|
|
|
|
} while (!handlingCustomFormats);
|
2013-03-11 19:49:48 +04:00
|
|
|
|
|
|
|
// only return the transferable if data was successfully added to it
|
|
|
|
if (added) {
|
|
|
|
return transferable.forget();
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
return nullptr;
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::ConvertFromVariant(nsIVariant* aVariant,
|
|
|
|
nsISupports** aSupports,
|
2016-04-22 16:53:20 +03:00
|
|
|
uint32_t* aLength) const
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aSupports = nullptr;
|
2008-08-27 16:07:27 +04:00
|
|
|
*aLength = 0;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t type;
|
2008-08-27 16:07:27 +04:00
|
|
|
aVariant->GetDataType(&type);
|
|
|
|
if (type == nsIDataType::VTYPE_INTERFACE ||
|
|
|
|
type == nsIDataType::VTYPE_INTERFACE_IS) {
|
2008-09-02 18:30:10 +04:00
|
|
|
nsCOMPtr<nsISupports> data;
|
2016-04-22 16:53:20 +03:00
|
|
|
if (NS_FAILED(aVariant->GetAsISupports(getter_AddRefs(data)))) {
|
2015-08-28 22:21:08 +03:00
|
|
|
return false;
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
2016-03-30 19:58:41 +03:00
|
|
|
|
2008-09-02 18:30:10 +04:00
|
|
|
nsCOMPtr<nsIFlavorDataProvider> fdp = do_QueryInterface(data);
|
|
|
|
if (fdp) {
|
|
|
|
// for flavour data providers, use kFlavorHasDataProvider (which has the
|
|
|
|
// value 0) as the length.
|
2015-03-31 17:03:49 +03:00
|
|
|
fdp.forget(aSupports);
|
2008-09-02 18:30:10 +04:00
|
|
|
*aLength = nsITransferable::kFlavorHasDataProvider;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// wrap the item in an nsISupportsInterfacePointer
|
|
|
|
nsCOMPtr<nsISupportsInterfacePointer> ptrSupports =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID);
|
2016-04-22 16:53:20 +03:00
|
|
|
if (!ptrSupports) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
2008-09-02 18:30:10 +04:00
|
|
|
|
|
|
|
ptrSupports->SetData(data);
|
2015-03-31 17:03:49 +03:00
|
|
|
ptrSupports.forget(aSupports);
|
2008-09-02 18:30:10 +04:00
|
|
|
|
|
|
|
*aLength = sizeof(nsISupportsInterfacePointer *);
|
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* chrs;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = 0;
|
2010-01-21 20:47:19 +03:00
|
|
|
nsresult rv = aVariant->GetAsWStringWithSize(&len, &chrs);
|
2016-04-22 16:53:20 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2010-01-21 20:47:19 +03:00
|
|
|
nsAutoString str;
|
|
|
|
str.Adopt(chrs, len);
|
|
|
|
|
2008-08-27 16:07:27 +04:00
|
|
|
nsCOMPtr<nsISupportsString>
|
|
|
|
strSupports(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
|
2016-04-22 16:53:20 +03:00
|
|
|
if (!strSupports) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
|
|
|
strSupports->SetData(str);
|
|
|
|
|
2015-03-31 17:03:49 +03:00
|
|
|
strSupports.forget(aSupports);
|
2008-08-27 16:07:27 +04:00
|
|
|
|
|
|
|
// each character is two bytes
|
|
|
|
*aLength = str.Length() << 1;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::ClearAll()
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
2015-08-28 22:21:08 +03:00
|
|
|
mItems->ClearAllItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
DataTransfer::MozItemCount() const
|
|
|
|
{
|
|
|
|
return mItems->MozItemCount();
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::SetDataWithPrincipal(const nsAString& aFormat,
|
|
|
|
nsIVariant* aData,
|
|
|
|
uint32_t aIndex,
|
|
|
|
nsIPrincipal* aPrincipal)
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
|
|
|
nsAutoString format;
|
|
|
|
GetRealFormat(aFormat, format);
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
RefPtr<DataTransferItem> item =
|
2016-05-31 22:03:44 +03:00
|
|
|
mItems->SetDataWithPrincipal(format, aData, aIndex, aPrincipal,
|
|
|
|
/* aInsertOnly = */ false,
|
|
|
|
/* aHidden= */ false,
|
|
|
|
rv);
|
2015-08-28 22:21:08 +03:00
|
|
|
return rv.StealNSResult();
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
void
|
|
|
|
DataTransfer::SetDataWithPrincipalFromOtherProcess(const nsAString& aFormat,
|
|
|
|
nsIVariant* aData,
|
|
|
|
uint32_t aIndex,
|
|
|
|
nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
if (aFormat.EqualsLiteral(kCustomTypesMime)) {
|
|
|
|
FillInExternalCustomTypes(aData, aIndex, aPrincipal);
|
|
|
|
} else {
|
|
|
|
SetDataWithPrincipal(aFormat, aData, aIndex, aPrincipal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-27 16:07:27 +04:00
|
|
|
void
|
2016-04-22 16:53:20 +03:00
|
|
|
DataTransfer::GetRealFormat(const nsAString& aInFormat,
|
|
|
|
nsAString& aOutFormat) const
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
|
|
|
// treat text/unicode as equivalent to text/plain
|
2011-11-08 15:42:53 +04:00
|
|
|
nsAutoString lowercaseFormat;
|
|
|
|
nsContentUtils::ASCIIToLower(aInFormat, lowercaseFormat);
|
2016-04-22 16:53:20 +03:00
|
|
|
if (lowercaseFormat.EqualsLiteral("text") ||
|
|
|
|
lowercaseFormat.EqualsLiteral("text/unicode")) {
|
2008-08-27 16:07:27 +04:00
|
|
|
aOutFormat.AssignLiteral("text/plain");
|
2016-04-22 16:53:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lowercaseFormat.EqualsLiteral("url")) {
|
2008-08-27 16:07:27 +04:00
|
|
|
aOutFormat.AssignLiteral("text/uri-list");
|
2016-04-22 16:53:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aOutFormat.Assign(lowercaseFormat);
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
|
2016-05-31 22:03:44 +03:00
|
|
|
nsresult
|
2016-04-22 16:53:20 +03:00
|
|
|
DataTransfer::CacheExternalData(const char* aFormat, uint32_t aIndex,
|
2016-05-31 22:03:44 +03:00
|
|
|
nsIPrincipal* aPrincipal, bool aHidden)
|
2013-03-11 19:49:48 +04:00
|
|
|
{
|
2016-05-31 22:03:44 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
RefPtr<DataTransferItem> item;
|
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
if (strcmp(aFormat, kUnicodeMime) == 0) {
|
2016-05-31 22:03:44 +03:00
|
|
|
item = mItems->SetDataWithPrincipal(NS_LITERAL_STRING("text/plain"), nullptr,
|
|
|
|
aIndex, aPrincipal, false, aHidden, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
2016-04-22 16:53:20 +03:00
|
|
|
|
|
|
|
if (strcmp(aFormat, kURLDataMime) == 0) {
|
2016-05-31 22:03:44 +03:00
|
|
|
item = mItems->SetDataWithPrincipal(NS_LITERAL_STRING("text/uri-list"), nullptr,
|
|
|
|
aIndex, aPrincipal, false, aHidden, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
|
|
|
|
2016-05-31 22:03:44 +03:00
|
|
|
nsAutoString format;
|
|
|
|
GetRealFormat(NS_ConvertUTF8toUTF16(aFormat), format);
|
|
|
|
item = mItems->SetDataWithPrincipal(format, nullptr, aIndex,
|
|
|
|
aPrincipal, false, aHidden, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
// there isn't a way to get a list of the formats that might be available on
|
|
|
|
// all platforms, so just check for the types that can actually be imported
|
|
|
|
// XXXndeakin there are some other formats but those are platform specific.
|
|
|
|
const char* kFormats[] = { kFileMime, kHTMLMime, kURLMime, kURLDataMime,
|
2016-05-05 17:07:24 +03:00
|
|
|
kUnicodeMime, kPNGImageMime, kJPEGImageMime,
|
|
|
|
kGIFImageMime };
|
2015-08-28 22:21:08 +03:00
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
void
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::CacheExternalDragFormats()
|
2008-08-27 16:07:27 +04:00
|
|
|
{
|
|
|
|
// Called during the constructor to cache the formats available from an
|
|
|
|
// external drag. The data associated with each format will be set to null.
|
|
|
|
// This data will instead only be retrieved in FillInExternalDragData when
|
|
|
|
// asked for, as it may be time consuming for the source application to
|
|
|
|
// generate it.
|
|
|
|
|
2010-03-02 17:21:20 +03:00
|
|
|
nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession();
|
2016-04-22 16:53:20 +03:00
|
|
|
if (!dragSession) {
|
2008-08-27 16:07:27 +04:00
|
|
|
return;
|
2016-04-22 16:53:20 +03:00
|
|
|
}
|
2008-08-27 16:07:27 +04:00
|
|
|
|
|
|
|
// make sure that the system principal is used for external drags
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
nsCOMPtr<nsIPrincipal> sysPrincipal;
|
|
|
|
ssm->GetSystemPrincipal(getter_AddRefs(sysPrincipal));
|
|
|
|
|
|
|
|
// there isn't a way to get a list of the formats that might be available on
|
|
|
|
// all platforms, so just check for the types that can actually be imported
|
|
|
|
// XXXndeakin there are some other formats but those are platform specific.
|
2016-04-21 21:11:14 +03:00
|
|
|
const char* formats[] = { kFileMime, kHTMLMime, kRTFMime,
|
|
|
|
kURLMime, kURLDataMime, kUnicodeMime };
|
2008-08-27 16:07:27 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2008-08-27 16:07:27 +04:00
|
|
|
dragSession->GetNumDropItems(&count);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t c = 0; c < count; c++) {
|
2016-05-31 22:03:44 +03:00
|
|
|
bool hasFileData = false;
|
|
|
|
dragSession->IsDataFlavorSupported(kFileMime, &hasFileData);
|
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
// First, check for the special format that holds custom types.
|
|
|
|
bool supported;
|
|
|
|
dragSession->IsDataFlavorSupported(kCustomTypesMime, &supported);
|
|
|
|
if (supported) {
|
|
|
|
FillInExternalCustomTypes(c, sysPrincipal);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t f = 0; f < ArrayLength(formats); f++) {
|
2008-08-27 16:07:27 +04:00
|
|
|
// IsDataFlavorSupported doesn't take an index as an argument and just
|
|
|
|
// checks if any of the items support a particular flavor, even though
|
|
|
|
// the GetData method does take an index. Here, we just assume that
|
|
|
|
// every item being dragged has the same set of flavors.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool supported;
|
2015-08-28 22:21:08 +03:00
|
|
|
dragSession->IsDataFlavorSupported(kFormats[f], &supported);
|
2008-08-27 16:07:27 +04:00
|
|
|
// if the format is supported, add an item to the array with null as
|
|
|
|
// the data. When retrieved, GetRealData will read the data.
|
|
|
|
if (supported) {
|
2016-05-31 22:03:44 +03:00
|
|
|
CacheExternalData(kFormats[f], c, sysPrincipal, /* hidden = */ f && hasFileData);
|
2008-08-27 16:07:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-12 18:45:33 +04:00
|
|
|
DataTransfer::CacheExternalClipboardFormats()
|
2013-03-11 19:49:48 +04:00
|
|
|
{
|
2015-09-08 17:33:35 +03:00
|
|
|
NS_ASSERTION(mEventMessage == ePaste,
|
2015-09-02 09:08:01 +03:00
|
|
|
"caching clipboard data for invalid event");
|
2013-03-11 19:49:48 +04:00
|
|
|
|
|
|
|
// Called during the constructor for paste events to cache the formats
|
|
|
|
// available on the clipboard. As with CacheExternalDragFormats, the
|
|
|
|
// data will only be retrieved when needed.
|
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard =
|
|
|
|
do_GetService("@mozilla.org/widget/clipboard;1");
|
2013-08-07 22:52:38 +04:00
|
|
|
if (!clipboard || mClipboardType < 0) {
|
2013-03-11 19:49:48 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
nsCOMPtr<nsIPrincipal> sysPrincipal;
|
|
|
|
ssm->GetSystemPrincipal(getter_AddRefs(sysPrincipal));
|
|
|
|
|
2016-05-31 22:03:44 +03:00
|
|
|
// Check if the clipboard has any files
|
|
|
|
bool hasFileData = false;
|
|
|
|
const char *fileMime[] = { kFileMime };
|
|
|
|
clipboard->HasDataMatchingFlavors(fileMime, 1, mClipboardType, &hasFileData);
|
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
// there isn't a way to get a list of the formats that might be available on
|
2016-04-21 21:11:14 +03:00
|
|
|
// all platforms, so just check for the types that can actually be imported.
|
|
|
|
// Note that the loop below assumes that kCustomTypesMime will be first.
|
|
|
|
const char* formats[] = { kCustomTypesMime, kFileMime, kHTMLMime, kRTFMime,
|
2016-05-05 17:07:24 +03:00
|
|
|
kURLMime, kURLDataMime, kUnicodeMime, kPNGImageMime,
|
|
|
|
kJPEGImageMime, kGIFImageMime };
|
2013-03-11 19:49:48 +04:00
|
|
|
|
|
|
|
for (uint32_t f = 0; f < mozilla::ArrayLength(formats); ++f) {
|
|
|
|
// check each format one at a time
|
|
|
|
bool supported;
|
2016-04-22 16:53:20 +03:00
|
|
|
clipboard->HasDataMatchingFlavors(&(formats[f]), 1, mClipboardType,
|
|
|
|
&supported);
|
2013-03-11 19:49:48 +04:00
|
|
|
// if the format is supported, add an item to the array with null as
|
|
|
|
// the data. When retrieved, GetRealData will read the data.
|
|
|
|
if (supported) {
|
2016-04-21 21:11:14 +03:00
|
|
|
if (f == 0) {
|
|
|
|
FillInExternalCustomTypes(0, sysPrincipal);
|
|
|
|
} else {
|
2016-05-31 22:03:44 +03:00
|
|
|
// If we aren't the file data, and we have file data, we want to be hidden
|
|
|
|
CacheExternalData(formats[f], 0, sysPrincipal, /* hidden = */ f != 1 && hasFileData);
|
2016-04-21 21:11:14 +03:00
|
|
|
}
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 21:48:11 +03:00
|
|
|
void
|
|
|
|
DataTransfer::FillAllExternalData()
|
|
|
|
{
|
|
|
|
if (mIsExternal) {
|
2015-08-28 22:21:08 +03:00
|
|
|
for (uint32_t i = 0; i < MozItemCount(); ++i) {
|
|
|
|
const nsTArray<RefPtr<DataTransferItem>>& items = *mItems->MozItemsAt(i);
|
|
|
|
for (uint32_t j = 0; j < items.Length(); ++j) {
|
|
|
|
MOZ_ASSERT(items[j]->Index() == i);
|
|
|
|
|
|
|
|
items[j]->FillInExternalData();
|
2015-04-08 21:48:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:11:14 +03:00
|
|
|
void
|
2016-04-22 16:53:20 +03:00
|
|
|
DataTransfer::FillInExternalCustomTypes(uint32_t aIndex,
|
|
|
|
nsIPrincipal* aPrincipal)
|
2016-04-21 21:11:14 +03:00
|
|
|
{
|
2015-08-28 22:21:08 +03:00
|
|
|
RefPtr<DataTransferItem> item = new DataTransferItem(mItems,
|
|
|
|
NS_LITERAL_STRING(kCustomTypesMime));
|
|
|
|
item->SetKind(DataTransferItem::KIND_STRING);
|
|
|
|
item->SetIndex(aIndex);
|
2016-04-21 21:11:14 +03:00
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
if (!item->Data()) {
|
2016-04-21 21:11:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-28 22:21:08 +03:00
|
|
|
FillInExternalCustomTypes(item->Data(), aIndex, aPrincipal);
|
2016-04-21 21:11:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-22 16:53:20 +03:00
|
|
|
DataTransfer::FillInExternalCustomTypes(nsIVariant* aData, uint32_t aIndex,
|
|
|
|
nsIPrincipal* aPrincipal)
|
2016-04-21 21:11:14 +03:00
|
|
|
{
|
|
|
|
char* chrs;
|
|
|
|
uint32_t len = 0;
|
|
|
|
nsresult rv = aData->GetAsStringWithSize(&len, &chrs);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString str;
|
|
|
|
str.Adopt(chrs, len);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stringStream;
|
|
|
|
NS_NewCStringInputStream(getter_AddRefs(stringStream), str);
|
|
|
|
|
2016-04-22 16:53:20 +03:00
|
|
|
nsCOMPtr<nsIBinaryInputStream> stream =
|
|
|
|
do_CreateInstance("@mozilla.org/binaryinputstream;1");
|
2016-04-21 21:11:14 +03:00
|
|
|
stream->SetInputStream(stringStream);
|
|
|
|
if (!stream) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t type;
|
|
|
|
do {
|
|
|
|
stream->Read32(&type);
|
|
|
|
if (type == eCustomClipboardTypeId_String) {
|
|
|
|
uint32_t formatLength;
|
|
|
|
stream->Read32(&formatLength);
|
|
|
|
char* formatBytes;
|
|
|
|
stream->ReadBytes(formatLength, &formatBytes);
|
|
|
|
nsAutoString format;
|
2016-04-22 16:53:20 +03:00
|
|
|
format.Adopt(reinterpret_cast<char16_t*>(formatBytes),
|
|
|
|
formatLength / sizeof(char16_t));
|
2016-04-21 21:11:14 +03:00
|
|
|
|
|
|
|
uint32_t dataLength;
|
|
|
|
stream->Read32(&dataLength);
|
|
|
|
char* dataBytes;
|
|
|
|
stream->ReadBytes(dataLength, &dataBytes);
|
|
|
|
nsAutoString data;
|
2016-04-22 16:53:20 +03:00
|
|
|
data.Adopt(reinterpret_cast<char16_t*>(dataBytes),
|
|
|
|
dataLength / sizeof(char16_t));
|
2016-04-21 21:11:14 +03:00
|
|
|
|
|
|
|
RefPtr<nsVariantCC> variant = new nsVariantCC();
|
|
|
|
variant->SetAsAString(data);
|
|
|
|
|
|
|
|
SetDataWithPrincipal(format, variant, aIndex, aPrincipal);
|
|
|
|
}
|
|
|
|
} while (type != eCustomClipboardTypeId_None);
|
|
|
|
}
|
|
|
|
|
2013-08-12 18:45:33 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|