Add function to nsContentUtils to report a localized error message to the error console, and use it from the three places that were doing pretty much the same thing. b=187007 r+sr=bzbarsky

This commit is contained in:
dbaron%dbaron.org 2004-09-14 17:26:35 +00:00
Родитель 3118860f30
Коммит 5f62984111
6 изменённых файлов: 148 добавлений и 206 удалений

Просмотреть файл

@ -72,6 +72,9 @@ class nsIImage;
class nsIImageLoadingContent; class nsIImageLoadingContent;
class nsIDOMHTMLFormElement; class nsIDOMHTMLFormElement;
class nsIDOMDocument; class nsIDOMDocument;
class nsIConsoleService;
class nsIStringBundleService;
class nsIStringBundle;
class nsContentUtils class nsContentUtils
{ {
@ -437,6 +440,34 @@ public:
return sXPConnect; return sXPConnect;
} }
/**
* Report a localized error message to the error console.
* @param aFile Properties file containing localized message.
* @param aMessageName Name of localized message.
* @param aParams Parameters to be substituted into localized message.
* @param aParamsLength Length of aParams.
* @param aURI URI of resource containing error (may be null).
* @param aLineNumber Line number within resource containing error.
* @param aColumnNumber Column number within resource containing error.
* @param aErrorFlags See nsIScriptError.
* @param aCategory Name of module reporting error.
*/
enum PropertiesFile {
eCSS_PROPERTIES,
eXBL_PROPERTIES,
eXUL_PROPERTIES,
PropertiesFile_COUNT
};
static nsresult ReportToConsole(PropertiesFile aFile,
const char *aMessageName,
const PRUnichar **aParams,
PRUint32 aParamsLength,
nsIURI* aURI,
PRUint32 aLineNumber,
PRUint32 aColumnNumber,
PRUint32 aErrorFlags,
const char *aCategory);
private: private:
static nsresult doReparentContentWrapper(nsIContent *aChild, static nsresult doReparentContentWrapper(nsIContent *aChild,
nsIDocument *aNewDocument, nsIDocument *aNewDocument,
@ -465,6 +496,11 @@ private:
static imgILoader* sImgLoader; static imgILoader* sImgLoader;
static nsIConsoleService* sConsoleService;
static nsIStringBundleService* sStringBundleService;
static nsIStringBundle* sStringBundles[PropertiesFile_COUNT];
static PRBool sInitialized; static PRBool sInitialized;
}; };

Просмотреть файл

