From a1d957f419b4c5f60032030d41196b711a7cf5d9 Mon Sep 17 00:00:00 2001 From: "scullin%netscape.com" Date: Wed, 22 Jul 1998 00:26:23 +0000 Subject: [PATCH] Propagate alerts, confirms and prompts up to browser window. --- uriloader/base/nsDocLoader.cpp | 108 +++++++++++++++++++++- webshell/src/nsDocLoader.cpp | 108 +++++++++++++++++++++- webshell/tests/viewer/nsBrowserWindow.cpp | 72 +++++++++++++++ webshell/tests/viewer/nsBrowserWindow.h | 14 +++ 4 files changed, 292 insertions(+), 10 deletions(-) diff --git a/uriloader/base/nsDocLoader.cpp b/uriloader/base/nsDocLoader.cpp index bf2d7bdc80c8..fe0cdf0d859a 100644 --- a/uriloader/base/nsDocLoader.cpp +++ b/uriloader/base/nsDocLoader.cpp @@ -16,12 +16,14 @@ * Corporation. Portions created by Netscape are Copyright (C) 1998 * Netscape Communications Corporation. All Rights Reserved. */ +#define NS_IMPL_IDS #include "prmem.h" #include "plstr.h" #include "nsString.h" #include "nsISupportsArray.h" #include "nsIURL.h" #include "nsIStreamListener.h" +#include "nsINetSupport.h" #include "nsIPostToServer.h" #include "nsIFactory.h" #include "nsIDocumentLoader.h" @@ -44,7 +46,7 @@ class nsDocLoaderImpl; * The Document Loader maintains a list of nsDocumentBindInfo instances which * represents the set of documents actively being loaded... */ -class nsDocumentBindInfo : public nsIStreamListener +class nsDocumentBindInfo : public nsIStreamListener, public nsINetSupport { public: nsDocumentBindInfo(nsDocLoaderImpl* aDocLoader, @@ -65,6 +67,18 @@ public: NS_IMETHOD OnDataAvailable(nsIURL* aURL, nsIInputStream *aStream, PRInt32 aLength); NS_IMETHOD OnStopBinding(nsIURL* aURL, PRInt32 aStatus, const nsString& aMsg); + /* nsINetSupport interface methods */ + NS_IMETHOD_(void) Alert(const nsString &aText); + NS_IMETHOD_(PRBool) Confirm(const nsString &aText); + NS_IMETHOD_(PRBool) Prompt(const nsString &aText, + const nsString &aDefault, + nsString &aResult); + NS_IMETHOD_(PRBool) PromptUserAndPassword(const nsString &aText, + nsString &aUser, + nsString &aPassword); + NS_IMETHOD_(PRBool) PromptPassword(const nsString &aText, + nsString &aPassword); + protected: virtual ~nsDocumentBindInfo(); @@ -74,6 +88,7 @@ protected: nsIContentViewerContainer* m_Container; nsISupports* m_ExtraInfo; nsIStreamObserver* m_Observer; + nsINetSupport* m_NetSupport; nsIStreamListener* m_NextStream; nsDocLoaderImpl* m_DocLoader; }; @@ -312,7 +327,7 @@ nsDocLoaderImpl::LoadURL(const nsString& aURLSpec, goto done; } /* The DocumentBindInfo reference is only held by the Array... */ - m_LoadingDocsList->AppendElement(loader); + m_LoadingDocsList->AppendElement((nsIStreamListener *)loader); rv = loader->Bind(aURLSpec, aPostData); @@ -354,6 +369,10 @@ nsDocumentBindInfo::nsDocumentBindInfo(nsDocLoaderImpl* aDocLoader, m_Observer = anObserver; NS_IF_ADDREF(m_Observer); + m_NetSupport = NULL; + if (m_Observer) { + m_Observer->QueryInterface(kINetSupportIID, (void **) &m_NetSupport); + } m_ExtraInfo = aExtraInfo; NS_IF_ADDREF(m_ExtraInfo); @@ -371,6 +390,7 @@ nsDocumentBindInfo::~nsDocumentBindInfo() NS_IF_RELEASE(m_NextStream); NS_IF_RELEASE(m_Container); NS_IF_RELEASE(m_Observer); + NS_IF_RELEASE(m_NetSupport); NS_IF_RELEASE(m_ExtraInfo); } @@ -378,9 +398,39 @@ nsDocumentBindInfo::~nsDocumentBindInfo() /* * Implementation of ISupports methods... */ -NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID); -NS_IMPL_ISUPPORTS(nsDocumentBindInfo,kIStreamListenerIID); +NS_DEFINE_IID(kIStreamObserverIID, NS_ISTREAMOBSERVER_IID); +NS_IMPL_ADDREF(nsDocumentBindInfo); +NS_IMPL_RELEASE(nsDocumentBindInfo); + +nsresult +nsDocumentBindInfo::QueryInterface(const nsIID& aIID, + void** aInstancePtrResult) +{ + NS_PRECONDITION(nsnull != aInstancePtrResult, "null pointer"); + if (nsnull == aInstancePtrResult) { + return NS_ERROR_NULL_POINTER; + } + + *aInstancePtrResult = NULL; + + if (aIID.Equals(kIStreamObserverIID)) { + *aInstancePtrResult = (void*) ((nsIStreamObserver*)this); + AddRef(); + return NS_OK; + } + if (aIID.Equals(kIStreamListenerIID)) { + *aInstancePtrResult = (void*) ((nsIStreamListener*)this); + AddRef(); + return NS_OK; + } + if (aIID.Equals(kINetSupportIID)) { + *aInstancePtrResult = (void*) ((nsINetSupport*)this); + AddRef(); + return NS_OK; + } + return NS_NOINTERFACE; +} nsresult nsDocumentBindInfo::Bind(const nsString& aURLSpec, nsIPostData* aPostData) @@ -545,11 +595,59 @@ NS_METHOD nsDocumentBindInfo::OnStopBinding(nsIURL* aURL, PRInt32 aStatus, * This should cause the nsDocumentBindInfo instance to be deleted, so * DO NOT assume this is valid after the call!! */ - m_DocLoader->LoadURLComplete(this); + m_DocLoader->LoadURLComplete((nsIStreamListener *)this); return rv; } +NS_IMETHODIMP_(void) +nsDocumentBindInfo::Alert(const nsString &aText) +{ + if (nsnull != m_NetSupport) { + m_NetSupport->Alert(aText); + } +} + +NS_IMETHODIMP_(PRBool) +nsDocumentBindInfo::Confirm(const nsString &aText) +{ + if (nsnull != m_NetSupport) { + return m_NetSupport->Confirm(aText); + } + return PR_FALSE; +} + +NS_IMETHODIMP_(PRBool) +nsDocumentBindInfo::Prompt(const nsString &aText, + const nsString &aDefault, + nsString &aResult) +{ + if (nsnull != m_NetSupport) { + return m_NetSupport->Prompt(aText, aDefault, aResult); + } + return PR_FALSE; +} + +NS_IMETHODIMP_(PRBool) +nsDocumentBindInfo::PromptUserAndPassword(const nsString &aText, + nsString &aUser, + nsString &aPassword) +{ + if (nsnull != m_NetSupport) { + return m_NetSupport->PromptUserAndPassword(aText, aUser, aPassword); + } + return PR_FALSE; +} + +NS_IMETHODIMP_(PRBool) +nsDocumentBindInfo::PromptPassword(const nsString &aText, + nsString &aPassword) +{ + if (nsnull != m_NetSupport) { + return m_NetSupport->PromptPassword(aText, aPassword); + } + return PR_FALSE; +} /******************************************* diff --git a/webshell/src/nsDocLoader.cpp b/webshell/src/nsDocLoader.cpp index bf2d7bdc80c8..fe0cdf0d859a 100644 --- a/webshell/src/nsDocLoader.cpp +++ b/webshell/src/nsDocLoader.cpp @@ -16,12 +16,14 @@ * Corporation. Portions created by Netscape are Copyright (C) 1998 * Netscape Communications Corporation. All Rights Reserved. */ +#define NS_IMPL_IDS #include "prmem.h" #include "plstr.h" #include "nsString.h" #include "nsISupportsArray.h" #include "nsIURL.h" #include "nsIStreamListener.h" +#include "nsINetSupport.h" #include "nsIPostToServer.h" #include "nsIFactory.h" #include "nsIDocumentLoader.h" @@ -44,7 +46,7 @@ class nsDocLoaderImpl; * The Document Loader maintains a list of nsDocumentBindInfo instances which * represents the set of documents actively being loaded... */ -class nsDocumentBindInfo : public nsIStreamListener +class nsDocumentBindInfo : public nsIStreamListener, public nsINetSupport { public: nsDocumentBindInfo(nsDocLoaderImpl* aDocLoader, @@ -65,6 +67,18 @@ public: NS_IMETHOD OnDataAvailable(nsIURL* aURL, nsIInputStream *aStream, PRInt32 aLength); NS_IMETHOD OnStopBinding(nsIURL* aURL, PRInt32 aStatus, const nsString& aMsg); + /* nsINetSupport interface methods */ + NS_IMETHOD_(void) Alert(const nsString &aText); + NS_IMETHOD_(PRBool) Confirm(const nsString &aText); + NS_IMETHOD_(PRBool) Prompt(const nsString &aText, + const nsString &aDefault, + nsString &aResult); + NS_IMETHOD_(PRBool) PromptUserAndPassword(const nsString &aText, + nsString &aUser, + nsString &aPassword); + NS_IMETHOD_(PRBool) PromptPassword(const nsString &aText, + nsString &aPassword); + protected: virtual ~nsDocumentBindInfo(); @@ -74,6 +88,7 @@ protected: nsIContentViewerContainer* m_Container; nsISupports* m_ExtraInfo; nsIStreamObserver* m_Observer; + nsINetSupport* m_NetSupport; nsIStreamListener* m_NextStream; nsDocLoaderImpl* m_DocLoader; }; @@ -312,7 +327,7 @@ nsDocLoaderImpl::LoadURL(const nsString& aURLSpec, goto done; } /* The DocumentBindInfo reference is only held by the Array... */ - m_LoadingDocsList->AppendElement(loader); + m_LoadingDocsList->AppendElement((nsIStreamListener *)loader); rv = loader->Bind(aURLSpec, aPostData); @@ -354,6 +369,10 @@ nsDocumentBindInfo::nsDocumentBindInfo(nsDocLoaderImpl* aDocLoader, m_Observer = anObserver; NS_IF_ADDREF(m_Observer); + m_NetSupport = NULL; + if (m_Observer) { + m_Observer->QueryInterface(kINetSupportIID, (void **) &m_NetSupport); + } m_ExtraInfo = aExtraInfo; NS_IF_ADDREF(m_ExtraInfo); @@ -371,6 +390,7 @@ nsDocumentBindInfo::~nsDocumentBindInfo() NS_IF_RELEASE(m_NextStream); NS_IF_RELEASE(m_Container); NS_IF_RELEASE(m_Observer); + NS_IF_RELEASE(m_NetSupport); NS_IF_RELEASE(m_ExtraInfo); } @@ -378,9 +398,39 @@ nsDocumentBindInfo::~nsDocumentBindInfo() /* * Implementation of ISupports methods... */ -NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID); -NS_IMPL_ISUPPORTS(nsDocumentBindInfo,kIStreamListenerIID); +NS_DEFINE_IID(kIStreamObserverIID, NS_ISTREAMOBSERVER_IID); +NS_IMPL_ADDREF(nsDocumentBindInfo); +NS_IMPL_RELEASE(nsDocumentBindInfo); + +nsresult +nsDocumentBindInfo::QueryInterface(const nsIID& aIID, + void** aInstancePtrResult) +{ + NS_PRECONDITION(nsnull != aInstancePtrResult, "null pointer"); + if (nsnull == aInstancePtrResult) { + return NS_ERROR_NULL_POINTER; + } + + *aInstancePtrResult = NULL; + + if (aIID.Equals(kIStreamObserverIID)) { + *aInstancePtrResult = (void*) ((nsIStreamObserver*)this); + AddRef(); + return NS_OK; + } + if (aIID.Equals(kIStreamListenerIID)) { + *aInstancePtrResult = (void*) ((nsIStreamListener*)this); + AddRef(); + return NS_OK; + } + if (aIID.Equals(kINetSupportIID)) { + *aInstancePtrResult = (void*) ((nsINetSupport*)this); + AddRef(); + return NS_OK; + } + return NS_NOINTERFACE; +} nsresult nsDocumentBindInfo::Bind(const nsString& aURLSpec, nsIPostData* aPostData) @@ -545,11 +595,59 @@ NS_METHOD nsDocumentBindInfo::OnStopBinding(nsIURL* aURL, PRInt32 aStatus, * This should cause the nsDocumentBindInfo instance to be deleted, so * DO NOT assume this is valid after the call!! */ - m_DocLoader->LoadURLComplete(this); + m_DocLoader->LoadURLComplete((nsIStreamListener *)this); return rv; } +NS_IMETHODIMP_(void) +nsDocumentBindInfo::Alert(const nsString &aText) +{ + if (nsnull != m_NetSupport) { + m_NetSupport->Alert(aText); + } +} + +NS_IMETHODIMP_(PRBool) +nsDocumentBindInfo::Confirm(const nsString &aText) +{ + if (nsnull != m_NetSupport) { + return m_NetSupport->Confirm(aText); + } + return PR_FALSE; +} + +NS_IMETHODIMP_(PRBool) +nsDocumentBindInfo::Prompt(const nsString &aText, + const nsString &aDefault, + nsString &aResult) +{ + if (nsnull != m_NetSupport) { + return m_NetSupport->Prompt(aText, aDefault, aResult); + } + return PR_FALSE; +} + +NS_IMETHODIMP_(PRBool) +nsDocumentBindInfo::PromptUserAndPassword(const nsString &aText, + nsString &aUser, + nsString &aPassword) +{ + if (nsnull != m_NetSupport) { + return m_NetSupport->PromptUserAndPassword(aText, aUser, aPassword); + } + return PR_FALSE; +} + +NS_IMETHODIMP_(PRBool) +nsDocumentBindInfo::PromptPassword(const nsString &aText, + nsString &aPassword) +{ + if (nsnull != m_NetSupport) { + return m_NetSupport->PromptPassword(aText, aPassword); + } + return PR_FALSE; +} /******************************************* diff --git a/webshell/tests/viewer/nsBrowserWindow.cpp b/webshell/tests/viewer/nsBrowserWindow.cpp index 52e3c594a2ec..aa23a40f5d82 100644 --- a/webshell/tests/viewer/nsBrowserWindow.cpp +++ b/webshell/tests/viewer/nsBrowserWindow.cpp @@ -16,6 +16,7 @@ * Corporation. Portions created by Netscape are Copyright (C) 1998 * Netscape Communications Corporation. All Rights Reserved. */ +#define NS_IMPL_IDS #include "nsBrowserWindow.h" #include "nsIStreamListener.h" #include "nsIAppShell.h" @@ -367,6 +368,9 @@ nsBrowserWindow::QueryInterface(const nsIID& aIID, if (nsnull == aInstancePtrResult) { return NS_ERROR_NULL_POINTER; } + + *aInstancePtrResult = NULL; + if (aIID.Equals(kIBrowserWindowIID)) { *aInstancePtrResult = (void*) ((nsIBrowserWindow*)this); AddRef(); @@ -382,6 +386,11 @@ nsBrowserWindow::QueryInterface(const nsIID& aIID, AddRef(); return NS_OK; } + if (aIID.Equals(kINetSupportIID)) { + *aInstancePtrResult = (void*) ((nsINetSupport*)this); + AddRef(); + return NS_OK; + } if (aIID.Equals(kISupportsIID)) { *aInstancePtrResult = (void*) ((nsISupports*)((nsIBrowserWindow*)this)); AddRef(); @@ -752,6 +761,69 @@ nsBrowserWindow::OnStopBinding(nsIURL* aURL, return NS_OK; } +NS_IMETHODIMP_(void) +nsBrowserWindow::Alert(const nsString &aText) +{ + nsAutoString str(aText); + printf("Browser Window Alert: %s\n", str); +} + +NS_IMETHODIMP_(PRBool) +nsBrowserWindow::Confirm(const nsString &aText) +{ + nsAutoString str(aText); + printf("Browser Window Confirm: %s (returning false)\n", str); + + return PR_FALSE; +} + +NS_IMETHODIMP_(PRBool) +nsBrowserWindow::Prompt(const nsString &aText, + const nsString &aDefault, + nsString &aResult) +{ + nsAutoString str(aText); + char buf[256]; + printf("Browser Window: %s\n", str); + printf("Prompt: "); + scanf("%s", buf); + aResult = buf; + + return (aResult.Length() > 0); +} + +NS_IMETHODIMP_(PRBool) +nsBrowserWindow::PromptUserAndPassword(const nsString &aText, + nsString &aUser, + nsString &aPassword) +{ + nsAutoString str(aText); + char buf[256]; + printf("Browser Window: %s\n", str); + printf("User: "); + scanf("%s", buf); + aUser = buf; + printf("Password: "); + scanf("%s", buf); + aPassword = buf; + + return (aUser.Length() > 0); +} + +NS_IMETHODIMP_(PRBool) +nsBrowserWindow::PromptPassword(const nsString &aText, + nsString &aPassword) +{ + nsAutoString str(aText); + char buf[256]; + printf("Browser Window: %s\n", str); + printf("Password: "); + scanf("%s", buf); + aPassword = buf; + + return PR_TRUE; +} + //---------------------------------------- // Toolbar support diff --git a/webshell/tests/viewer/nsBrowserWindow.h b/webshell/tests/viewer/nsBrowserWindow.h index 6533c12e4212..e14eafcfa019 100644 --- a/webshell/tests/viewer/nsBrowserWindow.h +++ b/webshell/tests/viewer/nsBrowserWindow.h @@ -21,6 +21,7 @@ #include "nsIBrowserWindow.h" #include "nsIStreamListener.h" +#include "nsINetSupport.h" #include "nsIWebShell.h" #include "nsIScriptContextOwner.h" #include "nsString.h" @@ -43,6 +44,7 @@ class nsIPresShell; */ class nsBrowserWindow : public nsIBrowserWindow, public nsIStreamObserver, + public nsINetSupport, public nsIWebShellContainer { public: @@ -82,6 +84,18 @@ public: NS_IMETHOD BeginLoadURL(nsIWebShell* aShell, const nsString& aURL); NS_IMETHOD EndLoadURL(nsIWebShell* aShell, const nsString& aURL); + // nsINetSupport + NS_IMETHOD_(void) Alert(const nsString &aText); + NS_IMETHOD_(PRBool) Confirm(const nsString &aText); + NS_IMETHOD_(PRBool) Prompt(const nsString &aText, + const nsString &aDefault, + nsString &aResult); + NS_IMETHOD_(PRBool) PromptUserAndPassword(const nsString &aText, + nsString &aUser, + nsString &aPassword); + NS_IMETHOD_(PRBool) PromptPassword(const nsString &aText, + nsString &aPassword); + // nsBrowserWindow virtual nsresult CreateMenuBar(PRInt32 aWidth) = 0; virtual nsresult CreateToolBar(PRInt32 aWidth);