Back to index

lightning-sunbird  0.9+nobinonly
nsPrincipal.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) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Christopher A. Aillon <christopher@aillon.com>
00024  *   Giorgio Maone <g.maone@informaction.com>
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 #ifndef nsPrincipal_h__
00041 #define nsPrincipal_h__
00042 
00043 #include "nsAutoPtr.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsVoidArray.h"
00046 #include "nsHashtable.h"
00047 #include "nsJSPrincipals.h"
00048 
00049 class nsIObjectInputStream;
00050 class nsIObjectOutputStream;
00051 
00052 class nsPrincipal : public nsIPrincipal
00053 {
00054 public:
00055   nsPrincipal();
00056 
00057 protected:
00058   virtual ~nsPrincipal();
00059 
00060 public:
00061   // Our refcount is managed by mJSPrincipals.  Use this macro to avoid
00062   // an extra refcount member.
00063   NS_DECL_ISUPPORTS_INHERITED
00064 public:
00065 
00066   NS_DECL_NSIPRINCIPAL
00067   NS_DECL_NSISERIALIZABLE
00068 
00069   // Either Init() or InitFromPersistent() must be called before
00070   // the principal is in a usable state.
00071   nsresult Init(const nsACString& aCertFingerprint,
00072                 const nsACString& aSubjectName,
00073                 const nsACString& aPrettyName,
00074                 nsISupports* aCert,
00075                 nsIURI *aCodebase);
00076   nsresult InitFromPersistent(const char* aPrefName,
00077                               const nsCString& aFingerprint,
00078                               const nsCString& aSubjectName,
00079                               const nsACString& aPrettyName,
00080                               const char* aGrantedList,
00081                               const char* aDeniedList,
00082                               nsISupports* aCert,
00083                               PRBool aIsCert,
00084                               PRBool aTrusted);
00085 
00086   // Call this to ensure that this principal has a subject name, a pretty name,
00087   // and a cert pointer.  This method will throw if there is already a
00088   // different subject name or if this principal has no certificate.
00089   nsresult EnsureCertData(const nsACString& aSubjectName,
00090                           const nsACString& aPrettyName,
00091                           nsISupports* aCert);
00092 
00093   enum AnnotationValue { AnnotationEnabled=1, AnnotationDisabled };
00094 
00095   void SetURI(nsIURI *aURI);
00096   nsresult SetCapability(const char *capability, void **annotation, 
00097                          AnnotationValue value);
00098 
00099   static const char sInvalid[];
00100 
00101 protected:
00102   nsJSPrincipals mJSPrincipals;
00103   nsVoidArray mAnnotations;
00104   nsHashtable mCapabilities;
00105   nsCString mPrefName;
00106   static PRInt32 sCapabilitiesOrdinal;
00107 
00108   // XXXcaa This is a semi-hack.  The best solution here is to keep
00109   // a reference to an interface here, except there is no interface
00110   // that we can use yet.
00111   struct Certificate
00112   {
00113     Certificate(const nsACString& aFingerprint, const nsACString& aSubjectName,
00114                 const nsACString& aPrettyName, nsISupports* aCert)
00115       : fingerprint(aFingerprint),
00116         subjectName(aSubjectName),
00117         prettyName(aPrettyName),
00118         cert(aCert)
00119     {
00120     };
00121     nsCString fingerprint;
00122     nsCString subjectName;
00123     nsCString prettyName;
00124     nsCOMPtr<nsISupports> cert;
00125   };
00126 
00127   nsresult SetCertificate(const nsACString& aFingerprint,
00128                           const nsACString& aSubjectName,
00129                           const nsACString& aPrettyName,
00130                           nsISupports* aCert);
00131 
00132   // Keep this is a pointer, even though it may slightly increase the
00133   // cost of keeping a certificate, this is a good tradeoff though since
00134   // it is very rare that we actually have a certificate.
00135   nsAutoPtr<Certificate> mCert;
00136 
00137   DomainPolicy* mSecurityPolicy;
00138 
00139   nsCOMPtr<nsIURI> mCodebase;
00140   nsCOMPtr<nsIURI> mDomain;
00141   PRPackedBool mTrusted;
00142   PRPackedBool mInitialized;
00143 };
00144 
00145 
00146 #define NS_PRINCIPAL_CLASSNAME  "principal"
00147 #define NS_PRINCIPAL_CONTRACTID "@mozilla.org/principal;1"
00148 #define NS_PRINCIPAL_CID \
00149   { 0x36102b6b, 0x7b62, 0x451a, \
00150     { 0xa1, 0xc8, 0xa0, 0xd4, 0x56, 0xc9, 0x2d, 0xc5 }}
00151 
00152 
00153 #endif // nsPrincipal_h__