void DataTransfer::GetData(const nsAString& aFormat, nsAString& aData, ErrorResult& aRv) { // return an empty string if data for the format was not found aData.Truncate(); nsCOMPtr<nsIVariant> data; nsresult rv = MozGetDataAt(aFormat, 0, getter_AddRefs(data)); if (NS_FAILED(rv)) { if (rv != NS_ERROR_DOM_INDEX_SIZE_ERR) { aRv.Throw(rv); } return; } 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 nsAutoString lowercaseFormat; aRv = nsContentUtils::ASCIIToLower(aFormat, lowercaseFormat); if (aRv.Failed()) { return; } if (lowercaseFormat.EqualsLiteral("url")) { int32_t lastidx = 0, idx; int32_t length = stringdata.Length(); while (lastidx < length) { idx = stringdata.FindChar('\n', lastidx); // lines beginning with # are comments if (stringdata[lastidx] == '#') { if (idx == -1) break; } else { if (idx == -1) aData.Assign(Substring(stringdata, lastidx)); else aData.Assign(Substring(stringdata, lastidx, idx - lastidx)); aData = nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(aData, true); return; } lastidx = idx + 1; } } else { aData = stringdata; } } }
FileList* DataTransfer::GetFiles(ErrorResult& aRv) { if (mEventType != NS_DRAGDROP_DROP && mEventType != NS_DRAGDROP_DRAGDROP && mEventType != NS_PASTE) { return nullptr; } if (!mFiles) { mFiles = new FileList(static_cast<nsIDOMDataTransfer*>(this)); uint32_t count = mItems.Length(); for (uint32_t i = 0; i < count; i++) { nsCOMPtr<nsIVariant> variant; aRv = MozGetDataAt(NS_ConvertUTF8toUTF16(kFileMime), i, getter_AddRefs(variant)); if (aRv.Failed()) { return nullptr; } if (!variant) continue; nsCOMPtr<nsISupports> supports; nsresult rv = variant->GetAsISupports(getter_AddRefs(supports)); if (NS_FAILED(rv)) continue; nsCOMPtr<nsIFile> file = do_QueryInterface(supports); nsRefPtr<File> domFile; if (file) { domFile = File::CreateFromFile(GetParentObject(), file); } else { nsCOMPtr<FileImpl> fileImpl = do_QueryInterface(supports); if (!fileImpl) { continue; } domFile = new File(GetParentObject(), static_cast<FileImpl*>(fileImpl.get())); } if (!mFiles->Append(domFile)) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } } } return mFiles; }
NS_IMETHODIMP nsDOMDataTransfer::GetData(const nsAString& aFormat, nsAString& aData) { // return an empty string if data for the format was not found aData.Truncate(); nsCOMPtr<nsIVariant> data; nsresult rv = MozGetDataAt(aFormat, 0, getter_AddRefs(data)); if (rv == NS_ERROR_DOM_INDEX_SIZE_ERR) return NS_OK; NS_ENSURE_SUCCESS(rv, rv); 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 if (aFormat.EqualsLiteral("URL")) { PRInt32 lastidx = 0, idx; PRInt32 length = stringdata.Length(); while (lastidx < length) { idx = stringdata.FindChar('\n', lastidx); // lines beginning with # are comments if (stringdata[lastidx] == '#') { if (idx == -1) break; } else { if (idx == -1) aData.Assign(Substring(stringdata, lastidx)); else aData.Assign(Substring(stringdata, lastidx, idx - lastidx)); aData = nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(aData, PR_TRUE); return NS_OK; } lastidx = idx + 1; } } else { aData = stringdata; } } return NS_OK; }
NS_IMETHODIMP nsDOMDataTransfer::GetFiles(nsIDOMFileList** aFileList) { *aFileList = nsnull; if (mEventType != NS_DRAGDROP_DROP && mEventType != NS_DRAGDROP_DRAGDROP) return NS_OK; if (!mFiles) { mFiles = new nsDOMFileList(); NS_ENSURE_TRUE(mFiles, NS_ERROR_OUT_OF_MEMORY); PRUint32 count = mItems.Length(); for (PRUint32 i = 0; i < count; i++) { nsCOMPtr<nsIVariant> variant; nsresult rv = MozGetDataAt(NS_ConvertUTF8toUTF16(kFileMime), i, getter_AddRefs(variant)); NS_ENSURE_SUCCESS(rv, rv); if (!variant) continue; nsCOMPtr<nsISupports> supports; rv = variant->GetAsISupports(getter_AddRefs(supports)); if (NS_FAILED(rv)) continue; nsCOMPtr<nsIFile> file = do_QueryInterface(supports); if (!file) continue; nsRefPtr<nsDOMFileFile> domFile = new nsDOMFileFile(file); if (!mFiles->Append(domFile)) return NS_ERROR_FAILURE; } } *aFileList = mFiles; NS_ADDREF(*aFileList); return NS_OK; }
void DataTransfer::MozGetDataAt(JSContext* aCx, const nsAString& aFormat, uint32_t aIndex, JS::MutableHandle<JS::Value> aRetval, mozilla::ErrorResult& aRv) { nsCOMPtr<nsIVariant> data; aRv = MozGetDataAt(aFormat, aIndex, getter_AddRefs(data)); if (aRv.Failed()) { return; } if (!data) { return; } JS::Rooted<JS::Value> result(aCx); if (!VariantToJsval(aCx, data, aRetval)) { aRv = NS_ERROR_FAILURE; return; } }
JS::Value DataTransfer::MozGetDataAt(JSContext* aCx, const nsAString& aFormat, uint32_t aIndex, mozilla::ErrorResult& aRv) { nsCOMPtr<nsIVariant> data; aRv = MozGetDataAt(aFormat, aIndex, getter_AddRefs(data)); if (aRv.Failed()) { return JS::UndefinedValue(); } if (!data) { return JS::NullValue(); } JS::Rooted<JS::Value> result(aCx); JS::Rooted<JSObject*> scope(aCx, GetWrapper()); if (!VariantToJsval(aCx, scope, data, &result)) { aRv = NS_ERROR_FAILURE; return JS::UndefinedValue(); } return result; }
FileList* DataTransfer::GetFiles(ErrorResult& aRv) { if (mEventType != NS_DRAGDROP_DROP && mEventType != NS_DRAGDROP_DRAGDROP && mEventType != NS_PASTE) { return nullptr; } if (!mFiles) { mFiles = new FileList(static_cast<nsIDOMDataTransfer*>(this)); uint32_t count = mItems.Length(); for (uint32_t i = 0; i < count; i++) { nsCOMPtr<nsIVariant> variant; aRv = MozGetDataAt(NS_ConvertUTF8toUTF16(kFileMime), i, getter_AddRefs(variant)); if (aRv.Failed()) { return nullptr; } if (!variant) continue; nsCOMPtr<nsISupports> supports; nsresult rv = variant->GetAsISupports(getter_AddRefs(supports)); if (NS_FAILED(rv)) continue; nsCOMPtr<nsIFile> file = do_QueryInterface(supports); nsRefPtr<File> domFile; if (file) { #ifdef DEBUG if (XRE_GetProcessType() == GeckoProcessType_Default) { bool isDir; file->IsDirectory(&isDir); MOZ_ASSERT(!isDir, "How did we get here?"); } #endif domFile = File::CreateFromFile(GetParentObject(), file); } else { nsCOMPtr<BlobImpl> blobImpl = do_QueryInterface(supports); if (!blobImpl) { continue; } MOZ_ASSERT(blobImpl->IsFile()); domFile = File::Create(GetParentObject(), blobImpl); MOZ_ASSERT(domFile); } if (!mFiles->Append(domFile)) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } } } return mFiles; }