@ -105,6 +105,11 @@
#include "nsNodeInfoManager.h" #include "nsNodeInfoManager.h"
#include "nsCRT.h" #include "nsCRT.h"
// for ReportToConsole
#include "nsIStringBundle.h"
#include "nsIScriptError.h"
#include "nsIConsoleService.h"
static const char kJSStackContractID[] = "@mozilla.org/js/xpc/ContextStack;1"; static const char kJSStackContractID[] = "@mozilla.org/js/xpc/ContextStack;1";
static NS_DEFINE_IID(kParserServiceCID, NS_PARSERSERVICE_CID); static NS_DEFINE_IID(kParserServiceCID, NS_PARSERSERVICE_CID);
@ -118,6 +123,10 @@ nsIIOService *nsContentUtils::sIOService = nsnull;
nsIPrefBranch *nsContentUtils::sPrefBranch = nsnull; nsIPrefBranch *nsContentUtils::sPrefBranch = nsnull;
nsIPref *nsContentUtils::sPref = nsnull; nsIPref *nsContentUtils::sPref = nsnull;
imgILoader *nsContentUtils::sImgLoader = nsnull; imgILoader *nsContentUtils::sImgLoader = nsnull;
nsIConsoleService *nsContentUtils::sConsoleService;
nsIStringBundleService *nsContentUtils::sStringBundleService;
nsIStringBundle *nsContentUtils::sStringBundles[PropertiesFile_COUNT];
PRBool nsContentUtils::sInitialized = PR_FALSE; PRBool nsContentUtils::sInitialized = PR_FALSE;
@ -372,6 +381,10 @@ nsContentUtils::Shutdown()
{ {
sInitialized = PR_FALSE; sInitialized = PR_FALSE;
for (PRInt32 i = 0; i < PRInt32(PropertiesFile_COUNT); ++i)
NS_IF_RELEASE(sStringBundles[i]);
NS_IF_RELEASE(sStringBundleService);
NS_IF_RELEASE(sConsoleService);
NS_IF_RELEASE(sDOMScriptObjectFactory); NS_IF_RELEASE(sDOMScriptObjectFactory);
NS_IF_RELEASE(sXPConnect); NS_IF_RELEASE(sXPConnect);
NS_IF_RELEASE(sSecurityManager); NS_IF_RELEASE(sSecurityManager);
@ -2021,3 +2034,60 @@ nsCxPusher::Pop()
mScx = nsnull; mScx = nsnull;
mScriptIsRunning = PR_FALSE; mScriptIsRunning = PR_FALSE;
} }
static const char gPropertiesFiles[nsContentUtils::PropertiesFile_COUNT][38] = {
// Must line up with the enum values in |PropertiesFile| enum.
"chrome://global/locale/css.properties",
"chrome://global/locale/xbl.properties",
"chrome://global/locale/xul.properties"
};
/* static */ nsresult
nsContentUtils::ReportToConsole(PropertiesFile aFile,
const char *aMessageName,
const PRUnichar **aParams,
PRUint32 aParamsLength,
nsIURI* aURI,
PRUint32 aLineNumber,
PRUint32 aColumnNumber,
PRUint32 aErrorFlags,
const char *aCategory)
{
nsresult rv;
nsIStringBundle *bundle = sStringBundles[aFile];
if (!bundle) {
if (!sStringBundleService) {
rv = CallGetService(NS_STRINGBUNDLE_CONTRACTID, &sStringBundleService);
NS_ENSURE_SUCCESS(rv, rv);
}
if (!sConsoleService) { // only need to bother null-checking here
rv = CallGetService(NS_CONSOLESERVICE_CONTRACTID, &sConsoleService);
NS_ENSURE_SUCCESS(rv, rv);
}
sStringBundleService->CreateBundle(gPropertiesFiles[aFile], &bundle);
sStringBundles[aFile] = bundle; // transfer ownership
}
nsXPIDLString errorText;
rv = bundle->FormatStringFromName(NS_ConvertASCIItoUCS2(aMessageName).get(),
aParams, aParamsLength,
getter_Copies(errorText));
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString spec;
if (aURI)
aURI->GetSpec(spec);
nsCOMPtr<nsIScriptError> errorObject =
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = errorObject->Init(errorText.get(),
NS_ConvertUTF8toUTF16(spec).get(), // file name
EmptyString().get(), // source line
aLineNumber, aColumnNumber,
aErrorFlags, aCategory);
NS_ENSURE_SUCCESS(rv, rv);
return sConsoleService->LogMessage(errorObject);
}

Просмотреть файл

