LCOV - code coverage report
Current view: directory - xpcom/build - Omnijar.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 81 48 59.3 %
Date: 2012-06-02 Functions: 6 6 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
       2                 :  * ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is Mozilla code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Mozilla Foundation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2010
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Michael Wu <mwu@mozilla.com>
      24                 :  *   Mike Hommey <mh@glandium.org>
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #include "Omnijar.h"
      41                 : 
      42                 : #include "nsDirectoryService.h"
      43                 : #include "nsDirectoryServiceDefs.h"
      44                 : #include "nsIFile.h"
      45                 : #include "nsZipArchive.h"
      46                 : #include "nsNetUtil.h"
      47                 : 
      48                 : namespace mozilla {
      49                 : 
      50                 : nsIFile *Omnijar::sPath[2] = { nsnull, nsnull };
      51                 : nsZipArchive *Omnijar::sReader[2] = { nsnull, nsnull };
      52                 : bool Omnijar::sInitialized = false;
      53                 : static bool sIsUnified = false;
      54                 : static bool sIsNested[2] = { false, false };
      55                 : 
      56                 : static const char *sProp[2] =
      57                 :     { NS_GRE_DIR, NS_XPCOM_CURRENT_PROCESS_DIR };
      58                 : 
      59                 : #define SPROP(Type) ((Type == mozilla::Omnijar::GRE) ? sProp[GRE] : sProp[APP])
      60                 : 
      61                 : void
      62            2838 : Omnijar::CleanUpOne(Type aType)
      63                 : {
      64            2838 :     if (sReader[aType]) {
      65               0 :         sReader[aType]->CloseArchive();
      66               0 :         NS_IF_RELEASE(sReader[aType]);
      67                 :     }
      68            2838 :     sReader[aType] = nsnull;
      69            2838 :     NS_IF_RELEASE(sPath[aType]);
      70            2838 : }
      71                 : 
      72                 : void
      73            2838 : Omnijar::InitOne(nsIFile *aPath, Type aType)
      74                 : {
      75            5676 :     nsCOMPtr<nsIFile> file;
      76            2838 :     if (aPath) {
      77               0 :         file = aPath;
      78                 :     } else {
      79            5676 :         nsCOMPtr<nsIFile> dir;
      80            2838 :         nsDirectoryService::gService->Get(SPROP(aType), NS_GET_IID(nsIFile), getter_AddRefs(dir));
      81            5676 :         NS_NAMED_LITERAL_CSTRING (kOmnijarName, NS_STRINGIFY(OMNIJAR_NAME));
      82            5676 :         if (NS_FAILED(dir->Clone(getter_AddRefs(file))) ||
      83            2838 :             NS_FAILED(file->AppendNative(kOmnijarName)))
      84                 :             return;
      85                 :     }
      86                 :     bool isFile;
      87            2838 :     if (NS_FAILED(file->IsFile(&isFile)) || !isFile) {
      88                 :         // If we're not using an omni.jar for GRE, and we don't have an
      89                 :         // omni.jar for APP, check if both directories are the same.
      90            2838 :         if ((aType == APP) && (!sPath[GRE])) {
      91            2838 :             nsCOMPtr<nsIFile> greDir, appDir;
      92                 :             bool equals;
      93            1419 :             nsDirectoryService::gService->Get(SPROP(GRE), NS_GET_IID(nsIFile), getter_AddRefs(greDir));
      94            1419 :             nsDirectoryService::gService->Get(SPROP(APP), NS_GET_IID(nsIFile), getter_AddRefs(appDir));
      95            1419 :             if (NS_SUCCEEDED(greDir->Equals(appDir, &equals)) && equals)
      96            1419 :                 sIsUnified = true;
      97                 :         }
      98                 :         return;
      99                 :     }
     100                 : 
     101                 :     bool equals;
     102               0 :     if ((aType == APP) && (sPath[GRE]) &&
     103               0 :         NS_SUCCEEDED(sPath[GRE]->Equals(file, &equals)) && equals) {
     104                 :         // If we're using omni.jar on both GRE and APP and their path
     105                 :         // is the same, we're in the unified case.
     106               0 :         sIsUnified = true;
     107                 :         return;
     108                 :     }
     109                 : 
     110               0 :     nsRefPtr<nsZipArchive> zipReader = new nsZipArchive();
     111               0 :     if (NS_FAILED(zipReader->OpenArchive(file))) {
     112                 :         return;
     113                 :     }
     114                 : 
     115               0 :     nsRefPtr<nsZipHandle> handle;
     116               0 :     if (NS_SUCCEEDED(nsZipHandle::Init(zipReader, NS_STRINGIFY(OMNIJAR_NAME), getter_AddRefs(handle)))) {
     117               0 :         zipReader = new nsZipArchive();
     118               0 :         if (NS_FAILED(zipReader->OpenArchive(handle)))
     119                 :             return;
     120               0 :         sIsNested[aType] = true;
     121                 :     }
     122                 : 
     123               0 :     CleanUpOne(aType);
     124               0 :     sReader[aType] = zipReader;
     125               0 :     NS_IF_ADDREF(sReader[aType]);
     126               0 :     sPath[aType] = file;
     127               0 :     NS_IF_ADDREF(sPath[aType]);
     128                 : }
     129                 : 
     130                 : void
     131            1419 : Omnijar::Init(nsIFile *aGrePath, nsIFile *aAppPath)
     132                 : {
     133            1419 :     InitOne(aGrePath, GRE);
     134            1419 :     InitOne(aAppPath, APP);
     135            1419 :     sInitialized = true;
     136            1419 : }
     137                 : 
     138                 : void
     139            1419 : Omnijar::CleanUp()
     140                 : {
     141            1419 :     CleanUpOne(GRE);
     142            1419 :     CleanUpOne(APP);
     143            1419 :     sInitialized = false;
     144            1419 : }
     145                 : 
     146                 : already_AddRefed<nsZipArchive>
     147            1666 : Omnijar::GetReader(nsIFile *aPath)
     148                 : {
     149            1666 :     NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized");
     150                 : 
     151                 :     bool equals;
     152                 :     nsresult rv;
     153                 : 
     154            1666 :     if (sPath[GRE] && !sIsNested[GRE]) {
     155               0 :         rv = sPath[GRE]->Equals(aPath, &equals);
     156               0 :         if (NS_SUCCEEDED(rv) && equals)
     157               0 :             return GetReader(GRE);
     158                 :     }
     159            1666 :     if (sPath[APP] && !sIsNested[APP]) {
     160               0 :         rv = sPath[APP]->Equals(aPath, &equals);
     161               0 :         if (NS_SUCCEEDED(rv) && equals)
     162               0 :             return GetReader(APP);
     163                 :     }
     164            1666 :     return nsnull;
     165                 : }
     166                 : 
     167                 : nsresult
     168           16489 : Omnijar::GetURIString(Type aType, nsACString &result)
     169                 : {
     170           16489 :     NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized");
     171                 : 
     172           16489 :     result.Truncate();
     173                 : 
     174                 :     // Return an empty string for APP in the unified case.
     175           16489 :     if ((aType == APP) && sIsUnified) {
     176            2311 :         return NS_OK;
     177                 :     }
     178                 : 
     179           28356 :     nsCAutoString omniJarSpec;
     180           14178 :     if (sPath[aType]) {
     181               0 :         nsresult rv = NS_GetURLSpecFromActualFile(sPath[aType], omniJarSpec);
     182               0 :         NS_ENSURE_SUCCESS(rv, rv);
     183                 : 
     184               0 :         result = "jar:";
     185               0 :         if (sIsNested[aType])
     186               0 :             result += "jar:";
     187               0 :         result += omniJarSpec;
     188               0 :         result += "!";
     189               0 :         if (sIsNested[aType])
     190               0 :             result += "/" NS_STRINGIFY(OMNIJAR_NAME) "!";
     191                 :     } else {
     192           28356 :         nsCOMPtr<nsIFile> dir;
     193           14178 :         nsDirectoryService::gService->Get(SPROP(aType), NS_GET_IID(nsIFile), getter_AddRefs(dir));
     194           14178 :         nsresult rv = NS_GetURLSpecFromActualFile(dir, result);
     195           14178 :         NS_ENSURE_SUCCESS(rv, rv);
     196                 :     }
     197           14178 :     result += "/";
     198           14178 :     return NS_OK;
     199                 : }
     200                 : 
     201                 : } /* namespace mozilla */

Generated by: LCOV version 1.7