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 nsIDOMHTMLFormElement;
class nsIDOMDocument;
class nsIConsoleService;
class nsIStringBundleService;
class nsIStringBundle;
class nsContentUtils
{
@ -437,6 +440,34 @@ public:
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:
static nsresult doReparentContentWrapper(nsIContent *aChild,
nsIDocument *aNewDocument,
@ -465,6 +496,11 @@ private:
static imgILoader* sImgLoader;
static nsIConsoleService* sConsoleService;
static nsIStringBundleService* sStringBundleService;
static nsIStringBundle* sStringBundles[PropertiesFile_COUNT];
static PRBool sInitialized;
};

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

@ -105,6 +105,11 @@
#include "nsNodeInfoManager.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 NS_DEFINE_IID(kParserServiceCID, NS_PARSERSERVICE_CID);
@ -118,6 +123,10 @@ nsIIOService *nsContentUtils::sIOService = nsnull;
nsIPrefBranch *nsContentUtils::sPrefBranch = nsnull;
nsIPref *nsContentUtils::sPref = nsnull;
imgILoader *nsContentUtils::sImgLoader = nsnull;
nsIConsoleService *nsContentUtils::sConsoleService;
nsIStringBundleService *nsContentUtils::sStringBundleService;
nsIStringBundle *nsContentUtils::sStringBundles[PropertiesFile_COUNT];
PRBool nsContentUtils::sInitialized = PR_FALSE;
@ -372,6 +381,10 @@ nsContentUtils::Shutdown()
{
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(sXPConnect);
NS_IF_RELEASE(sSecurityManager);
@ -2021,3 +2034,60 @@ nsCxPusher::Pop()
mScx = nsnull;
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 "nsITimelineService.h"
#include "nsIHttpChannel.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h"
#include "nsIStringBundle.h"
#include "nsMimeTypes.h"
#include "nsIAtom.h"
#include "nsCSSLoader.h"
@ -669,55 +667,6 @@ SheetLoadData::OnDetermineCharset(nsIUnicharStreamLoader* aLoader,
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>
SheetLoadData::GetReferrerURI()
{
@ -805,32 +754,33 @@ SheetLoadData::OnStreamComplete(nsIUnicharStreamLoader* aLoader,
if (!validType) {
nsCAutoString spec;
nsCAutoString referrer;
if (channelURI) {
channelURI->GetSpec(spec);
}
{
nsCOMPtr<nsIURI> referrerURI = GetReferrerURI();
if (referrerURI)
referrerURI->GetSpec(referrer);
}
const nsAFlatString& specUTF16 = NS_ConvertUTF8toUTF16(spec);
const nsAFlatString& ctypeUTF16 = NS_ConvertASCIItoUTF16(contentType);
const nsAFlatString& referrerUTF16 = NS_ConvertUTF8toUTF16(referrer);
const PRUnichar *strings[] = { specUTF16.get(), ctypeUTF16.get() };
const char *errorMessage;
PRUint32 errorFlag;
if (mLoader->mCompatMode == eCompatibility_NavQuirks) {
ReportToConsole(NS_LITERAL_STRING("MimeNotCssWarn").get(), strings, 2,
nsIScriptError::warningFlag, referrerUTF16.get());
errorMessage = "MimeNotCssWarn";
errorFlag = nsIScriptError::warningFlag;
} else {
// Drop the data stream so that we do not load it
aDataStream = nsnull;
ReportToConsole(NS_LITERAL_STRING("MimeNotCss").get(), strings, 2,
nsIScriptError::errorFlag, referrerUTF16.get());
errorMessage = "MimeNotCss";
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 "nsXBLProtoImplField.h"
#include "nsXBLPrototypeBinding.h"
#include "nsContentUtils.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h"
#include "nsIStringBundle.h"
#include "nsNodeInfoManager.h"
#include "nsINodeInfo.h"
@ -242,44 +242,15 @@ nsXBLContentSink::ReportUnexpectedElement(nsIAtom* aElementName,
nsAutoString 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() };
nsXPIDLString errorText;
rv = bundle->FormatStringFromName(NS_LITERAL_STRING("UnexpectedElement").get(),
params, NS_ARRAY_LENGTH(params),
getter_Copies(errorText));
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString documentURI;
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);
return nsContentUtils::ReportToConsole(nsContentUtils::eXBL_PROPERTIES,
"UnexpectedElement",
params, NS_ARRAY_LENGTH(params),
mDocumentURI,
aLineNumber, 0 /* column number */,
nsIScriptError::errorFlag,
"XBL Content Sink");
}
void

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

@ -115,9 +115,7 @@
#include "nsContentUtils.h"
#include "nsIParser.h"
#include "nsICSSStyleSheet.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h"
#include "nsIStringBundle.h"
//----------------------------------------------------------------------
//
@ -3005,53 +3003,20 @@ nsXULDocument::ReportMissingOverlay(nsIURI* aURI)
{
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;
aURI->GetSpec(spec);
NS_ConvertUTF8toUTF16 utfSpec(spec);
const PRUnichar* params[] = { utfSpec.get() };
nsXPIDLString errorText;
rv = bundle->FormatStringFromName(NS_LITERAL_STRING("MissingOverlay").get(),
params, NS_ARRAY_LENGTH(params),
getter_Copies(errorText));
if (NS_FAILED(rv))
return;
nsCAutoString documentURI;
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);
nsContentUtils::ReportToConsole(nsContentUtils::eXUL_PROPERTIES,
"MissingOverlay",
params, NS_ARRAY_LENGTH(params),
mDocumentURI,
0, /* line number */
0, /* column number */
nsIScriptError::warningFlag,
"XUL Document");
}
nsresult

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

@ -66,9 +66,7 @@
#include "nsIScriptGlobalObject.h"
#include "nsITimelineService.h"
#include "nsIHttpChannel.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h"
#include "nsIStringBundle.h"
#include "nsMimeTypes.h"
#include "nsIAtom.h"
#include "nsCSSLoader.h"
@ -669,55 +667,6 @@ SheetLoadData::OnDetermineCharset(nsIUnicharStreamLoader* aLoader,
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>
SheetLoadData::GetReferrerURI()
{
@ -805,32 +754,33 @@ SheetLoadData::OnStreamComplete(nsIUnicharStreamLoader* aLoader,
if (!validType) {
nsCAutoString spec;
nsCAutoString referrer;
if (channelURI) {
channelURI->GetSpec(spec);
}
{
nsCOMPtr<nsIURI> referrerURI = GetReferrerURI();
if (referrerURI)
referrerURI->GetSpec(referrer);
}
const nsAFlatString& specUTF16 = NS_ConvertUTF8toUTF16(spec);
const nsAFlatString& ctypeUTF16 = NS_ConvertASCIItoUTF16(contentType);
const nsAFlatString& referrerUTF16 = NS_ConvertUTF8toUTF16(referrer);
const PRUnichar *strings[] = { specUTF16.get(), ctypeUTF16.get() };
const char *errorMessage;
PRUint32 errorFlag;
if (mLoader->mCompatMode == eCompatibility_NavQuirks) {
ReportToConsole(NS_LITERAL_STRING("MimeNotCssWarn").get(), strings, 2,
nsIScriptError::warningFlag, referrerUTF16.get());
errorMessage = "MimeNotCssWarn";
errorFlag = nsIScriptError::warningFlag;
} else {
// Drop the data stream so that we do not load it
aDataStream = nsnull;
ReportToConsole(NS_LITERAL_STRING("MimeNotCss").get(), strings, 2,
nsIScriptError::errorFlag, referrerUTF16.get());
errorMessage = "MimeNotCss";
errorFlag = nsIScriptError::errorFlag;
}
nsCOMPtr<nsIURI> referrer = GetReferrerURI();
nsContentUtils::ReportToConsole(nsContentUtils::eCSS_PROPERTIES,
errorMessage,
strings, NS_ARRAY_LENGTH(strings),
referrer, 0, 0, errorFlag,
"CSS Loader");
}
}