first part of ContentPolicy stuff (#37983, r=brendan)

This commit is contained in:
shaver%mozilla.org 2000-05-05 05:28:49 +00:00
Родитель f346f8d86c
Коммит a09f39c8c7
14 изменённых файлов: 606 добавлений и 6 удалений

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

@ -0,0 +1,69 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla code.
*
* The Initial Developer of the Original Code is Zero-Knowledge Systems,
* Inc. Portions created by Zero-Knowledge are Copyright (C) 2000
* Zero-Knowledge Systems, Inc. All Rights Reserved.
*
* Contributor(s):
*/
/*
* Utility routines for checking content load/process policy settings.
*/
#include "nsString.h"
#include "nsIContentPolicy.h"
#include "nsIAllocator.h"
#include "nsIServiceManager.h"
#ifndef __nsContentPolicyUtils_h__
#define __nsContentPolicyUtils_h__
class nsIDOMElement;
#define NS_CONTENTPOLICY_PROGID "layout.content-policy.1"
#define NS_CONTENTPOLICY_CATEGORY "content-policy"
#define NS_CONTENTPOLICY_CID \
{0x0e3afd3d, 0xeb60, 0x4c2b, \
{ 0x96, 0x3b, 0x56, 0xd7, 0xc4, 0x39, 0xf1, 0x24 }}
/* takes contentType, aURL, and element from its context */
#define CHECK_CONTENT_POLICY(action, result) \
PR_BEGIN_MACRO \
nsresult rv; \
NS_WITH_SERVICE(nsIContentPolicy, policy, NS_CONTENTPOLICY_PROGID, &rv); \
if (NS_FAILED(rv)) \
return rv; \
\
return policy->##action(contentType, element, aURL.GetUnicode(), result); \
PR_END_MACRO
inline nsresult
NS_CheckContentLoadPolicy(PRInt32 contentType, const nsString &aURL,
nsIDOMElement *element, PRBool *shouldLoad)
{
CHECK_CONTENT_POLICY(ShouldLoad, shouldLoad);
}
inline nsresult
NS_CheckContentProcessPolicy(PRInt32 contentType, nsString &aURL,
nsIDOMElement *element, PRBool *shouldProcess)
{
CHECK_CONTENT_POLICY(ShouldProcess, shouldProcess);
}
#undef CHECK_CONTENT_POLICY
#endif /* __nsContentPolicyUtils_h__ */

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

@ -0,0 +1,56 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla code.
*
* The Initial Developer of the Original Code is Zero-Knowledge Systems,
* Inc. Portions created by Zero-Knowledge are Copyright (C) 2000
* Zero-Knowledge Systems, Inc. All Rights Reserved.
*
* Contributor(s):
*/
#include "nsISupports.idl"
#include "nsIURL.idl"
#include "domstubs.idl"
/**
* Interface for content policy mechanism. Implementations of this
* interface can be used to control loading of various types of out-of-line
* content, or processing of certain types of in-line content.
*/
[scriptable,uuid(1cb4085d-5407-4169-bcfe-4c5ba013fa5b)]
interface nsIContentPolicy : nsISupports
{
const short CONTENT_OTHER = 0;
const short CONTENT_SCRIPT = 1;
const short CONTENT_IMAGE = 2;
const short CONTENT_STYLESHEET = 3;
const short CONTENT_OBJECT = 4;
/**
* Should the content at this location be loaded and processed?
*
* XXX Permit URL-rewriting?
* XXX Use MIME types for contentType?
* XXX Use nsIURL for location?
*/
boolean shouldLoad(in PRInt32 contentType, in nsIDOMElement element,
in wstring contentLocation);
/**
* Should the contents of the element in question be processed?
*/
boolean shouldProcess(in PRInt32 contentType, in nsIDOMElement element,
in wstring documentLocation);
};

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

@ -0,0 +1,176 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla code.
*
* The Initial Developer of the Original Code is Zero-Knowledge Systems,
* Inc. Portions created by Zero-Knowledge are Copyright (C) 2000
* Zero-Knowledge Systems, Inc. All Rights Reserved.
*
* Contributor(s):
*/
#include "nsISupports.h"
#include "nsISupportsPrimitives.h"
#include "nsXPIDLString.h"
#include "nsContentPolicyUtils.h"
#include "nsContentPolicy.h"
#include "nsICategoryManager.h"
NS_IMPL_ISUPPORTS1(nsContentPolicy, nsIContentPolicy)
nsresult
NS_NewContentPolicy(nsIContentPolicy **aResult)
{
*aResult = new nsContentPolicy;
if (!*aResult)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*aResult);
return NS_OK;
}
/*
* This constructor does far too much. I wish there was a way to get
* an Init method called by the service manager after the factory
* returned the new object, so that errors could be propagated back to
* the caller correctly.
*/
nsContentPolicy::nsContentPolicy()
{
NS_INIT_REFCNT();
nsresult rv;
NS_WITH_SERVICE(nsICategoryManager, catman, NS_CATEGORYMANAGER_PROGID, &rv);
if (NS_FAILED(rv))
/* log an error? */
return;
/*
* I'd like to use GetCategoryContents, so that I can size the array
* correctly on the first go and avoid the enumerator overhead, but it's
* not yet implemented (see nsCategoryManager.cpp). No biggie, I guess.
*/
nsCOMPtr<nsISimpleEnumerator> catEnum;
if (NS_FAILED(catman->EnumerateCategory(NS_CONTENTPOLICY_CATEGORY,
getter_AddRefs(catEnum)))) {
/* no category, no problem */
return;
}
PRBool hasMore;
if (NS_FAILED(catEnum->HasMoreElements(&hasMore)) || !hasMore ||
NS_FAILED(NS_NewISupportsArray(getter_AddRefs(mPolicies)))) {
return;
}
/*
* Populate mPolicies with policy services named by progids in the
* "content-policy" category.
*/
nsCOMPtr<nsISupports> item;
while (NS_SUCCEEDED(catEnum->GetNext(getter_AddRefs(item)))) {
nsCOMPtr<nsISupportsString> string = do_QueryInterface(item, &rv);
if (NS_FAILED(rv))
continue;
nsXPIDLCString progid;
if (NS_FAILED(string->GetData(getter_Copies(progid))))
continue;
#ifdef DEBUG_shaver
fprintf(stderr, "POLICY: loading %s\n", (const char *)progid);
#endif
/*
* Create this policy service and add to mPolicies.
*
* Should we try to parse as a CID, in case the component prefers to be
* registered that way?
*/
nsCOMPtr<nsISupports> policy = do_GetService(progid, &rv);
if (NS_SUCCEEDED(rv))
mPolicies->AppendElement(policy);
}
}
nsContentPolicy::~nsContentPolicy()
{
}
#define POLICY_LOAD 0
#define POLICY_PROCESS 1
NS_IMETHODIMP
nsContentPolicy::CheckPolicy(PRInt32 policyType, PRInt32 contentType,
nsIDOMElement *element,
const PRUnichar *contentLocation,
PRBool *shouldProceed)
{
*shouldProceed = PR_TRUE;
if (!mPolicies)
return NS_OK;
/*
* Enumerate mPolicies and ask each of them, taking the logical AND of
* their permissions.
*/
nsresult rv;
nsCOMPtr<nsIContentPolicy> policy;
PRUint32 count;
if (NS_FAILED(rv = mPolicies->Count(&count)))
return NS_OK;
for (PRUint32 i = 0; i < count; i++) {
rv = mPolicies->QueryElementAt(i, NS_GET_IID(nsIContentPolicy),
getter_AddRefs(policy));
if (NS_FAILED(rv))
continue;
/* check the appropriate policy */
if (policyType == POLICY_LOAD)
rv = policy->ShouldLoad(contentType, element, contentLocation,
shouldProceed);
else
rv = policy->ShouldProcess(contentType, element, contentLocation,
shouldProceed);
if (NS_SUCCEEDED(rv) && !*shouldProceed)
/* policy says no, no point continuing to check */
return NS_OK;
}
/*
* One of the policy objects might be misbehaving and setting shouldProceed
* to PR_FALSE before returning an error, so force it back to PR_TRUE
* here.
*/
*shouldProceed = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
nsContentPolicy::ShouldLoad(PRInt32 contentType, nsIDOMElement *element,
const PRUnichar *contentLocation,
PRBool *shouldLoad)
{
return CheckPolicy(POLICY_LOAD, contentType, element, contentLocation,
shouldLoad);
}
NS_IMETHODIMP
nsContentPolicy::ShouldProcess(PRInt32 contentType, nsIDOMElement *element,
const PRUnichar *contentLocation,
PRBool *shouldProcess)
{
return CheckPolicy(POLICY_PROCESS, contentType, element, contentLocation,
shouldProcess);
}

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

@ -0,0 +1,46 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla code.
*
* The Initial Developer of the Original Code is Zero-Knowledge Systems,
* Inc. Portions created by Zero-Knowledge are Copyright (C) 2000
* Zero-Knowledge Systems, Inc. All Rights Reserved.
*
* Contributor(s):
*/
#include "nsIContentPolicy.h"
#include "nsISupportsArray.h"
#ifndef __nsContentPolicy_h__
#define __nsContentPolicy_h__
class nsContentPolicy : public nsIContentPolicy
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSICONTENTPOLICY
nsContentPolicy();
virtual ~nsContentPolicy();
private:
nsCOMPtr<nsISupportsArray> mPolicies;
NS_IMETHOD CheckPolicy(PRInt32 policyType, PRInt32 contentType,
nsIDOMElement *element,
const PRUnichar *contentLocation,
PRBool *shouldProceed);
};
nsresult
NS_NewContentPolicy(nsIContentPolicy **aResult);
#endif /* __nsContentPolicy_h__ */

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

