From e3e4d2ffbb709ac7dff9c6de34bb8dc3137f268b Mon Sep 17 00:00:00 2001 From: vidur Date: Thu, 25 Jun 1998 22:25:39 +0000 Subject: [PATCH] Added the nsIScriptContextOwner.h to aid in SCRIPT tag evaluation git-svn-id: svn://10.0.0.236/trunk@4536 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/dom/public/MANIFEST | 6 ++- mozilla/dom/public/Makefile | 1 + mozilla/dom/public/makefile.win | 2 +- mozilla/dom/public/nsIScriptContextOwner.h | 56 ++++++++++++++++++++++ mozilla/webshell/public/nsIWebWidget.h | 5 -- mozilla/webshell/src/nsWebWidget.cpp | 30 ++++++++---- mozilla/webshell/tests/viewer/winmain.cpp | 29 ++++++----- 7 files changed, 100 insertions(+), 29 deletions(-) create mode 100644 mozilla/dom/public/nsIScriptContextOwner.h diff --git a/mozilla/dom/public/MANIFEST b/mozilla/dom/public/MANIFEST index 9e2aa1c823c..0ec8192ca2a 100644 --- a/mozilla/dom/public/MANIFEST +++ b/mozilla/dom/public/MANIFEST @@ -17,7 +17,9 @@ # nsIScriptContext.h -nsIJSScriptObject.h +nsIScriptObject.h nsIScriptObjectOwner.h +nsIScriptContextOwner.h +nsIJSScriptObject.h nsIScriptGlobalObject.h -nsIDOMWindow.h +nsIDOMWindow.h \ No newline at end of file diff --git a/mozilla/dom/public/Makefile b/mozilla/dom/public/Makefile index 20e5ad6e735..29dfe3f7a2c 100644 --- a/mozilla/dom/public/Makefile +++ b/mozilla/dom/public/Makefile @@ -23,6 +23,7 @@ DEFINES = -D_IMPL_NS_DOM EXPORTS = \ nsIScriptContext.h \ + nsIScriptContextOwner.h \ nsIJSScriptObject.h \ nsIScriptObjectOwner.h \ nsIScriptGlobalObject.h \ diff --git a/mozilla/dom/public/makefile.win b/mozilla/dom/public/makefile.win index c548e2aa3c2..c706148a92f 100644 --- a/mozilla/dom/public/makefile.win +++ b/mozilla/dom/public/makefile.win @@ -21,7 +21,7 @@ IGNORE_MANIFEST=1 DIRS=coreDom coreEvents events DEFINES=-D_IMPL_NS_DOM EXPORTS=nsIScriptContext.h nsIJSScriptObject.h nsIScriptObjectOwner.h \ - nsIScriptGlobalObject.h nsIDOMWindow.h + nsIScriptGlobalObject.h nsIDOMWindow.h nsIScriptContextOwner.h MODULE=dom diff --git a/mozilla/dom/public/nsIScriptContextOwner.h b/mozilla/dom/public/nsIScriptContextOwner.h new file mode 100644 index 00000000000..04420aaedc0 --- /dev/null +++ b/mozilla/dom/public/nsIScriptContextOwner.h @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +#ifndef nsIScriptContextOwner_h__ +#define nsIScriptContextOwner_h__ + +#include "nscore.h" +#include "nsISupports.h" +#include "nsIScriptContext.h" + +class nsIScriptContext; + +#define NS_ISCRIPTCONTEXTOWNER_IID \ +{ /* a94ec640-0bba-11d2-b326-00805f8a3859 */ \ + 0xa94ec640, 0x0bba, 0x11d2, \ + {0xb3, 0x26, 0x00, 0x80, 0x5f, 0x8a, 0x38, 0x59} } + +/** + * Implemented by any object capable of supplying a nsIScriptContext. + * The implentor may create the script context on demand and is + * allowed (though not expected) to throw it away on release. + */ + +class nsIScriptContextOwner : public nsISupports { +public: + /** + * Returns a script context. The assumption is that the + * script context has an associated script global object and + * is ready for script evaluation. + */ + NS_IMETHOD GetScriptContext(nsIScriptContext **aContext) = 0; + + /** + * Called to indicate that the script context is no longer needed. + * The caller should not also call the context's Release() + * method. + */ + NS_IMETHOD ReleaseScriptContext(nsIScriptContext *aContext) = 0; +}; + +#endif // nsIScriptContextOwner_h__ diff --git a/mozilla/webshell/public/nsIWebWidget.h b/mozilla/webshell/public/nsIWebWidget.h index 8d6db42436f..482f69e7026 100644 --- a/mozilla/webshell/public/nsIWebWidget.h +++ b/mozilla/webshell/public/nsIWebWidget.h @@ -25,7 +25,6 @@ class nsIDOMDocument; class nsILinkHandler; class nsIPresContext; class nsIStyleSet; -class nsIScriptContext; // IID for the nsWebWidget interface #define NS_IWEBWIDGET_IID \ @@ -96,12 +95,8 @@ public: virtual PRBool GetShowFrameBorders() = 0; - NS_IMETHOD GetScriptContext(nsIScriptContext **aContext) = 0; - NS_IMETHOD GetDOMDocument(nsIDOMDocument** aDocument) = 0; - NS_IMETHOD ReleaseScriptContext() = 0; - virtual nsIPresContext* GetPresContext() = 0; }; diff --git a/mozilla/webshell/src/nsWebWidget.cpp b/mozilla/webshell/src/nsWebWidget.cpp index 892254df551..1eb29010ce1 100644 --- a/mozilla/webshell/src/nsWebWidget.cpp +++ b/mozilla/webshell/src/nsWebWidget.cpp @@ -36,6 +36,7 @@ #include "nsWidgetsCID.h" #include "nsString.h" #include "nsIScriptContext.h" +#include "nsIScriptContextOwner.h" #include "nsIScriptObjectOwner.h" #include "nsIScriptGlobalObject.h" #include "nsICSSParser.h" @@ -55,7 +56,7 @@ ((nsWebWidget*) ((char*)this - nsWebWidget::GetOuterOffset())) // Machine independent implementation portion of the web widget -class WebWidgetImpl : public nsIWebWidget { +class WebWidgetImpl : public nsIWebWidget, public nsIScriptContextOwner { public: WebWidgetImpl(); ~WebWidgetImpl(); @@ -115,8 +116,8 @@ public: virtual PRBool GetShowFrameBorders(); virtual nsIWidget* GetWWWindow(); NS_IMETHOD GetScriptContext(nsIScriptContext **aContext); + NS_IMETHOD ReleaseScriptContext(nsIScriptContext *aContext); NS_IMETHOD GetDOMDocument(nsIDOMDocument** aDocument); - NS_IMETHOD ReleaseScriptContext(); private: nsresult ProvideDefaultHandlers(); @@ -149,6 +150,7 @@ private: //---------------------------------------------------------------------- static NS_DEFINE_IID(kIWebWidgetIID, NS_IWEBWIDGET_IID); +static NS_DEFINE_IID(kIScriptContextOwnerIID, NS_ISCRIPTCONTEXTOWNER_IID); static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); //nsIWebWidget* WebWidgetImpl::gRootWebWidget = nsnull; @@ -169,6 +171,11 @@ nsresult WebWidgetImpl::QueryInterface(REFNSIID aIID, void** aInstancePtr) AddRef(); return NS_OK; } + if (aIID.Equals(kIScriptContextOwnerIID)) { + *aInstancePtr = (void*)(nsIScriptContextOwner*)this; + AddRef(); + return NS_OK; + } if (aIID.Equals(kISupportsIID)) { *aInstancePtr = (void*)(nsISupports*)(nsIWebWidget*)this; AddRef(); @@ -681,6 +688,9 @@ WebWidgetImpl::LoadURL(const nsString& aURLSpec, } } + // Set the script object owner to ourselves + doc->SetScriptContextOwner(this); + // Now load the document mPresShell->EnterReflowLock(); doc->LoadURL(url, aListener, this, aPostData); @@ -1022,11 +1032,19 @@ nsresult WebWidgetImpl::GetScriptContext(nsIScriptContext **aContext) if (NS_OK == res) { *aContext = mScriptContext; + NS_ADDREF(mScriptContext); } return res; } +nsresult WebWidgetImpl::ReleaseScriptContext(nsIScriptContext *aContext) +{ + NS_IF_RELEASE(aContext); + + return NS_OK; +} + nsresult WebWidgetImpl::GetDOMDocument(nsIDOMDocument** aDocument) { nsresult res = NS_OK; @@ -1043,14 +1061,6 @@ nsresult WebWidgetImpl::GetDOMDocument(nsIDOMDocument** aDocument) return res; } -nsresult WebWidgetImpl::ReleaseScriptContext() -{ - NS_IF_RELEASE(mScriptContext); - mScriptContext = nsnull; - - return NS_OK; -} - /******************************************* /* nsWebWidgetFactory /*******************************************/ diff --git a/mozilla/webshell/tests/viewer/winmain.cpp b/mozilla/webshell/tests/viewer/winmain.cpp index f7f5fd40d28..7a187114162 100644 --- a/mozilla/webshell/tests/viewer/winmain.cpp +++ b/mozilla/webshell/tests/viewer/winmain.cpp @@ -31,12 +31,15 @@ #include "nsCRT.h" #include "prenv.h" #include "nsIScriptContext.h" +#include "nsIScriptContextOwner.h" // Debug Robot options static int gDebugRobotLoads = 5000; static char gVerifyDir[_MAX_PATH]; static BOOL gVisualDebug = TRUE; +static NS_DEFINE_IID(kIScriptContextOwnerIID, NS_ISCRIPTCONTEXTOWNER_IID); + // DebugRobot call extern "C" NS_EXPORT int DebugRobot( nsVoidArray * workList, nsIWebWidget * ww, int imax, char * verify_dir, void (*yieldProc)(const char *)); @@ -197,22 +200,26 @@ void nsWin32Viewer::ShowConsole(WindowData* aWinData) JSConsole::sAccelTable = LoadAccelerators(gInstance, MAKEINTRESOURCE(ACCELERATOR_TABLE)); } - + + nsIScriptContextOwner *owner = nsnull; nsIScriptContext *context = nsnull; - if (NS_OK == aWinData->ww->GetScriptContext(&context)) { + if (NS_OK == aWinData->ww->QueryInterface(kIScriptContextOwnerIID, (void **)&owner)) { + if (NS_OK == owner->GetScriptContext(&context)) { - // create the console - gConsole = JSConsole::CreateConsole(); - gConsole->SetContext(context); - // lifetime of the context is still unclear at this point. - // Anyway, as long as the web widget is alive the context is alive. - // Maybe the context shouldn't even be RefCounted - context->Release(); - gConsole->SetNotification(DestroyConsole); + // create the console + gConsole = JSConsole::CreateConsole(); + gConsole->SetContext(context); + // lifetime of the context is still unclear at this point. + // Anyway, as long as the web widget is alive the context is alive. + // Maybe the context shouldn't even be RefCounted + context->Release(); + gConsole->SetNotification(DestroyConsole); + } + + NS_RELEASE(owner); } else { MessageBox(hWnd, "Unable to load JavaScript", "Viewer Error", MB_ICONSTOP); - } } }