1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/build/Omnijar.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,131 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_Omnijar_h 1.10 +#define mozilla_Omnijar_h 1.11 + 1.12 +#include "nscore.h" 1.13 +#include "nsCOMPtr.h" 1.14 +#include "nsString.h" 1.15 +#include "nsIFile.h" 1.16 +#include "nsZipArchive.h" 1.17 + 1.18 +class nsIURI; 1.19 + 1.20 +namespace mozilla { 1.21 + 1.22 +class Omnijar { 1.23 +private: 1.24 +/** 1.25 + * Store an nsIFile for an omni.jar. We can store two paths here, one 1.26 + * for GRE (corresponding to resource://gre/) and one for APP 1.27 + * (corresponding to resource:/// and resource://app/), but only 1.28 + * store one when both point to the same location (unified). 1.29 + */ 1.30 +static nsIFile *sPath[2]; 1.31 + 1.32 +/** 1.33 + * Cached nsZipArchives for the corresponding sPath 1.34 + */ 1.35 +static nsZipArchive *sReader[2]; 1.36 + 1.37 +/** 1.38 + * Has Omnijar::Init() been called? 1.39 + */ 1.40 +static bool sInitialized; 1.41 + 1.42 +public: 1.43 +enum Type { 1.44 + GRE = 0, 1.45 + APP = 1 1.46 +}; 1.47 + 1.48 +/** 1.49 + * Returns whether SetBase has been called at least once with 1.50 + * a valid nsIFile 1.51 + */ 1.52 +static inline bool 1.53 +IsInitialized() 1.54 +{ 1.55 + return sInitialized; 1.56 +} 1.57 + 1.58 +/** 1.59 + * Initializes the Omnijar API with the given directory or file for GRE and 1.60 + * APP. Each of the paths given can be: 1.61 + * - a file path, pointing to the omnijar file, 1.62 + * - a directory path, pointing to a directory containing an "omni.jar" file, 1.63 + * - nullptr for autodetection of an "omni.jar" file. 1.64 + */ 1.65 +static void Init(nsIFile *aGrePath = nullptr, nsIFile *aAppPath = nullptr); 1.66 + 1.67 +/** 1.68 + * Cleans up the Omnijar API 1.69 + */ 1.70 +static void CleanUp(); 1.71 + 1.72 +/** 1.73 + * Returns an nsIFile pointing to the omni.jar file for GRE or APP. 1.74 + * Returns nullptr when there is no corresponding omni.jar. 1.75 + * Also returns nullptr for APP in the unified case. 1.76 + */ 1.77 +static inline already_AddRefed<nsIFile> 1.78 +GetPath(Type aType) 1.79 +{ 1.80 + NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized"); 1.81 + nsCOMPtr<nsIFile> path = sPath[aType]; 1.82 + return path.forget(); 1.83 +} 1.84 + 1.85 +/** 1.86 + * Returns whether GRE or APP use an omni.jar. Returns PR_False for 1.87 + * APP when using an omni.jar in the unified case. 1.88 + */ 1.89 +static inline bool 1.90 +HasOmnijar(Type aType) 1.91 +{ 1.92 + NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized"); 1.93 + return !!sPath[aType]; 1.94 +} 1.95 + 1.96 +/** 1.97 + * Returns a nsZipArchive pointer for the omni.jar file for GRE or 1.98 + * APP. Returns nullptr in the same cases GetPath() would. 1.99 + */ 1.100 +static inline already_AddRefed<nsZipArchive> 1.101 +GetReader(Type aType) 1.102 +{ 1.103 + NS_ABORT_IF_FALSE(IsInitialized(), "Omnijar not initialized"); 1.104 + nsRefPtr<nsZipArchive> reader = sReader[aType]; 1.105 + return reader.forget(); 1.106 +} 1.107 + 1.108 +/** 1.109 + * Returns a nsZipArchive pointer for the given path IAOI the given 1.110 + * path is the omni.jar for either GRE or APP. 1.111 + */ 1.112 +static already_AddRefed<nsZipArchive> GetReader(nsIFile *aPath); 1.113 + 1.114 +/** 1.115 + * Returns the URI string corresponding to the omni.jar or directory 1.116 + * for GRE or APP. i.e. jar:/path/to/omni.jar!/ for omni.jar and 1.117 + * /path/to/base/dir/ otherwise. Returns an empty string for APP in 1.118 + * the unified case. 1.119 + * The returned URI is guaranteed to end with a slash. 1.120 + */ 1.121 +static nsresult GetURIString(Type aType, nsACString &result); 1.122 + 1.123 +private: 1.124 +/** 1.125 + * Used internally, respectively by Init() and CleanUp() 1.126 + */ 1.127 +static void InitOne(nsIFile *aPath, Type aType); 1.128 +static void CleanUpOne(Type aType); 1.129 + 1.130 +}; /* class Omnijar */ 1.131 + 1.132 +} /* namespace mozilla */ 1.133 + 1.134 +#endif /* mozilla_Omnijar_h */