@ -66,9 +66,7 @@
#include "nsIScriptGlobalObject.h" #include "nsIScriptGlobalObject.h"
#include "nsITimelineService.h" #include "nsITimelineService.h"
#include "nsIHttpChannel.h" #include "nsIHttpChannel.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h" #include "nsIScriptError.h"
#include "nsIStringBundle.h"
#include "nsMimeTypes.h" #include "nsMimeTypes.h"
#include "nsIAtom.h" #include "nsIAtom.h"
#include "nsCSSLoader.h" #include "nsCSSLoader.h"
@ -669,55 +667,6 @@ SheetLoadData::OnDetermineCharset(nsIUnicharStreamLoader* aLoader,
return NS_OK; return NS_OK;
} }
/**
* Report an error to the error console.
* @param aErrorName The name of a string in css.properties.
* @param aParams The parameters for that string in css.properties.
* @param aParamsLength The length of aParams.
* @param aErrorFlags Error/warning flag to pass to nsIScriptError::Init.
*
* XXX This should be a static method on a class called something like
* nsCSSUtils, since it's a general way of accessing css.properties and
* will be useful for localizability work on CSS parser error reporting.
* However, it would need some way of reporting source file name, text,
* line, and column information.
*/
static nsresult
ReportToConsole(const PRUnichar* aMessageName, const PRUnichar **aParams,
PRUint32 aParamsLength, PRUint32 aErrorFlags, const PRUnichar* aReferrer)
{
nsresult rv;
nsCOMPtr<nsIConsoleService> consoleService =
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIScriptError> errorObject =
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIStringBundleService> stringBundleService =
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIStringBundle> bundle;
rv = stringBundleService->CreateBundle(
"chrome://global/locale/css.properties", getter_AddRefs(bundle));
NS_ENSURE_SUCCESS(rv, rv);
nsXPIDLString errorText;
rv = bundle->FormatStringFromName(aMessageName, aParams, aParamsLength,
getter_Copies(errorText));
NS_ENSURE_SUCCESS(rv, rv);
rv = errorObject->Init(errorText.get(),
aReferrer, /* file name */
EmptyString().get(), /* source line */
0, /* line number */
0, /* column number */
aErrorFlags,
"CSS Loader");
NS_ENSURE_SUCCESS(rv, rv);
consoleService->LogMessage(errorObject);
return NS_OK;
}
already_AddRefed<nsIURI> already_AddRefed<nsIURI>
SheetLoadData::GetReferrerURI() SheetLoadData::GetReferrerURI()
{ {
@ -805,32 +754,33 @@ SheetLoadData::OnStreamComplete(nsIUnicharStreamLoader* aLoader,
if (!validType) { if (!validType) {
nsCAutoString spec; nsCAutoString spec;
nsCAutoString referrer;
if (channelURI) { if (channelURI) {
channelURI->GetSpec(spec); channelURI->GetSpec(spec);
} }
{
nsCOMPtr<nsIURI> referrerURI = GetReferrerURI();
if (referrerURI)
referrerURI->GetSpec(referrer);
}
const nsAFlatString& specUTF16 = NS_ConvertUTF8toUTF16(spec); const nsAFlatString& specUTF16 = NS_ConvertUTF8toUTF16(spec);
const nsAFlatString& ctypeUTF16 = NS_ConvertASCIItoUTF16(contentType); const nsAFlatString& ctypeUTF16 = NS_ConvertASCIItoUTF16(contentType);
const nsAFlatString& referrerUTF16 = NS_ConvertUTF8toUTF16(referrer);
const PRUnichar *strings[] = { specUTF16.get(), ctypeUTF16.get() }; const PRUnichar *strings[] = { specUTF16.get(), ctypeUTF16.get() };
const char *errorMessage;
PRUint32 errorFlag;
if (mLoader->mCompatMode == eCompatibility_NavQuirks) { if (mLoader->mCompatMode == eCompatibility_NavQuirks) {
ReportToConsole(NS_LITERAL_STRING("MimeNotCssWarn").get(), strings, 2, errorMessage = "MimeNotCssWarn";
nsIScriptError::warningFlag, referrerUTF16.get()); errorFlag = nsIScriptError::warningFlag;
} else { } else {
// Drop the data stream so that we do not load it // Drop the data stream so that we do not load it
aDataStream = nsnull; aDataStream = nsnull;
ReportToConsole(NS_LITERAL_STRING("MimeNotCss").get(), strings, 2, errorMessage = "MimeNotCss";
nsIScriptError::errorFlag, referrerUTF16.get()); errorFlag = nsIScriptError::errorFlag;
} }
nsCOMPtr<nsIURI> referrer = GetReferrerURI();
nsContentUtils::ReportToConsole(nsContentUtils::eCSS_PROPERTIES,
errorMessage,
strings, NS_ARRAY_LENGTH(strings),
referrer, 0, 0, errorFlag,
"CSS Loader");
} }
} }

Просмотреть файл

@ -56,9 +56,9 @@
#include "nsXBLProtoImplMethod.h" #include "nsXBLProtoImplMethod.h"
#include "nsXBLProtoImplField.h" #include "nsXBLProtoImplField.h"
#include "nsXBLPrototypeBinding.h" #include "nsXBLPrototypeBinding.h"
#include "nsContentUtils.h"
#include "nsIConsoleService.h" #include "nsIConsoleService.h"
#include "nsIScriptError.h" #include "nsIScriptError.h"
#include "nsIStringBundle.h"
#include "nsNodeInfoManager.h" #include "nsNodeInfoManager.h"
#include "nsINodeInfo.h" #include "nsINodeInfo.h"
@ -242,44 +242,15 @@ nsXBLContentSink::ReportUnexpectedElement(nsIAtom* aElementName,
nsAutoString elementName; nsAutoString elementName;
aElementName->ToString(elementName); aElementName->ToString(elementName);
nsresult rv;
nsCOMPtr<nsIConsoleService> consoleService =
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIScriptError> errorObject =
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIStringBundleService> stringBundleService =
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIStringBundle> bundle;
rv = stringBundleService->CreateBundle(
"chrome://global/locale/xbl.properties", getter_AddRefs(bundle));
NS_ENSURE_SUCCESS(rv, rv);
const PRUnichar* params[] = { elementName.get() }; const PRUnichar* params[] = { elementName.get() };
nsXPIDLString errorText; return nsContentUtils::ReportToConsole(nsContentUtils::eXBL_PROPERTIES,
rv = bundle->FormatStringFromName(NS_LITERAL_STRING("UnexpectedElement").get(), "UnexpectedElement",
params, NS_ARRAY_LENGTH(params), params, NS_ARRAY_LENGTH(params),
getter_Copies(errorText)); mDocumentURI,
NS_ENSURE_SUCCESS(rv, rv); aLineNumber, 0 /* column number */,
nsIScriptError::errorFlag,
nsCAutoString documentURI; "XBL Content Sink");
mDocumentURI->GetSpec(documentURI);
rv = errorObject->Init(errorText.get(),
NS_ConvertUTF8toUCS2(documentURI).get(),
EmptyString().get(), /* source line */
aLineNumber,
0, /* column number */
nsIScriptError::errorFlag,
"XBL Content Sink");
NS_ENSURE_SUCCESS(rv, rv);
return consoleService->LogMessage(errorObject);
} }
void void

