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 michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_FileLocation_h michael@0: #define mozilla_FileLocation_h michael@0: michael@0: #include "nsString.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsIFile.h" michael@0: #include "FileUtils.h" michael@0: michael@0: class nsZipArchive; michael@0: class nsZipItem; michael@0: michael@0: namespace mozilla { michael@0: michael@0: class FileLocation michael@0: { michael@0: public: michael@0: /** michael@0: * FileLocation is an helper to handle different kind of file locations michael@0: * within Gecko: michael@0: * - on filesystems michael@0: * - in archives michael@0: * - in archives within archives michael@0: * As such, it stores a path within an archive, as well as the archive michael@0: * path itself, or the complete file path alone when on a filesystem. michael@0: * When the archive is in an archive, an nsZipArchive is stored instead michael@0: * of a file path. michael@0: */ michael@0: FileLocation(); michael@0: ~FileLocation(); michael@0: michael@0: /** michael@0: * Constructor for plain files michael@0: */ michael@0: FileLocation(nsIFile *file); michael@0: michael@0: /** michael@0: * Constructors for path within an archive. The archive can be given either michael@0: * as nsIFile or nsZipArchive. michael@0: */ michael@0: FileLocation(nsIFile *zip, const char *path); michael@0: michael@0: FileLocation(nsZipArchive *zip, const char *path); michael@0: michael@0: /** michael@0: * Creates a new file location relative to another one. michael@0: */ michael@0: FileLocation(const FileLocation &file, const char *path = nullptr); michael@0: michael@0: /** michael@0: * Initialization functions corresponding to constructors michael@0: */ michael@0: void Init(nsIFile *file); michael@0: michael@0: void Init(nsIFile *zip, const char *path); michael@0: michael@0: void Init(nsZipArchive *zip, const char *path); michael@0: michael@0: /** michael@0: * Returns an URI string corresponding to the file location michael@0: */ michael@0: void GetURIString(nsACString &result) const; michael@0: michael@0: /** michael@0: * Returns the base file of the location, where base file is defined as: michael@0: * - The file itself when the location is on a filesystem michael@0: * - The archive file when the location is in an archive michael@0: * - The outer archive file when the location is in an archive in an archive michael@0: */ michael@0: already_AddRefed GetBaseFile(); michael@0: michael@0: /** michael@0: * Returns whether the "base file" (see GetBaseFile) is an archive michael@0: */ michael@0: bool IsZip() const michael@0: { michael@0: return !mPath.IsEmpty(); michael@0: } michael@0: michael@0: /** michael@0: * Returns the path within the archive, when within an archive michael@0: */ michael@0: void GetPath(nsACString &result) const michael@0: { michael@0: result = mPath; michael@0: } michael@0: michael@0: /** michael@0: * Boolean value corresponding to whether the file location is initialized michael@0: * or not. michael@0: */ michael@0: operator bool() const michael@0: { michael@0: return mBaseFile || mBaseZip; michael@0: } michael@0: michael@0: /** michael@0: * Returns whether another FileLocation points to the same resource michael@0: */ michael@0: bool Equals(const FileLocation &file) const; michael@0: michael@0: /** michael@0: * Data associated with a FileLocation. michael@0: */ michael@0: class Data michael@0: { michael@0: public: michael@0: /** michael@0: * Returns the data size michael@0: */ michael@0: nsresult GetSize(uint32_t *result); michael@0: michael@0: /** michael@0: * Copies the data in the given buffer michael@0: */ michael@0: nsresult Copy(char *buf, uint32_t len); michael@0: protected: michael@0: friend class FileLocation; michael@0: nsZipItem *mItem; michael@0: nsRefPtr mZip; michael@0: mozilla::AutoFDClose mFd; michael@0: }; michael@0: michael@0: /** michael@0: * Returns the data associated with the resource pointed at by the file michael@0: * location. michael@0: */ michael@0: nsresult GetData(Data &data); michael@0: private: michael@0: nsCOMPtr mBaseFile; michael@0: nsRefPtr mBaseZip; michael@0: nsCString mPath; michael@0: }; /* class FileLocation */ michael@0: michael@0: } /* namespace mozilla */ michael@0: michael@0: #endif /* mozilla_FileLocation_h */