michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "FileRequest.h" michael@0: michael@0: #include "mozilla/EventDispatcher.h" michael@0: #include "mozilla/dom/FileRequestBinding.h" michael@0: #include "nsCxPusher.h" michael@0: #include "nsError.h" michael@0: #include "nsIDOMProgressEvent.h" michael@0: #include "nsDOMClassInfoID.h" michael@0: #include "FileHelper.h" michael@0: #include "LockedFile.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: USING_FILE_NAMESPACE michael@0: michael@0: FileRequest::FileRequest(nsPIDOMWindow* aWindow) michael@0: : DOMRequest(aWindow), mWrapAsDOMRequest(false) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); michael@0: } michael@0: michael@0: FileRequest::~FileRequest() michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); michael@0: } michael@0: michael@0: // static michael@0: already_AddRefed michael@0: FileRequest::Create(nsPIDOMWindow* aOwner, LockedFile* aLockedFile, michael@0: bool aWrapAsDOMRequest) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); michael@0: michael@0: nsRefPtr request = new FileRequest(aOwner); michael@0: request->mLockedFile = aLockedFile; michael@0: request->mWrapAsDOMRequest = aWrapAsDOMRequest; michael@0: michael@0: return request.forget(); michael@0: } michael@0: michael@0: nsresult michael@0: FileRequest::PreHandleEvent(EventChainPreVisitor& aVisitor) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); michael@0: michael@0: aVisitor.mCanHandle = true; michael@0: aVisitor.mParentTarget = mLockedFile; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: FileRequest::NotifyHelperCompleted(FileHelper* aFileHelper) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); michael@0: michael@0: nsresult rv = aFileHelper->mResultCode; michael@0: michael@0: // If the request failed then fire error event and return. michael@0: if (NS_FAILED(rv)) { michael@0: FireError(rv); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Otherwise we need to get the result from the helper. michael@0: nsIScriptContext* sc = GetContextForEventHandlers(&rv); michael@0: NS_ENSURE_STATE(sc); michael@0: michael@0: AutoJSContext cx; michael@0: NS_ASSERTION(cx, "Failed to get a context!"); michael@0: michael@0: JS::Rooted result(cx); michael@0: michael@0: JS::Rooted global(cx, sc->GetWindowProxy()); michael@0: NS_ASSERTION(global, "Failed to get global object!"); michael@0: michael@0: JSAutoCompartment ac(cx, global); michael@0: michael@0: rv = aFileHelper->GetSuccessResult(cx, &result); michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("GetSuccessResult failed!"); michael@0: } michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: FireSuccess(result); michael@0: } michael@0: else { michael@0: FireError(rv); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_INHERITED(FileRequest, DOMRequest, michael@0: mLockedFile) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileRequest) michael@0: NS_INTERFACE_MAP_END_INHERITING(DOMRequest) michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(FileRequest, DOMRequest) michael@0: NS_IMPL_RELEASE_INHERITED(FileRequest, DOMRequest) michael@0: michael@0: // virtual michael@0: JSObject* michael@0: FileRequest::WrapObject(JSContext* aCx) michael@0: { michael@0: if (mWrapAsDOMRequest) { michael@0: return DOMRequest::WrapObject(aCx); michael@0: } michael@0: return FileRequestBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: LockedFile* michael@0: FileRequest::GetLockedFile() const michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!"); michael@0: return mLockedFile; michael@0: } michael@0: michael@0: void michael@0: FileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal) michael@0: { michael@0: if (NS_FAILED(CheckInnerWindowCorrectness())) { michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr event; michael@0: nsresult rv = NS_NewDOMProgressEvent(getter_AddRefs(event), this, michael@0: nullptr, nullptr); michael@0: if (NS_FAILED(rv)) { michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr progress = do_QueryInterface(event); michael@0: MOZ_ASSERT(progress); michael@0: rv = progress->InitProgressEvent(NS_LITERAL_STRING("progress"), false, false, michael@0: false, aLoaded, aTotal); michael@0: if (NS_FAILED(rv)) { michael@0: return; michael@0: } michael@0: michael@0: DispatchTrustedEvent(event); michael@0: }