Bug 642419 - Add error reporting for content scripts. r=smaug

This commit is contained in:
Josh Matthews 2011-03-18 00:08:18 -04:00
Родитель 5e26ddf354
Коммит d07f4b3f2d
4 изменённых файлов: 58 добавлений и 2 удалений

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

@ -49,6 +49,8 @@
#include "nsScriptLoader.h"
#include "nsIJSContextStack.h"
#include "nsIXULRuntime.h"
#include "nsIScriptError.h"
#include "nsIConsoleService.h"
static PRBool
IsChromeProcess()
@ -533,6 +535,53 @@ NS_NewGlobalMessageManager(nsIChromeFrameMessageManager** aResult)
return CallQueryInterface(mm, aResult);
}
void
ContentScriptErrorReporter(JSContext* aCx,
const char* aMessage,
JSErrorReport* aReport)
{
nsresult rv;
nsCOMPtr<nsIScriptError> scriptError =
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
return;
}
nsAutoString message, filename, line;
PRUint32 lineNumber, columnNumber, flags, errorNumber;
if (aReport) {
if (aReport->ucmessage) {
message.Assign(reinterpret_cast<const PRUnichar*>(aReport->ucmessage));
}
filename.AssignWithConversion(aReport->filename);
line.Assign(reinterpret_cast<const PRUnichar*>(aReport->uclinebuf));
lineNumber = aReport->lineno;
columnNumber = aReport->uctokenptr - aReport->uclinebuf;
flags = aReport->flags;
errorNumber = aReport->errorNumber;
} else {
lineNumber = columnNumber = errorNumber = 0;
flags = nsIScriptError::errorFlag | nsIScriptError::exceptionFlag;
}
if (message.IsEmpty()) {
message.AssignWithConversion(aMessage);
}
rv = scriptError->Init(message.get(), filename.get(), line.get(),
lineNumber, columnNumber, flags,
"Message manager content script");
if (NS_FAILED(rv)) {
return;
}
nsCOMPtr<nsIConsoleService> consoleService =
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
if (consoleService) {
(void) consoleService->LogMessage(scriptError);
}
}
nsDataHashtable<nsStringHashKey, nsFrameScriptExecutorJSObjectHolder*>*
nsFrameScriptExecutor::sCachedScripts = nsnull;
nsRefPtr<nsScriptCacheCleaner> nsFrameScriptExecutor::sScriptCacheCleaner;
@ -616,7 +665,7 @@ nsFrameScriptExecutor::LoadFrameScriptInternal(const nsAString& aURL)
JSObject* global = nsnull;
mGlobal->GetJSObject(&global);
if (global) {
JS_ExecuteScript(mCx, global, holder->mObject, nsnull);
(void) JS_ExecuteScript(mCx, global, holder->mObject, nsnull);
}
}
JSContext* unused;
@ -684,7 +733,7 @@ nsFrameScriptExecutor::LoadFrameScriptInternal(const nsAString& aURL)
"Cached message manager script");
sCachedScripts->Put(aURL, holder);
}
JS_ExecuteScript(mCx, global, scriptObj, nsnull);
(void) JS_ExecuteScript(mCx, global, scriptObj, nsnull);
}
//XXX Argh, JSPrincipals are manually refcounted!
JSPRINCIPALS_DROP(mCx, jsprin);

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

@ -180,6 +180,11 @@ public:
static nsFrameMessageManager* sChildProcessManager;
};
void
ContentScriptErrorReporter(JSContext* aCx,
const char* aMessage,
JSErrorReport* aReport);
class nsScriptCacheCleaner;
struct nsFrameScriptExecutorJSObjectHolder

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

@ -299,6 +299,7 @@ nsInProcessTabChildGlobal::InitTabChildGlobal()
JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_JIT | JSOPTION_ANONFUNFIX | JSOPTION_PRIVATE_IS_NSISUPPORTS);
JS_SetVersion(cx, JSVERSION_LATEST);
JS_SetErrorReporter(cx, ContentScriptErrorReporter);
xpc_LocalizeContext(cx);

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

@ -848,6 +848,7 @@ TabChild::InitTabChildGlobal()
JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_JIT | JSOPTION_ANONFUNFIX | JSOPTION_PRIVATE_IS_NSISUPPORTS);
JS_SetVersion(cx, JSVERSION_LATEST);
JS_SetErrorReporter(cx, ContentScriptErrorReporter);
xpc_LocalizeContext(cx);