@ -7,6 +7,7 @@ nsIAutoCopy.h
nsICaret.h
nsIContent.h
nsIContentIterator.h
nsContentPolicyUtils.h
nsIDiskDocument.h
nsIDocument.h
nsIDocumentContainer.h

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

@ -4,3 +4,4 @@
nsIChromeEventHandler.idl
nsISelectionController.idl
nsIContentPolicy.idl

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

@ -35,6 +35,7 @@ nsIAutoCopy.h \
nsICaret.h \
nsIContent.h \
nsIContentIterator.h \
nsContentPolicyUtils.h \
nsIDiskDocument.h \
nsIDocument.h \
nsIDocumentContainer.h \
@ -85,9 +86,10 @@ nsIScrollableFrame.h \
$(NULL)
XPIDLSRCS = \
nsIChromeEventHandler.idl \
nsISelectionController.idl \
nsIPrintListener.idl \
nsIChromeEventHandler.idl \
nsIContentPolicy.idl \
nsISelectionController.idl \
nsIPrintListener.idl \
$(NULL)
EXPORTS := $(addprefix $(srcdir)/, $(EXPORTS))

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

@ -23,6 +23,7 @@ DEPTH=..\..\..
EXPORTS = \
nslayout.h \
nsContentPolicyUtils.h \
nsHTMLReflowState.h \
nsIAutoCopy.h \
nsICaret.h \
@ -79,9 +80,10 @@ EXPORTS = \
MODULE=raptor
XPIDLSRCS= \
.\nsIChromeEventHandler.idl \
.\nsISelectionController.idl \
XPIDLSRCS= \
.\nsIChromeEventHandler.idl \
.\nsIContentPolicy.idl \
.\nsISelectionController.idl \
.\nsIPrintListener.idl \
$(NULL)

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

