diff --git a/mozilla/extensions/permissions/Makefile.in b/mozilla/extensions/permissions/Makefile.in new file mode 100644 index 00000000000..9b882815974 --- /dev/null +++ b/mozilla/extensions/permissions/Makefile.in @@ -0,0 +1,73 @@ +# ***** BEGIN LICENSE BLOCK ***** +# Version: MPL 1.1/GPL 2.0/LGPL 2.1 +# +# 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 content blocker code. +# +# The Initial Developer of the Original Code is +# Michiel van Leeuwen . +# Portions created by the Initial Developer are Copyright (C) 2004 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# +# Alternatively, the contents of this file may be used under the terms of +# either the GNU General Public License Version 2 or later (the "GPL"), or +# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), +# in which case the provisions of the GPL or the LGPL are applicable instead +# of those above. If you wish to allow use of your version of this file only +# under the terms of either the GPL or the LGPL, and not to allow others to +# use your version of this file under the terms of the MPL, indicate your +# decision by deleting the provisions above and replace them with the notice +# and other provisions required by the GPL or the LGPL. If you do not delete +# the provisions above, a recipient may use your version of this file under +# the terms of any one of the MPL, the GPL or the LGPL. +# +# ***** END LICENSE BLOCK ***** + +DEPTH = ../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = permissions +LIBRARY_NAME = permissions +EXPORT_LIBRARY = 1 +IS_COMPONENT = 1 +MODULE_NAME = nsPermissionsModule + +#PACKAGE_FILE = permissions.pkg + +REQUIRES = xpcom \ + string \ + necko \ + dom \ + widget \ + content \ + pref \ + docshell \ + appshell \ + cookie \ + $(NULL) + +CPPSRCS = \ + nsModuleFactory.cpp \ + nsContentBlocker.cpp \ + $(NULL) +ifdef MOZ_MAIL_NEWS +CPPSRCS += nsMailnewsContentBlocker.cpp +DEFINES += -DMOZ_MAIL_NEWS +endif + +include $(topsrcdir)/config/rules.mk diff --git a/mozilla/extensions/permissions/nsContentBlocker.cpp b/mozilla/extensions/permissions/nsContentBlocker.cpp new file mode 100644 index 00000000000..68b9ef21967 --- /dev/null +++ b/mozilla/extensions/permissions/nsContentBlocker.cpp @@ -0,0 +1,293 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * 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 content blocker code. + * + * The Initial Developer of the Original Code is + * Michiel van Leeuwen . + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ +#include "nsContentBlocker.h" +#include "nsIDocument.h" +#include "nsIContent.h" +#include "nsINodeInfo.h" +#include "nsIURI.h" +#include "nsIServiceManager.h" +#include "nsIScriptGlobalObject.h" +#include "nsIDOMWindow.h" +#include "nsIDOMDocument.h" +#include "nsIDocShellTreeItem.h" +#include "nsIPrefService.h" +#include "nsIPrefBranch.h" +#include "nsIDocShell.h" +#include "nsString.h" +#include "nsContentPolicyUtils.h" + +// Possible behavior pref values +// Those map to the nsIPermissionManager values where possible +#define BEHAVIOR_ACCEPT nsIPermissionManager::ALLOW_ACTION +#define BEHAVIOR_REJECT nsIPermissionManager::DENY_ACTION +#define BEHAVIOR_NOFOREIGN 3 + +// From nsIContentPolicy +static const char *kTypeString[NUMBER_OF_TYPES] = {"other", + "script", + "image", + "stylesheet", + "object", + "document", + "subdocument", + "refresh"}; + + +NS_IMPL_ISUPPORTS3(nsContentBlocker, + nsIContentPolicy, + nsIObserver, + nsSupportsWeakReference) + +nsContentBlocker::nsContentBlocker() +{ + memset(mBehaviorPref, BEHAVIOR_ACCEPT, NUMBER_OF_TYPES); +} + +nsresult +nsContentBlocker::Init() +{ + nsresult rv; + mPermissionManager = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr prefService = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr prefBranch; + rv = prefService->GetBranch("permissions.default.", getter_AddRefs(prefBranch)); + NS_ENSURE_SUCCESS(rv, rv); + + // The branch is not a copy of the prefservice, but a new object, because + // it is a non-default branch. Adding obeservers to it will only work if + // we make sure that the object doesn't die. So, keep a reference to it. + mPrefBranchInternal = do_QueryInterface(prefBranch, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + rv = mPrefBranchInternal->AddObserver("", this, PR_TRUE); + PrefChanged(prefBranch, nsnull); + + return rv; +} + +#undef LIMIT +#define LIMIT(x, low, high, default) ((x) >= (low) && (x) <= (high) ? (x) : (default)) + +void +nsContentBlocker::PrefChanged(nsIPrefBranch *aPrefBranch, + const char *aPref) +{ + PRInt32 val; + +#define PREF_CHANGED(_P) (!aPref || !strcmp(aPref, _P)) + + for(PRUint32 i = 0; i < NUMBER_OF_TYPES; ++i) { + if (PREF_CHANGED(kTypeString[i]) && + NS_SUCCEEDED(aPrefBranch->GetIntPref(kTypeString[i], &val))) + mBehaviorPref[i] = LIMIT(val, 1, 3, 1); + } + +} + +// nsIContentPolicy Implementation +NS_IMETHODIMP +nsContentBlocker::ShouldLoad(PRUint32 aContentType, + nsIURI *aContentLocation, + nsIURI *aRequestingLocation, + nsISupports *aRequestingContext, + const nsACString &aMimeGuess, + nsISupports *aExtra, + PRInt16 *aDecision) +{ + *aDecision = nsIContentPolicy::ACCEPT; + nsresult rv; + + // Ony support NUMBER_OF_TYPES content types. that all there is at the + // moment, but you never know... + if (aContentType > NUMBER_OF_TYPES) + return NS_OK; + + // we can't do anything without this + if (!aContentLocation) + return NS_OK; + + // we only want to check http, https, ftp + // for chrome:// and resources and others, no need to check. + nsCAutoString scheme; + aContentLocation->GetScheme(scheme); + if (!scheme.LowerCaseEqualsLiteral("ftp") && + !scheme.LowerCaseEqualsLiteral("http") && + !scheme.LowerCaseEqualsLiteral("https")) + return NS_OK; + + PRBool shouldLoad; + rv = TestPermission(aContentLocation, aRequestingLocation, aContentType, &shouldLoad); + NS_ENSURE_SUCCESS(rv, rv); + if (!shouldLoad) + *aDecision = nsIContentPolicy::REJECT_SERVER; + + return NS_OK; +} + +NS_IMETHODIMP +nsContentBlocker::ShouldProcess(PRUint32 aContentType, + nsIURI *aContentLocation, + nsIURI *aRequestingLocation, + nsISupports *aRequestingContext, + const nsACString &aMimeGuess, + nsISupports *aExtra, + PRInt16 *aDecision) +{ + // For loads where aRequestingContext is chrome, we should just + // accept. Those are most likely toplevel loads in windows, and + // chrome generally knows what it's doing anyway. + nsCOMPtr item = + do_QueryInterface(NS_CP_GetDocShellFromContext(aRequestingContext)); + + if (item) { + PRInt32 type; + item->GetItemType(&type); + if (type == nsIDocShellTreeItem::typeChrome) { + *aDecision = nsIContentPolicy::ACCEPT; + return NS_OK; + } + } + + // This isn't a load from chrome. Just do a ShouldLoad() check -- + // we want the same answer here + return ShouldLoad(aContentType, aContentLocation, aRequestingLocation, + aRequestingContext, aMimeGuess, aExtra, aDecision); +} + +nsresult +nsContentBlocker::TestPermission(nsIURI *aCurrentURI, + nsIURI *aFirstURI, + PRInt32 aContentType, + PRBool *aPermission) +{ + // This default will also get used if there is an unknown value in the + // permission list, or if the permission manager returns unknown values. + *aPermission = PR_TRUE; + + // check the permission list first; if we find an entry, it overrides + // default prefs. + // Don't forget the aContentType ranges from 1..8, while the + // array is indexed 0..7 + PRUint32 permission; + nsresult rv = mPermissionManager->TestPermission(aCurrentURI, + kTypeString[aContentType - 1], + &permission); + NS_ENSURE_SUCCESS(rv, rv); + + // If there is nothing on the list, use the default. + // Use the fact that the nsIPermissionManager values map to + // the BEHAVIOR_* values above. + switch (permission) { + case nsIPermissionManager::UNKNOWN_ACTION: + permission = mBehaviorPref[aContentType - 1]; + break; + + // if we found an entry, use it + case BEHAVIOR_ACCEPT: + *aPermission = PR_TRUE; + break; + case BEHAVIOR_REJECT: + *aPermission = PR_FALSE; + break; + + case BEHAVIOR_NOFOREIGN: + // Third party checking + + // compare tails of names checking to see if they have a common domain + // we do this by comparing the tails of both names where each tail + // includes at least one dot + + // A more generic method somewhere would be nice + + nsCAutoString currentHost; + rv = aCurrentURI->GetAsciiHost(currentHost); + NS_ENSURE_SUCCESS(rv, rv); + + // Search for two dots, starting at the end. + // If there are no two dots found, ++dot will turn to zero, + // that will return the entire string. + PRInt32 dot = currentHost.RFindChar('.'); + dot = currentHost.RFindChar('.', dot-1); + ++dot; + + // Get the domain, ie the last part of the host (www.domain.com -> domain.com) + // This will break on co.uk + const nsCSubstring &tail = + Substring(currentHost, dot, currentHost.Length() - dot); + + nsCAutoString firstHost; + rv = aFirstURI->GetAsciiHost(firstHost); + NS_ENSURE_SUCCESS(rv, rv); + + // If the tail is longer then the whole firstHost, it will never match + if (firstHost.Length() < tail.Length()) { + *aPermission = PR_FALSE; + return NS_OK; + } + + // Get the last part of the firstUri with the same length as |tail| + const nsCSubstring &firstTail = + Substring(firstHost, firstHost.Length() - tail.Length(), tail.Length()); + + // Check that both tails are the same, and that just before the tail in + // |firstUri| there is a dot. That means both url are in the same domain + if ((firstHost.Length() > tail.Length() && + firstHost.CharAt(firstHost.Length() - tail.Length() - 1) != '.') || + !tail.Equals(firstTail)) { + *aPermission = PR_FALSE; + } + break; + } + + return NS_OK; +} + +NS_IMETHODIMP +nsContentBlocker::Observe(nsISupports *aSubject, + const char *aTopic, + const PRUnichar *aData) +{ + NS_ASSERTION(!strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic), + "unexpected topic - we only deal with pref changes!"); + + if (mPrefBranchInternal) + PrefChanged(mPrefBranchInternal, NS_LossyConvertUTF16toASCII(aData).get()); + return NS_OK; +} diff --git a/mozilla/extensions/permissions/nsContentBlocker.h b/mozilla/extensions/permissions/nsContentBlocker.h new file mode 100644 index 00000000000..71a3929d43d --- /dev/null +++ b/mozilla/extensions/permissions/nsContentBlocker.h @@ -0,0 +1,86 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * 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 content blocker code. + * + * The Initial Developer of the Original Code is + * Michiel van Leeuwen . + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ +#ifndef nsContentBlocker_h__ +#define nsContentBlocker_h__ + +#include "nsIContentPolicy.h" +#include "nsIObserver.h" +#include "nsWeakReference.h" +#include "nsIPermissionManager.h" +#include "nsIPrefBranchInternal.h" + +class nsIPrefBranch; + +//////////////////////////////////////////////////////////////////////////////// + +// number of permission types in nsIContentPolicy +#define NUMBER_OF_TYPES 8 + +class nsContentBlocker : public nsIContentPolicy, + public nsIObserver, + public nsSupportsWeakReference +{ +public: + + // nsISupports + NS_DECL_ISUPPORTS + NS_DECL_NSICONTENTPOLICY + NS_DECL_NSIOBSERVER + + nsContentBlocker(); + nsresult Init(); + +private: + ~nsContentBlocker() {} + + void PrefChanged(nsIPrefBranch *, const char *); + nsresult TestPermission(nsIURI *aCurrentURI, + nsIURI *aFirstURI, + PRInt32 aContentType, + PRBool *aPermission); + + nsCOMPtr mPermissionManager; + nsCOMPtr mPrefBranchInternal; + PRUint8 mBehaviorPref[NUMBER_OF_TYPES]; +}; + +#define NS_CONTENTBLOCKER_CID \ +{ 0x4ca6b67b, 0x5cc7, 0x4e71, \ + { 0xa9, 0x8a, 0x97, 0xaf, 0x1c, 0x13, 0x48, 0x62 } } + +#define NS_CONTENTBLOCKER_CONTRACTID "@mozilla.org/permissions/contentblocker;1" + +#endif /* nsContentBlocker_h__ */ diff --git a/mozilla/extensions/permissions/nsMailnewsContentBlocker.cpp b/mozilla/extensions/permissions/nsMailnewsContentBlocker.cpp new file mode 100644 index 00000000000..3961b4588a5 --- /dev/null +++ b/mozilla/extensions/permissions/nsMailnewsContentBlocker.cpp @@ -0,0 +1,208 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * 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 content blocker code. + * + * The Initial Developer of the Original Code is + * Michiel van Leeuwen . + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsMailnewsContentBlocker.h" +#include "nsIURI.h" +#include "nsIServiceManager.h" +#include "nsIScriptGlobalObject.h" +#include "nsIDOMWindow.h" +#include "nsIDocShellTreeItem.h" +#include "nsIPrefService.h" +#include "nsIPrefBranch.h" +#include "nsIPrefBranchInternal.h" +#include "nsIDocShell.h" +#include "nsContentPolicyUtils.h" +#include "nsString.h" +#include "nsCRT.h" + +/* + * Block all remote stuff from loading in mailnews. Not only images, but + * also documents, javascripts, iframes etc + * remote is http, https, ftp. + */ + +static const char kBlockRemotePrefName[] = "permissions.mailnews.block_remote"; +static const PRBool kBlockRemotePrefDefault = PR_FALSE; + + +NS_IMPL_ISUPPORTS3(nsMailnewsContentBlocker, + nsIContentPolicy, + nsIObserver, + nsSupportsWeakReference) + +nsMailnewsContentBlocker::nsMailnewsContentBlocker() + : mBlockRemotePref(kBlockRemotePrefDefault) +{ +} + +nsresult +nsMailnewsContentBlocker::Init() +{ + nsCOMPtr prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID); + if (prefBranch) { + prefBranch->AddObserver(kBlockRemotePrefName, this, PR_TRUE); + + PrefChanged(prefBranch, nsnull); + } + + return NS_OK; +} + +void +nsMailnewsContentBlocker::PrefChanged(nsIPrefBranch *aPrefBranch, + const char *aPref) +{ + PRInt32 val; + +#define PREF_CHANGED(_P) (!aPref || !strcmp(aPref, _P)) + + if (PREF_CHANGED(kBlockRemotePrefName) && + NS_SUCCEEDED(aPrefBranch->GetBoolPref(kBlockRemotePrefName, &val))) + mBlockRemotePref = val; +} + +/** + * Helper function to get the root DocShell given a context + * + * @param aContext The context (can be null) + * @return the root DocShell containing aContext, if found + */ +static inline already_AddRefed +GetRootDocShell(nsISupports *context) +{ + nsIDocShell *docshell = NS_CP_GetDocShellFromContext(context); + if (!docshell) + return nsnull; + + nsresult rv; + nsCOMPtr docshellTreeItem(do_QueryInterface(docshell, &rv)); + if (NS_FAILED(rv)) + return nsnull; + + nsCOMPtr rootItem; + // we want the app docshell, so don't use GetSameTypeRootTreeItem + rv = docshellTreeItem->GetRootTreeItem(getter_AddRefs(rootItem)); + if (NS_FAILED(rv)) + return nsnull; + + nsIDocShell *result; + CallQueryInterface(rootItem, &result); + return result; +} + +// nsIContentPolicy Implementation +NS_IMETHODIMP +nsMailnewsContentBlocker::ShouldLoad(PRUint32 aContentType, + nsIURI *aContentLocation, + nsIURI *aRequestingLocation, + nsISupports *aRequestingContext, + const nsACString &aMimeGuess, + nsISupports *aExtra, + PRInt16 *aDecision) +{ + *aDecision = nsIContentPolicy::ACCEPT; + nsresult rv; + + // we can't do anything without this + if (!aContentLocation) + return NS_OK; + + // Go find out if we are dealing with mailnews. Anything else + // isn't our concern. + nsCOMPtr docshell = GetRootDocShell(aRequestingContext); + if (!docshell) + return NS_OK; + + PRUint32 appType; + rv = docshell->GetAppType(&appType); + // We only want to deal with mailnews + if (NS_FAILED(rv) || appType != nsIDocShell::APP_TYPE_MAIL) + return NS_OK; + + PRBool isFtp; + rv = aContentLocation->SchemeIs("ftp", &isFtp); + NS_ENSURE_SUCCESS(rv,rv); + if (isFtp) { + // never allow ftp for mail messages, + // because we don't want to send the users email address + // as the anonymous password + *aDecision = nsIContentPolicy::REJECT_REQUEST; + return NS_OK; + } + + + // we only want to check http, https + // for chrome:// and resources and others, no need to check. + nsCAutoString scheme; + aContentLocation->GetScheme(scheme); + if (!scheme.LowerCaseEqualsLiteral("http") && + !scheme.LowerCaseEqualsLiteral("https")) + return NS_OK; + + // This is mailnews, a protocol we want, now lets do the real work! + + *aDecision = mBlockRemotePref ? nsIContentPolicy::REJECT_REQUEST : + nsIContentPolicy::ACCEPT; + + return NS_OK; +} + +NS_IMETHODIMP +nsMailnewsContentBlocker::ShouldProcess(PRUint32 aContentType, + nsIURI *aContentLocation, + nsIURI *aRequestingLocation, + nsISupports *aRequestingContext, + const nsACString &aMimeGuess, + nsISupports *aExtra, + PRInt16 *aDecision) +{ + *aDecision = nsIContentPolicy::ACCEPT; + return NS_OK; +} + +NS_IMETHODIMP +nsMailnewsContentBlocker::Observe(nsISupports *aSubject, + const char *aTopic, + const PRUnichar *aData) +{ + nsCOMPtr prefBranch = do_QueryInterface(aSubject); + NS_ASSERTION(!strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic), + "unexpected topic - we only deal with pref changes!"); + + if (prefBranch) + PrefChanged(prefBranch, NS_LossyConvertUTF16toASCII(aData).get()); + return NS_OK; +} diff --git a/mozilla/extensions/permissions/nsMailnewsContentBlocker.h b/mozilla/extensions/permissions/nsMailnewsContentBlocker.h new file mode 100644 index 00000000000..d7ee53c7e7b --- /dev/null +++ b/mozilla/extensions/permissions/nsMailnewsContentBlocker.h @@ -0,0 +1,75 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * 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 content blocker code. + * + * The Initial Developer of the Original Code is + * Michiel van Leeuwen . + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsMailnewsContentBlocker_h__ +#define nsMailnewsContentBlocker_h__ + +#include "nsIContentPolicy.h" +#include "nsIObserver.h" +#include "nsWeakReference.h" + +class nsIPrefBranch; + +//////////////////////////////////////////////////////////////////////////////// + +class nsMailnewsContentBlocker : public nsIContentPolicy, + public nsIObserver, + public nsSupportsWeakReference +{ +public: + + // nsISupports + NS_DECL_ISUPPORTS + NS_DECL_NSICONTENTPOLICY + NS_DECL_NSIOBSERVER + + nsMailnewsContentBlocker(); + nsresult Init(); + +private: + ~nsMailnewsContentBlocker() {} + + void PrefChanged(nsIPrefBranch *, const char *); + PRBool mBlockRemotePref; +}; + +#define NS_MAILNEWSCONTENTBLOCKER_CID \ +{ 0xe37bc4ee, 0xc6b6, 0x4a05, \ + { 0xac, 0x14, 0x3c, 0x57, 0x05, 0x5a, 0x8e, 0xb5 } } + +#define NS_MAILNEWSCONTENTBLOCKER_CONTRACTID "@mozilla.org/permissions/mailnewscontentblocker;1" + +#endif /* nsMailnewsContentBlocker_h__ */ diff --git a/mozilla/extensions/permissions/nsModuleFactory.cpp b/mozilla/extensions/permissions/nsModuleFactory.cpp new file mode 100644 index 00000000000..7181d659804 --- /dev/null +++ b/mozilla/extensions/permissions/nsModuleFactory.cpp @@ -0,0 +1,137 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * 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 content blocker code. + * + * The Initial Developer of the Original Code is + * Michiel van Leeuwen . + * Portions created by the Initial Developer are Copyright (C) 2004 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsIModule.h" +#include "nsIGenericFactory.h" +#include "nsIServiceManager.h" +#include "nsContentBlocker.h" +#include "nsMailnewsContentBlocker.h" +#include "nsXPIDLString.h" +#include "nsICategoryManager.h" + +// Define the constructor function for the objects +NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsContentBlocker, Init) +#ifdef MOZ_MAIL_NEWS +NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsMailnewsContentBlocker, Init) +#endif + +static NS_METHOD +RegisterContentPolicy(nsIComponentManager *aCompMgr, nsIFile *aPath, + const char *registryLocation, const char *componentType, + const nsModuleComponentInfo *info) +{ + nsresult rv; + nsCOMPtr catman = + do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv); + if (NS_FAILED(rv)) return rv; + nsXPIDLCString previous; + return catman->AddCategoryEntry("content-policy", + NS_CONTENTBLOCKER_CONTRACTID, + NS_CONTENTBLOCKER_CONTRACTID, + PR_TRUE, PR_TRUE, getter_Copies(previous)); +} + +#ifdef MOZ_MAIL_NEWS +static NS_METHOD +RegisterMailnewsContentPolicy(nsIComponentManager *aCompMgr, nsIFile *aPath, + const char *registryLocation, const char *componentType, + const nsModuleComponentInfo *info) +{ + nsresult rv; + nsCOMPtr catman = + do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv); + if (NS_FAILED(rv)) return rv; + nsXPIDLCString previous; + return catman->AddCategoryEntry("content-policy", + NS_MAILNEWSCONTENTBLOCKER_CONTRACTID, + NS_MAILNEWSCONTENTBLOCKER_CONTRACTID, + PR_TRUE, PR_TRUE, getter_Copies(previous)); +} +#endif + +static NS_METHOD +UnregisterContentPolicy(nsIComponentManager *aCompMgr, nsIFile *aPath, + const char *registryLocation, + const nsModuleComponentInfo *info) +{ + nsresult rv; + nsCOMPtr catman = + do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv); + if (NS_FAILED(rv)) return rv; + + return catman->DeleteCategoryEntry("content-policy", + NS_CONTENTBLOCKER_CONTRACTID, + PR_TRUE); +} + +#ifdef MOZ_MAIL_NEWS +static NS_METHOD +UnregisterMailnewsContentPolicy(nsIComponentManager *aCompMgr, nsIFile *aPath, + const char *registryLocation, + const nsModuleComponentInfo *info) +{ + nsresult rv; + nsCOMPtr catman = + do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv); + if (NS_FAILED(rv)) return rv; + + return catman->DeleteCategoryEntry("content-policy", + NS_MAILNEWSCONTENTBLOCKER_CONTRACTID, + PR_TRUE); +} +#endif + + +// The list of components we register +static const nsModuleComponentInfo components[] = { + { "ContentBlocker", + NS_CONTENTBLOCKER_CID, + NS_CONTENTBLOCKER_CONTRACTID, + nsContentBlockerConstructor, + RegisterContentPolicy, UnregisterContentPolicy + } +#ifdef MOZ_MAIL_NEWS + , + { "MailnewsContentBlocker", + NS_MAILNEWSCONTENTBLOCKER_CID, + NS_MAILNEWSCONTENTBLOCKER_CONTRACTID, + nsMailnewsContentBlockerConstructor, + RegisterMailnewsContentPolicy, UnregisterMailnewsContentPolicy + } +#endif +}; + +NS_IMPL_NSGETMODULE(nsPermissionsModule, components)