Просмотреть файл

@ -115,9 +115,7 @@
#include "nsContentUtils.h" #include "nsContentUtils.h"
#include "nsIParser.h" #include "nsIParser.h"
#include "nsICSSStyleSheet.h" #include "nsICSSStyleSheet.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h" #include "nsIScriptError.h"
#include "nsIStringBundle.h"
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// //
@ -3005,53 +3003,20 @@ nsXULDocument::ReportMissingOverlay(nsIURI* aURI)
{ {
NS_PRECONDITION(aURI, "Must have a URI"); NS_PRECONDITION(aURI, "Must have a URI");
nsresult rv;
nsCOMPtr<nsIConsoleService> consoleService =
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv))
return;
nsCOMPtr<nsIScriptError> errorObject =
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
if (NS_FAILED(rv))
return;
nsCOMPtr<nsIStringBundleService> stringBundleService =
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
if (NS_FAILED(rv))
return;
nsCOMPtr<nsIStringBundle> bundle;
rv = stringBundleService->CreateBundle(
"chrome://global/locale/xul.properties", getter_AddRefs(bundle));
if (NS_FAILED(rv))
return;
nsCAutoString spec; nsCAutoString spec;
aURI->GetSpec(spec); aURI->GetSpec(spec);
NS_ConvertUTF8toUTF16 utfSpec(spec); NS_ConvertUTF8toUTF16 utfSpec(spec);
const PRUnichar* params[] = { utfSpec.get() }; const PRUnichar* params[] = { utfSpec.get() };
nsXPIDLString errorText; nsContentUtils::ReportToConsole(nsContentUtils::eXUL_PROPERTIES,
rv = bundle->FormatStringFromName(NS_LITERAL_STRING("MissingOverlay").get(), "MissingOverlay",
params, NS_ARRAY_LENGTH(params), params, NS_ARRAY_LENGTH(params),
getter_Copies(errorText)); mDocumentURI,
if (NS_FAILED(rv)) 0, /* line number */
return; 0, /* column number */
nsIScriptError::warningFlag,
nsCAutoString documentURI; "XUL Document");
mDocumentURI->GetSpec(documentURI);
rv = errorObject->Init(errorText.get(),
NS_ConvertUTF8toUTF16(documentURI).get(),/* file name */
EmptyString().get(), /* source line */
0, /* line number */
0, /* column number */
nsIScriptError::warningFlag,
"XUL Document");
if (NS_FAILED(rv))
return;
consoleService->LogMessage(errorObject);
} }
nsresult nsresult

Просмотреть файл