@ -0,0 +1,69 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla code.
*
* The Initial Developer of the Original Code is Zero-Knowledge Systems,
* Inc. Portions created by Zero-Knowledge are Copyright (C) 2000
* Zero-Knowledge Systems, Inc. All Rights Reserved.
*
* Contributor(s):
*/
/*
* Utility routines for checking content load/process policy settings.
*/
#include "nsString.h"
#include "nsIContentPolicy.h"
#include "nsIAllocator.h"
#include "nsIServiceManager.h"
#ifndef __nsContentPolicyUtils_h__
#define __nsContentPolicyUtils_h__
class nsIDOMElement;
#define NS_CONTENTPOLICY_PROGID "layout.content-policy.1"
#define NS_CONTENTPOLICY_CATEGORY "content-policy"
#define NS_CONTENTPOLICY_CID \
{0x0e3afd3d, 0xeb60, 0x4c2b, \
{ 0x96, 0x3b, 0x56, 0xd7, 0xc4, 0x39, 0xf1, 0x24 }}
/* takes contentType, aURL, and element from its context */
#define CHECK_CONTENT_POLICY(action, result) \
PR_BEGIN_MACRO \
nsresult rv; \
NS_WITH_SERVICE(nsIContentPolicy, policy, NS_CONTENTPOLICY_PROGID, &rv); \
if (NS_FAILED(rv)) \
return rv; \
\
return policy->##action(contentType, element, aURL.GetUnicode(), result); \
PR_END_MACRO
inline nsresult
NS_CheckContentLoadPolicy(PRInt32 contentType, const nsString &aURL,
nsIDOMElement *element, PRBool *shouldLoad)
{
CHECK_CONTENT_POLICY(ShouldLoad, shouldLoad);
}
inline nsresult
NS_CheckContentProcessPolicy(PRInt32 contentType, nsString &aURL,
nsIDOMElement *element, PRBool *shouldProcess)
{
CHECK_CONTENT_POLICY(ShouldProcess, shouldProcess);
}
#undef CHECK_CONTENT_POLICY
#endif /* __nsContentPolicyUtils_h__ */

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

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

