Allow passing a cached nsIContentPolicy service pointer to

NS_CheckContent*Policy if the caller has one.  Bug 304845, r=biesi, sr=jst
This commit is contained in:
bzbarsky@mit.edu
2005-08-19 15:00:01 +00:00
parent 83461bf191
commit 5ca0e3609e
9 changed files with 55 additions and 12 deletions

View File

@@ -141,17 +141,28 @@ NS_CP_ContentTypeName(PRUint32 contentType)
/* Passes on parameters from its "caller"'s context. */ /* Passes on parameters from its "caller"'s context. */
#define CHECK_CONTENT_POLICY(action) \ #define CHECK_CONTENT_POLICY(action) \
PR_BEGIN_MACRO \
nsCOMPtr<nsIContentPolicy> policy = \ nsCOMPtr<nsIContentPolicy> policy = \
do_GetService(NS_CONTENTPOLICY_CONTRACTID); \ do_GetService(NS_CONTENTPOLICY_CONTRACTID); \
if (!policy) \ if (!policy) \
return NS_ERROR_FAILURE; \ return NS_ERROR_FAILURE; \
\ \
return policy-> action (contentType, contentLocation, requestOrigin, \ return policy-> action (contentType, contentLocation, requestOrigin, \
context, mimeType, extra, decision); context, mimeType, extra, decision); \
PR_END_MACRO
/* Passes on parameters from its "caller"'s context. */
#define CHECK_CONTENT_POLICY_WITH_SERVICE(action, _policy) \
PR_BEGIN_MACRO \
return _policy-> action (contentType, contentLocation, requestOrigin, \
context, mimeType, extra, decision); \
PR_END_MACRO
/** /**
* Alias for calling ShouldLoad on the content policy service. * Alias for calling ShouldLoad on the content policy service.
* Parameters are the same as nsIContentPolicy::shouldLoad. * Parameters are the same as nsIContentPolicy::shouldLoad, except for
* the last parameter, which can be used to pass in a pointer to the
* service if the caller already has one.
*/ */
inline nsresult inline nsresult
NS_CheckContentLoadPolicy(PRUint32 contentType, NS_CheckContentLoadPolicy(PRUint32 contentType,
@@ -160,8 +171,12 @@ NS_CheckContentLoadPolicy(PRUint32 contentType,
nsISupports *context, nsISupports *context,
const nsACString &mimeType, const nsACString &mimeType,
nsISupports *extra, nsISupports *extra,
PRInt16 *decision) PRInt16 *decision,
nsIContentPolicy *policyService = nsnull)
{ {
if (policyService) {
CHECK_CONTENT_POLICY_WITH_SERVICE(ShouldLoad, policyService);
}
CHECK_CONTENT_POLICY(ShouldLoad); CHECK_CONTENT_POLICY(ShouldLoad);
} }
@@ -176,12 +191,17 @@ NS_CheckContentProcessPolicy(PRUint32 contentType,
nsISupports *context, nsISupports *context,
const nsACString &mimeType, const nsACString &mimeType,
nsISupports *extra, nsISupports *extra,
PRInt16 *decision) PRInt16 *decision,
nsIContentPolicy *policyService = nsnull)
{ {
if (policyService) {
CHECK_CONTENT_POLICY_WITH_SERVICE(ShouldProcess, policyService);
}
CHECK_CONTENT_POLICY(ShouldProcess); CHECK_CONTENT_POLICY(ShouldProcess);
} }
#undef CHECK_CONTENT_POLICY #undef CHECK_CONTENT_POLICY
#undef CHECK_CONTENT_POLICY_WITH_SERVICE
/** /**
* Helper function to get an nsIDocShell given a context. * Helper function to get an nsIDocShell given a context.

View File

@@ -77,6 +77,7 @@ class nsIDOMDocument;
class nsIConsoleService; class nsIConsoleService;
class nsIStringBundleService; class nsIStringBundleService;
class nsIStringBundle; class nsIStringBundle;
class nsIContentPolicy;
#ifdef MOZ_XTF #ifdef MOZ_XTF
class nsIXTFService; class nsIXTFService;
#endif #endif
@@ -554,7 +555,15 @@ public:
return sPtrsToPtrsToRelease->AppendElement(aSupportsPtr) ? NS_OK : return sPtrsToPtrsToRelease->AppendElement(aSupportsPtr) ? NS_OK :
NS_ERROR_OUT_OF_MEMORY; NS_ERROR_OUT_OF_MEMORY;
} }
/**
* Return the content policy service
*/
static nsIContentPolicy *GetContentPolicy()
{
return sContentPolicyService;
}
private: private:
static nsresult doReparentContentWrapper(nsIContent *aChild, static nsresult doReparentContentWrapper(nsIContent *aChild,
nsIDocument *aNewDocument, nsIDocument *aNewDocument,
@@ -594,6 +603,8 @@ private:
static nsIStringBundleService* sStringBundleService; static nsIStringBundleService* sStringBundleService;
static nsIStringBundle* sStringBundles[PropertiesFile_COUNT]; static nsIStringBundle* sStringBundles[PropertiesFile_COUNT];
static nsIContentPolicy* sContentPolicyService;
// Holds pointers to nsISupports* that should be released at shutdown // Holds pointers to nsISupports* that should be released at shutdown
static nsVoidArray* sPtrsToPtrsToRelease; static nsVoidArray* sPtrsToPtrsToRelease;

View File

@@ -134,6 +134,7 @@ imgILoader *nsContentUtils::sImgLoader = nsnull;
nsIConsoleService *nsContentUtils::sConsoleService; nsIConsoleService *nsContentUtils::sConsoleService;
nsIStringBundleService *nsContentUtils::sStringBundleService; nsIStringBundleService *nsContentUtils::sStringBundleService;
nsIStringBundle *nsContentUtils::sStringBundles[PropertiesFile_COUNT]; nsIStringBundle *nsContentUtils::sStringBundles[PropertiesFile_COUNT];
nsIContentPolicy *nsContentUtils::sContentPolicyService;
nsVoidArray *nsContentUtils::sPtrsToPtrsToRelease; nsVoidArray *nsContentUtils::sPtrsToPtrsToRelease;
@@ -159,6 +160,9 @@ nsContentUtils::Init()
// It's ok to not have prefs too. // It's ok to not have prefs too.
CallGetService(NS_PREF_CONTRACTID, &sPref); CallGetService(NS_PREF_CONTRACTID, &sPref);
// It's also OK to not have a content policy service
CallGetService(NS_CONTENTPOLICY_CONTRACTID, &sContentPolicyService);
rv = NS_GetNameSpaceManager(&sNameSpaceManager); rv = NS_GetNameSpaceManager(&sNameSpaceManager);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
@@ -410,6 +414,7 @@ nsContentUtils::Shutdown()
{ {
sInitialized = PR_FALSE; sInitialized = PR_FALSE;
NS_IF_RELEASE(sContentPolicyService);
PRInt32 i; PRInt32 i;
for (i = 0; i < PRInt32(PropertiesFile_COUNT); ++i) for (i = 0; i < PRInt32(PropertiesFile_COUNT); ++i)
NS_IF_RELEASE(sStringBundles[i]); NS_IF_RELEASE(sStringBundles[i]);
@@ -1851,7 +1856,8 @@ nsContentUtils::CanLoadImage(nsIURI* aURI, nsISupports* aContext,
aContext, aContext,
EmptyCString(), //mime guess EmptyCString(), //mime guess
nsnull, //extra nsnull, //extra
&decision); &decision,
sContentPolicyService);
if (aImageBlockingStatus) { if (aImageBlockingStatus) {
*aImageBlockingStatus = *aImageBlockingStatus =

View File

@@ -530,7 +530,8 @@ nsScriptLoader::ProcessScriptElement(nsIScriptElement *aElement,
aElement, aElement,
NS_LossyConvertUCS2toASCII(type), NS_LossyConvertUCS2toASCII(type),
nsnull, //extra nsnull, //extra
&shouldLoad); &shouldLoad,
nsContentUtils::GetContentPolicy());
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) { if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
if (NS_FAILED(rv) || shouldLoad != nsIContentPolicy::REJECT_TYPE) { if (NS_FAILED(rv) || shouldLoad != nsIContentPolicy::REJECT_TYPE) {
return FireErrorNotification(NS_ERROR_CONTENT_BLOCKED, aElement, return FireErrorNotification(NS_ERROR_CONTENT_BLOCKED, aElement,

View File

@@ -190,7 +190,8 @@ ImageListener::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
domWindow->GetFrameElementInternal(), domWindow->GetFrameElementInternal(),
mimeType, mimeType,
nsnull, nsnull,
&decision); &decision,
nsContentUtils::GetContentPolicy());
if (NS_FAILED(rv) || NS_CP_REJECTED(decision)) { if (NS_FAILED(rv) || NS_CP_REJECTED(decision)) {
request->Cancel(NS_ERROR_CONTENT_BLOCKED); request->Cancel(NS_ERROR_CONTENT_BLOCKED);

View File

@@ -576,7 +576,8 @@ nsXBLService::LoadBindings(nsIContent* aContent, nsIURI* aURL, PRBool aAugmentFl
document, // context document, // context
EmptyCString(), // mime guess EmptyCString(), // mime guess
nsnull, // extra nsnull, // extra
&decision); &decision,
nsContentUtils::GetContentPolicy());
if (NS_SUCCEEDED(rv) && !NS_CP_ACCEPTED(decision)) if (NS_SUCCEEDED(rv) && !NS_CP_ACCEPTED(decision))
rv = NS_ERROR_NOT_AVAILABLE; rv = NS_ERROR_NOT_AVAILABLE;

View File

@@ -643,7 +643,8 @@ nsXMLContentSink::ProcessStyleLink(nsIContent* aElement,
aElement, aElement,
type, type,
nsnull, nsnull,
&decision); &decision,
nsContentUtils::GetContentPolicy());
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);

View File

@@ -1363,7 +1363,8 @@ nsObjectFrame::InstantiatePlugin(nsPresContext* aPresContext,
mContent, mContent,
nsDependentCString(aMimeType ? aMimeType : ""), nsDependentCString(aMimeType ? aMimeType : ""),
nsnull, //extra nsnull, //extra
&shouldLoad); &shouldLoad,
nsContentUtils::GetContentPolicy());
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) { if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
return NS_ERROR_CONTENT_BLOCKED_SHOW_ALT; return NS_ERROR_CONTENT_BLOCKED_SHOW_ALT;
} }

View File

@@ -860,7 +860,8 @@ CSSLoaderImpl::CheckLoadAllowed(nsIURI* aSourceURI,
aContext, aContext,
NS_LITERAL_CSTRING("text/css"), NS_LITERAL_CSTRING("text/css"),
nsnull, //extra param nsnull, //extra param
&shouldLoad); &shouldLoad,
nsContentUtils::GetContentPolicy());
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) { if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
LOG((" Load blocked by content policy")); LOG((" Load blocked by content policy"));