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