/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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/. */
static already_AddRefed<Element> MakeAnonButton(
Document* aDoc, constchar* labelKey, HTMLInputElement* aInputElement) {
RefPtr<Element> button = aDoc->CreateHTMLElement(nsGkAtoms::button); // NOTE: SetIsNativeAnonymousRoot() has to be called before setting any // attribute.
button->SetIsNativeAnonymousRoot();
button->SetPseudoElementType(PseudoStyleType::fileSelectorButton);
// Set the file picking button text depending on the current locale.
nsAutoString buttonTxt;
nsContentUtils::GetMaybeLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
labelKey, aDoc, buttonTxt);
auto* nim = aDoc->NodeInfoManager(); // Set the browse button text. It's a bit of a pain to do because we want to // make sure we are not notifying.
RefPtr textContent = new (nim) nsTextNode(nim);
textContent->SetText(buttonTxt, false);
auto* buttonElement = HTMLButtonElement::FromNode(button); // We allow tabbing over the input itself, not the button.
buttonElement->SetTabIndex(-1, IgnoreErrors()); return button.forget();
}
mBrowseFilesOrDirs = MakeAnonButton(doc, "Browse", fileContent); if (!mBrowseFilesOrDirs) { return NS_ERROR_OUT_OF_MEMORY;
} // XXX(Bug 1631371) Check if this should use a fallible operation as it // pretended earlier, or change the return type to void.
aElements.AppendElement(mBrowseFilesOrDirs);
// Create and setup the text showing the selected files.
mTextContent = doc->CreateHTMLElement(nsGkAtoms::label); // NOTE: SetIsNativeAnonymousRoot() has to be called before setting any // attribute.
mTextContent->SetIsNativeAnonymousRoot();
RefPtr<nsTextNode> text = new (doc->NodeInfoManager()) nsTextNode(doc->NodeInfoManager());
mTextContent->AppendChildTo(text, false, IgnoreErrors());
aElements.AppendElement(mTextContent);
// We should be able to interact with the element by doing drag and drop.
mContent->AddSystemEventListener(u"drop"_ns, mMouseListener, false);
mContent->AddSystemEventListener(u"dragover"_ns, mMouseListener, false);
OwningFileOrDirectory* element = aArray.AppendElement();
element->SetAsDirectory() = directory;
}
/** * This is called when we receive a drop or a dragover.
*/
NS_IMETHODIMP
nsFileControlFrame::DnDListener::HandleEvent(Event* aEvent) {
NS_ASSERTION(mFrame, "We should have been unregistered");
if (aEvent->DefaultPrevented()) { return NS_OK;
}
DragEvent* dragEvent = aEvent->AsDragEvent(); if (!dragEvent) { return NS_OK;
}
RefPtr<DataTransfer> dataTransfer = dragEvent->GetDataTransfer(); if (!IsValidDropData(dataTransfer)) { return NS_OK;
}
nsAutoString eventType;
aEvent->GetType(eventType); if (eventType.EqualsLiteral("dragover")) { // Prevent default if we can accept this drag data
aEvent->PreventDefault(); return NS_OK;
}
if (eventType.EqualsLiteral("drop")) {
aEvent->StopPropagation();
aEvent->PreventDefault();
nsTArray<OwningFileOrDirectory> array; if (webkitDir) {
AppendBlobImplAsDirectory(array, webkitDir, inputElement);
inputElement->MozSetDndFilesAndDirectories(array);
} else { bool blinkFileSystemEnabled =
StaticPrefs::dom_webkitBlink_filesystem_enabled(); if (blinkFileSystemEnabled) {
FileList* files = static_cast<FileList*>(fileList.get()); if (files) { for (uint32_t i = 0; i < files->Length(); ++i) {
File* file = files->Item(i); if (file) { if (file->Impl() && file->Impl()->IsDirectory()) {
AppendBlobImplAsDirectory(array, file->Impl(), inputElement);
} else {
OwningFileOrDirectory* element = array.AppendElement();
element->SetAsFile() = file;
}
}
}
}
}
// Entries API. if (blinkFileSystemEnabled) { // This is rather ugly. Pass the directories as Files using SetFiles, // but then if blink filesystem API is enabled, it wants // FileOrDirectory array.
inputElement->SetFiles(fileList, true);
inputElement->UpdateEntries(array);
} // Normal DnD else {
inputElement->SetFiles(fileList, true);
}
// webkitdirectory doesn't care about the length of the file list but // only about the first item on it.
uint32_t len = aFileList->Length(); if (len) {
File* file = aFileList->Item(0); if (file) {
BlobImpl* impl = file->Impl(); if (impl && impl->IsDirectory()) {
RefPtr<BlobImpl> retVal = impl;
retVal.swap(*aBlobImpl); return NS_OK;
}
}
}
return NS_ERROR_FAILURE;
}
bool nsFileControlFrame::DnDListener::IsValidDropData(
DataTransfer* aDataTransfer) { if (!aDataTransfer) { returnfalse;
}
// We only support dropping files onto a file upload control return aDataTransfer->HasFile();
}
RefPtr<BlobImpl> webkitDir;
nsresult rv =
GetBlobImplForWebkitDirectory(fileList, getter_AddRefs(webkitDir)); // Just check if either there isn't webkitdirectory attribute, or // fileList has a directory which can be dropped to the element. // No need to use webkitDir for anything here.
NS_ENSURE_SUCCESS(rv, false);