diff --git a/mozilla/build/mac/NGLayoutBuildList.pm b/mozilla/build/mac/NGLayoutBuildList.pm index d467a4cd218..78f736934df 100644 --- a/mozilla/build/mac/NGLayoutBuildList.pm +++ b/mozilla/build/mac/NGLayoutBuildList.pm @@ -562,6 +562,11 @@ sub MakeResourceAliases() _InstallResources(":mozilla:extensions:wallet:cookieviewer:MANIFEST_SKIN", "$wallet_chrome_dir:skin:default:", 0); _InstallResources(":mozilla:extensions:wallet:signonviewer:MANIFEST_SKIN", "$wallet_chrome_dir:skin:default:", 0); } + { + my($caps_chrome_dir) = "$chrome_dir" . "caps"; + _InstallResources(":mozilla:caps:src:MANIFEST_PROPERTIES", "$caps_chrome_dir:locale:en-US:", 0); + } + # QA Menu diff --git a/mozilla/caps/idl/nsIPrincipal.idl b/mozilla/caps/idl/nsIPrincipal.idl index e44b862b8f3..df80b900b0e 100644 --- a/mozilla/caps/idl/nsIPrincipal.idl +++ b/mozilla/caps/idl/nsIPrincipal.idl @@ -14,10 +14,11 @@ * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1999 Netscape Communications Corporation. All + * Copyright (C) 1999-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): + * Norris Boyd */ /* Defines the abstract interface for a principal. */ @@ -28,6 +29,8 @@ struct JSPrincipals; %} +interface nsIPref; + [ptr] native JSPrincipals(JSPrincipals); [uuid(ff9313d0-25e1-11d2-8160-006008119d7a)] @@ -38,13 +41,15 @@ interface nsIPrincipal : nsISupports { const short ENABLE_DENIED = 2; const short ENABLE_WITH_USER_PERMISSION = 3; - void ToString(out string result); + string ToString(); - void Equals(in nsIPrincipal other, out boolean result); + string ToUserVisibleString(); + + boolean Equals(in nsIPrincipal other); unsigned long HashValue(); - void GetJSPrincipals(out JSPrincipals jsprin); + JSPrincipals GetJSPrincipals(); short CanEnableCapability(in string capability); @@ -58,7 +63,9 @@ interface nsIPrincipal : nsISupports { void DisableCapability(in string capability, inout voidStar annotation); - void CapabilitiesToString(out string result); + string ToStreamableForm(); + + void WriteToPrefs(in nsIPref prefs); }; diff --git a/mozilla/caps/include/nsBasePrincipal.h b/mozilla/caps/include/nsBasePrincipal.h index 85e7d50e8dd..892dde3dfad 100644 --- a/mozilla/caps/include/nsBasePrincipal.h +++ b/mozilla/caps/include/nsBasePrincipal.h @@ -12,8 +12,11 @@ * * The Initial Developer of this code under the NPL is Netscape * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1999 Netscape Communications Corporation. All Rights + * Copyright (C) 1999-2000 Netscape Communications Corporation. All Rights * Reserved. + * + * Contributors: + * Norris Boyd */ /* Shared implementation code for principals. */ @@ -33,9 +36,6 @@ public: virtual ~nsBasePrincipal(void); - NS_IMETHOD - CapabilitiesToString(char** aCapString); - NS_IMETHOD GetJSPrincipals(JSPrincipals **jsprin); @@ -59,8 +59,14 @@ public: DisableCapability(const char *capability, void **annotation); nsresult - Init(const char* data); - + InitFromPersistent(const char *name, const char *data); + + NS_IMETHOD + WriteToPrefs(nsIPref *prefs); + + NS_IMETHOD + ToStreamableForm(char **result); + protected: enum AnnotationValue { AnnotationEnabled=1, AnnotationDisabled }; @@ -71,6 +77,8 @@ protected: nsJSPrincipals mJSPrincipals; nsVoidArray mAnnotations; nsHashtable *mCapabilities; + char *mPrefName; + static int mCapabilitiesOrdinal; }; // special AddRef/Release to unify reference counts between XPCOM diff --git a/mozilla/caps/include/nsCertificatePrincipal.h b/mozilla/caps/include/nsCertificatePrincipal.h index ff49de822d7..48974f9c3c4 100644 --- a/mozilla/caps/include/nsCertificatePrincipal.h +++ b/mozilla/caps/include/nsCertificatePrincipal.h @@ -14,10 +14,11 @@ * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1998 Netscape Communications Corporation. All + * Copyright (C) 1998-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): + * Norris Boyd */ /* describes principals for use with signed scripts */ @@ -41,13 +42,15 @@ public: NS_IMETHOD ToString(char **result); + NS_IMETHOD ToUserVisibleString(char **result); + NS_IMETHOD Equals(nsIPrincipal *other, PRBool *result); NS_IMETHOD HashValue(PRUint32 *result); NS_IMETHOD CanEnableCapability(const char *capability, PRInt16 *result); - NS_IMETHOD Init(const char* data); + NS_IMETHOD InitFromPersistent(const char *name, const char* data); NS_IMETHOD Init(const char* aIssuerName, const char* aSerialNumber); diff --git a/mozilla/caps/include/nsCodebasePrincipal.h b/mozilla/caps/include/nsCodebasePrincipal.h index 9b23ebd4b78..20238a199ec 100644 --- a/mozilla/caps/include/nsCodebasePrincipal.h +++ b/mozilla/caps/include/nsCodebasePrincipal.h @@ -14,10 +14,11 @@ * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1998 Netscape Communications Corporation. All + * Copyright (C) 1998-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): + * Norris Boyd */ /* describes principals by their orginating URIs */ @@ -44,6 +45,8 @@ public: NS_IMETHOD ToString(char **result); + NS_IMETHOD ToUserVisibleString(char **result); + NS_IMETHOD Equals(nsIPrincipal *other, PRBool *result); NS_IMETHOD HashValue(PRUint32 *result); @@ -56,7 +59,7 @@ public: Init(nsIURI *uri); nsresult - Init(const char* data); + InitFromPersistent(const char *name, const char* data); virtual ~nsCodebasePrincipal(void); diff --git a/mozilla/caps/include/nsScriptSecurityManager.h b/mozilla/caps/include/nsScriptSecurityManager.h index d5b8fca9786..6ad732e94a7 100644 --- a/mozilla/caps/include/nsScriptSecurityManager.h +++ b/mozilla/caps/include/nsScriptSecurityManager.h @@ -14,11 +14,13 @@ * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1998-1999 Netscape Communications Corporation. All + * Copyright (C) 1998-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): + * Norris Boyd */ + #ifndef _NS_SCRIPT_SECURITY_MANAGER_H_ #define _NS_SCRIPT_SECURITY_MANAGER_H_ @@ -80,20 +82,24 @@ private: InitFromPrefs(); static void - enumeratePolicyCallback(const char *prefName, void *data); + EnumeratePolicyCallback(const char *prefName, void *data); static void - enumeratePrincipalsCallback(const char *prefName, void *data); + EnumeratePrincipalsCallback(const char *prefName, void *data); static int JSEnabledPrefChanged(const char *pref, void *data); + static int + PrincipalPrefChanged(const char *pref, void *data); + nsObjectHashtable *mOriginToPolicyMap; nsIPref *mPrefs; nsIPrincipal *mSystemPrincipal; nsSupportsHashtable *mPrincipals; PRBool mIsJavaScriptEnabled; PRBool mIsMailJavaScriptEnabled; + PRBool mIsWritingPrefs; unsigned char hasPolicyVector[(NS_DOM_PROP_MAX >> 3) + 1]; unsigned char hasDomainPolicyVector[(NS_DOM_PROP_MAX >> 3) + 1]; }; diff --git a/mozilla/caps/include/nsSystemPrincipal.h b/mozilla/caps/include/nsSystemPrincipal.h index 9fbb71b7698..a3fb9e1498a 100644 --- a/mozilla/caps/include/nsSystemPrincipal.h +++ b/mozilla/caps/include/nsSystemPrincipal.h @@ -14,10 +14,11 @@ * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1999 Netscape Communications Corporation. All + * Copyright (C) 1999-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): + * Norris Boyd */ /* The privileged system principal. */ @@ -39,6 +40,8 @@ public: NS_IMETHOD ToString(char **result); + NS_IMETHOD ToUserVisibleString(char **result); + NS_IMETHOD Equals(nsIPrincipal *other, PRBool *result); NS_IMETHOD HashValue(PRUint32 *result); @@ -57,6 +60,8 @@ public: NS_IMETHOD DisableCapability(const char *capability, void * *annotation); + NS_IMETHOD ToStreamableForm(char **result); + nsSystemPrincipal(); NS_IMETHOD Init(); diff --git a/mozilla/caps/src/MANIFEST_PROPERTIES b/mozilla/caps/src/MANIFEST_PROPERTIES new file mode 100644 index 00000000000..80cd8f8715b --- /dev/null +++ b/mozilla/caps/src/MANIFEST_PROPERTIES @@ -0,0 +1 @@ +security.properties diff --git a/mozilla/caps/src/Makefile.in b/mozilla/caps/src/Makefile.in index 10166fac64a..2bcc892adee 100644 --- a/mozilla/caps/src/Makefile.in +++ b/mozilla/caps/src/Makefile.in @@ -13,7 +13,7 @@ # # The Initial Developer of the Original Code is Netscape # Communications Corporation. Portions created by Netscape are -# Copyright (C) 1998 Netscape Communications Corporation. All +# Copyright (C) 1998-2000 Netscape Communications Corporation. All # Rights Reserved. # # Contributor(s): @@ -40,6 +40,9 @@ CPPSRCS = \ nsSecurityManagerFactory.cpp \ $(NULL) +CHROME_FILES = \ + $(srcdir)/security.properties + EXTRA_DSO_LDOPTS = \ -L$(DIST)/bin \ -L$(DIST)/lib \ @@ -53,3 +56,6 @@ include $(topsrcdir)/config/rules.mk INCLUDES += -I$(topsrcdir)/include -I$(srcdir)/../include +install:: + $(INSTALL) $(CHROME_FILES) $(DIST)/bin/chrome/security/locale/en-US + diff --git a/mozilla/caps/src/makefile.win b/mozilla/caps/src/makefile.win index 00b27d6fbb9..58a28e7e4e0 100755 --- a/mozilla/caps/src/makefile.win +++ b/mozilla/caps/src/makefile.win @@ -14,7 +14,7 @@ # # The Initial Developer of the Original Code is Netscape # Communications Corporation. Portions created by Netscape are -# Copyright (C) 1998 Netscape Communications Corporation. All +# Copyright (C) 1998-2000 Netscape Communications Corporation. All # Rights Reserved. # # Contributor(s): @@ -115,8 +115,10 @@ include <$(DEPTH)/config/rules.mak> install:: $(DLL) $(MAKE_INSTALL) .\$(OBJDIR)\$(DLLNAME).dll $(DIST)\bin\components $(MAKE_INSTALL) .\$(OBJDIR)\$(DLLNAME).lib $(DIST)\lib + $(MAKE_INSTALL) security.properties $(DIST)\bin\chrome\security\locale\en-US clobber:: rm -f $(DIST)\lib\$(DLLNAME).lib rm -f $(DIST)\bin\components\$(DLLNAME).dll + rm -f $(DIST)\bin\chrome\security\locale\en-US\security.properties diff --git a/mozilla/caps/src/nsBasePrincipal.cpp b/mozilla/caps/src/nsBasePrincipal.cpp index 9269678548c..f38a5fc7e2d 100644 --- a/mozilla/caps/src/nsBasePrincipal.cpp +++ b/mozilla/caps/src/nsBasePrincipal.cpp @@ -12,19 +12,23 @@ * * The Initial Developer of this code under the NPL is Netscape * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1999 Netscape Communications Corporation. All Rights + * Copyright (C) 1999-2000 Netscape Communications Corporation. All Rights * Reserved. + * + * Contributor(s): + * Norris Boyd */ #include "nsBasePrincipal.h" #include "nsString.h" #include "plstr.h" +#include "nsIPref.h" ////////////////////////// nsBasePrincipal::nsBasePrincipal() - : mCapabilities(nsnull) + : mCapabilities(nsnull), mPrefName(nsnull) { } @@ -40,6 +44,8 @@ nsBasePrincipal::~nsBasePrincipal(void) { mAnnotations.EnumerateForwards(deleteElement, nsnull); delete mCapabilities; + if (mPrefName) + Recycle(mPrefName); } NS_IMETHODIMP @@ -136,8 +142,10 @@ nsBasePrincipal::SetCapability(const char *capability, void **annotation, return NS_OK; } +int nsBasePrincipal::mCapabilitiesOrdinal = 0; + nsresult -nsBasePrincipal::Init(const char* data) +nsBasePrincipal::InitFromPersistent(const char *name, const char* data) { // Parses capabilities strings of the form // "Capability=value ..." @@ -145,6 +153,17 @@ nsBasePrincipal::Init(const char* data) // where value is from 0 to 3 as defined in nsIPrincipal.idl if (mCapabilities) mCapabilities->Reset(); + + nsCAutoString nameString(name); + mPrefName = nameString.ToNewCString(); + + static const char *prefix = ".X"; + const char *p = PL_strstr(name, prefix); + if (p) { + int n = atoi(p + sizeof(prefix)-1); + if (mCapabilitiesOrdinal <= n) + mCapabilitiesOrdinal = n+1; + } for (;;) { @@ -166,32 +185,46 @@ nsBasePrincipal::Init(const char* data) } PR_STATIC_CALLBACK(PRBool) -AppendCapability(nsHashKey *aKey, void* aData, void* aStr) +AppendCapability(nsHashKey *aKey, void *aData, void *aStr) { - nsAutoString name( ((nsStringKey*)aKey)->GetString() ); char value = (char)((unsigned int)aData) + '0'; - nsString* capStr = (nsString*)aStr; - + nsCString *capStr = (nsCString*) aStr; capStr->Append(' '); - capStr->Append(name); + capStr->Append(((nsStringKey *) aKey)->GetString()); capStr->Append('='); capStr->Append(value); - return (capStr != nsnull); + return PR_TRUE; } + NS_IMETHODIMP -nsBasePrincipal::CapabilitiesToString(char** aStr) +nsBasePrincipal::WriteToPrefs(nsIPref *aPref) { - if (!mCapabilities || !aStr) - return NS_OK; - - nsAutoString capStr; - // The following line is a guess at how long the capabilities string - // will be (~15 chars per capability). This will minimize copying. - capStr.SetCapacity(mCapabilities->Count() * 15); - mCapabilities->Enumerate(AppendCapability, (void*)&capStr); - *aStr = capStr.ToNewCString(); - if (!(*aStr)) return NS_ERROR_OUT_OF_MEMORY; - - return NS_OK; + char *streamableForm; + if (NS_FAILED(ToStreamableForm(&streamableForm))) + return NS_ERROR_FAILURE; + if (!mPrefName) { + nsCAutoString s("security.principal.X"); + s += mCapabilitiesOrdinal++; + mPrefName = s.ToNewCString(); + } + nsresult rv = aPref->SetCharPref(mPrefName, streamableForm); + Recycle(streamableForm); + return rv; } + +NS_IMETHODIMP +nsBasePrincipal::ToStreamableForm(char **aResult) +{ + if (NS_FAILED(ToString(aResult))) + return NS_ERROR_FAILURE; + if (mCapabilities) { + nsCAutoString result(*aResult); + mCapabilities->Enumerate(AppendCapability, (void*)&result); + Recycle(*aResult); + *aResult = result.ToNewCString(); + } + return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY; +} + + diff --git a/mozilla/caps/src/nsCertificatePrincipal.cpp b/mozilla/caps/src/nsCertificatePrincipal.cpp index 601ee56c474..b39e139eee0 100644 --- a/mozilla/caps/src/nsCertificatePrincipal.cpp +++ b/mozilla/caps/src/nsCertificatePrincipal.cpp @@ -14,11 +14,13 @@ * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1998 Netscape Communications Corporation. All + * Copyright (C) 1998-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): + * Norris Boyd */ + /*describes principals for use in signed scripts*/ #include "nsCertificatePrincipal.h" #include "nsCOMPtr.h" @@ -68,6 +70,13 @@ nsCertificatePrincipal::ToString(char **result) return (*result) ? NS_OK : NS_ERROR_OUT_OF_MEMORY; } +NS_IMETHODIMP +nsCertificatePrincipal::ToUserVisibleString(char **result) +{ + // XXX TODO: need company name rather than issuer and serial number here. + return ToString(result); +} + NS_IMETHODIMP nsCertificatePrincipal::Equals(nsIPrincipal * other, PRBool * result) { @@ -107,7 +116,7 @@ nsCertificatePrincipal::HashValue(PRUint32 *result) } NS_IMETHODIMP -nsCertificatePrincipal::Init(const char* data) +nsCertificatePrincipal::InitFromPersistent(const char *name, const char* data) { // Parses preference strings of the form // "[Certificate Issuer Serial#] capabilities string" @@ -131,16 +140,15 @@ nsCertificatePrincipal::Init(const char* data) *wordEnd = '\0'; const char* serial = data; - if(NS_FAILED(Init(issuer, serial))) return NS_ERROR_FAILURE; + if (NS_FAILED(Init(issuer, serial))) + return NS_ERROR_FAILURE; - if (wordEnd[1] != '\0') - { + if (wordEnd[1] != '\0') { data = wordEnd+2; // Jump to beginning of caps data - return nsBasePrincipal::Init(data); + return nsBasePrincipal::InitFromPersistent(name, data); } - else - return NS_OK; - } + return NS_OK; +} NS_IMETHODIMP nsCertificatePrincipal::Init(const char* aIssuerName, const char* aSerialNumber) diff --git a/mozilla/caps/src/nsCodebasePrincipal.cpp b/mozilla/caps/src/nsCodebasePrincipal.cpp index b140d638064..e3d19bcc69a 100644 --- a/mozilla/caps/src/nsCodebasePrincipal.cpp +++ b/mozilla/caps/src/nsCodebasePrincipal.cpp @@ -14,10 +14,11 @@ * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1999 Netscape Communications Corporation. All + * Copyright (C) 1999-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): + * Norris Boyd */ /* Describes principals by their orginating uris */ @@ -54,6 +55,12 @@ nsCodebasePrincipal::ToString(char **result) return *result ? NS_OK : NS_ERROR_OUT_OF_MEMORY; } +NS_IMETHODIMP +nsCodebasePrincipal::ToUserVisibleString(char **result) +{ + return GetOrigin(result); +} + NS_IMETHODIMP nsCodebasePrincipal::HashValue(PRUint32 *result) { @@ -251,9 +258,9 @@ nsCodebasePrincipal::Init(nsIURI *uri) return NS_OK; } -// This one overrides nsBasePrincipal::Init +// This method overrides nsBasePrincipal::InitFromPersistent nsresult -nsCodebasePrincipal::Init(const char* data) +nsCodebasePrincipal::InitFromPersistent(const char *name, const char* data) { // Parses preference strings of the form // "[Codebase URL] capabilities string" @@ -268,22 +275,19 @@ nsCodebasePrincipal::Init(const char* data) char* urlEnd = PL_strchr(data, ']'); // Find end of URL NS_ASSERTION(urlEnd, "Malformed security.principal preference."); - *urlEnd = '\0'; + *urlEnd = '\0'; // XXX modification of const char * - if (NS_FAILED(NS_NewURI(&mURI, data, nsnull))) - { + if (NS_FAILED(NS_NewURI(&mURI, data, nsnull))) { NS_ASSERTION(PR_FALSE, "Malformed URI in security.principal preference."); return NS_ERROR_FAILURE; } - if (urlEnd[1] != 0) - { + if (urlEnd[1] != '\0') { data = urlEnd+2; // Jump to beginning of caps data - return nsBasePrincipal::Init(data); + return nsBasePrincipal::InitFromPersistent(name, data); } - else - return NS_OK; - } + return NS_OK; +} nsCodebasePrincipal::~nsCodebasePrincipal(void) { diff --git a/mozilla/caps/src/nsScriptSecurityManager.cpp b/mozilla/caps/src/nsScriptSecurityManager.cpp index 332b2e11caa..73bb7c3cc97 100644 --- a/mozilla/caps/src/nsScriptSecurityManager.cpp +++ b/mozilla/caps/src/nsScriptSecurityManager.cpp @@ -18,6 +18,8 @@ * Rights Reserved. * * Contributor(s): + * Norris Boyd + * Steve Morse */ #include "nsScriptSecurityManager.h" #include "nsIServiceManager.h" @@ -43,7 +45,16 @@ #include "nsDOMPropNames.h" #include "nsIXPConnect.h" #include "nsIXPCSecurityManager.h" +#include "nsTextFormatter.h" +#include "nsIIOService.h" +#include "nsIStringBundle.h" +#include "nsINetSupportDialogService.h" +static NS_DEFINE_CID(kNetSupportDialogCID, NS_NETSUPPORTDIALOG_CID); +static NS_DEFINE_IID(kIIOServiceIID, NS_IIOSERVICE_IID); +static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); +static NS_DEFINE_IID(kIStringBundleServiceIID, NS_ISTRINGBUNDLESERVICE_IID); +static NS_DEFINE_IID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID); static NS_DEFINE_CID(kPrefServiceCID, NS_PREF_CID); static NS_DEFINE_CID(kCScriptNameSetRegistryCID, NS_SCRIPT_NAMESET_REGISTRY_CID); @@ -700,6 +711,126 @@ nsScriptSecurityManager::IsCapabilityEnabled(const char *capability, return NS_OK; } +#define PROPERTIES_URL "chrome://security/locale/security.properties" + +nsresult +Localize(char *genericString, nsString &result) +{ + nsresult ret; + + /* create a URL for the string resource file */ + nsIIOService *pNetService = nsnull; + ret = nsServiceManager::GetService(kIOServiceCID, kIIOServiceIID, + (nsISupports**) &pNetService); + if (NS_FAILED(ret)) { + NS_WARNING("cannot get net service\n"); + return ret; + } + nsIURI *uri = nsnull; + ret = pNetService->NewURI(PROPERTIES_URL, nsnull, &uri); + if (NS_FAILED(ret)) { + NS_WARNING("cannot create URI\n"); + nsServiceManager::ReleaseService(kIOServiceCID, pNetService); + return ret; + } + + nsIURI *url = nsnull; + ret = uri->QueryInterface(NS_GET_IID(nsIURI), (void**)&url); + nsServiceManager::ReleaseService(kIOServiceCID, pNetService); + + if (NS_FAILED(ret)) { + NS_WARNING("cannot create URL\n"); + return ret; + } + + /* create a bundle for the localization */ + nsIStringBundleService *pStringService = nsnull; + ret = nsServiceManager::GetService(kStringBundleServiceCID, + kIStringBundleServiceIID, (nsISupports**) &pStringService); + if (NS_FAILED(ret)) { + NS_WARNING("cannot get string service\n"); + return ret; + } + char *spec = nsnull; + ret = url->GetSpec(&spec); + if (NS_FAILED(ret)) { + NS_WARNING("cannot get url spec\n"); + nsServiceManager::ReleaseService(kStringBundleServiceCID, pStringService); + nsCRT::free(spec); + return ret; + } + nsILocale *locale = nsnull; + nsIStringBundle *bundle = nsnull; + ret = pStringService->CreateBundle(spec, locale, &bundle); + nsCRT::free(spec); + nsServiceManager::ReleaseService(kStringBundleServiceCID, pStringService); + if (NS_FAILED(ret)) { + NS_WARNING("cannot create instance\n"); + return ret; + } + + /* localize the given string */ + nsAutoString strtmp(genericString); + PRUnichar *ptrv = nsnull; + ret = bundle->GetStringFromName(strtmp.GetUnicode(), &ptrv); + NS_RELEASE(bundle); + if (NS_FAILED(ret)) { + NS_WARNING("cannot get string from name\n"); + } + result = ptrv; + nsCRT::free(ptrv); + return ret; +} + +static PRBool +CheckConfirmDialog(const PRUnichar *szMessage, const PRUnichar *szCheckMessage, + PRBool *checkValue) +{ + nsresult res; + NS_WITH_SERVICE(nsIPrompt, dialog, kNetSupportDialogCID, &res); + if (NS_FAILED(res)) { + *checkValue = 0; + return PR_FALSE; + } + + PRInt32 buttonPressed = 1; /* in case user exits dialog by clicking X */ + nsAutoString yes, no, titleline; + if (NS_FAILED(res = Localize("Yes", yes))) + return PR_FALSE; + if (NS_FAILED(res = Localize("No", no))) + return PR_FALSE; + if (NS_FAILED(res = Localize("Titleline", titleline))) + return PR_FALSE; + + res = dialog->UniversalDialog( + nsnull, /* title message */ + titleline.GetUnicode(), /* title text in top line of window */ + szMessage, /* this is the main message */ + szCheckMessage, /* This is the checkbox message */ + yes.GetUnicode(), /* first button text */ + no.GetUnicode(), /* second button text */ + nsnull, /* third button text */ + nsnull, /* fourth button text */ + nsnull, /* first edit field label */ + nsnull, /* second edit field label */ + nsnull, /* first edit field initial and final value */ + nsnull, /* second edit field initial and final value */ + nsnull, /* icon: question mark by default */ + checkValue, /* initial and final value of checkbox */ + 2, /* number of buttons */ + 0, /* number of edit fields */ + 0, /* is first edit field a password field */ + &buttonPressed); + + if (NS_FAILED(res)) { + *checkValue = 0; + } + if (*checkValue != 0 && *checkValue != 1) { + *checkValue = 0; /* this should never happen but it is happening!!! */ + } + return (buttonPressed == 0); +} + static nsresult GetPrincipalAndFrame(JSContext *cx, nsIPrincipal **result, JSStackFrame **frameResult) @@ -751,20 +882,45 @@ nsScriptSecurityManager::EnableCapability(const char *capability) if (NS_FAILED(principal->CanEnableCapability(capability, &canEnable))) return NS_ERROR_FAILURE; if (canEnable == nsIPrincipal::ENABLE_WITH_USER_PERMISSION) { - // XXX ask user! - canEnable = nsIPrincipal::ENABLE_GRANTED; - if (NS_FAILED(principal->SetCanEnableCapability(capability, canEnable))) + // Prompt user for permission to enable capability. + static PRBool remember = PR_TRUE; + nsAutoString query, check; + if (NS_FAILED(Localize("EnableCapabilityQuery", query))) return NS_ERROR_FAILURE; - nsIPrincipalKey key(principal); - if (!mPrincipals) { - mPrincipals = new nsSupportsHashtable(31); - if (!mPrincipals) - return NS_ERROR_OUT_OF_MEMORY; + if (NS_FAILED(Localize("CheckMessage", check))) + return NS_ERROR_FAILURE; + char *source; + if (NS_FAILED(principal->ToUserVisibleString(&source))) + return NS_ERROR_FAILURE; + PRUnichar *message = nsTextFormatter::smprintf(query.GetUnicode(), + source); + Recycle(source); + canEnable = CheckConfirmDialog(message, check.GetUnicode(), &remember) + ? nsIPrincipal::ENABLE_GRANTED + : nsIPrincipal::ENABLE_DENIED; + PR_FREEIF(message); + if (remember) { + if (NS_FAILED(principal->SetCanEnableCapability(capability, canEnable))) + return NS_ERROR_FAILURE; + mIsWritingPrefs = PR_TRUE; + if (NS_FAILED(principal->WriteToPrefs(mPrefs))) { + mIsWritingPrefs = PR_FALSE; + return NS_ERROR_FAILURE; + } + mIsWritingPrefs = PR_FALSE; + if (NS_FAILED(mPrefs->SavePrefFile())) + return NS_ERROR_FAILURE; + nsIPrincipalKey key(principal); + if (!mPrincipals) { + mPrincipals = new nsSupportsHashtable(31); + if (!mPrincipals) + return NS_ERROR_OUT_OF_MEMORY; + } + // This is a little sneaky. "supports" below is a void *, which won't + // be refcounted, but is matched with a key that is the same object, + // which will be refcounted. + mPrincipals->Put(&key, principal); } - // This is a little sneaky. "supports" below is a void *, which won't - // be refcounted, but is matched with a key that is the same object, - // which will be refcounted. - mPrincipals->Put(&key, principal); } if (canEnable != nsIPrincipal::ENABLE_GRANTED) { static const char msg[] = "enablePrivilege not granted"; @@ -881,7 +1037,8 @@ nsScriptSecurityManager::nsScriptSecurityManager(void) : mOriginToPolicyMap(nsnull), mPrefs(nsnull), mSystemPrincipal(nsnull), mPrincipals(nsnull), mIsJavaScriptEnabled(PR_FALSE), - mIsMailJavaScriptEnabled(PR_FALSE) + mIsMailJavaScriptEnabled(PR_FALSE), + mIsWritingPrefs(PR_FALSE) { NS_INIT_REFCNT(); memset(hasPolicyVector, 0, sizeof(hasPolicyVector)); @@ -1031,7 +1188,7 @@ nsScriptSecurityManager::CheckPermissions(JSContext *aCx, JSObject *aObj, ** Access tests failed, so now report error. */ char *str; - if (NS_FAILED(subject->ToString(&str))) + if (NS_FAILED(subject->ToUserVisibleString(&str))) return NS_ERROR_FAILURE; JS_ReportError(aCx, "access disallowed from scripts at %s to documents " "at another domain", str); @@ -1223,7 +1380,7 @@ DeleteEntry(nsHashKey *aKey, void *aData, void* closure) } void -nsScriptSecurityManager::enumeratePolicyCallback(const char *prefName, +nsScriptSecurityManager::EnumeratePolicyCallback(const char *prefName, void *data) { if (!prefName || !*prefName) @@ -1330,7 +1487,7 @@ struct EnumeratePrincipalsInfo { }; void -nsScriptSecurityManager::enumeratePrincipalsCallback(const char *prefName, +nsScriptSecurityManager::EnumeratePrincipalsCallback(const char *prefName, void *voidParam) { EnumeratePrincipalsInfo *info = (EnumeratePrincipalsInfo *) voidParam; @@ -1346,17 +1503,17 @@ nsScriptSecurityManager::enumeratePrincipalsCallback(const char *prefName, nsCodebasePrincipal *codebase = new nsCodebasePrincipal(); if (codebase) { NS_ADDREF(codebase); - if (NS_SUCCEEDED(codebase->Init(data))) + if (NS_SUCCEEDED(codebase->InitFromPersistent(prefName, data))) principal = do_QueryInterface((nsBasePrincipal*)codebase); NS_RELEASE(codebase); } } else if (PL_strncasecmp(data, certificateName, - sizeof(certificateName)-1) == 0) + sizeof(certificateName)-1) == 0) { nsCertificatePrincipal *certificate = new nsCertificatePrincipal(); if (certificate) { NS_ADDREF(certificate); - if (NS_SUCCEEDED(certificate->Init(data))) + if (NS_SUCCEEDED(certificate->InitFromPersistent(prefName, data))) principal = do_QueryInterface((nsBasePrincipal*)certificate); NS_RELEASE(certificate); } @@ -1393,6 +1550,19 @@ nsScriptSecurityManager::JSEnabledPrefChanged(const char *pref, void *data) return 0; } +int +nsScriptSecurityManager::PrincipalPrefChanged(const char *pref, void *data) +{ + nsScriptSecurityManager *secMgr = (nsScriptSecurityManager *) data; + if (secMgr->mIsWritingPrefs) + return 0; + EnumeratePrincipalsInfo info; + info.ht = secMgr->mPrincipals; + info.prefs = secMgr->mPrefs; + EnumeratePrincipalsCallback(pref, &info); + return 0; +} + NS_IMETHODIMP nsScriptSecurityManager::InitFromPrefs() @@ -1416,7 +1586,7 @@ nsScriptSecurityManager::InitFromPrefs() prefs->RegisterCallback(jsEnabledPrefName, JSEnabledPrefChanged, this); prefs->RegisterCallback(jsMailEnabledPrefName, JSEnabledPrefChanged, this); prefs->EnumerateChildren("security.policy", - nsScriptSecurityManager::enumeratePolicyCallback, + nsScriptSecurityManager::EnumeratePolicyCallback, (void *) this); if (!mPrincipals) { @@ -1428,8 +1598,10 @@ nsScriptSecurityManager::InitFromPrefs() info.ht = mPrincipals; info.prefs = mPrefs; prefs->EnumerateChildren("security.principal", - nsScriptSecurityManager::enumeratePrincipalsCallback, + nsScriptSecurityManager::EnumeratePrincipalsCallback, (void *) &info); + prefs->RegisterCallback("security.principal", PrincipalPrefChanged, this); + return NS_OK; } diff --git a/mozilla/caps/src/nsSystemPrincipal.cpp b/mozilla/caps/src/nsSystemPrincipal.cpp index 15a72ef1bfc..1193b0f73e6 100644 --- a/mozilla/caps/src/nsSystemPrincipal.cpp +++ b/mozilla/caps/src/nsSystemPrincipal.cpp @@ -14,10 +14,11 @@ * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are - * Copyright (C) 1999 Netscape Communications Corporation. All + * Copyright (C) 1999-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): + * Norris Boyd */ /* The privileged system principal. */ @@ -48,6 +49,12 @@ nsSystemPrincipal::ToString(char **result) return *result ? NS_OK : NS_ERROR_OUT_OF_MEMORY; } +NS_IMETHODIMP +nsSystemPrincipal::ToUserVisibleString(char **result) +{ + return ToString(result); +} + NS_IMETHODIMP nsSystemPrincipal::Equals(nsIPrincipal *other, PRBool *result) { @@ -107,6 +114,13 @@ nsSystemPrincipal::DisableCapability(const char *capability, void **annotation) return NS_ERROR_FAILURE; } +NS_IMETHODIMP +nsSystemPrincipal::ToStreamableForm(char **aResult) +{ + // The system principal should never be streamed out + return NS_ERROR_FAILURE; +} + ////////////////////////////////////////// // Constructor, Destructor, initialization ////////////////////////////////////////// diff --git a/mozilla/caps/src/security.properties b/mozilla/caps/src/security.properties new file mode 100644 index 00000000000..6a421da6d09 --- /dev/null +++ b/mozilla/caps/src/security.properties @@ -0,0 +1,27 @@ +# The contents of this file are subject to the Netscape Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/NPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 2000 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# +Yes = Yes +No = No +Titleline = Internet Security +CheckMessage = Remember this decision +EnableCapabilityQuery = A script from %1$s has requested enhanced \ + privileges. You should grant these privileges only if you are \ + comfortable downloading and executing a program from this \ + source. Do you wish to allow these privileges? diff --git a/mozilla/docshell/base/nsWebShell.cpp b/mozilla/docshell/base/nsWebShell.cpp index 943fae7a1a3..e42ad967f2b 100644 --- a/mozilla/docshell/base/nsWebShell.cpp +++ b/mozilla/docshell/base/nsWebShell.cpp @@ -2216,7 +2216,8 @@ nsWebShell::LoadURL(const PRUnichar *aURLSpec, /* Add the page to session history */ if (aModifyHistory && shist && (!isMail)) { PRInt32 ret; - ret = shist->Add(spec, this); + nsCAutoString referrer(aReferrer); + ret = shist->Add(spec, referrer, this); } nsCOMPtr parent; diff --git a/mozilla/dom/src/base/nsGlobalWindow.cpp b/mozilla/dom/src/base/nsGlobalWindow.cpp index ec69a31532c..ee9c04eba7d 100644 --- a/mozilla/dom/src/base/nsGlobalWindow.cpp +++ b/mozilla/dom/src/base/nsGlobalWindow.cpp @@ -47,6 +47,7 @@ // Interfaces Needed #include "nsIBaseWindow.h" #include "nsICharsetConverterManager.h" +#include "nsICodebasePrincipal.h" #include "nsIContent.h" #include "nsIContentViewerFile.h" #include "nsIContentViewerEdit.h" @@ -2193,11 +2194,11 @@ NS_IMETHODIMP GlobalWindowImpl::OpenInternal(JSContext* cx, jsval* argv, if(aDialog && argc > 3) AttachArguments(*aReturn, argv+3, argc-3); + nsCOMPtr secMan; if(loadURL) { // Get security manager, check to see if URI is allowed. nsCOMPtr newUrl; - nsCOMPtr secMan; nsCOMPtr scriptCX; nsJSUtils::nsGetStaticScriptContext(cx, (JSObject*)mScriptObject, getter_AddRefs(scriptCX)); @@ -2214,8 +2215,27 @@ NS_IMETHODIMP GlobalWindowImpl::OpenInternal(JSContext* cx, jsval* argv, newDocShellItem->SetName(nsnull); nsCOMPtr webShell(do_QueryInterface(newDocShellItem)); - if(loadURL) - webShell->LoadURL(mAbsURL.GetUnicode()); + if (loadURL) { + nsCOMPtr principal; + if (NS_FAILED(secMan->GetSubjectPrincipal(getter_AddRefs(principal)))) + return NS_ERROR_FAILURE; + nsCOMPtr codebase = do_QueryInterface(principal); + if (codebase) { + nsCOMPtr codebaseURI; + nsresult rv; + if (NS_FAILED(rv = codebase->GetURI(getter_AddRefs(codebaseURI)))) + return rv; + nsXPIDLCString spec; + if (NS_FAILED(rv = codebaseURI->GetSpec(getter_Copies(spec)))) + return rv; + nsAutoString referrer(spec); + webShell->LoadURL(mAbsURL.GetUnicode(), nsnull, PR_TRUE, + nsIChannel::LOAD_NORMAL, 0, nsnull, + referrer.GetUnicode()); + } else { + webShell->LoadURL(mAbsURL.GetUnicode()); + } + } if(windowIsNew) SizeOpenedDocShellItem(newDocShellItem, options, chromeFlags); diff --git a/mozilla/dom/src/base/nsGlobalWindow.h b/mozilla/dom/src/base/nsGlobalWindow.h index 2b74c63b2c4..b25da39c78b 100644 --- a/mozilla/dom/src/base/nsGlobalWindow.h +++ b/mozilla/dom/src/base/nsGlobalWindow.h @@ -329,7 +329,7 @@ protected: PRBool aReplace); nsresult GetSourceURL(JSContext* cx, nsIURI** sourceURL); - nsresult CheckURL(nsIURI *url); + nsresult CheckURL(nsIURI *url, nsString &aReferrerResult); nsIDocShell *mDocShell; // Weak Reference void *mScriptObject; diff --git a/mozilla/dom/src/base/nsLocation.cpp b/mozilla/dom/src/base/nsLocation.cpp index 9ee8cc5a829..079e38a94e7 100644 --- a/mozilla/dom/src/base/nsLocation.cpp +++ b/mozilla/dom/src/base/nsLocation.cpp @@ -35,10 +35,12 @@ #include "nsCOMPtr.h" #include "nsJSUtils.h" #include "nsIScriptSecurityManager.h" +#include "nsICodebasePrincipal.h" #include "nsIDOMWindow.h" #include "nsIDOMDocument.h" #include "nsIDocument.h" #include "nsIJSContextStack.h" +#include "nsXPIDLString.h" static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); @@ -98,7 +100,7 @@ NS_IMETHODIMP_(void) LocationImpl::SetDocShell(nsIDocShell *aDocShell) } nsresult -LocationImpl::CheckURL(nsIURI* aURL) +LocationImpl::CheckURL(nsIURI* aURL, nsString &aReferrerResult) { nsresult result; // Get JSContext from stack. @@ -120,6 +122,21 @@ LocationImpl::CheckURL(nsIURI* aURL) if (NS_FAILED(result = secMan->CheckLoadURIFromScript(cx, aURL))) return result; + // Now get the referrer to use when loading the URI + nsCOMPtr principal; + if (NS_FAILED(secMan->GetSubjectPrincipal(getter_AddRefs(principal)))) + return NS_ERROR_FAILURE; + nsCOMPtr codebase = do_QueryInterface(principal); + if (codebase) { + nsCOMPtr codebaseURI; + if (NS_FAILED(result = codebase->GetURI(getter_AddRefs(codebaseURI)))) + return result; + nsXPIDLCString spec; + if (NS_FAILED(result = codebaseURI->GetSpec(getter_Copies(spec)))) + return result; + aReferrerResult = spec; + } + return NS_OK; } @@ -133,11 +150,16 @@ LocationImpl::SetURL(nsIURI* aURL) nsAutoString s = spec; nsCRT::free(spec); - if (NS_FAILED(CheckURL(aURL))) + nsAutoString referrer; + if (NS_FAILED(CheckURL(aURL, referrer))) return NS_ERROR_FAILURE; nsCOMPtr webShell(do_QueryInterface(mDocShell)); - return webShell->LoadURL(s.GetUnicode(), nsnull, PR_TRUE); + return webShell->LoadURL(s.GetUnicode(), nsnull, PR_TRUE, + nsIChannel::LOAD_NORMAL, 0, nsnull, + referrer.Length() > 0 + ? referrer.GetUnicode() + : nsnull); } else { return NS_OK; @@ -358,12 +380,17 @@ LocationImpl::SetHrefWithBase(const nsString& aHref, if ((NS_OK == result) && (mDocShell)) { - if (NS_FAILED(CheckURL(newUrl))) + nsAutoString referrer; + if (NS_FAILED(CheckURL(newUrl, referrer))) return NS_ERROR_FAILURE; // Load new URI. nsCOMPtr webShell(do_QueryInterface(mDocShell)); - result = webShell->LoadURL(newHref.GetUnicode(), nsnull, aReplace); + result = webShell->LoadURL(newHref.GetUnicode(), nsnull, aReplace, + nsIChannel::LOAD_NORMAL, 0, nsnull, + referrer.Length() > 0 + ? referrer.GetUnicode() + : nsnull); } return result; diff --git a/mozilla/dom/src/jsurl/nsJSProtocolHandler.cpp b/mozilla/dom/src/jsurl/nsJSProtocolHandler.cpp index 9a4856efaad..c0f5f207eeb 100644 --- a/mozilla/dom/src/jsurl/nsJSProtocolHandler.cpp +++ b/mozilla/dom/src/jsurl/nsJSProtocolHandler.cpp @@ -204,7 +204,7 @@ nsJSProtocolHandler::NewChannel(const char* verb, // The event sink must be a script global Object Owner or we fail. nsCOMPtr globalOwner; notificationCallbacks->GetInterface(NS_GET_IID(nsIScriptGlobalObjectOwner), - getter_AddRefs(globalOwner)); + getter_AddRefs(globalOwner)); NS_ENSURE_TRUE(globalOwner, NS_ERROR_FAILURE); // So far so good: get the script context from its owner. @@ -223,30 +223,27 @@ nsJSProtocolHandler::NewChannel(const char* verb, if (NS_FAILED(rv)) return NS_ERROR_FAILURE; + // Get principal nsCOMPtr principal; - // script is currently executing; get principal from that script - if (NS_FAILED(securityManager->GetSubjectPrincipal(getter_AddRefs(principal)))) + nsCOMPtr referringUri; + if (originalURI) { + referringUri = originalURI; + } else { + nsCOMPtr webShell; + webShell = do_QueryInterface(globalOwner); + if (!webShell) return NS_ERROR_FAILURE; - if (!principal) { - // No scripts currently executing; get principal from referrer of link - nsCOMPtr referringUri; - if (originalURI) { - referringUri = originalURI; - } else { - nsCOMPtr webShell; - webShell = do_QueryInterface(globalOwner); - if (!webShell) - return NS_ERROR_FAILURE; - const PRUnichar* url; - if (NS_FAILED(webShell->GetURL(&url))) - return NS_ERROR_FAILURE; - nsString urlStr(url); - - if (NS_FAILED(NS_NewURI(getter_AddRefs(referringUri), urlStr, nsnull))) - return NS_ERROR_FAILURE; - } - if (NS_FAILED(securityManager->GetCodebasePrincipal(referringUri, getter_AddRefs(principal)))) + const PRUnichar* url; + if (NS_FAILED(webShell->GetURL(&url))) return NS_ERROR_FAILURE; + nsString urlStr(url); + if (NS_FAILED(NS_NewURI(getter_AddRefs(referringUri), urlStr, nsnull))) + return NS_ERROR_FAILURE; + } + if (NS_FAILED(securityManager->GetCodebasePrincipal(referringUri, + getter_AddRefs(principal)))) + { + return NS_ERROR_FAILURE; } diff --git a/mozilla/webshell/src/nsWebShell.cpp b/mozilla/webshell/src/nsWebShell.cpp index 943fae7a1a3..e42ad967f2b 100644 --- a/mozilla/webshell/src/nsWebShell.cpp +++ b/mozilla/webshell/src/nsWebShell.cpp @@ -2216,7 +2216,8 @@ nsWebShell::LoadURL(const PRUnichar *aURLSpec, /* Add the page to session history */ if (aModifyHistory && shist && (!isMail)) { PRInt32 ret; - ret = shist->Add(spec, this); + nsCAutoString referrer(aReferrer); + ret = shist->Add(spec, referrer, this); } nsCOMPtr parent; diff --git a/mozilla/xpfe/appshell/public/nsISessionHistory.idl b/mozilla/xpfe/appshell/public/nsISessionHistory.idl index fd5dcc6518f..3a5958350ab 100644 --- a/mozilla/xpfe/appshell/public/nsISessionHistory.idl +++ b/mozilla/xpfe/appshell/public/nsISessionHistory.idl @@ -64,7 +64,7 @@ interface nsISessionHistory: nsISupports /** * Add a new URL to the History List */ - [noscript] void add(in string url, in nsIWebShell aContainer); + [noscript] void add(in string url, in string aReferrer, in nsIWebShell aContainer); /** * Goto to a particular point in history diff --git a/mozilla/xpfe/appshell/src/nsSessionHistory.cpp b/mozilla/xpfe/appshell/src/nsSessionHistory.cpp index 3985f61e2f0..a9ddac764f9 100644 --- a/mozilla/xpfe/appshell/src/nsSessionHistory.cpp +++ b/mozilla/xpfe/appshell/src/nsSessionHistory.cpp @@ -42,8 +42,8 @@ static NS_DEFINE_CID(kWebShellCID, NS_WEB_SHELL_CID); - -static nsHistoryEntry * GenerateTree(const char * aStickyURL, nsIWebShell * aStickyContainer, nsIWebShell * aContainer,nsHistoryEntry *aParent, nsISessionHistory * aSHist); +static nsHistoryEntry * GenerateTree(const char * aStickyURL, nsIWebShell * aStickyContainer, nsIWebShell * aContainer, + nsHistoryEntry *aParent, nsISessionHistory * aSHist, const char * aReferrer); #define APP_DEBUG 0 @@ -63,7 +63,8 @@ public: * Create the History data structures for the current URL. This method * will also generate the history tree for the URL if it contains frames */ - nsresult Create(const char * aURL, nsIWebShell * aWebShell, nsHistoryEntry * aParent, nsISessionHistory * aSHist); + nsresult Create(const char * aURL, nsIWebShell * aWebShell, const char * referrer, + nsHistoryEntry * aParent, nsISessionHistory * aSHist); /** * Load the entry in the content Area @@ -100,6 +101,16 @@ public: */ nsresult SetURL(const char * aURL); + /** + * Get the referrer of the page + */ + nsresult GetReferrer(char ** aReferrer); + + /** + * Set the referrer of the page + */ + nsresult SetReferrer(const char * aReferrer); + /** * Get the webshell of the page */ @@ -166,6 +177,7 @@ public: nsIWebShell * mWS; //Webshell corresponding to this history entry nsString * mURL; // URL for this history entry + char * mReferrer; nsString * mTitle; // Name of the document nsVoidArray mChildren; // children list PRInt32 mChildCount; // # of children @@ -187,6 +199,7 @@ nsHistoryEntry::nsHistoryEntry() mHistoryList = nsnull; mParent = nsnull; mURL = nsnull; + mReferrer = nsnull; mTitle = nsnull; mHistoryState = nsnull; // NS_INIT_REFCNT(); @@ -195,10 +208,10 @@ nsHistoryEntry::nsHistoryEntry() nsHistoryEntry::~nsHistoryEntry() { MOZ_COUNT_DTOR(nsHistoryEntry); - if (mTitle) - delete mTitle; - if (mURL) - delete mURL; + delete mTitle; + delete mURL; + if (mReferrer) + nsCRT::free(mReferrer); NS_IF_RELEASE(mWS); // mHistoryList is a weak reference. Hence no release. mHistoryList = nsnull; @@ -243,10 +256,26 @@ nsHistoryEntry::SetURL(const char* aURL) if (mURL) delete mURL; - mURL = new nsString(aURL); + mURL = new nsString(aURL); return NS_OK; } +nsresult +nsHistoryEntry::GetReferrer(char** aReferrer) +{ + *aReferrer = mReferrer ? nsCRT::strdup(mReferrer) : nsnull; + return NS_OK; +} + +nsresult +nsHistoryEntry::SetReferrer(const char* aReferrer) +{ + + delete mReferrer; + mReferrer = nsCRT::strdup(aReferrer); + return aReferrer ? NS_OK : NS_ERROR_OUT_OF_MEMORY; +} + nsresult nsHistoryEntry::GetWebShell(nsIWebShell *& aResult) { @@ -374,21 +403,19 @@ nsHistoryEntry::AddChild(nsHistoryEntry* aChild) */ nsresult -nsHistoryEntry::Create(const char * aURL, nsIWebShell * aWebShell, nsHistoryEntry * aParent, nsISessionHistory * aSHist) { +nsHistoryEntry::Create(const char * aURL, nsIWebShell * aWebShell, const char * aReferrer, + nsHistoryEntry * aParent, nsISessionHistory * aSHist) +{ - // Get the webshell's url. - // aWebShell->GetURL(&url); - nsAutoString urlstr(aURL); - - // save the webshell's URL in the history entry - char * urlcstr = urlstr.ToNewCString(); - SetURL(urlcstr); - Recycle(urlcstr); + // save the webshell's URL and referrer in the history entry + SetURL(aURL); + if (aReferrer) + SetReferrer(aReferrer); //Save the webshell id SetWebShell(aWebShell); - if (APP_DEBUG) printf("SessionHistory::Create Creating Historyentry %x for webshell %x, url = %s parent entry = %x\n", (unsigned int)this, (unsigned int)aWebShell, urlstr.ToNewCString(), (unsigned int) aParent); + if (APP_DEBUG) printf("SessionHistory::Create Creating Historyentry %x for webshell %x, url = %s parent entry = %x\n", (unsigned int)this, (unsigned int)aWebShell, aURL, (unsigned int) aParent); if (aParent) aParent->AddChild(this); @@ -400,7 +427,9 @@ nsHistoryEntry::Create(const char * aURL, nsIWebShell * aWebShell, nsHistoryEntr } static nsHistoryEntry * -GenerateTree(const char * aStickyUrl, nsIWebShell * aStickyContainer, nsIWebShell * aContainer, nsHistoryEntry * aParent, nsISessionHistory * aSHist) { +GenerateTree(const char * aStickyUrl, nsIWebShell * aStickyContainer, nsIWebShell * aContainer, + nsHistoryEntry * aParent, nsISessionHistory * aSHist, const char * aReferrer) +{ nsHistoryEntry * hEntry = nsnull; const PRUnichar * url = nsnull; @@ -415,14 +444,14 @@ GenerateTree(const char * aStickyUrl, nsIWebShell * aStickyContainer, nsIWebShel } if (aStickyContainer == aContainer) { - hEntry->Create(aStickyUrl, aContainer, aParent, aSHist); + hEntry->Create(aStickyUrl, aContainer, aReferrer, aParent, aSHist); } else { // Get the webshell's url. aContainer->GetURL(&url); urlAStr = (url); aCStr = urlAStr.ToNewCString(); - hEntry->Create(aCStr, aContainer, aParent, aSHist); + hEntry->Create(aCStr, aContainer, aReferrer, aParent, aSHist); Recycle((char *) aCStr); } @@ -437,7 +466,7 @@ GenerateTree(const char * aStickyUrl, nsIWebShell * aStickyContainer, nsIWebShel //nsHistoryEntry * hChild = nsnull; aContainer->ChildAt(i, (*getter_AddRefs(childWS))); if (childWS) { - GenerateTree(aStickyUrl, aStickyContainer, childWS, hEntry, aSHist); + GenerateTree(aStickyUrl, aStickyContainer, childWS, hEntry, aSHist, aReferrer); } } } @@ -544,7 +573,9 @@ nsHistoryEntry::Load(nsIWebShell * aPrevEntry, PRBool aIsReload) { PRUnichar * uniURL = cSURL.ToNewUnicode(); prev->SetURL(uniURL); - prev->LoadURL(uniURL, nsnull, PR_FALSE, loadType, 0, historyObject); + nsAutoString referrer(mReferrer); + prev->LoadURL(uniURL, nsnull, PR_FALSE, loadType, 0, historyObject, + mReferrer ? referrer.GetUnicode() : nsnull); Recycle(uniURL); if (aIsReload && (pcount > 0)) { @@ -755,7 +786,7 @@ NS_IMPL_ISUPPORTS1(nsSessionHistory, nsISessionHistory); * by typing in the urlbar. */ NS_IMETHODIMP -nsSessionHistory::Add(const char * aURL, nsIWebShell * aWebShell) +nsSessionHistory::Add(const char * aURL, const char * aReferrer, nsIWebShell * aWebShell) { //nsresult rv = NS_OK; nsHistoryEntry * hEntry = nsnull; @@ -791,7 +822,8 @@ nsSessionHistory::Add(const char * aURL, nsIWebShell * aWebShell) NS_ASSERTION(PR_FALSE, "nsSessionHistory::add Low memory"); return NS_ERROR_OUT_OF_MEMORY; } - hEntry->Create(aURL, aWebShell, nsnull, this); + + hEntry->Create(aURL, aWebShell, aReferrer, nsnull, this); /* Set the flag in webshell that indicates that it has been * added to session History @@ -855,7 +887,7 @@ nsSessionHistory::Add(const char * aURL, nsIWebShell * aWebShell) NS_ASSERTION(PR_FALSE, "nsSessionHistory::add Low memory"); return NS_ERROR_OUT_OF_MEMORY; } - newEntry->Create(aURL, aWebShell, parentEntry, this); + newEntry->Create(aURL, aWebShell, aReferrer, parentEntry, this); aWebShell->SetIsInSHist(PR_TRUE); if (parentWS) @@ -900,7 +932,7 @@ nsSessionHistory::Add(const char * aURL, nsIWebShell * aWebShell) aWebShell->GetRootWebShell(root); if (root) - newEntry = GenerateTree(aURL, aWebShell, root, nsnull, this); + newEntry = GenerateTree(aURL, aWebShell, root, nsnull, this, aReferrer); if (newEntry) { if ((mHistoryLength - (mHistoryCurrentIndex+1)) > 0) { /* We are somewhere in the middle of the history and a diff --git a/mozilla/xpfe/browser/src/nsBrowserInstance.cpp b/mozilla/xpfe/browser/src/nsBrowserInstance.cpp index 1339eb0a69f..8ed4005d113 100644 --- a/mozilla/xpfe/browser/src/nsBrowserInstance.cpp +++ b/mozilla/xpfe/browser/src/nsBrowserInstance.cpp @@ -1798,7 +1798,7 @@ nsBrowserAppCore::Reload(nsIWebShell * aPrev, nsLoadFlags aType) } NS_IMETHODIMP -nsBrowserAppCore::Add(const char * aURL, nsIWebShell * aWebShell) +nsBrowserAppCore::Add(const char * aURL, const char * aReferrer, nsIWebShell * aWebShell) { return NS_OK; }