Bug 1852754: part 9) Add tests for dynamically loading <link rel="prefetch"> elements...
[gecko.git] / xpcom / build / FileLocation.h
blob0988a9595ffb64ac8b764fcb0c6599af9f162292
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/. */
7 #ifndef mozilla_FileLocation_h
8 #define mozilla_FileLocation_h
10 #include "nsString.h"
11 #include "nsCOMPtr.h"
12 #include "nsIFile.h"
13 #include "FileUtils.h"
15 class nsZipArchive;
16 class nsZipItem;
18 namespace mozilla {
20 class FileLocation {
21 public:
22 /**
23 * FileLocation is an helper to handle different kind of file locations
24 * within Gecko:
25 * - on filesystems
26 * - in archives
27 * - in archives within archives
28 * As such, it stores a path within an archive, as well as the archive
29 * path itself, or the complete file path alone when on a filesystem.
30 * When the archive is in an archive, an nsZipArchive is stored instead
31 * of a file path.
33 FileLocation();
34 ~FileLocation();
36 FileLocation(const FileLocation& aOther);
37 FileLocation(FileLocation&& aOther);
39 FileLocation& operator=(const FileLocation&) = default;
41 /**
42 * Constructor for plain files
44 explicit FileLocation(nsIFile* aFile);
46 /**
47 * Constructors for path within an archive. The archive can be given either
48 * as nsIFile or nsZipArchive.
50 FileLocation(nsIFile* aZip, const char* aPath);
52 FileLocation(nsZipArchive* aZip, const char* aPath);
54 /**
55 * Creates a new file location relative to another one.
57 FileLocation(const FileLocation& aFile, const char* aPath);
59 /**
60 * Initialization functions corresponding to constructors
62 void Init(nsIFile* aFile);
64 void Init(nsIFile* aZip, const char* aPath);
66 void Init(nsZipArchive* aZip, const char* aPath);
68 /**
69 * Returns an URI string corresponding to the file location
71 void GetURIString(nsACString& aResult) const;
73 /**
74 * Returns the base file of the location, where base file is defined as:
75 * - The file itself when the location is on a filesystem
76 * - The archive file when the location is in an archive
77 * - The outer archive file when the location is in an archive in an archive
79 already_AddRefed<nsIFile> GetBaseFile();
81 nsZipArchive* GetBaseZip() { return mBaseZip; }
83 /**
84 * Returns whether the "base file" (see GetBaseFile) is an archive
86 bool IsZip() const { return !mPath.IsEmpty(); }
88 /**
89 * Returns the path within the archive, when within an archive
91 void GetPath(nsACString& aResult) const { aResult = mPath; }
93 /**
94 * Boolean value corresponding to whether the file location is initialized
95 * or not.
97 explicit operator bool() const { return mBaseFile || mBaseZip; }
99 /**
100 * Returns whether another FileLocation points to the same resource
102 bool Equals(const FileLocation& aFile) const;
105 * Data associated with a FileLocation.
107 class Data {
108 public:
110 * Returns the data size
112 nsresult GetSize(uint32_t* aResult);
115 * Copies the data in the given buffer
117 nsresult Copy(char* aBuf, uint32_t aLen);
119 protected:
120 friend class FileLocation;
121 nsZipItem* mItem;
122 RefPtr<nsZipArchive> mZip;
123 mozilla::AutoFDClose mFd;
127 * Returns the data associated with the resource pointed at by the file
128 * location.
130 nsresult GetData(Data& aData);
132 private:
133 nsCOMPtr<nsIFile> mBaseFile;
134 RefPtr<nsZipArchive> mBaseZip;
135 nsCString mPath;
136 }; /* class FileLocation */
138 } /* namespace mozilla */
140 #endif /* mozilla_FileLocation_h */