@ -35,6 +35,7 @@ CPPSRCS = \
nsCommentNode.cpp \
nsContentIterator.cpp \
nsContentList.cpp \
nsContentPolicy.cpp \
nsDocument.cpp \
nsDocumentEncoder.cpp \
nsDocumentFragment.cpp \

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

@ -35,6 +35,7 @@ CPPSRCS = \
nsGenericDOMNodeList.cpp \
nsContentList.cpp \
nsContentIterator.cpp \
nsContentPolicy.cpp \
nsDocument.cpp \
nsDocumentEncoder.cpp \
nsDocumentFragment.cpp \

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

@ -0,0 +1,176 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla code.
*
* The Initial Developer of the Original Code is Zero-Knowledge Systems,
* Inc. Portions created by Zero-Knowledge are Copyright (C) 2000
* Zero-Knowledge Systems, Inc. All Rights Reserved.
*
* Contributor(s):
*/
#include "nsISupports.h"
#include "nsISupportsPrimitives.h"
#include "nsXPIDLString.h"
#include "nsContentPolicyUtils.h"
#include "nsContentPolicy.h"
#include "nsICategoryManager.h"
NS_IMPL_ISUPPORTS1(nsContentPolicy, nsIContentPolicy)
nsresult
NS_NewContentPolicy(nsIContentPolicy **aResult)
{
*aResult = new nsContentPolicy;
if (!*aResult)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*aResult);
return NS_OK;
}
/*
* This constructor does far too much. I wish there was a way to get
* an Init method called by the service manager after the factory
* returned the new object, so that errors could be propagated back to
* the caller correctly.
*/
nsContentPolicy::nsContentPolicy()
{
NS_INIT_REFCNT();
nsresult rv;
NS_WITH_SERVICE(nsICategoryManager, catman, NS_CATEGORYMANAGER_PROGID, &rv);
if (NS_FAILED(rv))
/* log an error? */
return;
/*
* I'd like to use GetCategoryContents, so that I can size the array
* correctly on the first go and avoid the enumerator overhead, but it's
* not yet implemented (see nsCategoryManager.cpp). No biggie, I guess.
*/
nsCOMPtr<nsISimpleEnumerator> catEnum;
if (NS_FAILED(catman->EnumerateCategory(NS_CONTENTPOLICY_CATEGORY,
getter_AddRefs(catEnum)))) {
/* no category, no problem */
return;
}
PRBool hasMore;
if (NS_FAILED(catEnum->HasMoreElements(&hasMore)) || !hasMore ||
NS_FAILED(NS_NewISupportsArray(getter_AddRefs(mPolicies)))) {
return;
}
/*
* Populate mPolicies with policy services named by progids in the
* "content-policy" category.
*/
nsCOMPtr<nsISupports> item;
while (NS_SUCCEEDED(catEnum->GetNext(getter_AddRefs(item)))) {
nsCOMPtr<nsISupportsString> string = do_QueryInterface(item, &rv);
if (NS_FAILED(rv))
continue;
nsXPIDLCString progid;
if (NS_FAILED(string->GetData(getter_Copies(progid))))
continue;
#ifdef DEBUG_shaver
fprintf(stderr, "POLICY: loading %s\n", (const char *)progid);
#endif
/*
* Create this policy service and add to mPolicies.
*
* Should we try to parse as a CID, in case the component prefers to be
* registered that way?
*/
nsCOMPtr<nsISupports> policy = do_GetService(progid, &rv);
if (NS_SUCCEEDED(rv))
mPolicies->AppendElement(policy);
}
}
nsContentPolicy::~nsContentPolicy()
{
}
#define POLICY_LOAD 0
#define POLICY_PROCESS 1
NS_IMETHODIMP
nsContentPolicy::CheckPolicy(PRInt32 policyType, PRInt32 contentType,
nsIDOMElement *element,
const PRUnichar *contentLocation,
PRBool *shouldProceed)
{
*shouldProceed = PR_TRUE;
if (!mPolicies)
return NS_OK;
/*
* Enumerate mPolicies and ask each of them, taking the logical AND of
* their permissions.
*/
nsresult rv;
nsCOMPtr<nsIContentPolicy> policy;
PRUint32 count;
if (NS_FAILED(rv = mPolicies->Count(&count)))
return NS_OK;
for (PRUint32 i = 0; i < count; i++) {
rv = mPolicies->QueryElementAt(i, NS_GET_IID(nsIContentPolicy),
getter_AddRefs(policy));
if (NS_FAILED(rv))
continue;
/* check the appropriate policy */
if (policyType == POLICY_LOAD)
rv = policy->ShouldLoad(contentType, element, contentLocation,
shouldProceed);
else
rv = policy->ShouldProcess(contentType, element, contentLocation,
shouldProceed);
if (NS_SUCCEEDED(rv) && !*shouldProceed)
/* policy says no, no point continuing to check */
return NS_OK;
}
/*
* One of the policy objects might be misbehaving and setting shouldProceed
* to PR_FALSE before returning an error, so force it back to PR_TRUE
* here.
*/
*shouldProceed = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
nsContentPolicy::ShouldLoad(PRInt32 contentType, nsIDOMElement *element,
const PRUnichar *contentLocation,
PRBool *shouldLoad)
{
return CheckPolicy(POLICY_LOAD, contentType, element, contentLocation,
shouldLoad);
}
NS_IMETHODIMP
nsContentPolicy::ShouldProcess(PRInt32 contentType, nsIDOMElement *element,
const PRUnichar *contentLocation,
PRBool *shouldProcess)
{
return CheckPolicy(POLICY_PROCESS, contentType, element, contentLocation,
shouldProcess);
}

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