1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
|
/*
* Copyright (C) 2006-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "DataTransfer.h"
#include "CachedImage.h"
#include "CachedImageClient.h"
#include "CommonAtomStrings.h"
#include "DataTransferItem.h"
#include "DataTransferItemList.h"
#include "DeprecatedGlobalSettings.h"
#include "DocumentFragment.h"
#include "DocumentInlines.h"
#include "DragData.h"
#include "Editor.h"
#include "FileList.h"
#include "FrameDestructionObserverInlines.h"
#include "FrameLoader.h"
#include "HTMLImageElement.h"
#include "HostWindow.h"
#include "Image.h"
#include "LocalFrame.h"
#include "Page.h"
#include "PagePasteboardContext.h"
#include "Pasteboard.h"
#include "Quirks.h"
#include "Settings.h"
#include "StaticPasteboard.h"
#include "WebContentReader.h"
#include "WebCorePasteboardFileReader.h"
#include "markup.h"
#include <wtf/TZoneMallocInlines.h>
#include <wtf/URLParser.h>
#include <wtf/unicode/CharacterNames.h>
namespace WebCore {
#if ENABLE(DRAG_SUPPORT)
class DragImageLoader final : public CachedImageClient {
WTF_MAKE_TZONE_ALLOCATED(DragImageLoader);
WTF_OVERRIDE_DELETE_FOR_CHECKED_PTR(DragImageLoader);
WTF_MAKE_NONCOPYABLE(DragImageLoader);
public:
explicit DragImageLoader(DataTransfer&, const Document&);
void startLoading(CachedResourceHandle<CachedImage>&);
void stopLoading(CachedResourceHandle<CachedImage>&);
void moveToDataTransfer(DataTransfer&);
private:
void imageChanged(CachedImage*, const IntRect*) override;
WeakRef<DataTransfer> m_dataTransfer;
WeakPtr<Document, WeakPtrImplWithEventTargetData> m_document;
};
WTF_MAKE_TZONE_ALLOCATED_IMPL(DragImageLoader);
#endif
DataTransfer::DataTransfer(StoreMode mode, std::unique_ptr<Pasteboard> pasteboard, Type type, String&& effectAllowed)
: m_storeMode(mode)
, m_pasteboard(WTFMove(pasteboard))
#if ENABLE(DRAG_SUPPORT)
, m_type(type)
, m_dropEffect("uninitialized"_s)
, m_effectAllowed(WTFMove(effectAllowed))
, m_shouldUpdateDragImage(false)
#endif
{
#if !ENABLE(DRAG_SUPPORT)
UNUSED_PARAM(effectAllowed);
ASSERT_UNUSED(type, type != Type::DragAndDropData && type != Type::DragAndDropFiles);
#endif
}
Ref<DataTransfer> DataTransfer::createForCopyAndPaste(const Document& document, StoreMode storeMode, std::unique_ptr<Pasteboard>&& pasteboard)
{
auto dataTransfer = adoptRef(*new DataTransfer(storeMode, WTFMove(pasteboard)));
dataTransfer->m_originIdentifier = document.originIdentifierForPasteboard();
return dataTransfer;
}
Ref<DataTransfer> DataTransfer::create()
{
return adoptRef(*new DataTransfer(StoreMode::ReadWrite, makeUnique<StaticPasteboard>(), Type::CopyAndPaste, "none"_s));
}
DataTransfer::~DataTransfer()
{
#if ENABLE(DRAG_SUPPORT)
if (m_dragImageLoader && m_dragImage)
m_dragImageLoader->stopLoading(m_dragImage);
#endif
}
bool DataTransfer::canReadTypes() const
{
return m_storeMode == StoreMode::Readonly || m_storeMode == StoreMode::Protected || m_storeMode == StoreMode::ReadWrite;
}
bool DataTransfer::canReadData() const
{
return m_storeMode == StoreMode::Readonly || m_storeMode == StoreMode::ReadWrite;
}
bool DataTransfer::canWriteData() const
{
return m_storeMode == StoreMode::ReadWrite;
}
static String normalizeType(const String& type)
{
if (type.isNull())
return type;
auto lowercaseType = type.trim(isASCIIWhitespace).convertToASCIILowercase();
if (lowercaseType == "text"_s || lowercaseType.startsWith(textPlainContentTypeAtom()))
return textPlainContentTypeAtom();
if (lowercaseType == "url"_s || lowercaseType.startsWith("text/uri-list;"_s))
return "text/uri-list"_s;
if (lowercaseType.startsWith("text/html;"_s))
return textHTMLContentTypeAtom();
return lowercaseType;
}
void DataTransfer::clearData(const String& type)
{
if (!canWriteData())
return;
String normalizedType = normalizeType(type);
if (normalizedType.isNull())
m_pasteboard->clear();
else
m_pasteboard->clear(normalizedType);
if (m_itemList)
m_itemList->didClearStringData(normalizedType);
}
static String readURLsFromPasteboardAsString(Page* page, Pasteboard& pasteboard, Function<bool(const String&)>&& shouldIncludeURL)
{
StringBuilder urlList;
auto urlStrings = pasteboard.readAllStrings("text/uri-list"_s);
if (page) {
urlStrings = urlStrings.map([&](auto& string) {
return page->applyLinkDecorationFiltering(string, LinkDecorationFilteringTrigger::Paste);
});
}
urlStrings.removeAllMatching([&](auto& string) {
return !shouldIncludeURL(string);
});
return makeStringByJoining(urlStrings, "\n"_s);
}
String DataTransfer::getDataForItem(Document& document, const String& type) const
{
if (!canReadData())
return { };
auto lowercaseType = type.trim(isASCIIWhitespace).convertToASCIILowercase();
if (shouldSuppressGetAndSetDataToAvoidExposingFilePaths()) {
if (lowercaseType == "text/uri-list"_s) {
return readURLsFromPasteboardAsString(document.page(), *m_pasteboard, [] (auto& urlString) {
return Pasteboard::canExposeURLToDOMWhenPasteboardContainsFiles(urlString);
});
}
if (lowercaseType == textHTMLContentTypeAtom() && DeprecatedGlobalSettings::customPasteboardDataEnabled()) {
// If the pasteboard contains files and the page requests 'text/html', we only read from rich text types to prevent file
// paths from leaking (e.g. from plain text data on the pasteboard) since we sanitize cross-origin markup. However, if
// custom pasteboard data is disabled, then we can't ensure that the markup we deliver is sanitized, so we fall back to
// current behavior and return an empty string.
return readStringFromPasteboard(document, lowercaseType, WebContentReadingPolicy::OnlyRichTextTypes);
}
return { };
}
return readStringFromPasteboard(document, lowercaseType, WebContentReadingPolicy::AnyType);
}
String DataTransfer::readStringFromPasteboard(Document& document, const String& lowercaseType, WebContentReadingPolicy policy) const
{
if (!DeprecatedGlobalSettings::customPasteboardDataEnabled())
return m_pasteboard->readString(lowercaseType);
// StaticPasteboard is only used to stage data written by websites before being committed to the system pasteboard.
bool isSameOrigin = is<StaticPasteboard>(*m_pasteboard) || (!m_originIdentifier.isNull() && m_originIdentifier == m_pasteboard->readOrigin());
if (isSameOrigin) {
String value = m_pasteboard->readStringInCustomData(lowercaseType);
if (!value.isNull())
return value;
}
if (!Pasteboard::isSafeTypeForDOMToReadAndWrite(lowercaseType))
return { };
if (!is<StaticPasteboard>(*m_pasteboard) && lowercaseType == textHTMLContentTypeAtom()) {
if (!document.frame())
return { };
WebContentMarkupReader reader { document.protectedFrame().releaseNonNull() };
m_pasteboard->read(reader, policy);
return reader.takeMarkup();
}
if (!is<StaticPasteboard>(*m_pasteboard) && lowercaseType == "text/uri-list"_s) {
return readURLsFromPasteboardAsString(document.protectedPage().get(), *m_pasteboard, [] (auto&) {
return true;
});
}
auto string = m_pasteboard->readString(lowercaseType);
if (RefPtr page = document.page())
return page->applyLinkDecorationFiltering(string, LinkDecorationFilteringTrigger::Paste);
return string;
}
String DataTransfer::getData(Document& document, const String& type) const
{
return getDataForItem(document, normalizeType(type));
}
bool DataTransfer::shouldSuppressGetAndSetDataToAvoidExposingFilePaths() const
{
if (!forFileDrag() && !DeprecatedGlobalSettings::customPasteboardDataEnabled())
return false;
return m_pasteboard->fileContentState() == Pasteboard::FileContentState::MayContainFilePaths;
}
void DataTransfer::setData(Document& document, const String& type, const String& data)
{
if (!canWriteData())
return;
if (shouldSuppressGetAndSetDataToAvoidExposingFilePaths())
return;
auto normalizedType = normalizeType(type);
setDataFromItemList(document, normalizedType, data);
if (m_itemList)
m_itemList->didSetStringData(normalizedType);
}
void DataTransfer::setDataFromItemList(Document& document, const String& type, const String& data)
{
ASSERT(canWriteData());
auto& pasteboard = downcast<StaticPasteboard>(*m_pasteboard);
if (!DeprecatedGlobalSettings::customPasteboardDataEnabled()) {
pasteboard.writeString(type, data);
return;
}
String sanitizedData;
if (type == "text/html"_s)
sanitizedData = sanitizeMarkup(data);
else if (type == "text/uri-list"_s) {
auto url = URL({ }, data);
if (url.isValid())
sanitizedData = url.string();
} else if (type == textPlainContentTypeAtom())
sanitizedData = data; // Nothing to sanitize.
if (type == "text/uri-list"_s || type == textPlainContentTypeAtom()) {
if (RefPtr page = document.page())
sanitizedData = page->applyLinkDecorationFiltering(sanitizedData, LinkDecorationFilteringTrigger::Copy);
}
if (sanitizedData != data)
pasteboard.writeStringInCustomData(type, data);
if (Pasteboard::isSafeTypeForDOMToReadAndWrite(type) && !sanitizedData.isNull())
pasteboard.writeString(type, sanitizedData);
}
void DataTransfer::updateFileList(ScriptExecutionContext* context)
{
ASSERT(canWriteData());
m_fileList->m_files = filesFromPasteboardAndItemList(context);
}
void DataTransfer::didAddFileToItemList()
{
ASSERT(canWriteData());
if (!m_fileList)
return;
auto& newItem = m_itemList->items().last();
ASSERT(newItem->isFile());
m_fileList->append(*newItem->file());
}
DataTransferItemList& DataTransfer::items(Document& document)
{
if (!m_itemList)
m_itemList = makeUniqueWithoutRefCountedCheck<DataTransferItemList>(document, *this);
return *m_itemList;
}
Vector<String> DataTransfer::types(Document& document) const
{
return types(document, AddFilesType::Yes);
}
Vector<String> DataTransfer::typesForItemList(Document& document) const
{
return types(document, AddFilesType::No);
}
Vector<String> DataTransfer::types(Document& document, AddFilesType addFilesType) const
{
if (!canReadTypes())
return { };
bool hideFilesType = !canWriteData() && !allowsFileAccess();
if (!DeprecatedGlobalSettings::customPasteboardDataEnabled()) {
auto types = m_pasteboard->typesForLegacyUnsafeBindings();
ASSERT(!types.contains("Files"_s));
if (!hideFilesType && m_pasteboard->fileContentState() != Pasteboard::FileContentState::NoFileOrImageData && addFilesType == AddFilesType::Yes)
types.append("Files"_s);
return types;
}
auto safeTypes = m_pasteboard->typesSafeForBindings(m_originIdentifier);
bool hasFileBackedItem = m_itemList && m_itemList->hasItems() && notFound != m_itemList->items().findIf([] (const auto& item) {
return item->isFile();
});
auto fileContentState = m_pasteboard->fileContentState();
if (hasFileBackedItem || fileContentState != Pasteboard::FileContentState::NoFileOrImageData) {
Vector<String> types;
if (!hideFilesType && addFilesType == AddFilesType::Yes) {
types.append("Files"_s);
if (document.quirks().needsMozillaFileTypeForDataTransfer())
types.append("application/x-moz-file"_s);
}
if (fileContentState != Pasteboard::FileContentState::MayContainFilePaths) {
types.appendVector(WTFMove(safeTypes));
return types;
}
if (safeTypes.contains("text/uri-list"_s))
types.append("text/uri-list"_s);
if (safeTypes.contains(textHTMLContentTypeAtom()) && DeprecatedGlobalSettings::customPasteboardDataEnabled())
types.append(textHTMLContentTypeAtom());
return types;
}
ASSERT(!safeTypes.contains("Files"_s));
return safeTypes;
}
Vector<Ref<File>> DataTransfer::filesFromPasteboardAndItemList(ScriptExecutionContext* context) const
{
bool addedFilesFromPasteboard = false;
Vector<Ref<File>> files;
if (allowsFileAccess() && m_pasteboard->fileContentState() != Pasteboard::FileContentState::NoFileOrImageData) {
WebCorePasteboardFileReader reader(context);
m_pasteboard->read(reader);
files = WTFMove(reader.files);
addedFilesFromPasteboard = !files.isEmpty();
}
bool itemListContainsItems = false;
if (m_itemList && m_itemList->hasItems()) {
for (auto& item : m_itemList->items()) {
if (auto file = item->file())
files.append(file.releaseNonNull());
}
itemListContainsItems = true;
}
bool containsItemsAndFiles = itemListContainsItems && addedFilesFromPasteboard;
ASSERT_UNUSED(containsItemsAndFiles, !containsItemsAndFiles);
return files;
}
FileList& DataTransfer::files(Document* document) const
{
if (!canReadData()) {
if (m_fileList)
m_fileList->clear();
else
m_fileList = FileList::create();
return *m_fileList;
}
if (!m_fileList)
m_fileList = FileList::create(filesFromPasteboardAndItemList(document));
return *m_fileList;
}
FileList& DataTransfer::files(Document& document) const
{
return files(&document);
}
struct PasteboardFileTypeReader final : PasteboardFileReader {
void readFilename(const String& filename)
{
types.add(File::contentTypeForFile(filename));
}
void readBuffer(const String&, const String& type, Ref<SharedBuffer>&&)
{
types.add(type);
}
UncheckedKeyHashSet<String, ASCIICaseInsensitiveHash> types;
};
bool DataTransfer::hasFileOfType(const String& type)
{
ASSERT_WITH_SECURITY_IMPLICATION(canReadTypes());
PasteboardFileTypeReader reader;
m_pasteboard->read(reader);
return reader.types.contains(type);
}
bool DataTransfer::hasStringOfType(Document& document, const String& type)
{
ASSERT_WITH_SECURITY_IMPLICATION(canReadTypes());
return !type.isNull() && types(document).contains(type);
}
Ref<DataTransfer> DataTransfer::createForInputEvent(const String& plainText, const String& htmlText)
{
auto pasteboard = makeUnique<StaticPasteboard>();
pasteboard->writeString(textPlainContentTypeAtom(), plainText);
pasteboard->writeString(textHTMLContentTypeAtom(), htmlText);
return adoptRef(*new DataTransfer(StoreMode::Readonly, WTFMove(pasteboard), Type::InputEvent));
}
void DataTransfer::commitToPasteboard(Pasteboard& nativePasteboard)
{
ASSERT(!is<StaticPasteboard>(nativePasteboard));
auto& staticPasteboard = downcast<StaticPasteboard>(*m_pasteboard);
if (!staticPasteboard.hasNonDefaultData()) {
// We clear the platform pasteboard here to ensure that the pasteboard doesn't contain any data
// that may have been written before starting the drag or copying, and after ending the last
// drag session or paste. After pushing the static pasteboard's contents to the platform, the
// pasteboard should only contain data that was in the static pasteboard.
nativePasteboard.clear();
return;
}
auto customData = staticPasteboard.takeCustomData();
if (DeprecatedGlobalSettings::customPasteboardDataEnabled()) {
customData.setOrigin(m_originIdentifier);
nativePasteboard.writeCustomData({ customData });
return;
}
nativePasteboard.clear();
customData.forEachPlatformString([&] (auto& type, auto& string) {
nativePasteboard.writeString(type, string);
});
customData.forEachCustomString([&] (auto& type, auto& string) {
nativePasteboard.writeString(type, string);
});
}
#if !ENABLE(DRAG_SUPPORT)
String DataTransfer::dropEffect() const
{
return noneAtom();
}
void DataTransfer::setDropEffect(const String&)
{
}
String DataTransfer::effectAllowed() const
{
return "uninitialized"_s;
}
void DataTransfer::setEffectAllowed(const String&)
{
}
void DataTransfer::setDragImage(Ref<Element>&&, int, int)
{
}
#else
Ref<DataTransfer> DataTransfer::createForDrag(const Document& document)
{
return adoptRef(*new DataTransfer(StoreMode::ReadWrite, Pasteboard::createForDragAndDrop(PagePasteboardContext::create(document.pageID())), Type::DragAndDropData));
}
Ref<DataTransfer> DataTransfer::createForDragStartEvent(const Document& document)
{
auto dataTransfer = adoptRef(*new DataTransfer(StoreMode::ReadWrite, makeUnique<StaticPasteboard>(), Type::DragAndDropData));
dataTransfer->m_originIdentifier = document.originIdentifierForPasteboard();
return dataTransfer;
}
Ref<DataTransfer> DataTransfer::createForDrop(const Document& document, std::unique_ptr<Pasteboard>&& pasteboard, OptionSet<DragOperation> sourceOperationMask, bool draggingFiles)
{
auto dataTransfer = adoptRef(*new DataTransfer(DataTransfer::StoreMode::Readonly, WTFMove(pasteboard), draggingFiles ? Type::DragAndDropFiles : Type::DragAndDropData));
dataTransfer->setSourceOperationMask(sourceOperationMask);
dataTransfer->m_originIdentifier = document.originIdentifierForPasteboard();
return dataTransfer;
}
Ref<DataTransfer> DataTransfer::createForUpdatingDropTarget(const Document& document, std::unique_ptr<Pasteboard>&& pasteboard, OptionSet<DragOperation> sourceOperationMask, bool draggingFiles)
{
auto dataTransfer = adoptRef(*new DataTransfer(DataTransfer::StoreMode::Protected, WTFMove(pasteboard), draggingFiles ? Type::DragAndDropFiles : Type::DragAndDropData));
dataTransfer->setSourceOperationMask(sourceOperationMask);
dataTransfer->m_originIdentifier = document.originIdentifierForPasteboard();
return dataTransfer;
}
void DataTransfer::setDragImage(Ref<Element>&& element, int x, int y)
{
if (!forDrag() || !canWriteData())
return;
CachedResourceHandle<CachedImage> image;
if (auto* imageElement = dynamicDowncast<HTMLImageElement>(element.get()); imageElement && !imageElement->isConnected())
image = imageElement->cachedImage();
m_dragLocation = IntPoint(x, y);
Ref document = element->protectedDocument();
if (m_dragImageLoader && m_dragImage)
m_dragImageLoader->stopLoading(m_dragImage);
m_dragImage = image;
if (m_dragImage) {
if (!m_dragImageLoader)
m_dragImageLoader = makeUnique<DragImageLoader>(*this, document);
m_dragImageLoader->startLoading(m_dragImage);
}
if (image)
m_dragImageElement = nullptr;
else
m_dragImageElement = WTFMove(element);
updateDragImage(document.ptr());
}
void DataTransfer::updateDragImage(const Document* document)
{
// Don't allow setting the image if we haven't started dragging yet; we'll rely on the dragging code
// to install this drag image as part of getting the drag kicked off.
if (!m_shouldUpdateDragImage)
return;
IntPoint computedHotSpot;
auto computedImage = DragImage { createDragImage(document, computedHotSpot) };
if (!computedImage)
return;
m_pasteboard->setDragImage(WTFMove(computedImage), computedHotSpot);
}
RefPtr<Element> DataTransfer::dragImageElement() const
{
return m_dragImageElement;
}
#if !PLATFORM(MAC)
DragImageRef DataTransfer::createDragImage(const Document* document, IntPoint& location) const
{
location = m_dragLocation;
if (m_dragImage) {
HostWindow* hostWindow = document && document->view() ? document->view()->hostWindow() : nullptr;
auto deviceScaleFactor = document ? document->deviceScaleFactor() : 1.f;
return createDragImageFromImage(m_dragImage->protectedImage().get(), ImageOrientation::Orientation::None, hostWindow, deviceScaleFactor);
}
if (m_dragImageElement) {
if (RefPtr frame = m_dragImageElement->document().frame())
return createDragImageForNode(*frame, dragImageElement().releaseNonNull());
}
// We do not have enough information to create a drag image, use the default icon.
return nullptr;
}
#endif
DragImageLoader::DragImageLoader(DataTransfer& dataTransfer, const Document& document)
: m_dataTransfer(dataTransfer)
, m_document { document }
{
}
void DragImageLoader::moveToDataTransfer(DataTransfer& newDataTransfer)
{
m_dataTransfer = newDataTransfer;
}
void DragImageLoader::startLoading(CachedResourceHandle<WebCore::CachedImage>& image)
{
// FIXME: Does this really trigger a load? Does it need to?
image->addClient(*this);
}
void DragImageLoader::stopLoading(CachedResourceHandle<WebCore::CachedImage>& image)
{
image->removeClient(*this);
}
void DragImageLoader::imageChanged(CachedImage*, const IntRect*)
{
RefPtr document = m_document.get();
m_dataTransfer->updateDragImage(document.get());
}
static OptionSet<DragOperation> dragOpFromIEOp(const String& operation)
{
if (operation == "uninitialized"_s)
return anyDragOperation();
if (operation == "none"_s)
return { };
if (operation == "copy"_s)
return { DragOperation::Copy };
if (operation == "link"_s)
return { DragOperation::Link };
if (operation == "move"_s)
return { DragOperation::Generic, DragOperation::Move };
if (operation == "copyLink"_s)
return { DragOperation::Copy, DragOperation::Link };
if (operation == "copyMove"_s)
return { DragOperation::Copy, DragOperation::Generic, DragOperation::Move };
if (operation == "linkMove"_s)
return { DragOperation::Link, DragOperation::Generic, DragOperation::Move };
if (operation == "all"_s)
return anyDragOperation();
return { DragOperation::Private }; // Really a marker for "no conversion".
}
static ASCIILiteral IEOpFromDragOp(OptionSet<DragOperation> operationMask)
{
bool isGenericMove = operationMask.containsAny({ DragOperation::Generic, DragOperation::Move });
if ((isGenericMove && operationMask.containsAll({ DragOperation::Copy, DragOperation::Link })) || operationMask.containsAll({ DragOperation::Copy, DragOperation::Link, DragOperation::Generic, DragOperation::Private, DragOperation::Move, DragOperation::Delete }))
return "all"_s;
if (isGenericMove && operationMask.contains(DragOperation::Copy))
return "copyMove"_s;
if (isGenericMove && operationMask.contains(DragOperation::Link))
return "linkMove"_s;
if (operationMask.containsAll({ DragOperation::Copy, DragOperation::Link }))
return "copyLink"_s;
if (isGenericMove)
return "move"_s;
if (operationMask.contains(DragOperation::Copy))
return "copy"_s;
if (operationMask.contains(DragOperation::Link))
return "link"_s;
return "none"_s;
}
OptionSet<DragOperation> DataTransfer::sourceOperationMask() const
{
auto operationMask = dragOpFromIEOp(m_effectAllowed);
ASSERT(operationMask != DragOperation::Private);
return operationMask;
}
OptionSet<DragOperation> DataTransfer::destinationOperationMask() const
{
auto operationMask = dragOpFromIEOp(m_dropEffect);
ASSERT(operationMask == DragOperation::Copy || operationMask.isEmpty() || operationMask == DragOperation::Link || operationMask == OptionSet<DragOperation>({ DragOperation::Generic, DragOperation::Move }) || operationMask.containsAll({ DragOperation::Copy, DragOperation::Link, DragOperation::Generic, DragOperation::Private, DragOperation::Move, DragOperation::Delete }));
return operationMask;
}
void DataTransfer::setSourceOperationMask(OptionSet<DragOperation> operationMask)
{
ASSERT_ARG(operationMask, operationMask != DragOperation::Private);
m_effectAllowed = IEOpFromDragOp(operationMask);
}
void DataTransfer::setDestinationOperationMask(OptionSet<DragOperation> operationMask)
{
ASSERT_ARG(operationMask, operationMask == DragOperation::Copy || operationMask.isEmpty() || operationMask == DragOperation::Link || operationMask == DragOperation::Generic || operationMask == DragOperation::Move || operationMask == OptionSet<DragOperation>({ DragOperation::Generic, DragOperation::Move }));
m_dropEffect = IEOpFromDragOp(operationMask);
}
String DataTransfer::dropEffect() const
{
return m_dropEffect == "uninitialized"_s ? "none"_s : m_dropEffect;
}
void DataTransfer::setDropEffect(const String& effect)
{
if (!forDrag())
return;
if (effect != "none"_s && effect != "copy"_s && effect != "link"_s && effect != "move"_s)
return;
// FIXME: The spec allows this in all circumstances. There is probably no value
// in ignoring attempts to change it.
if (!canReadTypes())
return;
m_dropEffect = effect;
}
String DataTransfer::effectAllowed() const
{
return m_effectAllowed;
}
void DataTransfer::setEffectAllowed(const String& effect)
{
if (!forDrag())
return;
// Ignore any attempts to set it to an unknown value.
if (dragOpFromIEOp(effect) == DragOperation::Private)
return;
if (!canWriteData())
return;
m_effectAllowed = effect;
}
void DataTransfer::moveDragState(Ref<DataTransfer>&& other)
{
RELEASE_ASSERT(is<StaticPasteboard>(other->pasteboard()));
other->commitToPasteboard(*m_pasteboard);
m_dropEffect = other->m_dropEffect;
m_effectAllowed = other->m_effectAllowed;
m_dragLocation = other->m_dragLocation;
m_dragImage = other->m_dragImage;
m_dragImageElement = WTFMove(other->m_dragImageElement);
m_dragImageLoader = WTFMove(other->m_dragImageLoader);
if (m_dragImageLoader)
m_dragImageLoader->moveToDataTransfer(*this);
m_fileList = WTFMove(other->m_fileList);
}
bool DataTransfer::hasDragImage() const
{
return m_dragImage || m_dragImageElement;
}
#endif // ENABLE(DRAG_SUPPORT)
} // namespace WebCore
|