Beispiel #1
0
void
nsBaseDragService::DiscardInternalTransferData()
{
    if (mDataTransfer && mSourceNode) {
        MOZ_ASSERT(!!DataTransfer::Cast(mDataTransfer));

        DataTransferItemList* items = DataTransfer::Cast(mDataTransfer)->Items();
        for (size_t i = 0; i < items->Length(); i++) {
            bool found;
            DataTransferItem* item = items->IndexedGetter(i, found);

            // Non-OTHER items may still be needed by JS. Skip them.
            if (!found || item->Kind() != DataTransferItem::KIND_OTHER) {
                continue;
            }

            nsCOMPtr<nsIVariant> variant = item->DataNoSecurityCheck();
            nsCOMPtr<nsIWritableVariant> writable = do_QueryInterface(variant);

            if (writable) {
                writable->SetAsEmpty();
            }
        }
    }
}
// static
PassRefPtrWillBeRawPtr<Entry> DataTransferItemFileSystem::webkitGetAsEntry(ExecutionContext* executionContext, DataTransferItem& item)
{
    if (!item.dataObjectItem()->isFilename())
        return nullptr;

    // For dragged files getAsFile must be pretty lightweight.
    Blob* file = item.getAsFile().get();
    // The clipboard may not be in a readable state.
    if (!file)
        return nullptr;
    ASSERT(file->isFile());

    DraggedIsolatedFileSystem* filesystem = DraggedIsolatedFileSystem::from(item.clipboard()->dataObject().get());
    DOMFileSystem* domFileSystem = filesystem ? filesystem->getDOMFileSystem(executionContext) : 0;
    if (!filesystem) {
        // IsolatedFileSystem may not be enabled.
        return nullptr;
    }

    ASSERT(domFileSystem);

    // The dropped entries are mapped as top-level entries in the isolated filesystem.
    String virtualPath = DOMFilePath::append("/", toFile(file)->name());

    // FIXME: This involves synchronous file operation. Consider passing file type data when we dispatch drag event.
    FileMetadata metadata;
    if (!getFileMetadata(toFile(file)->path(), metadata))
        return nullptr;

    if (metadata.type == FileMetadata::TypeDirectory)
        return DirectoryEntry::create(domFileSystem, virtualPath);
    return FileEntry::create(domFileSystem, virtualPath);
}
Beispiel #3
0
already_AddRefed<DOMStringList>
DataTransfer::GetTypes(ErrorResult& aRv) const
{
  RefPtr<DOMStringList> types = new DOMStringList();

  const nsTArray<RefPtr<DataTransferItem>>* items = mItems->MozItemsAt(0);
  if (NS_WARN_IF(!items)) {
    return types.forget();
  }

  for (uint32_t i = 0; i < items->Length(); i++) {
    DataTransferItem* item = items->ElementAt(i);
    MOZ_ASSERT(item);

    if (item->ChromeOnly() && !nsContentUtils::LegacyIsCallerChromeOrNativeCode()) {
      continue;
    }

    nsAutoString type;
    item->GetType(type);
    if (item->Kind() == DataTransferItem::KIND_STRING || type.EqualsASCII(kFileMime)) {
      // If the entry has kind KIND_STRING, we want to add it to the list.
      if (NS_WARN_IF(!types->Add(type))) {
        aRv.Throw(NS_ERROR_FAILURE);
        return nullptr;
      }
    }
  }

  for (uint32_t i = 0; i < mItems->Length(); ++i) {
    ErrorResult rv;
    bool found = false;
    DataTransferItem* item = mItems->IndexedGetter(i, found, rv);
    if (!found || rv.Failed() || item->Kind() != DataTransferItem::KIND_FILE) {
      rv.SuppressException();
      continue;
    }
    if (NS_WARN_IF(!types->Add(NS_LITERAL_STRING("Files")))) {
      aRv.Throw(NS_ERROR_FAILURE);
      return nullptr;
    }
    break;
  }

  return types.forget();
}
void
DataTransfer::GetTypes(nsTArray<nsString>& aTypes,
                       nsIPrincipal& aSubjectPrincipal) const
{
  // When called from bindings, aTypes will be empty, but since we might have
  // Gecko-internal callers too, clear it to be safe.
  aTypes.Clear();
  
  const nsTArray<RefPtr<DataTransferItem>>* items = mItems->MozItemsAt(0);
  if (NS_WARN_IF(!items)) {
    return;
  }

  for (uint32_t i = 0; i < items->Length(); i++) {
    DataTransferItem* item = items->ElementAt(i);
    MOZ_ASSERT(item);

    if (item->ChromeOnly() && !nsContentUtils::IsSystemPrincipal(&aSubjectPrincipal)) {
      continue;
    }

    // NOTE: The reason why we get the internal type here is because we want
    // kFileMime to appear in the types list for backwards compatibility
    // reasons.
    nsAutoString type;
    item->GetInternalType(type);
    if (item->Kind() == DataTransferItem::KIND_STRING || type.EqualsASCII(kFileMime)) {
      // If the entry has kind KIND_STRING, we want to add it to the list.
      aTypes.AppendElement(type);
    }
  }

  for (uint32_t i = 0; i < mItems->Length(); ++i) {
    bool found = false;
    DataTransferItem* item = mItems->IndexedGetter(i, found);
    MOZ_ASSERT(found);
    if (item->Kind() != DataTransferItem::KIND_FILE) {
      continue;
    }
    aTypes.AppendElement(NS_LITERAL_STRING("Files"));
    break;
  }
}