@ -66,9 +66,7 @@
#include "nsIScriptGlobalObject.h" #include "nsIScriptGlobalObject.h"
#include "nsITimelineService.h" #include "nsITimelineService.h"
#include "nsIHttpChannel.h" #include "nsIHttpChannel.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h" #include "nsIScriptError.h"
#include "nsIStringBundle.h"
#include "nsMimeTypes.h" #include "nsMimeTypes.h"
#include "nsIAtom.h" #include "nsIAtom.h"
#include "nsCSSLoader.h" #include "nsCSSLoader.h"
@ -669,55 +667,6 @@ SheetLoadData::OnDetermineCharset(nsIUnicharStreamLoader* aLoader,
return NS_OK; return NS_OK;
} }
/**
* Report an error to the error console.
* @param aErrorName The name of a string in css.properties.
* @param aParams The parameters for that string in css.properties.
* @param aParamsLength The length of aParams.
* @param aErrorFlags Error/warning flag to pass to nsIScriptError::Init.
*
* XXX This should be a static method on a class called something like
* nsCSSUtils, since it's a general way of accessing css.properties and
* will be useful for localizability work on CSS parser error reporting.
* However, it would need some way of reporting source file name, text,
* line, and column information.
*/
static nsresult
ReportToConsole(const PRUnichar* aMessageName, const PRUnichar **aParams,
PRUint32 aParamsLength, PRUint32 aErrorFlags, const PRUnichar* aReferrer)
{
nsresult rv;
nsCOMPtr<nsIConsoleService> consoleService =
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIScriptError> errorObject =
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIStringBundleService> stringBundleService =
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIStringBundle> bundle;
rv = stringBundleService->CreateBundle(
"chrome://global/locale/css.properties", getter_AddRefs(bundle));
NS_ENSURE_SUCCESS(rv, rv);
nsXPIDLString errorText;
rv = bundle->FormatStringFromName(aMessageName, aParams, aParamsLength,
getter_Copies(errorText));
NS_ENSURE_SUCCESS(rv, rv);
rv = errorObject->Init(errorText.get(),
aReferrer, /* file name */
EmptyString().get(), /* source line */
0, /* line number */
0, /* column number */
aErrorFlags,
"CSS Loader");
NS_ENSURE_SUCCESS(rv, rv);
consoleService->LogMessage(errorObject);
return NS_OK;
}
already_AddRefed<nsIURI> already_AddRefed<nsIURI>
SheetLoadData::GetReferrerURI() SheetLoadData::GetReferrerURI()
{ {
@ -805,32 +754,33 @@ SheetLoadData::OnStreamComplete(nsIUnicharStreamLoader* aLoader,
if (!validType) { if (!validType) {
nsCAutoString spec; nsCAutoString spec;
nsCAutoString referrer;
if (channelURI) { if (channelURI) {
channelURI->GetSpec(spec); channelURI->GetSpec(spec);
} }
{
nsCOMPtr<nsIURI> referrerURI = GetReferrerURI();
if (referrerURI)
referrerURI->GetSpec(referrer);
}
const nsAFlatString& specUTF16 = NS_ConvertUTF8toUTF16(spec); const nsAFlatString& specUTF16 = NS_ConvertUTF8toUTF16(spec);
const nsAFlatString& ctypeUTF16 = NS_ConvertASCIItoUTF16(contentType); const nsAFlatString& ctypeUTF16 = NS_ConvertASCIItoUTF16(contentType);
const nsAFlatString& referrerUTF16 = NS_ConvertUTF8toUTF16(referrer);
const PRUnichar *strings[] = { specUTF16.get(), ctypeUTF16.get() }; const PRUnichar *strings[] = { specUTF16.get(), ctypeUTF16.get() };
const char *errorMessage;
PRUint32 errorFlag;
if (mLoader->mCompatMode == eCompatibility_NavQuirks) { if (mLoader->mCompatMode == eCompatibility_NavQuirks) {
ReportToConsole(NS_LITERAL_STRING("MimeNotCssWarn").get(), strings, 2, errorMessage = "MimeNotCssWarn";
nsIScriptError::warningFlag, referrerUTF16.get()); errorFlag = nsIScriptError::warningFlag;
} else { } else {
// Drop the data stream so that we do not load it // Drop the data stream so that we do not load it
aDataStream = nsnull; aDataStream = nsnull;
ReportToConsole(NS_LITERAL_STRING("MimeNotCss").get(), strings, 2, errorMessage = "MimeNotCss";
nsIScriptError::errorFlag, referrerUTF16.get()); errorFlag = nsIScriptError::errorFlag;
} }
nsCOMPtr<nsIURI> referrer = GetReferrerURI();
nsContentUtils::ReportToConsole(nsContentUtils::eCSS_PROPERTIES,
errorMessage,
strings, NS_ARRAY_LENGTH(strings),
referrer, 0, 0, errorFlag,
"CSS Loader");
} }
} }