From 4cd7a0dc5dad146ad95af26d07fd6d8f4951cbcf Mon Sep 17 00:00:00 2001 From: "valeski%netscape.com" Date: Mon, 21 May 2001 22:42:46 +0000 Subject: [PATCH] r=ccarlen, sr=rpotts on the mozilla/docshell/* and mozilla/embedding/* mods (81263). embedding apps can now control whether or not images are loaded in a specific nsWebBrowser window. This is done via nsIWebBrowserSetup. shaver@mozilla.org provided a nsWebBrowserContentPolicy implementation which registers for, and responds to, nsIContentPolicy callbacks giving nsIContentPolicies an opportunity to cancel a load or the processing of an element. processing isn't hooked up yet, and some of the content policy types in the switch statement won't become relevant until further nsIContentPolicy hookup is done. Those cases are handled inside the docshell/webshell until the content policy hook-up is made. git-svn-id: svn://10.0.0.236/trunk@95622 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/docshell/base/nsDocShell.cpp | 15 ++++ mozilla/docshell/base/nsDocShell.h | 1 + mozilla/docshell/base/nsIDocShell.idl | 5 ++ .../browser/build/nsWebBrowserModule.cpp | 46 +++++++++- .../embedding/browser/webBrowser/Makefile.in | 5 +- .../embedding/browser/webBrowser/makefile.win | 1 + .../browser/webBrowser/nsIWebBrowserSetup.idl | 25 ++++-- .../browser/webBrowser/nsWebBrowser.cpp | 6 ++ .../webBrowser/nsWebBrowserContentPolicy.cpp | 88 +++++++++++++++++++ .../webBrowser/nsWebBrowserContentPolicy.h | 38 ++++++++ 10 files changed, 217 insertions(+), 13 deletions(-) create mode 100644 mozilla/embedding/browser/webBrowser/nsWebBrowserContentPolicy.cpp create mode 100644 mozilla/embedding/browser/webBrowser/nsWebBrowserContentPolicy.h diff --git a/mozilla/docshell/base/nsDocShell.cpp b/mozilla/docshell/base/nsDocShell.cpp index f31cd17ce77..ef5e8aca4a9 100644 --- a/mozilla/docshell/base/nsDocShell.cpp +++ b/mozilla/docshell/base/nsDocShell.cpp @@ -158,6 +158,7 @@ nsDocShell::nsDocShell(): mAllowJavascript(PR_TRUE), mAllowMetaRedirects(PR_TRUE), mAllowSubframes(PR_TRUE), + mAllowImages(PR_TRUE), mAppType(nsIDocShell::APP_TYPE_UNKNOWN), mBusyFlags(BUSY_FLAGS_NONE), mEODForCurrentDocument(PR_FALSE), @@ -1154,6 +1155,20 @@ NS_IMETHODIMP nsDocShell::SetAllowSubframes(PRBool aAllowSubframes) return NS_OK; } +NS_IMETHODIMP nsDocShell::GetAllowImages(PRBool * aAllowImages) +{ + NS_ENSURE_ARG_POINTER(aAllowImages); + + *aAllowImages = mAllowImages; + return NS_OK; +} + +NS_IMETHODIMP nsDocShell::SetAllowImages(PRBool aAllowImages) +{ + mAllowImages = aAllowImages; + return NS_OK; +} + NS_IMETHODIMP nsDocShell::GetDocShellEnumerator(PRInt32 aItemType, PRInt32 aDirection, nsISimpleEnumerator **outEnum) { diff --git a/mozilla/docshell/base/nsDocShell.h b/mozilla/docshell/base/nsDocShell.h index 27b29d572c7..90931d67332 100644 --- a/mozilla/docshell/base/nsDocShell.h +++ b/mozilla/docshell/base/nsDocShell.h @@ -302,6 +302,7 @@ protected: PRBool mAllowJavascript; PRBool mAllowMetaRedirects; PRBool mAllowSubframes; + PRBool mAllowImages; PRUint32 mAppType; PRInt32 mChildOffset; // Offset in the parent's child list. PRUint32 mBusyFlags; diff --git a/mozilla/docshell/base/nsIDocShell.idl b/mozilla/docshell/base/nsIDocShell.idl index 61044278224..fbbc448a482 100644 --- a/mozilla/docshell/base/nsIDocShell.idl +++ b/mozilla/docshell/base/nsIDocShell.idl @@ -189,6 +189,11 @@ interface nsIDocShell : nsISupports */ attribute boolean allowSubframes; + /** + * Attribute stating whether or not images should be loaded. + */ + attribute boolean allowImages; + /** * Get an enumerator over this docShell and its children. * diff --git a/mozilla/embedding/browser/build/nsWebBrowserModule.cpp b/mozilla/embedding/browser/build/nsWebBrowserModule.cpp index 86c269f02ee..daf60811236 100644 --- a/mozilla/embedding/browser/build/nsWebBrowserModule.cpp +++ b/mozilla/embedding/browser/build/nsWebBrowserModule.cpp @@ -22,26 +22,64 @@ #include "nsIModule.h" #include "nsIGenericFactory.h" +#include "nsICategoryManager.h" +#include "nsXPIDLString.h" #include "nsWebBrowser.h" #include "nsCommandHandler.h" - +#include "nsWebBrowserContentPolicy.h" // Factory Constructors NS_GENERIC_FACTORY_CONSTRUCTOR(nsWebBrowser) -//NS_GENERIC_FACTORY_CONSTRUCTOR(nsWebBrowserSetup) +NS_GENERIC_FACTORY_CONSTRUCTOR(nsWebBrowserContentPolicy) NS_GENERIC_FACTORY_CONSTRUCTOR(nsCommandHandler) +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_WEBBROWSERCONTENTPOLICY_CONTRACTID, + NS_WEBBROWSERCONTENTPOLICY_CONTRACTID, + PR_TRUE, PR_TRUE, getter_Copies(previous)); +} + +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; + + nsXPIDLCString previous; + return catman->DeleteCategoryEntry("content-policy", + NS_WEBBROWSERCONTENTPOLICY_CONTRACTID, + PR_TRUE, getter_Copies(previous)); +} // Component Table static nsModuleComponentInfo components[] = { { "WebBrowser Component", NS_WEBBROWSER_CID, - NS_WEBBROWSER_CONTRACTID, nsWebBrowserConstructor }, + NS_WEBBROWSER_CONTRACTID, nsWebBrowserConstructor }, { "CommandHandler Component", NS_COMMANDHANDLER_CID, - NS_COMMANDHANDLER_CONTRACTID, nsCommandHandlerConstructor } + NS_COMMANDHANDLER_CONTRACTID, nsCommandHandlerConstructor }, + { "nsIWebBrowserSetup content policy enforcer", + NS_WEBBROWSERCONTENTPOLICY_CID, + NS_WEBBROWSERCONTENTPOLICY_CONTRACTID, + nsWebBrowserContentPolicyConstructor, + RegisterContentPolicy, UnregisterContentPolicy } // { "WebBrowserSetup Component", NS_WEBBROWSER_SETUP_CID, // NS_WEBBROWSER_SETUP_CONTRACTID, nsWebBrowserSetupConstructor } }; diff --git a/mozilla/embedding/browser/webBrowser/Makefile.in b/mozilla/embedding/browser/webBrowser/Makefile.in index d9114df96c1..4074e74c878 100644 --- a/mozilla/embedding/browser/webBrowser/Makefile.in +++ b/mozilla/embedding/browser/webBrowser/Makefile.in @@ -26,7 +26,7 @@ VPATH = @srcdir@ MODULE = webbrwsr XPIDL_MODULE = webBrowser_core LIBRARY_NAME = nsWebBrowser_s -REQUIRES = xpcom string docshell widget layout dom js locale necko uriloader shistory webshell mimetype exthandler timer windowwatcher txtsvc gfx2 wallet appcomps +REQUIRES = xpcom string docshell widget layout dom js locale necko uriloader shistory webshell mimetype exthandler timer windowwatcher txtsvc gfx2 wallet appcomps content include $(DEPTH)/config/autoconf.mk @@ -51,7 +51,8 @@ CPPSRCS = \ nsCommandHandler.cpp \ nsWebBrowserPersist.cpp \ nsDOMWalker.cpp \ - nsNonPersistAuthPrompt.cpp \ + nsNonPersistAuthPrompt.cpp \ + nsWebBrowserContentPolicy.cpp \ $(NULL) # we don't want the shared lib, but we want to force the creation of a diff --git a/mozilla/embedding/browser/webBrowser/makefile.win b/mozilla/embedding/browser/webBrowser/makefile.win index f413349b1e8..31dddf32bfa 100644 --- a/mozilla/embedding/browser/webBrowser/makefile.win +++ b/mozilla/embedding/browser/webBrowser/makefile.win @@ -46,6 +46,7 @@ CPP_OBJS= \ .\$(OBJDIR)\nsWebBrowserPersist.obj \ .\$(OBJDIR)\nsDOMWalker.obj \ .\$(OBJDIR)\nsNonPersistAuthPrompt.obj \ + .\$(OBJDIR)\nsWebBrowserContentPolicy.obj \ $(NULL) include <$(DEPTH)\config\rules.mak> diff --git a/mozilla/embedding/browser/webBrowser/nsIWebBrowserSetup.idl b/mozilla/embedding/browser/webBrowser/nsIWebBrowserSetup.idl index f9921b97e7d..4d86c63528f 100644 --- a/mozilla/embedding/browser/webBrowser/nsIWebBrowserSetup.idl +++ b/mozilla/embedding/browser/webBrowser/nsIWebBrowserSetup.idl @@ -32,12 +32,23 @@ [scriptable, uuid(F15398A0-8018-11d3-AF70-00A024FFC08C)] interface nsIWebBrowserSetup : nsISupports { - const unsigned long SETUP_ALLOW_PLUGINS = 1; - const unsigned long SETUP_ALLOW_JAVASCRIPT = 2; - const unsigned long SETUP_ALLOW_META_REDIRECTS = 3; - const unsigned long SETUP_ALLOW_SUBFRAMES = 4; - - const unsigned long SETUP_USE_GLOBAL_HISTORY = 256; + const unsigned long SETUP_ALLOW_PLUGINS = 1; + const unsigned long SETUP_ALLOW_JAVASCRIPT = 2; + const unsigned long SETUP_ALLOW_META_REDIRECTS = 3; + const unsigned long SETUP_ALLOW_SUBFRAMES = 4; + + /** + * SETUP_ALLOW_IMAGES enables/disables image loading for a given webBrowser + * window. If you disable the images, load a page, then enable the images, + * the page will *not* automatically load the images for the previously + * loaded page. This flag controls the state of a webBrowser at load time + * and does not automatcially re-load a page when the state is toggled. + * Reloading must be done by hand, or by walking through the DOM tree and + * re-setting the src attributes. + */ + const unsigned long SETUP_ALLOW_IMAGES = 5; + const unsigned long SETUP_USE_GLOBAL_HISTORY = 256; void setProperty(in unsigned long aId, in unsigned long aValue); -}; \ No newline at end of file +}; + diff --git a/mozilla/embedding/browser/webBrowser/nsWebBrowser.cpp b/mozilla/embedding/browser/webBrowser/nsWebBrowser.cpp index 8ce1f5532e4..75f45a063c3 100644 --- a/mozilla/embedding/browser/webBrowser/nsWebBrowser.cpp +++ b/mozilla/embedding/browser/webBrowser/nsWebBrowser.cpp @@ -656,6 +656,12 @@ NS_IMETHODIMP nsWebBrowser::SetProperty(PRUint32 aId, PRUint32 aValue) mDocShell->SetAllowSubframes(aValue); } break; + case nsIWebBrowserSetup::SETUP_ALLOW_IMAGES: + { + NS_ENSURE_STATE(mDocShell); + NS_ENSURE_TRUE((aValue == PR_TRUE || aValue == PR_FALSE), NS_ERROR_INVALID_ARG); + mDocShell->SetAllowImages(aValue); + } case nsIWebBrowserSetup::SETUP_USE_GLOBAL_HISTORY: { NS_ENSURE_STATE(mDocShell); diff --git a/mozilla/embedding/browser/webBrowser/nsWebBrowserContentPolicy.cpp b/mozilla/embedding/browser/webBrowser/nsWebBrowserContentPolicy.cpp new file mode 100644 index 00000000000..10d65a9de1d --- /dev/null +++ b/mozilla/embedding/browser/webBrowser/nsWebBrowserContentPolicy.cpp @@ -0,0 +1,88 @@ +/* -*- 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 embedding content-control code. + * + * The Initial Developer of the Original Code is Mike Shaver. + * Portions created by Mike Shaver are Copyright (C) 2001 Mike Shaver. + * All Rights Reserved. + * + */ + +#include "nsWebBrowserContentPolicy.h" +#include "nsIScriptGlobalObject.h" +#include "nsIDocShell.h" +#include "nsCOMPtr.h" +#include "nsIDOMWindow.h" + +nsWebBrowserContentPolicy::nsWebBrowserContentPolicy() +{ + MOZ_COUNT_CTOR(nsWebBrowserContentPolicy); + NS_INIT_REFCNT(); +} + +nsWebBrowserContentPolicy::~nsWebBrowserContentPolicy() +{ + MOZ_COUNT_DTOR(nsWebBrowserContentPolicy); +} + +NS_IMPL_ISUPPORTS1(nsWebBrowserContentPolicy, nsIContentPolicy) + +NS_IMETHODIMP +nsWebBrowserContentPolicy::ShouldLoad(PRInt32 contentType, + nsIURI *contentLocation, + nsISupports *context, + nsIDOMWindow *window, PRBool *shouldLoad) +{ + *shouldLoad = PR_TRUE; + + nsCOMPtr scriptGlobal = + do_QueryInterface(window); + if (!scriptGlobal) + return NS_OK; + + nsCOMPtr shell; + if (NS_FAILED(scriptGlobal->GetDocShell(getter_AddRefs(shell)))) + return NS_OK; + + switch (contentType) { + case nsIContentPolicy::OBJECT: + return shell->GetAllowPlugins(shouldLoad); + case nsIContentPolicy::SCRIPT: + return shell->GetAllowJavascript(shouldLoad); + case nsIContentPolicy::SUBDOCUMENT: + return shell->GetAllowSubframes(shouldLoad); +#if 0 + /* need to actually check that it's a meta tag, maybe */ + case nsIContentPolicy::CONTROL_TAG: + return shell->GetAllowMetaRedirects(shouldLoad); /* meta _refresh_ */ +#endif + case nsIContentPolicy::IMAGE: + return shell->GetAllowImages(shouldLoad); + default: + return NS_OK; + } + +} + +NS_IMETHODIMP +nsWebBrowserContentPolicy::ShouldProcess(PRInt32 contentType, + nsIURI *contentLocation, + nsISupports *context, + nsIDOMWindow *window, + PRBool *shouldProcess) +{ + /* XXX use this for AllowJavascript (sic) control? */ + *shouldProcess = PR_TRUE; + return NS_OK; +} + diff --git a/mozilla/embedding/browser/webBrowser/nsWebBrowserContentPolicy.h b/mozilla/embedding/browser/webBrowser/nsWebBrowserContentPolicy.h new file mode 100644 index 00000000000..a34dcd4968e --- /dev/null +++ b/mozilla/embedding/browser/webBrowser/nsWebBrowserContentPolicy.h @@ -0,0 +1,38 @@ +/* -*- 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 embedding content-control code. + * + * The Initial Developer of the Original Code is Mike Shaver. + * Portions created by Mike Shaver are Copyright (C) 2001 Mike Shaver. + * All Rights Reserved. + * + */ + +#include "nsIContentPolicy.h" + +/* f66bc334-1dd1-11b2-bab2-90e04fe15c19 */ +#define NS_WEBBROWSERCONTENTPOLICY_CID \ +{ 0xf66bc334, 0x1dd1, 0x11b2, { 0xba, 0xb2, 0x90, 0xe0, 0x4f, 0xe1, 0x5c, 0x19 } } + +#define NS_WEBBROWSERCONTENTPOLICY_CONTRACTID "@mozilla.org/embedding/browser/content-policy;1" + +class nsWebBrowserContentPolicy : public nsIContentPolicy +{ +public: + nsWebBrowserContentPolicy(); + virtual ~nsWebBrowserContentPolicy(); + + NS_DECL_ISUPPORTS + NS_DECL_NSICONTENTPOLICY +}; +