From fed086b1d7efc52cae01de32aaeb2dd00d1836c3 Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Tue, 12 Jan 2010 10:45:20 +1300 Subject: [PATCH] Bug 526394. Part 34: Create nsContentUtils::AddScriptBlockerAndPreventAddingRunners. r=sicking --- content/base/public/nsContentUtils.h | 8 ++++++++ content/base/src/nsContentUtils.cpp | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/content/base/public/nsContentUtils.h b/content/base/public/nsContentUtils.h index 740facda054f..3c0e8906c7e4 100644 --- a/content/base/public/nsContentUtils.h +++ b/content/base/public/nsContentUtils.h @@ -1366,6 +1366,13 @@ public: */ static void AddScriptBlocker(); + /** + * Increases the count of blockers preventing scripts from running. + * Also, while this script blocker is active, script runners must not be + * added --- we'll assert if one is, and ignore it. + */ + static void AddScriptBlockerAndPreventAddingRunners(); + /** * Decreases the count of blockers preventing scripts from running. * NOTE: You might want to use nsAutoScriptBlocker rather than calling @@ -1590,6 +1597,7 @@ private: static PRUint32 sRemovableScriptBlockerCount; static nsCOMArray* sBlockedScriptRunners; static PRUint32 sRunnersCountAtFirstBlocker; + static PRUint32 sScriptBlockerCountWhereRunnersPrevented; static nsIInterfaceRequestor* sSameOriginChecker; }; diff --git a/content/base/src/nsContentUtils.cpp b/content/base/src/nsContentUtils.cpp index 9a7694109030..fd3e3fda1839 100644 --- a/content/base/src/nsContentUtils.cpp +++ b/content/base/src/nsContentUtils.cpp @@ -223,6 +223,7 @@ PRUint32 nsContentUtils::sScriptBlockerCount = 0; PRUint32 nsContentUtils::sRemovableScriptBlockerCount = 0; nsCOMArray* nsContentUtils::sBlockedScriptRunners = nsnull; PRUint32 nsContentUtils::sRunnersCountAtFirstBlocker = 0; +PRUint32 nsContentUtils::sScriptBlockerCountWhereRunnersPrevented = 0; nsIInterfaceRequestor* nsContentUtils::sSameOriginChecker = nsnull; nsIJSRuntimeService *nsAutoGCRoot::sJSRuntimeService; @@ -4493,12 +4494,25 @@ nsContentUtils::AddScriptBlocker() ++sScriptBlockerCount; } +/* static */ +void +nsContentUtils::AddScriptBlockerAndPreventAddingRunners() +{ + AddScriptBlocker(); + if (sScriptBlockerCountWhereRunnersPrevented == 0) { + sScriptBlockerCountWhereRunnersPrevented = sScriptBlockerCount; + } +} + /* static */ void nsContentUtils::RemoveScriptBlocker() { NS_ASSERTION(sScriptBlockerCount != 0, "Negative script blockers"); --sScriptBlockerCount; + if (sScriptBlockerCount < sScriptBlockerCountWhereRunnersPrevented) { + sScriptBlockerCountWhereRunnersPrevented = 0; + } if (sScriptBlockerCount) { return; } @@ -4522,7 +4536,6 @@ nsContentUtils::RemoveScriptBlocker() } } - /* static */ PRBool nsContentUtils::AddScriptRunner(nsIRunnable* aRunnable) @@ -4532,6 +4545,10 @@ nsContentUtils::AddScriptRunner(nsIRunnable* aRunnable) } if (sScriptBlockerCount) { + if (sScriptBlockerCountWhereRunnersPrevented > 0) { + NS_ERROR("Adding a script runner when that is prevented!"); + return PR_FALSE; + } return sBlockedScriptRunners->AppendObject(aRunnable); }