Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "Omnijar.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsDirectoryService.h" |
michael@0 | 9 | #include "nsDirectoryServiceDefs.h" |
michael@0 | 10 | #include "nsIFile.h" |
michael@0 | 11 | #include "nsZipArchive.h" |
michael@0 | 12 | #include "nsNetUtil.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | |
michael@0 | 16 | nsIFile *Omnijar::sPath[2] = { nullptr, nullptr }; |
michael@0 | 17 | nsZipArchive *Omnijar::sReader[2] = { nullptr, nullptr }; |
michael@0 | 18 | bool Omnijar::sInitialized = false; |
michael@0 | 19 | static bool sIsUnified = false; |
michael@0 | 20 | static bool sIsNested[2] = { false, false }; |
michael@0 | 21 | |
michael@0 | 22 | static const char *sProp[2] = |
michael@0 | 23 | { NS_GRE_DIR, NS_XPCOM_CURRENT_PROCESS_DIR }; |
michael@0 | 24 | |
michael@0 | 25 | #define SPROP(Type) ((Type == mozilla::Omnijar::GRE) ? sProp[GRE] : sProp[APP]) |
michael@0 | 26 | |
michael@0 | 27 | void |
michael@0 | 28 | Omnijar::CleanUpOne(Type aType) |
michael@0 | 29 | { |
michael@0 | 30 | if (sReader[aType]) { |
michael@0 | 31 | sReader[aType]->CloseArchive(); |
michael@0 | 32 | NS_IF_RELEASE(sReader[aType]); |
michael@0 | 33 | } |
michael@0 | 34 | sReader[aType] = nullptr; |
michael@0 | 35 | NS_IF_RELEASE(sPath[aType]); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | void |
michael@0 | 39 | Omnijar::InitOne(nsIFile *aPath, Type aType) |
michael@0 | 40 | { |
michael@0 | 41 | nsCOMPtr<nsIFile> file; |
michael@0 | 42 | if (aPath) { |
michael@0 | 43 | file = aPath; |
michael@0 | 44 | } else { |
michael@0 | 45 | nsCOMPtr<nsIFile> dir; |
michael@0 | 46 | nsDirectoryService::gService->Get(SPROP(aType), NS_GET_IID(nsIFile), getter_AddRefs(dir)); |
michael@0 | 47 | NS_NAMED_LITERAL_CSTRING (kOmnijarName, NS_STRINGIFY(OMNIJAR_NAME)); |
michael@0 | 48 | if (NS_FAILED(dir->Clone(getter_AddRefs(file))) || |
michael@0 | 49 | NS_FAILED(file->AppendNative(kOmnijarName))) |
michael@0 | 50 | return; |
michael@0 | 51 | } |
michael@0 | 52 | bool isFile; |
michael@0 | 53 | if (NS_FAILED(file->IsFile(&isFile)) || !isFile) { |
michael@0 | 54 | // If we're not using an omni.jar for GRE, and we don't have an |
michael@0 | 55 | // omni.jar for APP, check if both directories are the same. |
michael@0 | 56 | if ((aType == APP) && (!sPath[GRE])) { |
michael@0 | 57 | nsCOMPtr<nsIFile> greDir, appDir; |
michael@0 | 58 | bool equals; |
michael@0 | 59 | nsDirectoryService::gService->Get(SPROP(GRE), NS_GET_IID(nsIFile), getter_AddRefs(greDir)); |
michael@0 | 60 | nsDirectoryService::gService->Get(SPROP(APP), NS_GET_IID(nsIFile), getter_AddRefs(appDir)); |
michael@0 | 61 | if (NS_SUCCEEDED(greDir->Equals(appDir, &equals)) && equals) |
michael@0 | 62 | sIsUnified = true; |
michael@0 | 63 | } |
michael@0 | 64 | return; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | bool equals; |
michael@0 | 68 | if ((aType == APP) && (sPath[GRE]) && |
michael@0 | 69 | NS_SUCCEEDED(sPath[GRE]->Equals(file, &equals)) && equals) { |
michael@0 | 70 | // If we're using omni.jar on both GRE and APP and their path |
michael@0 | 71 | // is the same, we're in the unified case. |
michael@0 | 72 | sIsUnified = true; |
michael@0 | 73 | return; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | nsRefPtr<nsZipArchive> zipReader = new nsZipArchive(); |
michael@0 | 77 | if (NS_FAILED(zipReader->OpenArchive(file))) { |
michael@0 | 78 | return; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | nsRefPtr<nsZipHandle> handle; |
michael@0 | 82 | if (NS_SUCCEEDED(nsZipHandle::Init(zipReader, NS_STRINGIFY(OMNIJAR_NAME), getter_AddRefs(handle)))) { |
michael@0 | 83 | zipReader = new nsZipArchive(); |
michael@0 | 84 | if (NS_FAILED(zipReader->OpenArchive(handle))) |
michael@0 | 85 | return; |
michael@0 | 86 | sIsNested[aType] = true; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | CleanUpOne(aType); |
michael@0 | 90 | sReader[aType] = zipReader; |
michael@0 | 91 | NS_IF_ADDREF(sReader[aType]); |
michael@0 | 92 | sPath[aType] = file; |
michael@0 | 93 | NS_IF_ADDREF(sPath[aType]); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | void |
michael@0 | 97 | Omnijar::Init(nsIFile *aGrePath, nsIFile *aAppPath) |
michael@0 | 98 | { |
michael@0 | 99 | InitOne(aGrePath, GRE); |
michael@0 | 100 | InitOne(aAppPath, APP); |
michael@0 | 101 | sInitialized = true; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | void |
michael@0 | 105 | Omnijar::CleanUp() |
michael@0 | 106 | { |
michael@0 | 107 | CleanUpOne(GRE); |
michael@0 | 108 | CleanUpOne(APP); |
michael@0 | 109 | sInitialized = false; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | already_AddRefed<nsZipArchive> |
michael@0 | 113 | Omnijar::GetReader(nsIFile *aPath) |
michael@0 | 114 | { |
michael@0 | 115 | NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized"); |
michael@0 | 116 | |
michael@0 | 117 | bool equals; |
michael@0 | 118 | nsresult rv; |
michael@0 | 119 | |
michael@0 | 120 | if (sPath[GRE] && !sIsNested[GRE]) { |
michael@0 | 121 | rv = sPath[GRE]->Equals(aPath, &equals); |
michael@0 | 122 | if (NS_SUCCEEDED(rv) && equals) |
michael@0 | 123 | return GetReader(GRE); |
michael@0 | 124 | } |
michael@0 | 125 | if (sPath[APP] && !sIsNested[APP]) { |
michael@0 | 126 | rv = sPath[APP]->Equals(aPath, &equals); |
michael@0 | 127 | if (NS_SUCCEEDED(rv) && equals) |
michael@0 | 128 | return GetReader(APP); |
michael@0 | 129 | } |
michael@0 | 130 | return nullptr; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | nsresult |
michael@0 | 134 | Omnijar::GetURIString(Type aType, nsACString &result) |
michael@0 | 135 | { |
michael@0 | 136 | NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized"); |
michael@0 | 137 | |
michael@0 | 138 | result.Truncate(); |
michael@0 | 139 | |
michael@0 | 140 | // Return an empty string for APP in the unified case. |
michael@0 | 141 | if ((aType == APP) && sIsUnified) { |
michael@0 | 142 | return NS_OK; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | nsAutoCString omniJarSpec; |
michael@0 | 146 | if (sPath[aType]) { |
michael@0 | 147 | nsresult rv = NS_GetURLSpecFromActualFile(sPath[aType], omniJarSpec); |
michael@0 | 148 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 149 | return rv; |
michael@0 | 150 | |
michael@0 | 151 | result = "jar:"; |
michael@0 | 152 | if (sIsNested[aType]) |
michael@0 | 153 | result += "jar:"; |
michael@0 | 154 | result += omniJarSpec; |
michael@0 | 155 | result += "!"; |
michael@0 | 156 | if (sIsNested[aType]) |
michael@0 | 157 | result += "/" NS_STRINGIFY(OMNIJAR_NAME) "!"; |
michael@0 | 158 | } else { |
michael@0 | 159 | nsCOMPtr<nsIFile> dir; |
michael@0 | 160 | nsDirectoryService::gService->Get(SPROP(aType), NS_GET_IID(nsIFile), getter_AddRefs(dir)); |
michael@0 | 161 | nsresult rv = NS_GetURLSpecFromActualFile(dir, result); |
michael@0 | 162 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 163 | return rv; |
michael@0 | 164 | } |
michael@0 | 165 | result += "/"; |
michael@0 | 166 | return NS_OK; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | } /* namespace mozilla */ |