123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
- /* 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 "FileSnapshot.h"
- #include "IDBFileHandle.h"
- #include "mozilla/Assertions.h"
- #include "nsIIPCSerializableInputStream.h"
- namespace mozilla {
- namespace dom {
- namespace indexedDB {
- using namespace mozilla::ipc;
- namespace {
- class StreamWrapper final
- : public nsIInputStream
- , public nsIIPCSerializableInputStream
- {
- class CloseRunnable;
- nsCOMPtr<nsIEventTarget> mOwningThread;
- nsCOMPtr<nsIInputStream> mInputStream;
- RefPtr<IDBFileHandle> mFileHandle;
- bool mFinished;
- public:
- StreamWrapper(nsIInputStream* aInputStream,
- IDBFileHandle* aFileHandle)
- : mOwningThread(NS_GetCurrentThread())
- , mInputStream(aInputStream)
- , mFileHandle(aFileHandle)
- , mFinished(false)
- {
- AssertIsOnOwningThread();
- MOZ_ASSERT(aInputStream);
- MOZ_ASSERT(aFileHandle);
- aFileHandle->AssertIsOnOwningThread();
- mFileHandle->OnNewRequest();
- }
- private:
- virtual ~StreamWrapper();
- bool
- IsOnOwningThread() const
- {
- MOZ_ASSERT(mOwningThread);
- bool current;
- return NS_SUCCEEDED(mOwningThread->
- IsOnCurrentThread(¤t)) && current;
- }
- void
- AssertIsOnOwningThread() const
- {
- MOZ_ASSERT(IsOnOwningThread());
- }
- void
- Finish()
- {
- AssertIsOnOwningThread();
- if (mFinished) {
- return;
- }
- mFinished = true;
- mFileHandle->OnRequestFinished(/* aActorDestroyedNormally */ true);
- }
- void
- Destroy()
- {
- if (IsOnOwningThread()) {
- delete this;
- return;
- }
- nsCOMPtr<nsIRunnable> destroyRunnable =
- NewNonOwningRunnableMethod(this, &StreamWrapper::Destroy);
- MOZ_ALWAYS_SUCCEEDS(mOwningThread->Dispatch(destroyRunnable,
- NS_DISPATCH_NORMAL));
- }
- NS_DECL_THREADSAFE_ISUPPORTS
- NS_DECL_NSIINPUTSTREAM
- NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
- };
- class StreamWrapper::CloseRunnable final
- : public Runnable
- {
- friend class StreamWrapper;
- RefPtr<StreamWrapper> mStreamWrapper;
- public:
- NS_DECL_ISUPPORTS_INHERITED
- private:
- explicit
- CloseRunnable(StreamWrapper* aStreamWrapper)
- : mStreamWrapper(aStreamWrapper)
- { }
- ~CloseRunnable()
- { }
- NS_IMETHOD
- Run() override;
- };
- } // anonymous namespace
- BlobImplSnapshot::BlobImplSnapshot(BlobImpl* aFileImpl,
- IDBFileHandle* aFileHandle)
- : mBlobImpl(aFileImpl)
- {
- MOZ_ASSERT(aFileImpl);
- MOZ_ASSERT(aFileHandle);
- mFileHandle =
- do_GetWeakReference(NS_ISUPPORTS_CAST(EventTarget*, aFileHandle));
- }
- BlobImplSnapshot::BlobImplSnapshot(BlobImpl* aFileImpl,
- nsIWeakReference* aFileHandle)
- : mBlobImpl(aFileImpl)
- , mFileHandle(aFileHandle)
- {
- MOZ_ASSERT(aFileImpl);
- MOZ_ASSERT(aFileHandle);
- }
- BlobImplSnapshot::~BlobImplSnapshot()
- {
- }
- NS_IMPL_ISUPPORTS_INHERITED(BlobImplSnapshot, BlobImpl, PIBlobImplSnapshot)
- already_AddRefed<BlobImpl>
- BlobImplSnapshot::CreateSlice(uint64_t aStart,
- uint64_t aLength,
- const nsAString& aContentType,
- ErrorResult& aRv)
- {
- RefPtr<BlobImpl> blobImpl =
- mBlobImpl->CreateSlice(aStart, aLength, aContentType, aRv);
- if (NS_WARN_IF(aRv.Failed())) {
- return nullptr;
- }
- blobImpl = new BlobImplSnapshot(blobImpl, mFileHandle);
- return blobImpl.forget();
- }
- void
- BlobImplSnapshot::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
- {
- nsCOMPtr<EventTarget> et = do_QueryReferent(mFileHandle);
- RefPtr<IDBFileHandle> fileHandle = static_cast<IDBFileHandle*>(et.get());
- if (!fileHandle || !fileHandle->IsOpen()) {
- aRv.Throw(NS_ERROR_DOM_FILEHANDLE_INACTIVE_ERR);
- return;
- }
- nsCOMPtr<nsIInputStream> stream;
- mBlobImpl->GetInternalStream(getter_AddRefs(stream), aRv);
- if (NS_WARN_IF(aRv.Failed())) {
- return;
- }
- RefPtr<StreamWrapper> wrapper = new StreamWrapper(stream, fileHandle);
- wrapper.forget(aStream);
- }
- BlobImpl*
- BlobImplSnapshot::GetBlobImpl() const
- {
- nsCOMPtr<EventTarget> et = do_QueryReferent(mFileHandle);
- RefPtr<IDBFileHandle> fileHandle = static_cast<IDBFileHandle*>(et.get());
- if (!fileHandle || !fileHandle->IsOpen()) {
- return nullptr;
- }
- return mBlobImpl;
- }
- StreamWrapper::~StreamWrapper()
- {
- AssertIsOnOwningThread();
- Finish();
- }
- NS_IMPL_ADDREF(StreamWrapper)
- NS_IMPL_RELEASE_WITH_DESTROY(StreamWrapper, Destroy())
- NS_IMPL_QUERY_INTERFACE(StreamWrapper,
- nsIInputStream,
- nsIIPCSerializableInputStream)
- NS_IMETHODIMP
- StreamWrapper::Close()
- {
- MOZ_ASSERT(!IsOnOwningThread());
- RefPtr<CloseRunnable> closeRunnable = new CloseRunnable(this);
- MOZ_ALWAYS_SUCCEEDS(mOwningThread->Dispatch(closeRunnable,
- NS_DISPATCH_NORMAL));
- return NS_OK;
- }
- NS_IMETHODIMP
- StreamWrapper::Available(uint64_t* _retval)
- {
- // Can't assert here, this method is sometimes called on the owning thread
- // (nsInputStreamChannel::OpenContentStream calls Available before setting
- // the content length property).
- return mInputStream->Available(_retval);
- }
- NS_IMETHODIMP
- StreamWrapper::Read(char* aBuf, uint32_t aCount, uint32_t* _retval)
- {
- MOZ_ASSERT(!IsOnOwningThread());
- return mInputStream->Read(aBuf, aCount, _retval);
- }
- NS_IMETHODIMP
- StreamWrapper::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
- uint32_t aCount, uint32_t* _retval)
- {
- MOZ_ASSERT(!IsOnOwningThread());
- return mInputStream->ReadSegments(aWriter, aClosure, aCount, _retval);
- }
- NS_IMETHODIMP
- StreamWrapper::IsNonBlocking(bool* _retval)
- {
- return mInputStream->IsNonBlocking(_retval);
- }
- void
- StreamWrapper::Serialize(InputStreamParams& aParams,
- FileDescriptorArray& aFileDescriptors)
- {
- nsCOMPtr<nsIIPCSerializableInputStream> stream =
- do_QueryInterface(mInputStream);
- if (stream) {
- stream->Serialize(aParams, aFileDescriptors);
- }
- }
- bool
- StreamWrapper::Deserialize(const InputStreamParams& aParams,
- const FileDescriptorArray& aFileDescriptors)
- {
- nsCOMPtr<nsIIPCSerializableInputStream> stream =
- do_QueryInterface(mInputStream);
- if (stream) {
- return stream->Deserialize(aParams, aFileDescriptors);
- }
- return false;
- }
- Maybe<uint64_t>
- StreamWrapper::ExpectedSerializedLength()
- {
- nsCOMPtr<nsIIPCSerializableInputStream> stream =
- do_QueryInterface(mInputStream);
- if (stream) {
- return stream->ExpectedSerializedLength();
- }
- return Nothing();
- }
- NS_IMPL_ISUPPORTS_INHERITED0(StreamWrapper::CloseRunnable,
- Runnable)
- NS_IMETHODIMP
- StreamWrapper::
- CloseRunnable::Run()
- {
- mStreamWrapper->Finish();
- return NS_OK;
- }
- } // namespace indexedDB
- } // namespace dom
- } // namespace mozilla
|