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; } }