/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=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/. */ #ifndef mozilla_dom_IOUtils__ #define mozilla_dom_IOUtils__ #include "mozilla/AlreadyAddRefed.h" #include "mozilla/DataMutex.h" #include "mozilla/dom/BindingDeclarations.h" #include "mozilla/dom/IOUtilsBinding.h" #include "mozilla/dom/TypedArray.h" #include "mozilla/ErrorResult.h" #include "mozilla/MozPromise.h" #include "nspr/prio.h" #include "nsIAsyncShutdown.h" #include "nsISerialEventTarget.h" #include "nsLocalFile.h" namespace mozilla { /** * Utility class to be used with |UniquePtr| to automatically close NSPR file * descriptors when they go out of scope. * * Example: * * UniquePtr fd = PR_Open(path, flags, mode); */ class PR_CloseDelete { public: constexpr PR_CloseDelete() = default; PR_CloseDelete(const PR_CloseDelete& aOther) = default; PR_CloseDelete(PR_CloseDelete&& aOther) = default; PR_CloseDelete& operator=(const PR_CloseDelete& aOther) = default; PR_CloseDelete& operator=(PR_CloseDelete&& aOther) = default; void operator()(PRFileDesc* aPtr) const { PR_Close(aPtr); } }; namespace dom { class IOUtils final { public: static already_AddRefed Read(GlobalObject& aGlobal, const nsAString& aPath, const Optional& aMaxBytes); static already_AddRefed WriteAtomic( GlobalObject& aGlobal, const nsAString& aPath, const Uint8Array& aData, const WriteAtomicOptions& aOptions); static already_AddRefed Move(GlobalObject& aGlobal, const nsAString& aSourcePath, const nsAString& aDestPath, const MoveOptions& aOptions); static bool IsAbsolutePath(const nsAString& aPath); private: ~IOUtils() = default; friend class IOUtilsShutdownBlocker; static StaticDataMutex> sBackgroundEventTarget; static StaticRefPtr sBarrier; static Atomic sShutdownStarted; static already_AddRefed GetShutdownBarrier(); static already_AddRefed GetBackgroundEventTarget(); static void SetShutdownHooks(); static already_AddRefed CreateJSPromise(GlobalObject& aGlobal); /** * Opens an existing file at |path|. * * @param path The location of the file as an absolute path string. * @param flags PRIO flags, excluding |PR_CREATE| and |PR_EXCL|. */ static UniquePtr OpenExistingSync( const nsAString& aPath, int32_t aFlags); /** * Creates a new file at |path|. * * @param aPath The location of the file as an absolute path string. * @param aFlags PRIO flags to be used in addition to |PR_CREATE| and * |PR_EXCL|. * @param aMode Optional file mode. Defaults to 0666 to allow the system * umask to compute the best mode for the new file. */ static UniquePtr CreateFileSync( const nsAString& aPath, int32_t aFlags, int32_t aMode = 0666); static nsresult ReadSync(PRFileDesc* aFd, const uint32_t aBufSize, nsTArray& aResult); static nsresult WriteSync(PRFileDesc* aFd, const nsTArray& aBytes, uint32_t& aResult); static nsresult MoveSync(const nsAString& aSource, const nsAString& aDest, bool noOverwrite); using IOReadMozPromise = mozilla::MozPromise, const nsCString, /* IsExclusive */ true>; using IOWriteMozPromise = mozilla::MozPromise; using IOMoveMozPromise = mozilla::MozPromise; }; class IOUtilsShutdownBlocker : public nsIAsyncShutdownBlocker { public: NS_DECL_THREADSAFE_ISUPPORTS NS_DECL_NSIASYNCSHUTDOWNBLOCKER private: virtual ~IOUtilsShutdownBlocker() = default; }; } // namespace dom } // namespace mozilla #endif