home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / ubrowser.exe / ubrowser / embeddedbrowserwindow.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-02-21  |  10.8 KB  |  330 lines

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  uBrowser - a program that illustrates one way of embedding the
  4. //  Mozilla Gecko (tm) Rendering Engine in an application, grabbing the
  5. //  rendered output and displaying it on the surface of a 3D polygon as
  6. //  texture in an OpenGL (tm) application.
  7. //
  8. //  uBrowser is free software; you can redistribute it and/or modify
  9. //  it under the terms of the GNU General Public License as published by
  10. //  the Free Software Foundation; either version 2 of the License, or
  11. //  (at your option) any later version.
  12. //
  13. //  uBrowser is distributed in the hope that it will be useful,
  14. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. //  GNU General Public License for more details.
  17. //
  18. //  You should have received a copy of the GNU General Public License
  19. //  along with uBrowser; if not, write to the Free Software
  20. //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. //
  22. //  Original code:  Copyright 2005 Linden Research Inc.
  23. //                  http://www.lindenlab.com
  24. //
  25. //  Primary author and site maintainer: Callum Prentice (callum@ubrowser.com)
  26. //
  27. //  See contributors.txt or http://ubrowser.com for a list of contributors
  28. //  without whose generous donation of time and effort, this application
  29. //  would not have been possible.
  30. //
  31. ////////////////////////////////////////////////////////////////////////////////
  32.  
  33. #include "embeddedbrowserwindow.h"
  34. #include "embeddedbrowser.h"
  35.  
  36. #include "nsIDOMWindow.h"
  37. #include "nsIDocument.h"
  38.  
  39. ////////////////////////////////////////////////////////////////////////////////
  40. //
  41. embeddedBrowserWindow::embeddedBrowserWindow() :
  42.     mEmbedBrowser ( nsnull ),
  43.     mWebBrowser ( nsnull ),
  44.     mPercentComplete( 0 ),
  45.     mCurrentUri( "" ),
  46.     mStatusText( "" )
  47. {
  48. }
  49.  
  50. ////////////////////////////////////////////////////////////////////////////////
  51. //
  52. embeddedBrowserWindow::~embeddedBrowserWindow()
  53. {
  54.     // We don't own this pointer
  55.     mEmbedBrowser = NULL;
  56.  
  57.     // mWebBrowser nsCOMPtr falls out of scope, released.
  58. }
  59.  
  60. ////////////////////////////////////////////////////////////////////////////////
  61. //
  62. NS_METHOD embeddedBrowserWindow::Init( embeddedBrowser* browserIn, nsIWebBrowser* webBrowserIn )
  63. {
  64.     mEmbedBrowser = browserIn;
  65.  
  66.     SetWebBrowser ( webBrowserIn );
  67.  
  68.     return NS_OK;
  69. }
  70.  
  71. ////////////////////////////////////////////////////////////////////////////////
  72. //
  73. NS_IMPL_ADDREF( embeddedBrowserWindow )
  74. NS_IMPL_RELEASE( embeddedBrowserWindow )
  75.  
  76. ////////////////////////////////////////////////////////////////////////////////
  77. //
  78. NS_INTERFACE_MAP_BEGIN( embeddedBrowserWindow )
  79.     NS_INTERFACE_MAP_ENTRY_AMBIGUOUS( nsISupports, nsIWebBrowserChrome )
  80.     NS_INTERFACE_MAP_ENTRY( nsIInterfaceRequestor )
  81.     NS_INTERFACE_MAP_ENTRY( nsIWebBrowserChrome )
  82.     NS_INTERFACE_MAP_ENTRY( nsIWebProgressListener )
  83.     NS_INTERFACE_MAP_ENTRY( nsISupportsWeakReference )
  84. NS_INTERFACE_MAP_END
  85.  
  86. ////////////////////////////////////////////////////////////////////////////////
  87. //
  88. NS_IMETHODIMP embeddedBrowserWindow::GetInterface( const nsIID &aIID, void** aInstancePtr )
  89. {
  90.     if ( aIID.Equals( NS_GET_IID( nsIDOMWindow ) ) )
  91.     {
  92.         if ( mWebBrowser )
  93.         {
  94.             return mWebBrowser->GetContentDOMWindow( ( nsIDOMWindow** )aInstancePtr );
  95.         };
  96.  
  97.         return NS_ERROR_NOT_INITIALIZED;
  98.     };
  99.  
  100.     return QueryInterface( aIID, aInstancePtr );
  101. }
  102.  
  103. ////////////////////////////////////////////////////////////////////////////////
  104. // called when something changes the status text - emits event to consumer
  105. NS_IMETHODIMP embeddedBrowserWindow::SetStatus( PRUint32 aType, const PRUnichar* aStatus )
  106. {
  107.     mStatusText = std::string( NS_ConvertUTF16toUTF8( aStatus ).get() );
  108.  
  109.     embeddedBrowserWindowEvent event( this );
  110.     mEventEmitter.update( &embeddedBrowserWindowObserver::onStatusTextChange, event );
  111.  
  112.     return NS_OK;
  113. }
  114.  
  115. ////////////////////////////////////////////////////////////////////////////////
  116. //
  117. NS_IMETHODIMP embeddedBrowserWindow::GetWebBrowser( nsIWebBrowser** aWebBrowser )
  118. {
  119.     NS_ENSURE_ARG_POINTER( aWebBrowser );
  120.  
  121.     *aWebBrowser = mWebBrowser;
  122.  
  123.     NS_IF_ADDREF( *aWebBrowser );
  124.  
  125.     return NS_OK;
  126. }
  127.  
  128. ////////////////////////////////////////////////////////////////////////////////
  129. //
  130. NS_IMETHODIMP embeddedBrowserWindow::SetWebBrowser( nsIWebBrowser* aWebBrowser )
  131. {
  132.     NS_ENSURE_ARG_POINTER( aWebBrowser );
  133.  
  134.     mWebBrowser = aWebBrowser;
  135.  
  136.     return NS_OK;
  137. }
  138.  
  139. ////////////////////////////////////////////////////////////////////////////////
  140. //
  141. NS_IMETHODIMP embeddedBrowserWindow::GetChromeFlags( PRUint32* aChromeMask )
  142. {
  143.     return NS_ERROR_NOT_IMPLEMENTED;
  144. }
  145.  
  146. ////////////////////////////////////////////////////////////////////////////////
  147. //
  148. NS_IMETHODIMP embeddedBrowserWindow::SetChromeFlags( PRUint32 aChromeMask )
  149. {
  150.     return NS_ERROR_NOT_IMPLEMENTED;
  151. }
  152.  
  153. ////////////////////////////////////////////////////////////////////////////////
  154. //
  155. NS_IMETHODIMP embeddedBrowserWindow::DestroyBrowserWindow()
  156. {
  157.     return NS_OK;
  158. }
  159.  
  160. ////////////////////////////////////////////////////////////////////////////////
  161. //
  162. NS_IMETHODIMP embeddedBrowserWindow::SizeBrowserTo( PRInt32 aCX, PRInt32 aCY )
  163. {
  164.     return NS_OK;
  165. }
  166.  
  167. ////////////////////////////////////////////////////////////////////////////////
  168. //
  169. NS_IMETHODIMP embeddedBrowserWindow::ShowAsModal()
  170. {
  171.     return NS_ERROR_NOT_IMPLEMENTED;
  172. }
  173.  
  174. ////////////////////////////////////////////////////////////////////////////////
  175. //
  176. //
  177. NS_IMETHODIMP embeddedBrowserWindow::IsWindowModal( PRBool* retval )
  178. {
  179.     *retval = PR_FALSE;
  180.  
  181.     return NS_OK;
  182. }
  183.  
  184. ////////////////////////////////////////////////////////////////////////////////
  185. //
  186. NS_IMETHODIMP embeddedBrowserWindow::ExitModalEventLoop( nsresult aStatus )
  187. {
  188.     return NS_OK;
  189. }
  190.  
  191. ////////////////////////////////////////////////////////////////////////////////
  192. // called when the page loading progress changes - emits event to consumer
  193. NS_IMETHODIMP embeddedBrowserWindow::OnProgressChange( nsIWebProgress* progress, nsIRequest* request,
  194.                                                     PRInt32 curSelfProgress, PRInt32 maxSelfProgress,
  195.                                                         PRInt32 curTotalProgress, PRInt32 maxTotalProgress )
  196. {
  197.     mPercentComplete = static_cast< PRInt16 >
  198.         ( static_cast< float >( curTotalProgress * 100.0f ) / static_cast< float >( maxTotalProgress ) );
  199.  
  200.     if ( mPercentComplete < 0 )
  201.         mPercentComplete = 0;
  202.  
  203.     if ( mPercentComplete > 100 )
  204.         mPercentComplete = 100;
  205.  
  206.     embeddedBrowserWindowEvent event( this );
  207.     mEventEmitter.update( &embeddedBrowserWindowObserver::onUpdateProgress, event );
  208.  
  209.     return NS_OK;
  210. }
  211.  
  212. ////////////////////////////////////////////////////////////////////////////////
  213. // called when the browser state changes - as described below - emits event to consumer
  214. NS_IMETHODIMP embeddedBrowserWindow::OnStateChange( nsIWebProgress* progress, nsIRequest* request,
  215.                                                     PRUint32 progressStateFlags, nsresult status )
  216. {
  217.      if ( ( progressStateFlags & STATE_START ) && ( progressStateFlags & STATE_IS_DOCUMENT ) )
  218.     {
  219.         mStatusText = std::string( "Browser loaded" );
  220.  
  221.         embeddedBrowserWindowEvent event( this );
  222.         mEventEmitter.update( &embeddedBrowserWindowObserver::onNavigateBegin, event );
  223.     };
  224.  
  225.     if ( ( progressStateFlags & STATE_STOP ) && ( progressStateFlags & STATE_IS_WINDOW ) )
  226.     {
  227.         mStatusText = std::string( "Done" );
  228.  
  229.         embeddedBrowserWindowEvent event( this );
  230.         mEventEmitter.update( &embeddedBrowserWindowObserver::onNavigateComplete, event );
  231.     };
  232.  
  233.     if ( progressStateFlags & STATE_REDIRECTING )
  234.     {
  235.         mStatusText = std::string( "Redirecting..." );
  236.     };
  237.  
  238.     if ( progressStateFlags & STATE_TRANSFERRING )
  239.     {
  240.         mStatusText = std::string( "Transferring..." );
  241.     };
  242.  
  243.     if ( progressStateFlags & STATE_NEGOTIATING )
  244.     {
  245.         mStatusText = std::string( "Negotiating..." );
  246.     };
  247.  
  248.     embeddedBrowserWindowEvent event( this );
  249.     mEventEmitter.update( &embeddedBrowserWindowObserver::onStatusTextChange, event );
  250.  
  251.     return NS_OK;
  252. }
  253.  
  254. ////////////////////////////////////////////////////////////////////////////////
  255. // call when the location changes - e.g. when a site redirects - emits event to consumer
  256. // TODO: ought to check that this change is on the top frame and
  257. // indicate this to the consumer of this class
  258. NS_IMETHODIMP embeddedBrowserWindow::OnLocationChange( nsIWebProgress* aWebProgress,
  259.                                                         nsIRequest* aRequest,
  260.                                                             nsIURI* aLocation )
  261. {
  262.     nsCAutoString newURI;
  263.     aLocation->GetSpec( newURI );
  264.  
  265.     mCurrentUri = newURI.get();
  266.  
  267.     embeddedBrowserWindowEvent event( this );
  268.     mEventEmitter.update( &embeddedBrowserWindowObserver::onLocationChange, event );
  269.  
  270.     return NS_OK;
  271. }
  272.  
  273. ////////////////////////////////////////////////////////////////////////////////
  274. // allow consumers of this class to observe events - add themselves as an observer
  275. bool embeddedBrowserWindow::addObserver( embeddedBrowserWindowObserver* observerIn )
  276. {
  277.     return mEventEmitter.addObserver( observerIn );
  278. }
  279.  
  280. ////////////////////////////////////////////////////////////////////////////////
  281. // allow consumers of this class to observe events - remove themselves as an observer
  282. bool embeddedBrowserWindow::remObserver( embeddedBrowserWindowObserver* observerIn )
  283. {
  284.     return mEventEmitter.remObserver( observerIn );
  285. }
  286.  
  287. ////////////////////////////////////////////////////////////////////////////////
  288. // used by observers of this class to get the current URI
  289. const std::string& embeddedBrowserWindow::getCurrentUri()
  290. {
  291.     return mCurrentUri;
  292. }
  293.  
  294. ////////////////////////////////////////////////////////////////////////////////
  295. // utility method that is used by observers to retrieve data after an event
  296. const PRInt16 embeddedBrowserWindow::getPercentComplete()
  297. {
  298.     return mPercentComplete;
  299. }
  300.  
  301. ////////////////////////////////////////////////////////////////////////////////
  302. // utility method that is used by observers to retrieve data after an event
  303. const std::string& embeddedBrowserWindow::getStatusMsg()
  304. {
  305.     return mStatusText;
  306. }
  307.  
  308. ////////////////////////////////////////////////////////////////////////////////
  309. // called when the status text is changed - emits event to consumer
  310. NS_IMETHODIMP embeddedBrowserWindow::OnStatusChange( nsIWebProgress* aWebProgress,
  311.                                                         nsIRequest* aRequest,
  312.                                                             nsresult aStatus,
  313.                                                                 const PRUnichar* aMessage )
  314. {
  315.     mStatusText = std::string( NS_ConvertUTF16toUTF8( aMessage ).get() );
  316.  
  317.     embeddedBrowserWindowEvent event( this );
  318.     mEventEmitter.update( &embeddedBrowserWindowObserver::onStatusTextChange, event );
  319.  
  320.     return NS_OK;
  321. }
  322.  
  323. ////////////////////////////////////////////////////////////////////////////////
  324. // implement this if you want to do something when the security state changtes
  325. NS_IMETHODIMP embeddedBrowserWindow::OnSecurityChange( nsIWebProgress* aWebProgress,
  326.                                                     nsIRequest* aRequest,
  327.                                                         PRUint32 state )
  328. {
  329.     return NS_OK;
  330. }