diff --git a/dom/base/nsDOMClassInfo.cpp b/dom/base/nsDOMClassInfo.cpp index 72576b8bb4e..fccf8b995f8 100644 --- a/dom/base/nsDOMClassInfo.cpp +++ b/dom/base/nsDOMClassInfo.cpp @@ -158,7 +158,6 @@ #endif #include "nsIDOMGlobalObjectConstructor.h" -#include "nsIDOMLockedFile.h" #include "nsDebug.h" #include "mozilla/dom/BindingUtils.h" @@ -459,8 +458,6 @@ static nsDOMClassInfoData sClassInfoData[] = { DOM_DEFAULT_SCRIPTABLE_FLAGS) #endif - NS_DEFINE_CLASSINFO_DATA(LockedFile, nsEventTargetSH, - EVENTTARGET_SCRIPTABLE_FLAGS) NS_DEFINE_CLASSINFO_DATA(CSSFontFeatureValuesRule, nsDOMGenericSH, DOM_DEFAULT_SCRIPTABLE_FLAGS) @@ -1154,10 +1151,6 @@ nsDOMClassInfo::Init() #endif - DOM_CLASSINFO_MAP_BEGIN(LockedFile, nsIDOMLockedFile) - DOM_CLASSINFO_MAP_ENTRY(nsIDOMLockedFile) - DOM_CLASSINFO_MAP_END - DOM_CLASSINFO_MAP_BEGIN(CSSFontFeatureValuesRule, nsIDOMCSSFontFeatureValuesRule) DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSFontFeatureValuesRule) DOM_CLASSINFO_MAP_END diff --git a/dom/base/nsDOMClassInfoClasses.h b/dom/base/nsDOMClassInfoClasses.h index a2cca4632ba..ee95b311fc9 100644 --- a/dom/base/nsDOMClassInfoClasses.h +++ b/dom/base/nsDOMClassInfoClasses.h @@ -98,8 +98,6 @@ DOMCI_CLASS(CSSPageRule) DOMCI_CLASS(MozIccManager) #endif -DOMCI_CLASS(LockedFile) - DOMCI_CLASS(CSSFontFeatureValuesRule) DOMCI_CLASS(UserDataHandler) diff --git a/dom/bindings/Bindings.conf b/dom/bindings/Bindings.conf index e3afdcb20e1..6afb2009fc5 100644 --- a/dom/bindings/Bindings.conf +++ b/dom/bindings/Bindings.conf @@ -692,6 +692,10 @@ DOMInterfaces = { 'register': False }, +'LockedFile': { + 'nativeType': 'mozilla::dom::file::LockedFile', +}, + 'MediaList': { 'nativeType': 'nsMediaList', 'headerFile': 'nsIMediaList.h', @@ -1853,7 +1857,6 @@ addExternalIface('File') addExternalIface('HitRegionOptions', nativeType='nsISupports') addExternalIface('imgINotificationObserver', nativeType='imgINotificationObserver') addExternalIface('imgIRequest', nativeType='imgIRequest', notflattened=True) -addExternalIface('LockedFile') addExternalIface('MenuBuilder', nativeType='nsIMenuBuilder', notflattened=True) addExternalIface('MozBoxObject', nativeType='nsIBoxObject') addExternalIface('MozControllers', nativeType='nsIControllers') diff --git a/dom/bindings/Errors.msg b/dom/bindings/Errors.msg index af50d8b5dec..acc12c82246 100644 --- a/dom/bindings/Errors.msg +++ b/dom/bindings/Errors.msg @@ -52,3 +52,5 @@ MSG_DEF(MSG_NOT_DATE, 1, "{0} is not a date.") MSG_DEF(MSG_INVALID_ADVANCE_COUNT, 0, "0 (Zero) is not a valid advance count.") MSG_DEF(MSG_DEFINEPROPERTY_ON_GSP, 0, "Not allowed to define a property on the named properties object.") MSG_DEF(MSG_INVALID_URL, 1, "{0} is not a valid URL.") +MSG_DEF(MSG_METADATA_NOT_CONFIGURED, 0, "Either size or lastModified should be true.") +MSG_DEF(MSG_INVALID_READ_SIZE, 0, "0 (Zero) is not a valid read size.") diff --git a/dom/file/FileHandle.cpp b/dom/file/FileHandle.cpp index 7f68903ddf2..89e86cdebbe 100644 --- a/dom/file/FileHandle.cpp +++ b/dom/file/FileHandle.cpp @@ -128,7 +128,7 @@ FileHandle::WrapObject(JSContext* aCx, JS::Handle aScope) return FileHandleBinding::Wrap(aCx, aScope, this); } -already_AddRefed +already_AddRefed FileHandle::Open(FileMode aMode, ErrorResult& aError) { MOZ_ASSERT(NS_IsMainThread()); diff --git a/dom/file/FileHandle.h b/dom/file/FileHandle.h index 43eae9b1f96..deda8a9547e 100644 --- a/dom/file/FileHandle.h +++ b/dom/file/FileHandle.h @@ -113,7 +113,7 @@ public: aType = mType; } - already_AddRefed + already_AddRefed Open(FileMode aMode, ErrorResult& aError); already_AddRefed diff --git a/dom/file/FileRequest.cpp b/dom/file/FileRequest.cpp index 9d5db0ba474..ed2f405522c 100644 --- a/dom/file/FileRequest.cpp +++ b/dom/file/FileRequest.cpp @@ -115,7 +115,7 @@ FileRequest::WrapObject(JSContext* aCx, JS::Handle aScope) return FileRequestBinding::Wrap(aCx, aScope, this); } -nsIDOMLockedFile* +LockedFile* FileRequest::GetLockedFile() const { MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!"); diff --git a/dom/file/FileRequest.h b/dom/file/FileRequest.h index 7b2159f3955..ee9c9e979ff 100644 --- a/dom/file/FileRequest.h +++ b/dom/file/FileRequest.h @@ -51,7 +51,7 @@ public: WrapObject(JSContext* aCx, JS::Handle aScope) MOZ_OVERRIDE; // WebIDL - nsIDOMLockedFile* + LockedFile* GetLockedFile() const; IMPL_EVENT_HANDLER(progress) diff --git a/dom/file/FileService.cpp b/dom/file/FileService.cpp index d8a04f53348..22ee65ad419 100644 --- a/dom/file/FileService.cpp +++ b/dom/file/FileService.cpp @@ -284,7 +284,8 @@ FileService::AbortLockedFilesForStorage(nsIFileStorage* aFileStorage) lockedFiles); for (uint32_t index = 0; index < lockedFiles.Length(); index++) { - lockedFiles[index]->Abort(); + ErrorResult ignored; + lockedFiles[index]->Abort(ignored); } } diff --git a/dom/file/LockedFile.cpp b/dom/file/LockedFile.cpp index a4dd64f9c9c..4e5e516a404 100644 --- a/dom/file/LockedFile.cpp +++ b/dom/file/LockedFile.cpp @@ -6,21 +6,8 @@ #include "LockedFile.h" -#include "nsIAppShell.h" -#include "nsIDOMEvent.h" -#include "nsIDOMFile.h" -#include "nsIFileStorage.h" -#include "nsISeekableStream.h" - -#include "jsfriendapi.h" -#include "nsNetUtil.h" -#include "nsDOMClassInfoID.h" -#include "nsJSUtils.h" -#include "nsStringStream.h" -#include "nsWidgetsCID.h" -#include "xpcpublic.h" - #include "AsyncHelper.h" +#include "nsIDOMEvent.h" #include "FileHandle.h" #include "FileHelper.h" #include "FileRequest.h" @@ -28,18 +15,25 @@ #include "FileStreamWrappers.h" #include "MemoryStreams.h" #include "MetadataHelper.h" -#include "nsError.h" -#include "nsContentUtils.h" - -#include "mozilla/EventDispatcher.h" +#include "mozilla/dom/DOMRequest.h" #include "mozilla/dom/EncodingUtils.h" #include "mozilla/dom/LockedFileBinding.h" +#include "mozilla/dom/TypedArray.h" +#include "mozilla/dom/UnionTypes.h" +#include "mozilla/EventDispatcher.h" +#include "nsContentUtils.h" +#include "nsError.h" +#include "nsIAppShell.h" +#include "nsIDOMFile.h" +#include "nsIFileStorage.h" +#include "nsISeekableStream.h" +#include "nsNetUtil.h" +#include "nsStringStream.h" +#include "nsWidgetsCID.h" #define STREAM_COPY_BLOCK_SIZE 32768 -using namespace mozilla; -using namespace mozilla::dom; -USING_FILE_NAMESPACE +BEGIN_FILE_NAMESPACE namespace { @@ -215,61 +209,6 @@ CreateGenericEvent(mozilla::dom::EventTarget* aEventOwner, return event.forget(); } -inline nsresult -GetInputStreamForJSVal(JS::Handle aValue, JSContext* aCx, - nsIInputStream** aInputStream, uint64_t* aInputLength) -{ - nsresult rv; - - if (aValue.isObject()) { - JS::Rooted obj(aCx, &aValue.toObject()); - if (JS_IsArrayBufferObject(obj)) { - char* data = reinterpret_cast(JS_GetArrayBufferData(obj)); - uint32_t length = JS_GetArrayBufferByteLength(obj); - - rv = NS_NewByteInputStream(aInputStream, data, length, - NS_ASSIGNMENT_COPY); - NS_ENSURE_SUCCESS(rv, rv); - - *aInputLength = length; - - return NS_OK; - } - - nsCOMPtr blob = do_QueryInterface( - nsContentUtils::XPConnect()->GetNativeOfWrapper(aCx, obj)); - if (blob) { - rv = blob->GetSize(aInputLength); - NS_ENSURE_SUCCESS(rv, rv); - - rv = blob->GetInternalStream(aInputStream); - NS_ENSURE_SUCCESS(rv, rv); - - return NS_OK; - } - } - - JSString* jsstr = JS::ToString(aCx, aValue); - NS_ENSURE_TRUE(jsstr, NS_ERROR_XPC_BAD_CONVERT_JS); - - nsDependentJSString str; - if (!str.init(aCx, jsstr)) { - return NS_ERROR_FAILURE; - } - - nsCString cstr; - CopyUTF16toUTF8(str, cstr); - - nsCOMPtr stream; - rv = NS_NewCStringInputStream(getter_AddRefs(stream), cstr); - NS_ENSURE_SUCCESS(rv, rv); - - stream.forget(aInputStream); - *aInputLength = cstr.Length(); - - return NS_OK; -} - } // anonymous namespace // static @@ -305,6 +244,54 @@ LockedFile::Create(FileHandle* aFileHandle, return lockedFile.forget(); } +/* static */ already_AddRefed +LockedFile::GetInputStream(const ArrayBuffer& aValue, uint64_t* aInputLength, + ErrorResult& aRv) +{ + const char* data = reinterpret_cast(aValue.Data()); + uint32_t length = aValue.Length(); + + nsCOMPtr stream; + aRv = NS_NewByteInputStream(getter_AddRefs(stream), data, length, + NS_ASSIGNMENT_COPY); + if (aRv.Failed()) { + return nullptr; + } + + *aInputLength = length; + return stream.forget(); +} + +/* static */ already_AddRefed +LockedFile::GetInputStream(nsIDOMBlob* aValue, uint64_t* aInputLength, + ErrorResult& aRv) +{ + aRv = aValue->GetSize(aInputLength); + if (aRv.Failed()) { + return nullptr; + } + + nsCOMPtr stream; + aRv = aValue->GetInternalStream(getter_AddRefs(stream)); + return stream.forget(); +} + +/* static */ already_AddRefed +LockedFile::GetInputStream(const nsAString& aValue, uint64_t* aInputLength, + ErrorResult& aRv) +{ + NS_ConvertUTF16toUTF8 cstr(aValue); + + nsCOMPtr stream; + aRv = NS_NewCStringInputStream(getter_AddRefs(stream), cstr); + if (aRv.Failed()) { + return nullptr; + } + + *aInputLength = cstr.Length(); + return stream.forget(); +} + LockedFile::LockedFile() : mReadyState(INITIAL), mMode(FileMode::Readonly), @@ -315,6 +302,7 @@ LockedFile::LockedFile() mCreating(false) { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); + SetIsDOMBinding(); } LockedFile::~LockedFile() @@ -326,20 +314,12 @@ NS_IMPL_CYCLE_COLLECTION_INHERITED_1(LockedFile, nsDOMEventTargetHelper, mFileHandle) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(LockedFile) - NS_INTERFACE_MAP_ENTRY(nsIDOMLockedFile) NS_INTERFACE_MAP_ENTRY(nsIRunnable) - NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(LockedFile) NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper) NS_IMPL_ADDREF_INHERITED(LockedFile, nsDOMEventTargetHelper) NS_IMPL_RELEASE_INHERITED(LockedFile, nsDOMEventTargetHelper) -DOMCI_DATA(LockedFile, LockedFile) - -NS_IMPL_EVENT_HANDLER(LockedFile, complete) -NS_IMPL_EVENT_HANDLER(LockedFile, abort) -NS_IMPL_EVENT_HANDLER(LockedFile, error) - nsresult LockedFile::PreHandleEvent(EventChainPreVisitor& aVisitor) { @@ -457,103 +437,27 @@ LockedFile::IsOpen() const return false; } -NS_IMETHODIMP -LockedFile::GetFileHandle(nsISupports** aFileHandle) -{ - nsCOMPtr result(mFileHandle); - result.forget(aFileHandle); - return NS_OK; -} - -NS_IMETHODIMP -LockedFile::GetMode(nsAString& aMode) -{ - NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - - switch (mMode) { - case FileMode::Readonly: - aMode.AssignLiteral("readonly"); - break; - case FileMode::Readwrite: - aMode.AssignLiteral("readwrite"); - break; - default: - NS_NOTREACHED("Unknown mode!"); - } - - return NS_OK; -} - -NS_IMETHODIMP -LockedFile::GetActive(bool* aActive) -{ - NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - *aActive = IsOpen(); - return NS_OK; -} - -NS_IMETHODIMP -LockedFile::GetLocation(JSContext* aCx, - JS::MutableHandle aLocation) -{ - NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - - if (mLocation == UINT64_MAX) { - aLocation.setNull(); - } - else { - aLocation.setDouble(double(mLocation)); - } - return NS_OK; -} - -NS_IMETHODIMP -LockedFile::SetLocation(JSContext* aCx, - JS::Handle aLocation) -{ - NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - - // Null means the end-of-file. - if (aLocation.isNull()) { - mLocation = UINT64_MAX; - return NS_OK; - } - - uint64_t location; - if (!JS::ToUint64(aCx, aLocation, &location)) { - return NS_ERROR_TYPE_ERR; - } - - mLocation = location; - return NS_OK; -} - -NS_IMETHODIMP -LockedFile::GetMetadata(JS::Handle aParameters, - JSContext* aCx, - nsISupports** _retval) +already_AddRefed +LockedFile::GetMetadata(const DOMFileMetadataParameters& aParameters, + ErrorResult& aRv) { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); if (!IsOpen()) { - return NS_ERROR_DOM_FILEHANDLE_LOCKEDFILE_INACTIVE_ERR; + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_LOCKEDFILE_INACTIVE_ERR); + return nullptr; } // Do nothing if the window is closed if (!GetOwner()) { - return NS_OK; + return nullptr; } - // Get optional arguments. - DOMFileMetadataParameters config; - JS::Rooted parameters(aCx, aParameters); - bool result = config.Init(aCx, parameters); - NS_ENSURE_TRUE(result, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); - nsRefPtr params = - new MetadataParameters(config.mSize, config.mLastModified); + new MetadataParameters(aParameters.mSize, aParameters.mLastModified); if (!params->IsConfigured()) { - return NS_ERROR_TYPE_ERR; + aRv.ThrowTypeError(MSG_METADATA_NOT_CONFIGURED); + return nullptr; } nsRefPtr fileRequest = GenerateFileRequest(); @@ -561,36 +465,47 @@ LockedFile::GetMetadata(JS::Handle aParameters, nsRefPtr helper = new MetadataHelper(this, fileRequest, params); - nsresult rv = helper->Enqueue(); - NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + if (NS_FAILED(helper->Enqueue())) { + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + return nullptr; + } - nsRefPtr request = fileRequest.forget(); - request.forget(_retval); - return NS_OK; + return fileRequest.forget(); } -NS_IMETHODIMP -LockedFile::ReadAsArrayBuffer(uint64_t aSize, - JSContext* aCx, - nsISupports** _retval) +bool +LockedFile::CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv) { - NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - if (!IsOpen()) { - return NS_ERROR_DOM_FILEHANDLE_LOCKEDFILE_INACTIVE_ERR; + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_LOCKEDFILE_INACTIVE_ERR); + return false; } if (mLocation == UINT64_MAX) { - return NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR; + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR); + return false; } if (!aSize) { - return NS_ERROR_TYPE_ERR; + aRv.ThrowTypeError(MSG_INVALID_READ_SIZE); + return false; } // Do nothing if the window is closed if (!GetOwner()) { - return NS_OK; + return false; + } + + return true; +} + +already_AddRefed +LockedFile::ReadAsArrayBuffer(uint64_t aSize, ErrorResult& aRv) +{ + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); + + if (!CheckStateAndArgumentsForRead(aSize, aRv)) { + return nullptr; } nsRefPtr fileRequest = GenerateFileRequest(); @@ -598,41 +513,24 @@ LockedFile::ReadAsArrayBuffer(uint64_t aSize, nsRefPtr helper = new ReadHelper(this, fileRequest, mLocation, aSize); - nsresult rv = helper->Init(); - NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); - - rv = helper->Enqueue(); - NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + if (NS_FAILED(helper->Init()) || NS_FAILED(helper->Enqueue())) { + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + return nullptr; + } mLocation += aSize; - nsRefPtr request = fileRequest.forget(); - request.forget(_retval); - return NS_OK; + return fileRequest.forget(); } -NS_IMETHODIMP -LockedFile::ReadAsText(uint64_t aSize, - const nsAString& aEncoding, - nsISupports** _retval) +already_AddRefed +LockedFile::ReadAsText(uint64_t aSize, const nsAString& aEncoding, + ErrorResult& aRv) { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - if (!IsOpen()) { - return NS_ERROR_DOM_FILEHANDLE_LOCKEDFILE_INACTIVE_ERR; - } - - if (mLocation == UINT64_MAX) { - return NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR; - } - - if (!aSize) { - return NS_ERROR_TYPE_ERR; - } - - // Do nothing if the window is closed - if (!GetOwner()) { - return NS_OK; + if (!CheckStateAndArgumentsForRead(aSize, aRv)) { + return nullptr; } nsRefPtr fileRequest = GenerateFileRequest(); @@ -640,70 +538,57 @@ LockedFile::ReadAsText(uint64_t aSize, nsRefPtr helper = new ReadTextHelper(this, fileRequest, mLocation, aSize, aEncoding); - nsresult rv = helper->Init(); - NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); - - rv = helper->Enqueue(); - NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + if (NS_FAILED(helper->Init()) || NS_FAILED(helper->Enqueue())) { + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + return nullptr; + } mLocation += aSize; - nsRefPtr request = fileRequest.forget(); - request.forget(_retval); - return NS_OK; + return fileRequest.forget(); } -NS_IMETHODIMP -LockedFile::Write(JS::Handle aValue, - JSContext* aCx, - nsISupports** _retval) +bool +LockedFile::CheckStateForWrite(ErrorResult& aRv) { - NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - - return WriteOrAppend(aValue, aCx, _retval, false); -} - -NS_IMETHODIMP -LockedFile::Append(JS::Handle aValue, - JSContext* aCx, - nsISupports** _retval) -{ - NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - - return WriteOrAppend(aValue, aCx, _retval, true); -} - -NS_IMETHODIMP -LockedFile::Truncate(uint64_t aSize, - uint8_t aOptionalArgCount, - nsISupports** _retval) -{ - NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - if (!IsOpen()) { - return NS_ERROR_DOM_FILEHANDLE_LOCKEDFILE_INACTIVE_ERR; + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_LOCKEDFILE_INACTIVE_ERR); + return false; } if (mMode != FileMode::Readwrite) { - return NS_ERROR_DOM_FILEHANDLE_READ_ONLY_ERR; - } - - uint64_t location; - if (aOptionalArgCount) { - // Just in case someone calls us from C++ - NS_ASSERTION(aSize != UINT64_MAX, "Passed wrong size!"); - location = aSize; - } - else { - if (mLocation == UINT64_MAX) { - return NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR; - } - location = mLocation; + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_READ_ONLY_ERR); + return false; } // Do nothing if the window is closed if (!GetOwner()) { - return NS_OK; + return false; + } + + return true; +} + +already_AddRefed +LockedFile::Truncate(const Optional& aSize, ErrorResult& aRv) +{ + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); + + uint64_t location; + if (aSize.WasPassed()) { + // Just in case someone calls us from C++ + NS_ASSERTION(aSize.Value() != UINT64_MAX, "Passed wrong size!"); + location = aSize.Value(); + } else { + if (mLocation == UINT64_MAX) { + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR); + return nullptr; + } + location = mLocation; + } + + if (!CheckStateForWrite(aRv)) { + return nullptr; } nsRefPtr fileRequest = GenerateFileRequest(); @@ -711,50 +596,41 @@ LockedFile::Truncate(uint64_t aSize, nsRefPtr helper = new TruncateHelper(this, fileRequest, location); - nsresult rv = helper->Enqueue(); - NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); - - if (aOptionalArgCount) { - mLocation = aSize; + if (NS_FAILED(helper->Enqueue())) { + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + return nullptr; } - nsRefPtr request = fileRequest.forget(); - request.forget(_retval); - return NS_OK; + if (aSize.WasPassed()) { + mLocation = aSize.Value(); + } + + return fileRequest.forget(); } -NS_IMETHODIMP -LockedFile::Flush(nsISupports** _retval) +already_AddRefed +LockedFile::Flush(ErrorResult& aRv) { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); - if (!IsOpen()) { - return NS_ERROR_DOM_FILEHANDLE_LOCKEDFILE_INACTIVE_ERR; - } - - if (mMode != FileMode::Readwrite) { - return NS_ERROR_DOM_FILEHANDLE_READ_ONLY_ERR; - } - - // Do nothing if the window is closed - if (!GetOwner()) { - return NS_OK; + if (!CheckStateForWrite(aRv)) { + return nullptr; } nsRefPtr fileRequest = GenerateFileRequest(); nsRefPtr helper = new FlushHelper(this, fileRequest); - nsresult rv = helper->Enqueue(); - NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + if (NS_FAILED(helper->Enqueue())) { + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + return nullptr; + } - nsRefPtr request = fileRequest.forget(); - request.forget(_retval); - return NS_OK; + return fileRequest.forget(); } -NS_IMETHODIMP -LockedFile::Abort() +void +LockedFile::Abort(ErrorResult& aRv) { NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); @@ -762,7 +638,8 @@ LockedFile::Abort() // even from outside of transaction callbacks. if (mReadyState != LockedFile::INITIAL && mReadyState != LockedFile::LOADING) { - return NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR; + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR); + return; } bool needToFinish = mReadyState == INITIAL; @@ -773,10 +650,8 @@ LockedFile::Abort() // Fire the abort event if there are no outstanding requests. Otherwise the // abort event will be fired when all outstanding requests finish. if (needToFinish) { - return Finish(); + aRv = Finish(); } - - return NS_OK; } NS_IMETHODIMP @@ -829,38 +704,23 @@ LockedFile::OpenInputStream(bool aWholeFile, uint64_t aStart, uint64_t aLength, return NS_OK; } -nsresult -LockedFile::WriteOrAppend(JS::Handle aValue, - JSContext* aCx, - nsISupports** _retval, - bool aAppend) +already_AddRefed +LockedFile::WriteOrAppend(nsIInputStream* aInputStream, uint64_t aInputLength, + bool aAppend, ErrorResult& aRv) { - if (!IsOpen()) { - return NS_ERROR_DOM_FILEHANDLE_LOCKEDFILE_INACTIVE_ERR; - } - - if (mMode != FileMode::Readwrite) { - return NS_ERROR_DOM_FILEHANDLE_READ_ONLY_ERR; - } + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); if (!aAppend && mLocation == UINT64_MAX) { - return NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR; + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR); + return nullptr; } - // Do nothing if the window is closed - if (!GetOwner()) { - return NS_OK; + if (!CheckStateForWrite(aRv)) { + return nullptr; } - nsCOMPtr inputStream; - uint64_t inputLength; - nsresult rv = - GetInputStreamForJSVal(aValue, aCx, getter_AddRefs(inputStream), - &inputLength); - NS_ENSURE_SUCCESS(rv, rv); - - if (!inputLength) { - return NS_OK; + if (!aInputLength) { + return nullptr; } nsRefPtr fileRequest = GenerateFileRequest(); @@ -868,21 +728,21 @@ LockedFile::WriteOrAppend(JS::Handle aValue, uint64_t location = aAppend ? UINT64_MAX : mLocation; nsRefPtr helper = - new WriteHelper(this, fileRequest, location, inputStream, inputLength); + new WriteHelper(this, fileRequest, location, aInputStream, aInputLength); - rv = helper->Enqueue(); - NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + if (NS_FAILED(helper->Enqueue())) { + aRv.Throw(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); + return nullptr; + } if (aAppend) { mLocation = UINT64_MAX; } else { - mLocation += inputLength; + mLocation += aInputLength; } - nsRefPtr request = fileRequest.forget(); - request.forget(_retval); - return NS_OK; + return fileRequest.forget(); } nsresult @@ -903,6 +763,13 @@ LockedFile::Finish() return NS_OK; } +/* virtual */ JSObject* +LockedFile::WrapObject(JSContext* aCx, JS::Handle aScope) +{ + return LockedFileBinding::Wrap(aCx, aScope, this); +} + + FinishHelper::FinishHelper(LockedFile* aLockedFile) : mLockedFile(aLockedFile), mAborted(aLockedFile->mAborted) @@ -1152,3 +1019,5 @@ OpenStreamHelper::DoAsyncRun(nsISupports* aStream) return NS_OK; } + +END_FILE_NAMESPACE diff --git a/dom/file/LockedFile.h b/dom/file/LockedFile.h index 5f00ac125b2..4b5a03f9164 100644 --- a/dom/file/LockedFile.h +++ b/dom/file/LockedFile.h @@ -7,15 +7,20 @@ #ifndef mozilla_dom_file_lockedfile_h__ #define mozilla_dom_file_lockedfile_h__ -#include "mozilla/Attributes.h" #include "FileCommon.h" +#include "mozilla/Attributes.h" #include "mozilla/dom/FileModeBinding.h" -#include "nsIDOMLockedFile.h" +#include "mozilla/dom/TypedArray.h" +#include "nsDOMEventTargetHelper.h" +#include "nsIInputStream.h" #include "nsIRunnable.h" -#include "nsDOMEventTargetHelper.h" - -class nsIInputStream; +namespace mozilla { +namespace dom { +class DOMFileMetadataParameters; +class DOMRequest; +} // namespace dom +} // namespace mozilla namespace mozilla { class EventChainPreVisitor; @@ -28,7 +33,6 @@ class FileRequest; class MetadataHelper; class LockedFile : public nsDOMEventTargetHelper, - public nsIDOMLockedFile, public nsIRunnable { friend class FinishHelper; @@ -38,7 +42,6 @@ class LockedFile : public nsDOMEventTargetHelper, public: NS_DECL_ISUPPORTS_INHERITED - NS_DECL_NSIDOMLOCKEDFILE NS_DECL_NSIRUNNABLE NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(LockedFile, nsDOMEventTargetHelper) @@ -91,6 +94,94 @@ public: OpenInputStream(bool aWholeFile, uint64_t aStart, uint64_t aLength, nsIInputStream** aResult); + // WrapperCache + nsPIDOMWindow* GetParentObject() const + { + return GetOwner(); + } + + virtual JSObject* + WrapObject(JSContext* aCx, JS::Handle aScope) MOZ_OVERRIDE; + + // WebIDL + FileHandle* GetFileHandle() const + { + return Handle(); + } + + FileMode Mode() const + { + return mMode; + } + + bool Active() const + { + return IsOpen(); + } + + Nullable GetLocation() const + { + if (mLocation == UINT64_MAX) { + return Nullable(); + } + return Nullable(mLocation); + } + + void SetLocation(const Nullable& aLocation) + { + // Null means the end-of-file. + if (aLocation.IsNull()) { + mLocation = UINT64_MAX; + } else { + mLocation = aLocation.Value(); + } + } + + already_AddRefed + GetMetadata(const DOMFileMetadataParameters& aParameters, ErrorResult& aRv); + + already_AddRefed + ReadAsArrayBuffer(uint64_t aSize, ErrorResult& aRv); + + already_AddRefed + ReadAsText(uint64_t aSize, const nsAString& aEncoding, ErrorResult& aRv); + + template + already_AddRefed + Write(const T& aValue, ErrorResult& aRv) + { + uint64_t length; + nsCOMPtr stream = GetInputStream(aValue, &length, aRv); + if (aRv.Failed()) { + return nullptr; + } + return Write(stream, length, aRv); + } + + template + already_AddRefed + Append(const T& aValue, ErrorResult& aRv) + { + uint64_t length; + nsCOMPtr stream = GetInputStream(aValue, &length, aRv); + if (aRv.Failed()) { + return nullptr; + } + return Append(stream, length, aRv); + } + + already_AddRefed + Truncate(const Optional& aSize, ErrorResult& aRv); + + already_AddRefed + Flush(ErrorResult& aRv); + + void Abort(ErrorResult& aRv); + + IMPL_EVENT_HANDLER(complete) + IMPL_EVENT_HANDLER(abort) + IMPL_EVENT_HANDLER(error) + private: LockedFile(); ~LockedFile(); @@ -101,16 +192,40 @@ private: void OnRequestFinished(); + bool CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv); + bool CheckStateForWrite(ErrorResult& aRv); + already_AddRefed GenerateFileRequest(); - nsresult - WriteOrAppend(JS::Handle aValue, JSContext* aCx, - nsISupports** _retval, bool aAppend); + already_AddRefed + Write(nsIInputStream* aInputStream, uint64_t aInputLength, ErrorResult& aRv) + { + return WriteOrAppend(aInputStream, aInputLength, false, aRv); + } + + already_AddRefed + Append(nsIInputStream* aInputStream, uint64_t aInputLength, ErrorResult& aRv) + { + return WriteOrAppend(aInputStream, aInputLength, true, aRv); + } + + already_AddRefed + WriteOrAppend(nsIInputStream* aInputStream, uint64_t aInputLength, + bool aAppend, ErrorResult& aRv); nsresult Finish(); + static already_AddRefed + GetInputStream(const ArrayBuffer& aValue, uint64_t* aInputLength, + ErrorResult& aRv); + static already_AddRefed + GetInputStream(nsIDOMBlob* aValue, uint64_t* aInputLength, ErrorResult& aRv); + static already_AddRefed + GetInputStream(const nsAString& aValue, uint64_t* aInputLength, + ErrorResult& aRv); + nsRefPtr mFileHandle; ReadyState mReadyState; FileMode mMode; diff --git a/dom/file/moz.build b/dom/file/moz.build index 5418baa3032..7dbcd85f0b6 100644 --- a/dom/file/moz.build +++ b/dom/file/moz.build @@ -6,12 +6,6 @@ TEST_DIRS += ['test'] -XPIDL_SOURCES += [ - 'nsIDOMLockedFile.idl', -] - -XPIDL_MODULE = 'dom_file' - EXPORTS += [ 'nsIFileStorage.h', ] diff --git a/dom/file/nsIDOMLockedFile.idl b/dom/file/nsIDOMLockedFile.idl deleted file mode 100644 index 6532f1fb9fc..00000000000 --- a/dom/file/nsIDOMLockedFile.idl +++ /dev/null @@ -1,60 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=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/. */ - -#include "nsISupports.idl" - -interface nsIDOMEventListener; - -[scriptable, builtinclass, uuid(a09cdc35-6b1e-42ce-95bb-f8f10a354202)] -interface nsIDOMLockedFile : nsISupports -{ - readonly attribute nsISupports /* FileHandle */ fileHandle; - - // "readonly" or "readwrite" - readonly attribute DOMString mode; - - readonly attribute boolean active; - - [implicit_jscontext] - attribute jsval location; - - [implicit_jscontext] - nsISupports /* FileRequest */ - getMetadata(/* DOMFileMetadataParameters */ - [optional /* all */] in jsval parameters); - - [implicit_jscontext] - nsISupports /* FileRequest */ - readAsArrayBuffer(in unsigned long long size); - - nsISupports /* FileRequest */ - readAsText(in unsigned long long size, - [optional] in DOMString encoding); - - [implicit_jscontext] - nsISupports /* FileRequest */ - write(in jsval value); - - [implicit_jscontext] - nsISupports /* FileRequest */ - append(in jsval value); - - [optional_argc] - nsISupports /* FileRequest */ - truncate([optional] in unsigned long long size); - - nsISupports /* FileRequest */ - flush(); - - void - abort(); - - [implicit_jscontext] attribute jsval oncomplete; - - [implicit_jscontext] attribute jsval onabort; - - [implicit_jscontext] attribute jsval onerror; -}; diff --git a/dom/webidl/FileHandle.webidl b/dom/webidl/FileHandle.webidl index 5e8212a6284..a35bcbb1c0a 100644 --- a/dom/webidl/FileHandle.webidl +++ b/dom/webidl/FileHandle.webidl @@ -3,8 +3,6 @@ * 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/. */ -interface LockedFile; - interface FileHandle : EventTarget { readonly attribute DOMString name; readonly attribute DOMString type; diff --git a/dom/webidl/FileRequest.webidl b/dom/webidl/FileRequest.webidl index e052dc845ee..3a9927b9354 100644 --- a/dom/webidl/FileRequest.webidl +++ b/dom/webidl/FileRequest.webidl @@ -4,8 +4,6 @@ * 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/. */ -interface LockedFile; - interface FileRequest : DOMRequest { readonly attribute LockedFile? lockedFile; diff --git a/dom/webidl/LockedFile.webidl b/dom/webidl/LockedFile.webidl index cc42a8662b7..b1cf1dd517a 100644 --- a/dom/webidl/LockedFile.webidl +++ b/dom/webidl/LockedFile.webidl @@ -7,3 +7,42 @@ dictionary DOMFileMetadataParameters boolean size = true; boolean lastModified = true; }; + +interface LockedFile : EventTarget +{ + readonly attribute FileHandle? fileHandle; + readonly attribute FileMode mode; + readonly attribute boolean active; + attribute unsigned long long? location; + + [Throws] + FileRequest? getMetadata(optional DOMFileMetadataParameters parameters); + [Throws] + FileRequest? readAsArrayBuffer(unsigned long long size); + [Throws] + FileRequest? readAsText(unsigned long long size, + optional DOMString? encoding = null); + + [Throws] + FileRequest? write(ArrayBuffer value); + [Throws] + FileRequest? write(Blob value); + [Throws] + FileRequest? write(DOMString value); + [Throws] + FileRequest? append(ArrayBuffer value); + [Throws] + FileRequest? append(Blob value); + [Throws] + FileRequest? append(DOMString value); + [Throws] + FileRequest? truncate(optional unsigned long long size); + [Throws] + FileRequest? flush(); + [Throws] + void abort(); + + attribute EventHandler oncomplete; + attribute EventHandler onabort; + attribute EventHandler onerror; +}; diff --git a/js/xpconnect/src/dom_quickstubs.qsconf b/js/xpconnect/src/dom_quickstubs.qsconf index 38b5df75920..71fdd2524b4 100644 --- a/js/xpconnect/src/dom_quickstubs.qsconf +++ b/js/xpconnect/src/dom_quickstubs.qsconf @@ -65,9 +65,6 @@ members = [ # dom/indexedDB 'nsIIndexedDatabaseManager.*', - # dom/file - 'nsIDOMLockedFile.*', - # dom/quota 'nsIQuotaManager.*', 'nsIQuotaRequest.*',