2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
#ifndef mozilla_FileLocation_h
|
|
|
|
#define mozilla_FileLocation_h
|
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2011-12-08 14:03:36 +04:00
|
|
|
#include "FileUtils.h"
|
|
|
|
|
|
|
|
class nsZipArchive;
|
|
|
|
class nsZipItem;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class FileLocation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* FileLocation is an helper to handle different kind of file locations
|
|
|
|
* within Gecko:
|
|
|
|
* - on filesystems
|
|
|
|
* - in archives
|
|
|
|
* - in archives within archives
|
|
|
|
* As such, it stores a path within an archive, as well as the archive
|
|
|
|
* path itself, or the complete file path alone when on a filesystem.
|
|
|
|
* When the archive is in an archive, an nsZipArchive is stored instead
|
|
|
|
* of a file path.
|
|
|
|
*/
|
2013-09-23 21:26:47 +04:00
|
|
|
FileLocation();
|
|
|
|
~FileLocation();
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for plain files
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
explicit FileLocation(nsIFile* aFile);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructors for path within an archive. The archive can be given either
|
2012-06-06 06:08:30 +04:00
|
|
|
* as nsIFile or nsZipArchive.
|
2011-12-08 14:03:36 +04:00
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
FileLocation(nsIFile* aZip, const char* aPath);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
FileLocation(nsZipArchive* aZip, const char* aPath);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new file location relative to another one.
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
FileLocation(const FileLocation& aFile, const char* aPath = nullptr);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization functions corresponding to constructors
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
void Init(nsIFile* aFile);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void Init(nsIFile* aZip, const char* aPath);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void Init(nsZipArchive* aZip, const char* aPath);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an URI string corresponding to the file location
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
void GetURIString(nsACString& aResult) const;
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the base file of the location, where base file is defined as:
|
|
|
|
* - The file itself when the location is on a filesystem
|
|
|
|
* - The archive file when the location is in an archive
|
|
|
|
* - The outer archive file when the location is in an archive in an archive
|
|
|
|
*/
|
2012-06-06 06:08:30 +04:00
|
|
|
already_AddRefed<nsIFile> GetBaseFile();
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the "base file" (see GetBaseFile) is an archive
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
bool IsZip() const { return !mPath.IsEmpty(); }
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the path within the archive, when within an archive
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
void GetPath(nsACString& aResult) const { aResult = mPath; }
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Boolean value corresponding to whether the file location is initialized
|
|
|
|
* or not.
|
|
|
|
*/
|
2015-04-03 16:52:00 +03:00
|
|
|
#if defined(MOZILLA_XPCOMRT_API)
|
2015-04-11 06:05:46 +03:00
|
|
|
explicit operator bool() const { return mBaseFile; }
|
2015-04-03 16:52:00 +03:00
|
|
|
#else
|
2015-04-11 06:05:46 +03:00
|
|
|
explicit operator bool() const { return mBaseFile || mBaseZip; }
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // defined(MOZILLA_XPCOMRT_API)
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether another FileLocation points to the same resource
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
bool Equals(const FileLocation& aFile) const;
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Data associated with a FileLocation.
|
|
|
|
*/
|
|
|
|
class Data
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Returns the data size
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
nsresult GetSize(uint32_t* aResult);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the data in the given buffer
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
nsresult Copy(char* aBuf, uint32_t aLen);
|
2011-12-08 14:03:36 +04:00
|
|
|
protected:
|
|
|
|
friend class FileLocation;
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2014-08-13 22:45:37 +04:00
|
|
|
nsZipItem* mItem;
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2011-12-08 14:03:36 +04:00
|
|
|
nsRefPtr<nsZipArchive> mZip;
|
|
|
|
mozilla::AutoFDClose mFd;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the data associated with the resource pointed at by the file
|
|
|
|
* location.
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
nsresult GetData(Data& aData);
|
2011-12-08 14:03:36 +04:00
|
|
|
private:
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> mBaseFile;
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2011-12-08 14:03:36 +04:00
|
|
|
nsRefPtr<nsZipArchive> mBaseZip;
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif // !defined(MOZILLA_XPCOMRT_API)
|
2011-12-08 14:03:36 +04:00
|
|
|
nsCString mPath;
|
|
|
|
}; /* class FileLocation */
|
|
|
|
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* mozilla_FileLocation_h */
|