Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef mozilla_FileLocation_h |
michael@0 | 6 | #define mozilla_FileLocation_h |
michael@0 | 7 | |
michael@0 | 8 | #include "nsString.h" |
michael@0 | 9 | #include "nsCOMPtr.h" |
michael@0 | 10 | #include "nsAutoPtr.h" |
michael@0 | 11 | #include "nsIFile.h" |
michael@0 | 12 | #include "FileUtils.h" |
michael@0 | 13 | |
michael@0 | 14 | class nsZipArchive; |
michael@0 | 15 | class nsZipItem; |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | |
michael@0 | 19 | class FileLocation |
michael@0 | 20 | { |
michael@0 | 21 | public: |
michael@0 | 22 | /** |
michael@0 | 23 | * FileLocation is an helper to handle different kind of file locations |
michael@0 | 24 | * within Gecko: |
michael@0 | 25 | * - on filesystems |
michael@0 | 26 | * - in archives |
michael@0 | 27 | * - in archives within archives |
michael@0 | 28 | * As such, it stores a path within an archive, as well as the archive |
michael@0 | 29 | * path itself, or the complete file path alone when on a filesystem. |
michael@0 | 30 | * When the archive is in an archive, an nsZipArchive is stored instead |
michael@0 | 31 | * of a file path. |
michael@0 | 32 | */ |
michael@0 | 33 | FileLocation(); |
michael@0 | 34 | ~FileLocation(); |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * Constructor for plain files |
michael@0 | 38 | */ |
michael@0 | 39 | FileLocation(nsIFile *file); |
michael@0 | 40 | |
michael@0 | 41 | /** |
michael@0 | 42 | * Constructors for path within an archive. The archive can be given either |
michael@0 | 43 | * as nsIFile or nsZipArchive. |
michael@0 | 44 | */ |
michael@0 | 45 | FileLocation(nsIFile *zip, const char *path); |
michael@0 | 46 | |
michael@0 | 47 | FileLocation(nsZipArchive *zip, const char *path); |
michael@0 | 48 | |
michael@0 | 49 | /** |
michael@0 | 50 | * Creates a new file location relative to another one. |
michael@0 | 51 | */ |
michael@0 | 52 | FileLocation(const FileLocation &file, const char *path = nullptr); |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * Initialization functions corresponding to constructors |
michael@0 | 56 | */ |
michael@0 | 57 | void Init(nsIFile *file); |
michael@0 | 58 | |
michael@0 | 59 | void Init(nsIFile *zip, const char *path); |
michael@0 | 60 | |
michael@0 | 61 | void Init(nsZipArchive *zip, const char *path); |
michael@0 | 62 | |
michael@0 | 63 | /** |
michael@0 | 64 | * Returns an URI string corresponding to the file location |
michael@0 | 65 | */ |
michael@0 | 66 | void GetURIString(nsACString &result) const; |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Returns the base file of the location, where base file is defined as: |
michael@0 | 70 | * - The file itself when the location is on a filesystem |
michael@0 | 71 | * - The archive file when the location is in an archive |
michael@0 | 72 | * - The outer archive file when the location is in an archive in an archive |
michael@0 | 73 | */ |
michael@0 | 74 | already_AddRefed<nsIFile> GetBaseFile(); |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * Returns whether the "base file" (see GetBaseFile) is an archive |
michael@0 | 78 | */ |
michael@0 | 79 | bool IsZip() const |
michael@0 | 80 | { |
michael@0 | 81 | return !mPath.IsEmpty(); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Returns the path within the archive, when within an archive |
michael@0 | 86 | */ |
michael@0 | 87 | void GetPath(nsACString &result) const |
michael@0 | 88 | { |
michael@0 | 89 | result = mPath; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | /** |
michael@0 | 93 | * Boolean value corresponding to whether the file location is initialized |
michael@0 | 94 | * or not. |
michael@0 | 95 | */ |
michael@0 | 96 | operator bool() const |
michael@0 | 97 | { |
michael@0 | 98 | return mBaseFile || mBaseZip; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | /** |
michael@0 | 102 | * Returns whether another FileLocation points to the same resource |
michael@0 | 103 | */ |
michael@0 | 104 | bool Equals(const FileLocation &file) const; |
michael@0 | 105 | |
michael@0 | 106 | /** |
michael@0 | 107 | * Data associated with a FileLocation. |
michael@0 | 108 | */ |
michael@0 | 109 | class Data |
michael@0 | 110 | { |
michael@0 | 111 | public: |
michael@0 | 112 | /** |
michael@0 | 113 | * Returns the data size |
michael@0 | 114 | */ |
michael@0 | 115 | nsresult GetSize(uint32_t *result); |
michael@0 | 116 | |
michael@0 | 117 | /** |
michael@0 | 118 | * Copies the data in the given buffer |
michael@0 | 119 | */ |
michael@0 | 120 | nsresult Copy(char *buf, uint32_t len); |
michael@0 | 121 | protected: |
michael@0 | 122 | friend class FileLocation; |
michael@0 | 123 | nsZipItem *mItem; |
michael@0 | 124 | nsRefPtr<nsZipArchive> mZip; |
michael@0 | 125 | mozilla::AutoFDClose mFd; |
michael@0 | 126 | }; |
michael@0 | 127 | |
michael@0 | 128 | /** |
michael@0 | 129 | * Returns the data associated with the resource pointed at by the file |
michael@0 | 130 | * location. |
michael@0 | 131 | */ |
michael@0 | 132 | nsresult GetData(Data &data); |
michael@0 | 133 | private: |
michael@0 | 134 | nsCOMPtr<nsIFile> mBaseFile; |
michael@0 | 135 | nsRefPtr<nsZipArchive> mBaseZip; |
michael@0 | 136 | nsCString mPath; |
michael@0 | 137 | }; /* class FileLocation */ |
michael@0 | 138 | |
michael@0 | 139 | } /* namespace mozilla */ |
michael@0 | 140 | |
michael@0 | 141 | #endif /* mozilla_FileLocation_h */ |