Back to index

lightning-sunbird  0.9+nobinonly
nsChromeRegistry.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Original Author: David W. Hyatt (hyatt@netscape.com)
00024  *   Benjamin Smedberg <benjamin@smedbergs.us>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsIChromeRegistry.h"
00041 #include "nsIToolkitChromeRegistry.h"
00042 #include "nsIXULOverlayProvider.h"
00043 #include "nsIObserver.h"
00044 #include "nsWeakReference.h"
00045 
00046 #include "pldhash.h"
00047 
00048 #include "nsCOMArray.h"
00049 #include "nsString.h"
00050 #include "nsTHashtable.h"
00051 #include "nsURIHashKey.h"
00052 #include "nsVoidArray.h"
00053 #include "nsInterfaceHashtable.h"
00054 
00055 struct PRFileDesc;
00056 class nsIAtom;
00057 class nsICSSStyleSheet;
00058 class nsIDOMWindowInternal;
00059 class nsILocalFile;
00060 class nsIRDFDataSource;
00061 class nsIRDFResource;
00062 class nsIRDFService;
00063 class nsIURL;
00064 
00065 // for component registration
00066 // {47049e42-1d87-482a-984d-56ae185e367a}
00067 #define NS_CHROMEREGISTRY_CID \
00068 { 0x47049e42, 0x1d87, 0x482a, { 0x98, 0x4d, 0x56, 0xae, 0x18, 0x5e, 0x36, 0x7a } }
00069 
00070 class nsChromeRegistry : public nsIToolkitChromeRegistry,
00071                          public nsIXULOverlayProvider,
00072                          public nsIObserver,
00073                          public nsSupportsWeakReference
00074 {
00075 public:
00076   NS_DECL_ISUPPORTS
00077 
00078   // nsIChromeRegistry methods:
00079   NS_DECL_NSICHROMEREGISTRY
00080   NS_DECL_NSIXULCHROMEREGISTRY
00081   NS_DECL_NSITOOLKITCHROMEREGISTRY
00082   NS_DECL_NSIXULOVERLAYPROVIDER
00083 
00084   NS_DECL_NSIOBSERVER
00085 
00086   // nsChromeRegistry methods:
00087   nsChromeRegistry() : mInitialized(PR_FALSE) { }
00088   ~nsChromeRegistry();
00089 
00090   nsresult Init();
00091 
00092   static nsChromeRegistry* gChromeRegistry;
00093 
00094   static nsresult Canonify(nsIURL* aChromeURL);
00095 
00096 protected:
00097   nsresult GetDynamicInfo(nsIURI *aChromeURL, PRBool aIsOverlay, nsISimpleEnumerator **aResult);
00098 
00099   static nsresult LoadStyleSheetWithURL(nsIURI* aURL, PRBool aAllowUnsafeRules, nsICSSStyleSheet** aSheet);
00100 
00101   nsresult LoadInstallDataSource();
00102   nsresult LoadProfileDataSource();
00103 
00104   void FlushSkinCaches();
00105   void FlushAllCaches();
00106 
00107 private:
00108   static nsresult RefreshWindow(nsIDOMWindowInternal* aWindow);
00109   static nsresult GetProviderAndPath(nsIURL* aChromeURL,
00110                                      nsACString& aProvider, nsACString& aPath);
00111 
00112   NS_HIDDEN_(void) ProcessProvider(PRFileDesc *fd, nsIRDFService* aRDFs,
00113                                    nsIRDFDataSource* ds, nsIRDFResource* aRoot,
00114                                    PRBool aIsLocale, const nsACString& aBaseURL);
00115   NS_HIDDEN_(void) ProcessOverlays(PRFileDesc *fd, nsIRDFDataSource* ds,
00116                                    nsIRDFResource* aRoot,
00117                                    const nsCSubstring& aType);
00118 
00119   NS_HIDDEN_(nsresult) ProcessManifest(nsILocalFile* aManifest, PRBool aSkinOnly);
00120   NS_HIDDEN_(nsresult) ProcessManifestBuffer(char *aBuffer, PRInt32 aLength, nsILocalFile* aManifest, PRBool aSkinOnly);
00121   NS_HIDDEN_(nsresult) ProcessNewChromeFile(nsILocalFile *aListFile, nsIURI* aManifest);
00122   NS_HIDDEN_(nsresult) ProcessNewChromeBuffer(char *aBuffer, PRInt32 aLength, nsIURI* aManifest);
00123 
00124 public:
00125   struct ProviderEntry
00126   {
00127     ProviderEntry(const nsACString& aProvider, nsIURI* aBase) :
00128       provider(aProvider),
00129       baseURI(aBase) { }
00130 
00131     nsCString        provider;
00132     nsCOMPtr<nsIURI> baseURI;
00133   };
00134 
00135   class nsProviderArray
00136   {
00137   public:
00138     nsProviderArray() :
00139       mArray(1) { }
00140     ~nsProviderArray()
00141       { Clear(); }
00142 
00143     // When looking up locales and skins, the "selected" locale is not always
00144     // available. This enum identifies what kind of match is desired/found.
00145     enum MatchType {
00146       EXACT = 0,
00147       LOCALE = 1, // "en-GB" is selected, we found "en-US"
00148       ANY = 2
00149     };
00150 
00151     nsIURI* GetBase(const nsACString& aPreferred, MatchType aType);
00152     const nsACString& GetSelected(const nsACString& aPreferred, MatchType aType);
00153     void    SetBase(const nsACString& aProvider, nsIURI* base);
00154     void    EnumerateToArray(nsCStringArray *a);
00155     void    Clear();
00156 
00157   private:
00158     ProviderEntry* GetProvider(const nsACString& aPreferred, MatchType aType);
00159 
00160     nsVoidArray mArray;
00161   };
00162 
00163   struct PackageEntry : public PLDHashEntryHdr
00164   {
00165     PackageEntry(const nsACString& package);
00166     ~PackageEntry() { }
00167 
00168     // Available flags
00169     enum {
00170       // This is a "platform" package (e.g. chrome://global-platform/).
00171       // Appends one of win/ unix/ mac/ to the base URI.
00172       PLATFORM_PACKAGE = 1 << 0,
00173 
00174       // This package should use the new XPCNativeWrappers to separate
00175       // content from chrome. This flag is currently unused (because we call
00176       // into xpconnect at registration time).
00177       XPCNATIVEWRAPPERS = 1 << 1
00178     };
00179 
00180     nsCString        package;
00181     nsCOMPtr<nsIURI> baseURI;
00182     PRUint32         flags;
00183     nsProviderArray  locales;
00184     nsProviderArray  skins;
00185   };
00186 
00187 private:
00188   static const void*   GetKey(PLDHashTable *table, PLDHashEntryHdr *entry);
00189   static PLDHashNumber HashKey(PLDHashTable *table, const void *key);
00190   static PRBool        MatchKey(PLDHashTable *table, const PLDHashEntryHdr *entry,
00191                                 const void *key);
00192   static void          ClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry);
00193   static PRBool        InitEntry(PLDHashTable *table, PLDHashEntryHdr *entry,
00194                                  const void *key);
00195 
00196   static const PLDHashTableOps kTableOps;
00197 
00198 public:
00199   class OverlayListEntry : public nsURIHashKey
00200   {
00201   public:
00202     typedef nsURIHashKey::KeyType        KeyType;
00203     typedef nsURIHashKey::KeyTypePointer KeyTypePointer;
00204 
00205     OverlayListEntry(KeyTypePointer aKey) : nsURIHashKey(aKey) { }
00206     OverlayListEntry(OverlayListEntry& toCopy) : nsURIHashKey(toCopy),
00207                                                  mArray(toCopy.mArray) { }
00208     ~OverlayListEntry() { }
00209 
00210     void AddURI(nsIURI* aURI);
00211 
00212     nsCOMArray<nsIURI> mArray;
00213   };
00214 
00215   class OverlayListHash
00216   {
00217   public:
00218     OverlayListHash() { }
00219     ~OverlayListHash() { }
00220 
00221     PRBool Init() { return mTable.Init(); }
00222     void Add(nsIURI* aBase, nsIURI* aOverlay);
00223     void Clear() { mTable.Clear(); }
00224     const nsCOMArray<nsIURI>* GetArray(nsIURI* aBase);
00225 
00226   private:
00227     nsTHashtable<OverlayListEntry> mTable;
00228   };
00229 
00230 private:
00231   PRBool mInitialized;
00232 
00233   // Hash of package names ("global") to PackageEntry objects
00234   PLDHashTable mPackagesHash;
00235 
00236   // Hashes on the file to be overlaid (chrome://browser/content/browser.xul)
00237   // to a list of overlays/stylesheets
00238   OverlayListHash mOverlayHash;
00239   OverlayListHash mStyleHash;
00240 
00241   // "Override" table (chrome URI string -> real URI)
00242   nsInterfaceHashtable<nsURIHashKey, nsIURI> mOverrideTable;
00243 
00244   nsCString mSelectedLocale;
00245   nsCString mSelectedSkin;
00246 };