From 1173d808a5ee98aea0988e4837a5726353047e48 Mon Sep 17 00:00:00 2001 From: "bsmedberg%covad.net" Date: Sat, 29 May 2004 17:43:59 +0000 Subject: [PATCH] Parts of bug 205425 - turn the embedding widget into a GRE client - patch by marco@gnome.org r=me sr=blizzard git-svn-id: svn://10.0.0.236/trunk@157100 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/dom/public/base/nsPIDOMWindow.h | 2 - .../dom/src/base/nsDOMScriptObjectFactory.cpp | 1 + .../embedding/browser/webBrowser/Makefile.in | 2 + .../webBrowser/nsDocShellTreeOwner.cpp | 1 + .../browser/webBrowser/nsEmbedStream.cpp | 290 ++++++++++++++++++ .../browser/webBrowser/nsEmbedStream.h | 65 ++++ .../webBrowser/nsIWebBrowserStream.idl | 49 +++ .../browser/webBrowser/nsWebBrowser.cpp | 48 +++ .../browser/webBrowser/nsWebBrowser.h | 8 + 9 files changed, 464 insertions(+), 2 deletions(-) create mode 100644 mozilla/embedding/browser/webBrowser/nsEmbedStream.cpp create mode 100644 mozilla/embedding/browser/webBrowser/nsEmbedStream.h create mode 100644 mozilla/embedding/browser/webBrowser/nsIWebBrowserStream.idl diff --git a/mozilla/dom/public/base/nsPIDOMWindow.h b/mozilla/dom/public/base/nsPIDOMWindow.h index 60d55c0ca6c..666d89e7769 100644 --- a/mozilla/dom/public/base/nsPIDOMWindow.h +++ b/mozilla/dom/public/base/nsPIDOMWindow.h @@ -40,10 +40,8 @@ #define nsPIDOMWindow_h__ #include "nsISupports.h" -#include "nsString.h" #include "nsIDOMLocation.h" #include "nsIDOMXULCommandDispatcher.h" -#include "nsIDocument.h" #include "nsIDOMElement.h" #include "nsIDOMWindowInternal.h" #include "nsIChromeEventHandler.h" diff --git a/mozilla/dom/src/base/nsDOMScriptObjectFactory.cpp b/mozilla/dom/src/base/nsDOMScriptObjectFactory.cpp index e2025de8808..a7d763626c6 100644 --- a/mozilla/dom/src/base/nsDOMScriptObjectFactory.cpp +++ b/mozilla/dom/src/base/nsDOMScriptObjectFactory.cpp @@ -58,6 +58,7 @@ #include "nsGlobalWindow.h" #include "nsIJSContextStack.h" #include "nsDOMException.h" +#include "nsCRT.h" #ifdef MOZ_XUL #include "nsIXULPrototypeCache.h" #endif diff --git a/mozilla/embedding/browser/webBrowser/Makefile.in b/mozilla/embedding/browser/webBrowser/Makefile.in index 2836c26b2a2..36a939cdac9 100644 --- a/mozilla/embedding/browser/webBrowser/Makefile.in +++ b/mozilla/embedding/browser/webBrowser/Makefile.in @@ -91,6 +91,7 @@ SDK_XPIDLSRCS = \ $(NULL) XPIDLSRCS = \ + nsIWebBrowserStream.idl \ nsCWebBrowser.idl \ nsICommandHandler.idl \ nsIEmbeddingSiteWindow2.idl \ @@ -105,6 +106,7 @@ CPPSRCS = \ nsCommandHandler.cpp \ nsWebBrowserContentPolicy.cpp \ nsContextMenuInfo.cpp \ + nsEmbedStream.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/nsDocShellTreeOwner.cpp b/mozilla/embedding/browser/webBrowser/nsDocShellTreeOwner.cpp index bef4db23706..a79b35f0534 100644 --- a/mozilla/embedding/browser/webBrowser/nsDocShellTreeOwner.cpp +++ b/mozilla/embedding/browser/webBrowser/nsDocShellTreeOwner.cpp @@ -58,6 +58,7 @@ #include "nsISimpleEnumerator.h" // Interfaces needed to be included +#include "nsIPresContext.h" #include "nsIContextMenuListener.h" #include "nsIContextMenuListener2.h" #include "nsITooltipListener.h" diff --git a/mozilla/embedding/browser/webBrowser/nsEmbedStream.cpp b/mozilla/embedding/browser/webBrowser/nsEmbedStream.cpp new file mode 100644 index 00000000000..82486a0fa63 --- /dev/null +++ b/mozilla/embedding/browser/webBrowser/nsEmbedStream.cpp @@ -0,0 +1,290 @@ +/* + * 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.org code. + * + * The Initial Developer of the Original Code is Christopher Blizzard. + * Portions created by Christopher Blizzard are Copyright (C) + * Christopher Blizzard. All Rights Reserved. + * + * Contributor(s): + * Christopher Blizzard + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "nsXPCOMCID.h" +#include "nsICategoryManager.h" + +#include "nsIContentViewer.h" + +#include "nsEmbedStream.h" +#include "nsReadableUtils.h" + +// nsIInputStream interface + +NS_IMPL_ISUPPORTS1(nsEmbedStream, nsIInputStream) + +nsEmbedStream::nsEmbedStream() +{ + mOwner = nsnull; + mOffset = 0; + mDoingStream = PR_FALSE; +} + +nsEmbedStream::~nsEmbedStream() +{ +} + +void +nsEmbedStream::InitOwner(nsIWebBrowser *aOwner) +{ + mOwner = aOwner; +} + +NS_METHOD +nsEmbedStream::Init(void) +{ + nsresult rv = NS_OK; + + nsCOMPtr bufInStream; + nsCOMPtr bufOutStream; + + rv = NS_NewPipe(getter_AddRefs(bufInStream), + getter_AddRefs(bufOutStream)); + + if (NS_FAILED(rv)) return rv; + + mInputStream = bufInStream; + mOutputStream = bufOutStream; + + return NS_OK; +} + +NS_METHOD +nsEmbedStream::OpenStream(const char *aBaseURI, const char *aContentType) +{ + NS_ENSURE_ARG_POINTER(aBaseURI); + NS_ENSURE_ARG_POINTER(aContentType); + + nsresult rv = NS_OK; + + // if we're already doing a stream then close the current one + if (mDoingStream) + CloseStream(); + + // set our state + mDoingStream = PR_TRUE; + + // initialize our streams + rv = Init(); + if (NS_FAILED(rv)) + return rv; + + // get the viewer container + nsCOMPtr viewerContainer; + viewerContainer = do_GetInterface(mOwner); + + // create a new uri object + nsCOMPtr uri; + nsCAutoString spec(aBaseURI); + rv = NS_NewURI(getter_AddRefs(uri), spec.get()); + + if (NS_FAILED(rv)) + return rv; + + // create a new load group + rv = NS_NewLoadGroup(getter_AddRefs(mLoadGroup), nsnull); + if (NS_FAILED(rv)) + return rv; + + // create a new input stream channel + rv = NS_NewInputStreamChannel(getter_AddRefs(mChannel), uri, + NS_STATIC_CAST(nsIInputStream *, this), + nsDependentCString(aContentType), + EmptyCString()); + if (NS_FAILED(rv)) + return rv; + + // set the channel's load group + rv = mChannel->SetLoadGroup(mLoadGroup); + if (NS_FAILED(rv)) + return rv; + + // find a document loader for this content type + + nsXPIDLCString docLoaderContractID; + nsCOMPtr catMan(do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv)); + if (NS_FAILED(rv)) + return rv; + rv = catMan->GetCategoryEntry("Gecko-Content-Viewers", aContentType, + getter_Copies(docLoaderContractID)); + if (NS_FAILED(rv)) + return rv; + + nsCOMPtr docLoaderFactory; + docLoaderFactory = do_GetService(docLoaderContractID.get(), &rv); + if (NS_FAILED(rv)) + return rv; + + // ok, create an instance of the content viewer for that command and + // mime type + nsCOMPtr contentViewer; + rv = docLoaderFactory->CreateInstance("view", mChannel, mLoadGroup, + aContentType, viewerContainer, + nsnull, + getter_AddRefs(mStreamListener), + getter_AddRefs(contentViewer)); + if (NS_FAILED(rv)) + return rv; + + // set the container viewer container for this content view + rv = contentViewer->SetContainer(viewerContainer); + if (NS_FAILED(rv)) + return rv; + + // embed this sucker + rv = viewerContainer->Embed(contentViewer, "view", nsnull); + if (NS_FAILED(rv)) + return rv; + + // start our request + nsCOMPtr request = do_QueryInterface(mChannel); + rv = mStreamListener->OnStartRequest(request, NULL); + if (NS_FAILED(rv)) + return rv; + + return NS_OK; +} + +NS_METHOD +nsEmbedStream::AppendToStream(const char *aData, PRInt32 aLen) +{ + nsresult rv; + + // append the data + rv = Append(aData, aLen); + if (NS_FAILED(rv)) + return rv; + + // notify our listeners + nsCOMPtr request = do_QueryInterface(mChannel); + rv = mStreamListener->OnDataAvailable(request, + NULL, + NS_STATIC_CAST(nsIInputStream *, this), + mOffset, /* offset */ + aLen); /* len */ + // move our counter + mOffset += aLen; + if (NS_FAILED(rv)) + return rv; + + return NS_OK; +} + +NS_METHOD +nsEmbedStream::CloseStream(void) +{ + nsresult rv = NS_OK; + + NS_ENSURE_STATE(mDoingStream); + mDoingStream = PR_FALSE; + + nsCOMPtr request = do_QueryInterface(mChannel, &rv); + if (NS_FAILED(rv)) + goto loser; + + rv = mStreamListener->OnStopRequest(request, NULL, NS_OK); + if (NS_FAILED(rv)) + return rv; + + loser: + mLoadGroup = nsnull; + mChannel = nsnull; + mStreamListener = nsnull; + mOffset = 0; + + return rv; +} + +NS_METHOD +nsEmbedStream::Append(const char *aData, PRUint32 aLen) +{ + nsresult rv = NS_OK; + PRUint32 bytesWritten = 0; + rv = mOutputStream->Write(aData, aLen, &bytesWritten); + if (NS_FAILED(rv)) + return rv; + + NS_ASSERTION(bytesWritten == aLen, + "underlying byffer couldn't handle the write"); + return rv; +} + +NS_IMETHODIMP +nsEmbedStream::Available(PRUint32 *_retval) +{ + return mInputStream->Available(_retval); +} + +NS_IMETHODIMP +nsEmbedStream::Read(char * aBuf, PRUint32 aCount, PRUint32 *_retval) +{ + return mInputStream->Read(aBuf, aCount, _retval); +} + +NS_IMETHODIMP nsEmbedStream::Close(void) +{ + return mInputStream->Close(); +} + +NS_IMETHODIMP +nsEmbedStream::ReadSegments(nsWriteSegmentFun aWriter, void * aClosure, + PRUint32 aCount, PRUint32 *_retval) +{ + char *readBuf = (char *)malloc(aCount); + PRUint32 nBytes; + + if (!readBuf) + return NS_ERROR_OUT_OF_MEMORY; + + nsresult rv = mInputStream->Read(readBuf, aCount, &nBytes); + + *_retval = 0; + + if (NS_SUCCEEDED(rv)) { + PRUint32 writeCount = 0; + rv = aWriter(this, aClosure, readBuf, 0, nBytes, &writeCount); + + // XXX writeCount may be less than nBytes!! This is the wrong + // way to synthesize ReadSegments. + NS_ASSERTION(writeCount == nBytes, "data loss"); + + // errors returned from the writer end here! + rv = NS_OK; + } + + free(readBuf); + + return rv; +} + +NS_IMETHODIMP +nsEmbedStream::IsNonBlocking(PRBool *aNonBlocking) +{ + return mInputStream->IsNonBlocking(aNonBlocking); +} diff --git a/mozilla/embedding/browser/webBrowser/nsEmbedStream.h b/mozilla/embedding/browser/webBrowser/nsEmbedStream.h new file mode 100644 index 00000000000..b5112647e0c --- /dev/null +++ b/mozilla/embedding/browser/webBrowser/nsEmbedStream.h @@ -0,0 +1,65 @@ +/* + * 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.org code. + * + * The Initial Developer of the Original Code is Christopher Blizzard. + * Portions created by Christopher Blizzard are Copyright (C) + * Christopher Blizzard. All Rights Reserved. + * + * Contributor(s): + * Christopher Blizzard + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +class nsEmbedStream : public nsIInputStream +{ + public: + + nsEmbedStream(); + virtual ~nsEmbedStream(); + + void InitOwner (nsIWebBrowser *aOwner); + NS_METHOD Init (void); + + NS_METHOD OpenStream (const char *aBaseURI, const char *aContentType); + NS_METHOD AppendToStream (const char *aData, PRInt32 aLen); + NS_METHOD CloseStream (void); + + NS_METHOD Append (const char *aData, PRUint32 aLen); + + // nsISupports + NS_DECL_ISUPPORTS + // nsIInputStream + NS_DECL_NSIINPUTSTREAM + + private: + nsCOMPtr mOutputStream; + nsCOMPtr mInputStream; + + nsCOMPtr mLoadGroup; + nsCOMPtr mChannel; + nsCOMPtr mStreamListener; + + PRUint32 mOffset; + PRBool mDoingStream; + + nsIWebBrowser *mOwner; + +}; diff --git a/mozilla/embedding/browser/webBrowser/nsIWebBrowserStream.idl b/mozilla/embedding/browser/webBrowser/nsIWebBrowserStream.idl new file mode 100644 index 00000000000..730a65132e7 --- /dev/null +++ b/mozilla/embedding/browser/webBrowser/nsIWebBrowserStream.idl @@ -0,0 +1,49 @@ +/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** 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 mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2001, 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Marco Pesenti Gritti + * + * 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 "nsISupports.idl" + +[scriptable, uuid(27108bc2-dc7c-42d9-9c64-132550c78ce4)] +interface nsIWebBrowserStream : nsISupports +{ + void openStream(in string aBaseURI, in string aContentType); + + void appendToStream(in string aData, in long aLen); + + void closeStream(); +}; diff --git a/mozilla/embedding/browser/webBrowser/nsWebBrowser.cpp b/mozilla/embedding/browser/webBrowser/nsWebBrowser.cpp index 34b529e3e88..18474270d13 100644 --- a/mozilla/embedding/browser/webBrowser/nsWebBrowser.cpp +++ b/mozilla/embedding/browser/webBrowser/nsWebBrowser.cpp @@ -62,6 +62,7 @@ #include "nsIWebProgress.h" #include "nsIWebProgressListener.h" #include "nsIWebBrowserFocus.h" +#include "nsIWebBrowserStream.h" #include "nsIPresShell.h" #include "nsIGlobalHistory.h" #include "nsIDocShellHistory.h" @@ -110,6 +111,7 @@ nsWebBrowser::nsWebBrowser() : mDocShellTreeOwner(nsnull), mPersistCurrentState(nsIWebBrowserPersist::PERSIST_STATE_READY), mPersistResult(NS_OK), mPersistFlags(nsIWebBrowserPersist::PERSIST_FLAGS_NONE), + mStream(nsnull), mParentWidget(nsnull), mParent(nsnull), mListenerArray(nsnull) @@ -186,6 +188,7 @@ NS_INTERFACE_MAP_BEGIN(nsWebBrowser) NS_INTERFACE_MAP_ENTRY(nsIWebBrowserPersist) NS_INTERFACE_MAP_ENTRY(nsIWebBrowserFocus) NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener) + NS_INTERFACE_MAP_ENTRY(nsIWebBrowserStream) NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) NS_INTERFACE_MAP_END @@ -1892,3 +1895,48 @@ NS_IMETHODIMP nsWebBrowser::SetFocusedElement(nsIDOMElement * aFocusedElement) return NS_OK; } +//***************************************************************************** +// nsWebBrowser::nsIWebBrowserStream +//***************************************************************************** + +/* void openStream (in string aBaseURI, in string aContentType); */ +NS_IMETHODIMP nsWebBrowser::OpenStream(const char *aBaseURI, const char *aContentType) +{ + nsresult rv; + + if (!mStream) { + mStream = new nsEmbedStream(); + mStreamGuard = do_QueryInterface(mStream); + mStream->InitOwner(this); + rv = mStream->Init(); + if (NS_FAILED(rv)) + return rv; + } + + return mStream->OpenStream(aBaseURI, aContentType); +} + +/* void appendStream (in string aData, in long aLen); */ +NS_IMETHODIMP nsWebBrowser::AppendToStream(const char *aData, PRInt32 aLen) +{ + if (!mStream) + return NS_ERROR_FAILURE; + + return mStream->AppendToStream(aData, aLen); +} + +/* void closeStream (); */ +NS_IMETHODIMP nsWebBrowser::CloseStream() +{ + nsresult rv; + + if (!mStream) + return NS_ERROR_FAILURE; + rv = mStream->CloseStream(); + + // release + mStream = 0; + mStreamGuard = 0; + + return rv; +} diff --git a/mozilla/embedding/browser/webBrowser/nsWebBrowser.h b/mozilla/embedding/browser/webBrowser/nsWebBrowser.h index e83496bcbf4..20e1767b325 100644 --- a/mozilla/embedding/browser/webBrowser/nsWebBrowser.h +++ b/mozilla/embedding/browser/webBrowser/nsWebBrowser.h @@ -64,8 +64,10 @@ #include "nsIWebBrowserSetup.h" #include "nsIWebBrowserPersist.h" #include "nsIWebBrowserFocus.h" +#include "nsIWebBrowserStream.h" #include "nsIWindowWatcher.h" #include "nsIPrintSettings.h" +#include "nsEmbedStream.h" #include "nsVoidArray.h" #include "nsWeakPtr.h" @@ -108,6 +110,7 @@ class nsWebBrowser : public nsIWebBrowser, public nsIWebBrowserPersist, public nsIWebBrowserFocus, public nsIWebProgressListener, + public nsIWebBrowserStream, public nsSupportsWeakReference { friend class nsDocShellTreeOwner; @@ -126,6 +129,7 @@ public: NS_DECL_NSIWEBBROWSERSETUP NS_DECL_NSIWEBBROWSERPERSIST NS_DECL_NSIWEBBROWSERFOCUS + NS_DECL_NSIWEBBROWSERSTREAM NS_DECL_NSIWEBPROGRESSLISTENER protected: @@ -171,6 +175,10 @@ protected: PRUint32 mPersistResult; PRUint32 mPersistFlags; + // stream + nsEmbedStream *mStream; + nsCOMPtr mStreamGuard; + //Weak Reference interfaces... nsIWidget* mParentWidget; nsIDocShellTreeItem* mParent;