Back to index

lightning-sunbird  0.9+nobinonly
EmbedProgress.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Christopher Blizzard. Portions created by Christopher Blizzard are Copyright (C) Christopher Blizzard.  All Rights Reserved.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Christopher Blizzard <blizzard@mozilla.org>
00023  *   Brian Edmond <briane@qnx.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "EmbedProgress.h"
00040 
00041 #include <nsXPIDLString.h>
00042 #include <nsIChannel.h>
00043 
00044 #include <nsCWebBrowser.h>
00045 #include <nsIComponentManager.h>
00046 #include <nsIDocShellTreeItem.h>
00047 #include "nsIWidget.h"
00048 #include <nsString.h>
00049 #include <nsError.h>
00050 #include "nsReadableUtils.h"
00051 #include <nsILocalFile.h>
00052 #include <nsIRequestObserver.h>
00053 #include <nsISocketTransportService.h>
00054 #include <netCore.h>
00055 #include "nsGfxCIID.h"
00056 
00057 #include "nsIDOMWindow.h"
00058 
00059 #include "EmbedWindow.h"
00060 #include "EmbedPrivate.h"
00061 
00062 #include "nsIURI.h"
00063 #include "PtMozilla.h"
00064 
00065 EmbedProgress::EmbedProgress(void)
00066 {
00067        NS_INIT_ISUPPORTS();
00068   mOwner = nsnull;
00069   mSkipOnState = mDownloadDocument = 0;
00070 }
00071 
00072 EmbedProgress::~EmbedProgress()
00073 {
00074 }
00075 
00076 NS_IMPL_ISUPPORTS2(EmbedProgress,
00077                  nsIWebProgressListener,
00078                  nsISupportsWeakReference)
00079 
00080 nsresult
00081 EmbedProgress::Init(EmbedPrivate *aOwner)
00082 {
00083   mOwner = aOwner;
00084   return NS_OK;
00085 }
00086 
00087 NS_IMETHODIMP 
00088 EmbedProgress::OnStateChange(nsIWebProgress *aWebProgress, 
00089                              nsIRequest   *aRequest, 
00090                              PRUint32            aStateFlags, 
00091                              nsresult            aStatus)
00092 {
00093        PtMozillaWidget_t           *moz = (PtMozillaWidget_t *) mOwner->mOwningWidget;
00094        PtCallbackList_t            *cb = NULL;
00095        PtCallbackInfo_t            cbinfo;
00096        PtMozillaNetStateCb_t   state;
00097 
00098        // get the uri for this request
00099        nsXPIDLCString uriString;
00100        RequestToURIString(aRequest, getter_Copies(uriString));
00101        nsString tmpString;
00102        tmpString.AssignWithConversion(uriString);
00103 
00104        if( ( aStateFlags & STATE_IS_REQUEST ) && NS_FAILED( aStatus ) ) 
00105        {
00106               PtWebErrorCallback_t cbw;
00107 
00108               /* invoke the Pt_CB_WEB_ERROR in the client */
00109               cb = moz->web_error_cb;
00110               memset(&cbinfo, 0, sizeof(cbinfo));
00111               cbinfo.reason = Pt_CB_MOZ_ERROR;
00112               cbinfo.cbdata = &cbw;
00113 
00114               memset( &cbw, 0, sizeof( PtWebErrorCallback_t ) );
00115               cbw.url = (char*)((const char *)uriString);
00116               cbw.description = "";
00117 
00118               cbw.type = Pt_WEB_ERROR_TOPVIEW;
00119               switch( aStatus ) 
00120               {
00121                      case NS_ERROR_UNKNOWN_HOST:                      
00122                             cbw.reason = -12; 
00123                             break;
00124                      case NS_ERROR_NET_TIMEOUT:                       
00125                             cbw.reason = -408; 
00126                             break;
00127                      case NS_ERROR_NO_CONTENT:                               
00128                             cbw.reason = -8; 
00129                             break;
00130                      case NS_ERROR_FILE_NOT_FOUND:                    
00131                             cbw.reason = -404; 
00132                             break;
00133                      case NS_ERROR_CONNECTION_REFUSED:  
00134                             cbw.reason = -13; 
00135                             break;
00136                      default:             
00137                             cbw.reason = -1; 
00138                             break;
00139               }
00140               if( cbw.reason != -1) 
00141                      PtInvokeCallbackList(cb, (PtWidget_t *)moz, &cbinfo);
00142               /* let it check for STATE_STOP */
00143        }
00144 
00145        memset(&cbinfo, 0, sizeof(cbinfo));
00146 
00147        if( aStateFlags & STATE_IS_NETWORK /* STATE_IS_REQUEST STATE_IS_DOCUMENT */ ) 
00148        {
00149        if( aStateFlags & STATE_START ) 
00150               {
00151                      cbinfo.reason = Pt_CB_MOZ_START;
00152                      if( ( cb = moz->start_cb ) )
00153                             PtInvokeCallbackList(cb, (PtWidget_t *) moz, &cbinfo);
00154        }
00155        else if( aStateFlags & STATE_STOP ) 
00156               {
00157                      PtWebCompleteCallback_t cbcomplete;
00158 
00159                      cbinfo.reason = Pt_CB_MOZ_COMPLETE;
00160                      cbinfo.cbdata = &cbcomplete;
00161                      memset( &cbcomplete, 0, sizeof( PtWebCompleteCallback_t ) );
00162                      cbcomplete.url = strdup( uriString );
00163 
00164                      if( ( cb = moz->complete_cb ) )
00165                             PtInvokeCallbackList(cb, (PtWidget_t *) moz, &cbinfo);
00166 
00167                      free( cbcomplete.url );
00168 
00169                      if( moz->text_zoom != moz->actual_text_zoom ) {
00170                             nsCOMPtr<nsIDOMWindow> domWindow;
00171                             moz->EmbedRef->mWindow->mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00172                             if(domWindow) {
00173                                    domWindow->SetTextZoom( moz->text_zoom/100. );
00174                                    float vv;
00175                                    domWindow->GetTextZoom( &vv );
00176                                    moz->actual_text_zoom = (int) ( vv * 100 );
00177                                    }
00178                             }
00179        }
00180        }
00181 
00182        // invoke the raw status callbacks for page load status
00183        cbinfo.reason = Pt_CB_MOZ_NET_STATE;
00184        cbinfo.cbdata = &state;
00185        state.flags = aStateFlags;
00186        state.status = aStatus;
00187        //state.url = (const char *)uriString;
00188        state.url = NULL;
00189        char *statusMessage = "";
00190        PRInt32 flags = aStateFlags;
00191 
00192        if (flags & STATE_IS_REQUEST) 
00193        {
00194          if (flags & STATE_REDIRECTING)
00195               statusMessage = "Redirecting to site:";
00196          else if (flags & STATE_TRANSFERRING)
00197               statusMessage = "Receiving Data:";
00198          else if (flags & STATE_NEGOTIATING)
00199          statusMessage = "Waiting for authorization:";
00200        }
00201 
00202        if (flags & STATE_IS_DOCUMENT) 
00203        {
00204          if (flags & STATE_START)
00205               statusMessage = "Loading site:";
00206          else if (flags & STATE_STOP)
00207               statusMessage = "Finishing:";
00208        }
00209        state.message = statusMessage;
00210        if( ( cb = moz->net_state_cb ) && statusMessage[0])
00211               PtInvokeCallbackList(cb, (PtWidget_t *) moz, &cbinfo);
00212        return NS_OK;
00213 }
00214 
00215 NS_IMETHODIMP
00216 EmbedProgress::OnProgressChange(nsIWebProgress *aWebProgress,
00217                             nsIRequest     *aRequest,
00218                             PRInt32         aCurSelfProgress,
00219                             PRInt32         aMaxSelfProgress,
00220                             PRInt32         aCurTotalProgress,
00221                             PRInt32         aMaxTotalProgress)
00222 {
00223        PtMozillaWidget_t       *moz = (PtMozillaWidget_t *) mOwner->mOwningWidget;
00224        PtCallbackList_t        *cb;
00225        PtCallbackInfo_t        cbinfo;
00226        PtMozillaProgressCb_t   prog;
00227        PRInt32                                   cur, max;
00228        nsXPIDLCString                     uriString;
00229        if (!moz->progress_cb)
00230               return NS_OK;
00231 
00232        cur = aCurTotalProgress;
00233        max = aMaxTotalProgress;
00234 
00235        if (cur > max && max != -1 && max != 0)
00236               cur = max; // Progress complete
00237 
00238        cb = moz->progress_cb;
00239        memset(&cbinfo, 0, sizeof(cbinfo));
00240        cbinfo.reason = Pt_CB_MOZ_PROGRESS;
00241        cbinfo.cbdata = &prog;
00242 
00243        prog.cur = cur;
00244        prog.max = max;
00245        PtInvokeCallbackList(cb, (PtWidget_t *)moz, &cbinfo);
00246 
00247        return NS_OK;
00248 }
00249 
00250 NS_IMETHODIMP
00251 EmbedProgress::OnLocationChange(nsIWebProgress *aWebProgress,
00252                             nsIRequest     *aRequest,
00253                             nsIURI         *aLocation)
00254 {
00255        NS_ENSURE_ARG_POINTER(aLocation);
00256 
00257        PtMozillaWidget_t   *moz = (PtMozillaWidget_t *) mOwner->mOwningWidget;
00258        PtCallbackList_t    *cb = NULL;
00259        PtCallbackInfo_t    cbinfo;
00260        PtMozillaUrlCb_t    url;
00261        nsCAutoString newURI;
00262 
00263        aLocation->GetSpec(newURI);
00264        mOwner->SetURI(newURI.get());
00265 
00266        if (!moz->url_cb)
00267            return NS_OK;
00268 
00269        memset(&cbinfo, 0, sizeof(cbinfo));
00270        cbinfo.cbdata = &url;
00271        cbinfo.reason = Pt_CB_MOZ_URL;
00272        url.url = (char *) newURI.get();
00273 //     aLocation->GetSpec(&(url.url));
00274        cb = moz->url_cb;
00275        PtInvokeCallbackList(cb, (PtWidget_t *) moz, &cbinfo);
00276        
00277        return NS_OK;
00278 }
00279 
00280 NS_IMETHODIMP
00281 EmbedProgress::OnStatusChange(nsIWebProgress  *aWebProgress,
00282                            nsIRequest      *aRequest,
00283                            nsresult         aStatus,
00284                            const PRUnichar *aMessage)
00285 {
00286        PtMozillaWidget_t    *moz = (PtMozillaWidget_t *) mOwner->mOwningWidget;
00287        PtCallbackList_t        *cb;
00288        PtCallbackInfo_t        cbinfo;
00289        PtMozillaInfoCb_t       info;
00290        nsAutoString Text ( aMessage );
00291 
00292        if (!moz->info_cb)
00293               return NS_OK;
00294 
00295        memset(&cbinfo, 0, sizeof(cbinfo));
00296        cbinfo.cbdata = &info;
00297        cbinfo.reason = Pt_CB_MOZ_INFO;
00298        cb = moz->info_cb;
00299 
00300        const char* msg = ToNewCString(Text);
00301        info.type = Pt_MOZ_INFO_CONNECT;
00302        info.status = 0;
00303        info.data = (char *)msg;
00304        PtInvokeCallbackList(cb, (PtWidget_t *) moz, &cbinfo);
00305 
00306        nsMemory::Free( (void*)msg );
00307        return NS_OK;
00308 }
00309 
00310 NS_IMETHODIMP
00311 EmbedProgress::OnSecurityChange(nsIWebProgress *aWebProgress,
00312                             nsIRequest     *aRequest,
00313                             PRUint32         aState)
00314 {
00315   return NS_OK;
00316 }
00317 
00318 /* static */
00319 void
00320 EmbedProgress::RequestToURIString(nsIRequest *aRequest, char **aString)
00321 {
00322        // is it a channel
00323        nsCOMPtr<nsIChannel> channel;
00324        channel = do_QueryInterface(aRequest);
00325        if (!channel)
00326               return;
00327 
00328        nsCOMPtr<nsIURI> uri;
00329        channel->GetURI(getter_AddRefs(uri));
00330        if (!uri)
00331        return;
00332 
00333        nsCAutoString uriString;
00334        uri->GetSpec(uriString);
00335 
00336        *aString = strdup(uriString.get());
00337 }