2010-05-21 10:58:32 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07: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/. */
|
2010-05-21 10:58:32 -07:00
|
|
|
|
|
|
|
#include "Omnijar.h"
|
|
|
|
|
2011-02-25 03:53:36 -08:00
|
|
|
#include "nsDirectoryService.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsIFile.h"
|
2010-05-21 10:58:32 -07:00
|
|
|
#include "nsZipArchive.h"
|
2011-02-25 03:53:36 -08:00
|
|
|
#include "nsNetUtil.h"
|
2010-05-21 10:58:32 -07:00
|
|
|
|
2011-02-25 03:53:36 -08:00
|
|
|
namespace mozilla {
|
2010-05-21 10:58:32 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIFile *Omnijar::sPath[2] = { nullptr, nullptr };
|
|
|
|
nsZipArchive *Omnijar::sReader[2] = { nullptr, nullptr };
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Omnijar::sInitialized = false;
|
|
|
|
static bool sIsUnified = false;
|
2011-12-08 02:03:36 -08:00
|
|
|
static bool sIsNested[2] = { false, false };
|
2011-02-25 03:53:36 -08:00
|
|
|
|
|
|
|
static const char *sProp[2] =
|
|
|
|
{ NS_GRE_DIR, NS_XPCOM_CURRENT_PROCESS_DIR };
|
|
|
|
|
|
|
|
#define SPROP(Type) ((Type == mozilla::Omnijar::GRE) ? sProp[GRE] : sProp[APP])
|
|
|
|
|
|
|
|
void
|
|
|
|
Omnijar::CleanUpOne(Type aType)
|
2011-03-24 07:28:14 -07:00
|
|
|
{
|
2011-02-25 03:53:36 -08:00
|
|
|
if (sReader[aType]) {
|
|
|
|
sReader[aType]->CloseArchive();
|
2011-12-08 02:03:36 -08:00
|
|
|
NS_IF_RELEASE(sReader[aType]);
|
2011-02-25 03:53:36 -08:00
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
sReader[aType] = nullptr;
|
2011-02-25 03:53:36 -08:00
|
|
|
NS_IF_RELEASE(sPath[aType]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2011-11-12 19:58:19 -08:00
|
|
|
NS_NAMED_LITERAL_CSTRING (kOmnijarName, NS_STRINGIFY(OMNIJAR_NAME));
|
2011-02-25 03:53:36 -08:00
|
|
|
if (NS_FAILED(dir->Clone(getter_AddRefs(file))) ||
|
2011-11-12 19:58:19 -08:00
|
|
|
NS_FAILED(file->AppendNative(kOmnijarName)))
|
2011-02-25 03:53:36 -08:00
|
|
|
return;
|
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isFile;
|
2011-02-25 03:53:36 -08:00
|
|
|
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;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool equals;
|
2011-02-25 03:53:36 -08:00
|
|
|
nsDirectoryService::gService->Get(SPROP(GRE), NS_GET_IID(nsIFile), getter_AddRefs(greDir));
|
|
|
|
nsDirectoryService::gService->Get(SPROP(APP), NS_GET_IID(nsIFile), getter_AddRefs(appDir));
|
|
|
|
if (NS_SUCCEEDED(greDir->Equals(appDir, &equals)) && equals)
|
2011-10-17 07:59:28 -07:00
|
|
|
sIsUnified = true;
|
2011-02-25 03:53:36 -08:00
|
|
|
}
|
2011-03-24 21:07:05 -07:00
|
|
|
return;
|
2011-03-24 07:28:14 -07:00
|
|
|
}
|
2010-08-10 15:08:06 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool equals;
|
2011-02-25 03:53:36 -08:00
|
|
|
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.
|
2011-10-17 07:59:28 -07:00
|
|
|
sIsUnified = true;
|
2011-03-24 21:07:05 -07:00
|
|
|
return;
|
2010-08-10 15:08:06 -07:00
|
|
|
}
|
|
|
|
|
2011-12-08 02:03:36 -08:00
|
|
|
nsRefPtr<nsZipArchive> zipReader = new nsZipArchive();
|
2011-02-25 03:53:36 -08:00
|
|
|
if (NS_FAILED(zipReader->OpenArchive(file))) {
|
2011-03-24 21:07:05 -07:00
|
|
|
return;
|
2011-03-24 07:28:14 -07:00
|
|
|
}
|
|
|
|
|
2011-12-08 02:03:36 -08:00
|
|
|
nsRefPtr<nsZipHandle> handle;
|
|
|
|
if (NS_SUCCEEDED(nsZipHandle::Init(zipReader, NS_STRINGIFY(OMNIJAR_NAME), getter_AddRefs(handle)))) {
|
|
|
|
zipReader = new nsZipArchive();
|
|
|
|
if (NS_FAILED(zipReader->OpenArchive(handle)))
|
|
|
|
return;
|
|
|
|
sIsNested[aType] = true;
|
|
|
|
}
|
|
|
|
|
2011-02-25 03:53:36 -08:00
|
|
|
CleanUpOne(aType);
|
|
|
|
sReader[aType] = zipReader;
|
2011-12-08 02:03:36 -08:00
|
|
|
NS_IF_ADDREF(sReader[aType]);
|
2011-02-25 03:53:36 -08:00
|
|
|
sPath[aType] = file;
|
2011-09-16 13:22:44 -07:00
|
|
|
NS_IF_ADDREF(sPath[aType]);
|
2010-05-21 10:58:32 -07:00
|
|
|
}
|
|
|
|
|
2011-02-25 03:53:36 -08:00
|
|
|
void
|
|
|
|
Omnijar::Init(nsIFile *aGrePath, nsIFile *aAppPath)
|
2010-05-21 10:58:32 -07:00
|
|
|
{
|
2011-02-25 03:53:36 -08:00
|
|
|
InitOne(aGrePath, GRE);
|
|
|
|
InitOne(aAppPath, APP);
|
2011-10-17 07:59:28 -07:00
|
|
|
sInitialized = true;
|
2011-02-25 03:53:36 -08:00
|
|
|
}
|
2011-03-24 07:28:14 -07:00
|
|
|
|
2011-02-25 03:53:36 -08:00
|
|
|
void
|
|
|
|
Omnijar::CleanUp()
|
|
|
|
{
|
|
|
|
CleanUpOne(GRE);
|
|
|
|
CleanUpOne(APP);
|
2011-10-17 07:59:28 -07:00
|
|
|
sInitialized = false;
|
2010-05-21 10:58:32 -07:00
|
|
|
}
|
|
|
|
|
2011-12-08 02:03:36 -08:00
|
|
|
already_AddRefed<nsZipArchive>
|
2011-02-25 03:53:36 -08:00
|
|
|
Omnijar::GetReader(nsIFile *aPath)
|
2010-05-21 10:58:32 -07:00
|
|
|
{
|
2011-02-25 03:53:36 -08:00
|
|
|
NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized");
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool equals;
|
2011-02-25 03:53:36 -08:00
|
|
|
nsresult rv;
|
2011-03-24 07:28:14 -07:00
|
|
|
|
2011-12-08 02:03:36 -08:00
|
|
|
if (sPath[GRE] && !sIsNested[GRE]) {
|
2011-02-25 03:53:36 -08:00
|
|
|
rv = sPath[GRE]->Equals(aPath, &equals);
|
|
|
|
if (NS_SUCCEEDED(rv) && equals)
|
2011-12-08 02:03:36 -08:00
|
|
|
return GetReader(GRE);
|
2011-02-25 03:53:36 -08:00
|
|
|
}
|
2011-12-08 02:03:36 -08:00
|
|
|
if (sPath[APP] && !sIsNested[APP]) {
|
2011-02-25 03:53:36 -08:00
|
|
|
rv = sPath[APP]->Equals(aPath, &equals);
|
|
|
|
if (NS_SUCCEEDED(rv) && equals)
|
2011-12-08 02:03:36 -08:00
|
|
|
return GetReader(APP);
|
2011-02-25 03:53:36 -08:00
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-03-24 07:28:14 -07:00
|
|
|
}
|
|
|
|
|
2011-02-25 03:53:36 -08:00
|
|
|
nsresult
|
|
|
|
Omnijar::GetURIString(Type aType, nsACString &result)
|
2011-03-24 07:28:14 -07:00
|
|
|
{
|
2011-02-25 03:53:36 -08:00
|
|
|
NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized");
|
|
|
|
|
|
|
|
result.Truncate();
|
|
|
|
|
|
|
|
// Return an empty string for APP in the unified case.
|
|
|
|
if ((aType == APP) && sIsUnified) {
|
|
|
|
return NS_OK;
|
2011-03-24 07:28:14 -07:00
|
|
|
}
|
2011-03-24 21:07:05 -07:00
|
|
|
|
2011-02-25 03:53:36 -08:00
|
|
|
nsCAutoString omniJarSpec;
|
|
|
|
if (sPath[aType]) {
|
|
|
|
nsresult rv = NS_GetURLSpecFromActualFile(sPath[aType], omniJarSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
result = "jar:";
|
2011-12-08 02:03:36 -08:00
|
|
|
if (sIsNested[aType])
|
|
|
|
result += "jar:";
|
2011-02-25 03:53:36 -08:00
|
|
|
result += omniJarSpec;
|
|
|
|
result += "!";
|
2011-12-08 02:03:36 -08:00
|
|
|
if (sIsNested[aType])
|
|
|
|
result += "/" NS_STRINGIFY(OMNIJAR_NAME) "!";
|
2011-02-25 03:53:36 -08:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
nsDirectoryService::gService->Get(SPROP(aType), NS_GET_IID(nsIFile), getter_AddRefs(dir));
|
|
|
|
nsresult rv = NS_GetURLSpecFromActualFile(dir, result);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
result += "/";
|
|
|
|
return NS_OK;
|
2010-05-21 10:58:32 -07:00
|
|
|
}
|
|
|
|
|
2011-02-25 03:53:36 -08:00
|
|
|
} /* namespace mozilla */
|