Bug 1492664 - generate portable URLs for Android mach commands; r=nalexander
[gecko.git] / xpcom / build / FileLocation.h
blob4ff65a0ea643ca1d2fb386dc8fe4695a386293ba
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 "nsAutoPtr.h"
13 #include "nsIFile.h"
14 #include "FileUtils.h"
16 class nsZipArchive;
17 class nsZipItem;
19 namespace mozilla {
21 class FileLocation {
22 public:
23 /**
24 * FileLocation is an helper to handle different kind of file locations
25 * within Gecko:
26 * - on filesystems
27 * - in archives
28 * - in archives within archives
29 * As such, it stores a path within an archive, as well as the archive
30 * path itself, or the complete file path alone when on a filesystem.
31 * When the archive is in an archive, an nsZipArchive is stored instead
32 * of a file path.
34 FileLocation();
35 ~FileLocation();
37 FileLocation(const FileLocation& aOther);
38 FileLocation(FileLocation&& aOther);
40 FileLocation& operator=(const FileLocation&) = default;
42 /**
43 * Constructor for plain files
45 explicit FileLocation(nsIFile* aFile);
47 /**
48 * Constructors for path within an archive. The archive can be given either
49 * as nsIFile or nsZipArchive.
51 FileLocation(nsIFile* aZip, const char* aPath);
53 FileLocation(nsZipArchive* aZip, const char* aPath);
55 /**
56 * Creates a new file location relative to another one.
58 FileLocation(const FileLocation& aFile, const char* aPath);
60 /**
61 * Initialization functions corresponding to constructors
63 void Init(nsIFile* aFile);
65 void Init(nsIFile* aZip, const char* aPath);
67 void Init(nsZipArchive* aZip, const char* aPath);
69 /**
70 * Returns an URI string corresponding to the file location
72 void GetURIString(nsACString& aResult) const;
74 /**
75 * Returns the base file of the location, where base file is defined as:
76 * - The file itself when the location is on a filesystem
77 * - The archive file when the location is in an archive
78 * - The outer archive file when the location is in an archive in an archive
80 already_AddRefed<nsIFile> GetBaseFile();
82 nsZipArchive* GetBaseZip() { return mBaseZip; }
84 /**
85 * Returns whether the "base file" (see GetBaseFile) is an archive
87 bool IsZip() const { return !mPath.IsEmpty(); }
89 /**
90 * Returns the path within the archive, when within an archive
92 void GetPath(nsACString& aResult) const { aResult = mPath; }
94 /**
95 * Boolean value corresponding to whether the file location is initialized
96 * or not.
98 explicit operator bool() const { return mBaseFile || mBaseZip; }
101 * Returns whether another FileLocation points to the same resource
103 bool Equals(const FileLocation& aFile) const;
106 * Data associated with a FileLocation.
108 class Data {
109 public:
111 * Returns the data size
113 nsresult GetSize(uint32_t* aResult);
116 * Copies the data in the given buffer
118 nsresult Copy(char* aBuf, uint32_t aLen);
120 protected:
121 friend class FileLocation;
122 nsZipItem* mItem;
123 RefPtr<nsZipArchive> mZip;
124 mozilla::AutoFDClose mFd;
128 * Returns the data associated with the resource pointed at by the file
129 * location.
131 nsresult GetData(Data& aData);
133 private:
134 nsCOMPtr<nsIFile> mBaseFile;
135 RefPtr<nsZipArchive> mBaseZip;
136 nsCString mPath;
137 }; /* class FileLocation */
139 } /* namespace mozilla */
141 #endif /* mozilla_FileLocation_h */