home *** CD-ROM | disk | FTP | other *** search
- ////////////////////////////////////////////////////////////////////////////////
- //
- // uBrowser - a program that illustrates one way of embedding the
- // Mozilla Gecko (tm) Rendering Engine in an application, grabbing the
- // rendered output and displaying it on the surface of a 3D polygon as
- // texture in an OpenGL (tm) application.
- //
- // uBrowser is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2 of the License, or
- // (at your option) any later version.
- //
- // uBrowser is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with uBrowser; if not, write to the Free Software
- // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- //
- // Original code: Copyright 2005 Linden Research Inc.
- // http://www.lindenlab.com
- //
- // Primary author and site maintainer: Callum Prentice (callum@ubrowser.com)
- //
- // See contributors.txt or http://ubrowser.com for a list of contributors
- // without whose generous donation of time and effort, this application
- // would not have been possible.
- //
- ////////////////////////////////////////////////////////////////////////////////
-
- #include "embeddedbrowserwindow.h"
- #include "embeddedbrowser.h"
-
- #include "nsIDOMWindow.h"
- #include "nsIDocument.h"
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- embeddedBrowserWindow::embeddedBrowserWindow() :
- mEmbedBrowser ( nsnull ),
- mWebBrowser ( nsnull ),
- mPercentComplete( 0 ),
- mCurrentUri( "" ),
- mStatusText( "" )
- {
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- embeddedBrowserWindow::~embeddedBrowserWindow()
- {
- // We don't own this pointer
- mEmbedBrowser = NULL;
-
- // mWebBrowser nsCOMPtr falls out of scope, released.
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_METHOD embeddedBrowserWindow::Init( embeddedBrowser* browserIn, nsIWebBrowser* webBrowserIn )
- {
- mEmbedBrowser = browserIn;
-
- SetWebBrowser ( webBrowserIn );
-
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_IMPL_ADDREF( embeddedBrowserWindow )
- NS_IMPL_RELEASE( embeddedBrowserWindow )
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_INTERFACE_MAP_BEGIN( embeddedBrowserWindow )
- NS_INTERFACE_MAP_ENTRY_AMBIGUOUS( nsISupports, nsIWebBrowserChrome )
- NS_INTERFACE_MAP_ENTRY( nsIInterfaceRequestor )
- NS_INTERFACE_MAP_ENTRY( nsIWebBrowserChrome )
- NS_INTERFACE_MAP_ENTRY( nsIWebProgressListener )
- NS_INTERFACE_MAP_ENTRY( nsISupportsWeakReference )
- NS_INTERFACE_MAP_END
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_IMETHODIMP embeddedBrowserWindow::GetInterface( const nsIID &aIID, void** aInstancePtr )
- {
- if ( aIID.Equals( NS_GET_IID( nsIDOMWindow ) ) )
- {
- if ( mWebBrowser )
- {
- return mWebBrowser->GetContentDOMWindow( ( nsIDOMWindow** )aInstancePtr );
- };
-
- return NS_ERROR_NOT_INITIALIZED;
- };
-
- return QueryInterface( aIID, aInstancePtr );
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // called when something changes the status text - emits event to consumer
- NS_IMETHODIMP embeddedBrowserWindow::SetStatus( PRUint32 aType, const PRUnichar* aStatus )
- {
- mStatusText = std::string( NS_ConvertUTF16toUTF8( aStatus ).get() );
-
- embeddedBrowserWindowEvent event( this );
- mEventEmitter.update( &embeddedBrowserWindowObserver::onStatusTextChange, event );
-
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_IMETHODIMP embeddedBrowserWindow::GetWebBrowser( nsIWebBrowser** aWebBrowser )
- {
- NS_ENSURE_ARG_POINTER( aWebBrowser );
-
- *aWebBrowser = mWebBrowser;
-
- NS_IF_ADDREF( *aWebBrowser );
-
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_IMETHODIMP embeddedBrowserWindow::SetWebBrowser( nsIWebBrowser* aWebBrowser )
- {
- NS_ENSURE_ARG_POINTER( aWebBrowser );
-
- mWebBrowser = aWebBrowser;
-
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_IMETHODIMP embeddedBrowserWindow::GetChromeFlags( PRUint32* aChromeMask )
- {
- return NS_ERROR_NOT_IMPLEMENTED;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_IMETHODIMP embeddedBrowserWindow::SetChromeFlags( PRUint32 aChromeMask )
- {
- return NS_ERROR_NOT_IMPLEMENTED;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_IMETHODIMP embeddedBrowserWindow::DestroyBrowserWindow()
- {
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_IMETHODIMP embeddedBrowserWindow::SizeBrowserTo( PRInt32 aCX, PRInt32 aCY )
- {
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_IMETHODIMP embeddedBrowserWindow::ShowAsModal()
- {
- return NS_ERROR_NOT_IMPLEMENTED;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- //
- NS_IMETHODIMP embeddedBrowserWindow::IsWindowModal( PRBool* retval )
- {
- *retval = PR_FALSE;
-
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- NS_IMETHODIMP embeddedBrowserWindow::ExitModalEventLoop( nsresult aStatus )
- {
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // called when the page loading progress changes - emits event to consumer
- NS_IMETHODIMP embeddedBrowserWindow::OnProgressChange( nsIWebProgress* progress, nsIRequest* request,
- PRInt32 curSelfProgress, PRInt32 maxSelfProgress,
- PRInt32 curTotalProgress, PRInt32 maxTotalProgress )
- {
- mPercentComplete = static_cast< PRInt16 >
- ( static_cast< float >( curTotalProgress * 100.0f ) / static_cast< float >( maxTotalProgress ) );
-
- if ( mPercentComplete < 0 )
- mPercentComplete = 0;
-
- if ( mPercentComplete > 100 )
- mPercentComplete = 100;
-
- embeddedBrowserWindowEvent event( this );
- mEventEmitter.update( &embeddedBrowserWindowObserver::onUpdateProgress, event );
-
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // called when the browser state changes - as described below - emits event to consumer
- NS_IMETHODIMP embeddedBrowserWindow::OnStateChange( nsIWebProgress* progress, nsIRequest* request,
- PRUint32 progressStateFlags, nsresult status )
- {
- if ( ( progressStateFlags & STATE_START ) && ( progressStateFlags & STATE_IS_DOCUMENT ) )
- {
- mStatusText = std::string( "Browser loaded" );
-
- embeddedBrowserWindowEvent event( this );
- mEventEmitter.update( &embeddedBrowserWindowObserver::onNavigateBegin, event );
- };
-
- if ( ( progressStateFlags & STATE_STOP ) && ( progressStateFlags & STATE_IS_WINDOW ) )
- {
- mStatusText = std::string( "Done" );
-
- embeddedBrowserWindowEvent event( this );
- mEventEmitter.update( &embeddedBrowserWindowObserver::onNavigateComplete, event );
- };
-
- if ( progressStateFlags & STATE_REDIRECTING )
- {
- mStatusText = std::string( "Redirecting..." );
- };
-
- if ( progressStateFlags & STATE_TRANSFERRING )
- {
- mStatusText = std::string( "Transferring..." );
- };
-
- if ( progressStateFlags & STATE_NEGOTIATING )
- {
- mStatusText = std::string( "Negotiating..." );
- };
-
- embeddedBrowserWindowEvent event( this );
- mEventEmitter.update( &embeddedBrowserWindowObserver::onStatusTextChange, event );
-
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // call when the location changes - e.g. when a site redirects - emits event to consumer
- // TODO: ought to check that this change is on the top frame and
- // indicate this to the consumer of this class
- NS_IMETHODIMP embeddedBrowserWindow::OnLocationChange( nsIWebProgress* aWebProgress,
- nsIRequest* aRequest,
- nsIURI* aLocation )
- {
- nsCAutoString newURI;
- aLocation->GetSpec( newURI );
-
- mCurrentUri = newURI.get();
-
- embeddedBrowserWindowEvent event( this );
- mEventEmitter.update( &embeddedBrowserWindowObserver::onLocationChange, event );
-
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // allow consumers of this class to observe events - add themselves as an observer
- bool embeddedBrowserWindow::addObserver( embeddedBrowserWindowObserver* observerIn )
- {
- return mEventEmitter.addObserver( observerIn );
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // allow consumers of this class to observe events - remove themselves as an observer
- bool embeddedBrowserWindow::remObserver( embeddedBrowserWindowObserver* observerIn )
- {
- return mEventEmitter.remObserver( observerIn );
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // used by observers of this class to get the current URI
- const std::string& embeddedBrowserWindow::getCurrentUri()
- {
- return mCurrentUri;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // utility method that is used by observers to retrieve data after an event
- const PRInt16 embeddedBrowserWindow::getPercentComplete()
- {
- return mPercentComplete;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // utility method that is used by observers to retrieve data after an event
- const std::string& embeddedBrowserWindow::getStatusMsg()
- {
- return mStatusText;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // called when the status text is changed - emits event to consumer
- NS_IMETHODIMP embeddedBrowserWindow::OnStatusChange( nsIWebProgress* aWebProgress,
- nsIRequest* aRequest,
- nsresult aStatus,
- const PRUnichar* aMessage )
- {
- mStatusText = std::string( NS_ConvertUTF16toUTF8( aMessage ).get() );
-
- embeddedBrowserWindowEvent event( this );
- mEventEmitter.update( &embeddedBrowserWindowObserver::onStatusTextChange, event );
-
- return NS_OK;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // implement this if you want to do something when the security state changtes
- NS_IMETHODIMP embeddedBrowserWindow::OnSecurityChange( nsIWebProgress* aWebProgress,
- nsIRequest* aRequest,
- PRUint32 state )
- {
- return NS_OK;
- }