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: */
|
|
|
|
/* 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];
|
|
|
|
StaticRefPtr<nsZipArchive> Omnijar::sReader[2];
|
2016-04-28 23:07:02 +03:00
|
|
|
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)
|
2011-03-24 17:28:14 +03:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-08-13 22:45:37 +04:00
|
|
|
Omnijar::InitOne(nsIFile* aPath, Type aType)
|
2011-02-25 14:53:36 +03:00
|
|
|
{
|
2014-08-13 22:45:37 +04:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
if (aPath) {
|
|
|
|
file = aPath;
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
nsDirectoryService::gService->Get(SPROP(aType), NS_GET_IID(nsIFile),
|
|
|
|
getter_AddRefs(dir));
|
|
|
|
NS_NAMED_LITERAL_CSTRING(kOmnijarName, NS_STRINGIFY(OMNIJAR_NAME));
|
|
|
|
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();
|
2014-08-13 22:45:37 +04:00
|
|
|
if (NS_FAILED(zipReader->OpenArchive(file))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-28 23:07:02 +03:00
|
|
|
RefPtr<nsZipArchive> outerReader;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipHandle> handle;
|
2014-08-13 22:45:37 +04:00
|
|
|
if (NS_SUCCEEDED(nsZipHandle::Init(zipReader, NS_STRINGIFY(OMNIJAR_NAME),
|
|
|
|
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);
|
|
|
|
sReader[aType] = zipReader;
|
2016-04-28 23:07:02 +03:00
|
|
|
sOuterReader[aType] = outerReader;
|
2014-08-13 22:45:37 +04:00
|
|
|
sPath[aType] = file;
|
2010-05-21 21:58:32 +04:00
|
|
|
}
|
|
|
|
|
2011-02-25 14:53:36 +03:00
|
|
|
void
|
2014-08-13 22:45:37 +04:00
|
|
|
Omnijar::Init(nsIFile* aGrePath, nsIFile* aAppPath)
|
2010-05-21 21:58:32 +04:00
|
|
|
{
|
2014-08-13 22:45:37 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-12-08 14:03:36 +04:00
|
|
|
already_AddRefed<nsZipArchive>
|
2014-08-13 22:45:37 +04:00
|
|
|
Omnijar::GetReader(nsIFile* aPath)
|
2010-05-21 21:58:32 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2011-02-25 14:53:36 +03:00
|
|
|
nsresult
|
2014-08-13 22:45:37 +04:00
|
|
|
Omnijar::GetURIString(Type aType, nsACString& aResult)
|
2011-03-24 17:28:14 +03:00
|
|
|
{
|
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)) {
|
2014-08-13 22:45:37 +04:00
|
|
|
aResult += "/" NS_STRINGIFY(OMNIJAR_NAME) "!";
|
|
|
|
}
|
|
|
|
} 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 */
|