2020-09-14 20:00:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-04-09 20:25:05 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2010-05-21 21:58:32 +04:00
|
|
|
|
|
|
|
#include "Omnijar.h"
|
|
|
|
|
2011-02-25 14:53:36 +03:00
|
|
|
#include "nsDirectoryService.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsIFile.h"
|
2010-05-21 21:58:32 +04:00
|
|
|
#include "nsZipArchive.h"
|
2011-02-25 14:53:36 +03:00
|
|
|
#include "nsNetUtil.h"
|
2010-05-21 21:58:32 +04:00
|
|
|
|
2011-02-25 14:53:36 +03:00
|
|
|
namespace mozilla {
|
2010-05-21 21:58:32 +04:00
|
|
|
|
2016-04-28 23:07:02 +03:00
|
|
|
StaticRefPtr<nsIFile> Omnijar::sPath[2];
|
2020-09-14 20:00:53 +03:00
|
|
|
StaticRefPtr<nsZipArchive> Omnijar::sReader[2];
|
|
|
|
StaticRefPtr<nsZipArchive> Omnijar::sOuterReader[2];
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Omnijar::sInitialized = false;
|
2016-04-28 23:07:02 +03:00
|
|
|
bool Omnijar::sIsUnified = false;
|
2011-02-25 14:53:36 +03:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
static const char* sProp[2] = {NS_GRE_DIR, NS_XPCOM_CURRENT_PROCESS_DIR};
|
2011-02-25 14:53:36 +03:00
|
|
|
|
|
|
|
#define SPROP(Type) ((Type == mozilla::Omnijar::GRE) ? sProp[GRE] : sProp[APP])
|
|
|
|
|
|
|
|
void Omnijar::CleanUpOne(Type aType) {
|
2014-08-13 22:45:37 +04:00
|
|
|
if (sReader[aType]) {
|
|
|
|
sReader[aType]->CloseArchive();
|
2016-04-28 23:07:02 +03:00
|
|
|
sReader[aType] = nullptr;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2016-04-28 23:07:02 +03:00
|
|
|
if (sOuterReader[aType]) {
|
|
|
|
sOuterReader[aType]->CloseArchive();
|
|
|
|
sOuterReader[aType] = nullptr;
|
|
|
|
}
|
2016-04-28 23:07:02 +03:00
|
|
|
sPath[aType] = nullptr;
|
2011-02-25 14:53:36 +03:00
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void Omnijar::InitOne(nsIFile* aPath, Type aType) {
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
if (aPath) {
|
|
|
|
file = aPath;
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
nsDirectoryService::gService->Get(SPROP(aType), NS_GET_IID(nsIFile),
|
|
|
|
getter_AddRefs(dir));
|
2019-09-04 05:40:32 +03:00
|
|
|
constexpr auto kOmnijarName = nsLiteralCString{MOZ_STRINGIFY(OMNIJAR_NAME)};
|
2014-08-13 22:45:37 +04:00
|
|
|
if (NS_FAILED(dir->Clone(getter_AddRefs(file))) ||
|
|
|
|
NS_FAILED(file->AppendNative(kOmnijarName))) {
|
|
|
|
return;
|
2011-03-24 17:28:14 +03:00
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
|
|
|
bool isFile;
|
|
|
|
if (NS_FAILED(file->IsFile(&isFile)) || !isFile) {
|
|
|
|
// If we're not using an omni.jar for GRE, and we don't have an
|
|
|
|
// omni.jar for APP, check if both directories are the same.
|
|
|
|
if ((aType == APP) && (!sPath[GRE])) {
|
|
|
|
nsCOMPtr<nsIFile> greDir, appDir;
|
|
|
|
bool equals;
|
2015-09-22 02:36:40 +03:00
|
|
|
nsDirectoryService::gService->Get(sProp[GRE], NS_GET_IID(nsIFile),
|
2014-08-13 22:45:37 +04:00
|
|
|
getter_AddRefs(greDir));
|
2015-09-22 02:36:40 +03:00
|
|
|
nsDirectoryService::gService->Get(sProp[APP], NS_GET_IID(nsIFile),
|
2014-08-13 22:45:37 +04:00
|
|
|
getter_AddRefs(appDir));
|
|
|
|
if (NS_SUCCEEDED(greDir->Equals(appDir, &equals)) && equals) {
|
2011-10-17 18:59:28 +04:00
|
|
|
sIsUnified = true;
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2010-08-11 02:08:06 +04:00
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool equals;
|
|
|
|
if ((aType == APP) && (sPath[GRE]) &&
|
|
|
|
NS_SUCCEEDED(sPath[GRE]->Equals(file, &equals)) && equals) {
|
|
|
|
// If we're using omni.jar on both GRE and APP and their path
|
|
|
|
// is the same, we're in the unified case.
|
|
|
|
sIsUnified = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipArchive> zipReader = new nsZipArchive();
|
2020-08-13 20:34:05 +03:00
|
|
|
if (NS_FAILED(zipReader->OpenArchive(file))) {
|
|
|
|
return;
|
2020-07-08 05:43:02 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 23:07:02 +03:00
|
|
|
RefPtr<nsZipArchive> outerReader;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipHandle> handle;
|
2019-09-04 05:40:32 +03:00
|
|
|
if (NS_SUCCEEDED(nsZipHandle::Init(zipReader, MOZ_STRINGIFY(OMNIJAR_NAME),
|
2014-08-13 22:45:37 +04:00
|
|
|
getter_AddRefs(handle)))) {
|
2016-04-28 23:07:02 +03:00
|
|
|
outerReader = zipReader;
|
2014-08-13 22:45:37 +04:00
|
|
|
zipReader = new nsZipArchive();
|
|
|
|
if (NS_FAILED(zipReader->OpenArchive(handle))) {
|
|
|
|
return;
|
2011-12-08 14:03:36 +04:00
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CleanUpOne(aType);
|
2020-09-14 20:00:53 +03:00
|
|
|
sReader[aType] = zipReader;
|
|
|
|
sOuterReader[aType] = outerReader;
|
2014-08-13 22:45:37 +04:00
|
|
|
sPath[aType] = file;
|
2010-05-21 21:58:32 +04:00
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
void Omnijar::Init(nsIFile* aGrePath, nsIFile* aAppPath) {
|
|
|
|
InitOne(aGrePath, GRE);
|
|
|
|
InitOne(aAppPath, APP);
|
|
|
|
sInitialized = true;
|
2011-02-25 14:53:36 +03:00
|
|
|
}
|
2011-03-24 17:28:14 +03:00
|
|
|
|
2011-02-25 14:53:36 +03:00
|
|
|
void Omnijar::CleanUp() {
|
2014-08-13 22:45:37 +04:00
|
|
|
CleanUpOne(GRE);
|
|
|
|
CleanUpOne(APP);
|
|
|
|
sInitialized = false;
|
2010-05-21 21:58:32 +04:00
|
|
|
}
|
|
|
|
|
2020-09-14 20:00:53 +03:00
|
|
|
already_AddRefed<nsZipArchive> Omnijar::GetReader(nsIFile* aPath) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
|
2011-02-25 14:53:36 +03:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
bool equals;
|
|
|
|
nsresult rv;
|
2011-03-24 17:28:14 +03:00
|
|
|
|
2016-04-28 23:07:02 +03:00
|
|
|
if (sPath[GRE]) {
|
2014-08-13 22:45:37 +04:00
|
|
|
rv = sPath[GRE]->Equals(aPath, &equals);
|
|
|
|
if (NS_SUCCEEDED(rv) && equals) {
|
2016-04-28 23:07:02 +03:00
|
|
|
return IsNested(GRE) ? GetOuterReader(GRE) : GetReader(GRE);
|
2011-02-25 14:53:36 +03:00
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
2016-04-28 23:07:02 +03:00
|
|
|
if (sPath[APP]) {
|
2014-08-13 22:45:37 +04:00
|
|
|
rv = sPath[APP]->Equals(aPath, &equals);
|
|
|
|
if (NS_SUCCEEDED(rv) && equals) {
|
2016-04-28 23:07:02 +03:00
|
|
|
return IsNested(APP) ? GetOuterReader(APP) : GetReader(APP);
|
2011-02-25 14:53:36 +03:00
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
|
|
|
return nullptr;
|
2011-03-24 17:28:14 +03:00
|
|
|
}
|
|
|
|
|
2020-09-14 20:00:53 +03:00
|
|
|
already_AddRefed<nsZipArchive> Omnijar::GetInnerReader(
|
2020-03-06 23:35:53 +03:00
|
|
|
nsIFile* aPath, const nsACString& aEntry) {
|
|
|
|
MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
|
|
|
|
|
|
|
|
if (!aEntry.EqualsLiteral(MOZ_STRINGIFY(OMNIJAR_NAME))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool equals;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (sPath[GRE]) {
|
|
|
|
rv = sPath[GRE]->Equals(aPath, &equals);
|
|
|
|
if (NS_SUCCEEDED(rv) && equals) {
|
|
|
|
return IsNested(GRE) ? GetReader(GRE) : nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sPath[APP]) {
|
|
|
|
rv = sPath[APP]->Equals(aPath, &equals);
|
|
|
|
if (NS_SUCCEEDED(rv) && equals) {
|
|
|
|
return IsNested(APP) ? GetReader(APP) : nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
nsresult Omnijar::GetURIString(Type aType, nsACString& aResult) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(IsInitialized(), "Omnijar not initialized");
|
2014-08-13 22:45:37 +04:00
|
|
|
|
|
|
|
aResult.Truncate();
|
2011-02-25 14:53:36 +03:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
// Return an empty string for APP in the unified case.
|
|
|
|
if ((aType == APP) && sIsUnified) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-02-25 14:53:36 +03:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
nsAutoCString omniJarSpec;
|
|
|
|
if (sPath[aType]) {
|
|
|
|
nsresult rv = NS_GetURLSpecFromActualFile(sPath[aType], omniJarSpec);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2011-03-24 17:28:14 +03:00
|
|
|
}
|
2011-03-25 07:07:05 +03:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
aResult = "jar:";
|
2016-04-28 23:07:02 +03:00
|
|
|
if (IsNested(aType)) {
|
2014-08-13 22:45:37 +04:00
|
|
|
aResult += "jar:";
|
2011-02-25 14:53:36 +03:00
|
|
|
}
|
2014-08-13 22:45:37 +04:00
|
|
|
aResult += omniJarSpec;
|
|
|
|
aResult += "!";
|
2016-04-28 23:07:02 +03:00
|
|
|
if (IsNested(aType)) {
|
2019-09-04 05:40:32 +03:00
|
|
|
aResult += "/" MOZ_STRINGIFY(OMNIJAR_NAME) "!";
|
2014-08-13 22:45:37 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
nsDirectoryService::gService->Get(SPROP(aType), NS_GET_IID(nsIFile),
|
|
|
|
getter_AddRefs(dir));
|
|
|
|
nsresult rv = NS_GetURLSpecFromActualFile(dir, aResult);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aResult += "/";
|
|
|
|
return NS_OK;
|
2010-05-21 21:58:32 +04:00
|
|
|
}
|
|
|
|
|
2011-02-25 14:53:36 +03:00
|
|
|
} /* namespace mozilla */
|