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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "EmbedProgress.h"
00039 
00040 #include <nsXPIDLString.h>
00041 #include <nsIChannel.h>
00042 #include <nsIWebProgress.h>
00043 #include <nsIDOMWindow.h>
00044 
00045 #include "nsIURI.h"
00046 #include "nsCRT.h"
00047 
00048 EmbedProgress::EmbedProgress(void)
00049 {
00050   mOwner = nsnull;
00051 }
00052 
00053 EmbedProgress::~EmbedProgress()
00054 {
00055 }
00056 
00057 NS_IMPL_ISUPPORTS2(EmbedProgress,
00058                  nsIWebProgressListener,
00059                  nsISupportsWeakReference)
00060 
00061 nsresult
00062 EmbedProgress::Init(EmbedPrivate *aOwner)
00063 {
00064   mOwner = aOwner;
00065   return NS_OK;
00066 }
00067 
00068 NS_IMETHODIMP
00069 EmbedProgress::OnStateChange(nsIWebProgress *aWebProgress,
00070                           nsIRequest     *aRequest,
00071                           PRUint32        aStateFlags,
00072                           nsresult        aStatus)
00073 {
00074   // give the widget a chance to attach any listeners
00075   mOwner->ContentStateChange();
00076   // if we've got the start flag, emit the signal
00077   if ((aStateFlags & GTK_MOZ_EMBED_FLAG_IS_NETWORK) && 
00078       (aStateFlags & GTK_MOZ_EMBED_FLAG_START))
00079   {
00080     gtk_signal_emit(GTK_OBJECT(mOwner->mOwningWidget),
00081                   moz_embed_signals[NET_START]);
00082   }
00083 
00084   // get the uri for this request
00085   nsXPIDLCString uriString;
00086   RequestToURIString(aRequest, getter_Copies(uriString));
00087   nsString tmpString;
00088   CopyUTF8toUTF16(uriString, tmpString);
00089 
00090   // is it the same as the current URI?
00091   if (mOwner->mURI.Equals(tmpString))
00092   {
00093     // for people who know what they are doing
00094     gtk_signal_emit(GTK_OBJECT(mOwner->mOwningWidget),
00095                   moz_embed_signals[NET_STATE],
00096                   aStateFlags, aStatus);
00097   }
00098   gtk_signal_emit(GTK_OBJECT(mOwner->mOwningWidget),
00099                 moz_embed_signals[NET_STATE_ALL],
00100                 (const char *)uriString,
00101                 (gint)aStateFlags, (gint)aStatus);
00102   // and for stop, too
00103   if ((aStateFlags & GTK_MOZ_EMBED_FLAG_IS_NETWORK) && 
00104       (aStateFlags & GTK_MOZ_EMBED_FLAG_STOP))
00105   {
00106     gtk_signal_emit(GTK_OBJECT(mOwner->mOwningWidget),
00107                   moz_embed_signals[NET_STOP]);
00108     // let our owner know that the load finished
00109     mOwner->ContentFinishedLoading();
00110   }
00111 
00112   return NS_OK;
00113 }
00114 
00115 NS_IMETHODIMP
00116 EmbedProgress::OnProgressChange(nsIWebProgress *aWebProgress,
00117                             nsIRequest     *aRequest,
00118                             PRInt32         aCurSelfProgress,
00119                             PRInt32         aMaxSelfProgress,
00120                             PRInt32         aCurTotalProgress,
00121                             PRInt32         aMaxTotalProgress)
00122 {
00123 
00124   nsXPIDLCString uriString;
00125   RequestToURIString(aRequest, getter_Copies(uriString));
00126   
00127   nsString tmpString;
00128   CopyUTF8toUTF16(uriString, tmpString);
00129 
00130   // is it the same as the current uri?
00131   if (mOwner->mURI.Equals(tmpString)) {
00132     gtk_signal_emit(GTK_OBJECT(mOwner->mOwningWidget),
00133                   moz_embed_signals[PROGRESS],
00134                   aCurTotalProgress, aMaxTotalProgress);
00135   }
00136 
00137   gtk_signal_emit(GTK_OBJECT(mOwner->mOwningWidget),
00138                 moz_embed_signals[PROGRESS_ALL],
00139                 (const char *)uriString,
00140                 aCurTotalProgress, aMaxTotalProgress);
00141   return NS_OK;
00142 }
00143 
00144 NS_IMETHODIMP
00145 EmbedProgress::OnLocationChange(nsIWebProgress *aWebProgress,
00146                             nsIRequest     *aRequest,
00147                             nsIURI         *aLocation)
00148 {
00149   nsCAutoString newURI;
00150   NS_ENSURE_ARG_POINTER(aLocation);
00151   aLocation->GetSpec(newURI);
00152 
00153   // Make sure that this is the primary frame change and not
00154   // just a subframe.
00155   PRBool isSubFrameLoad = PR_FALSE;
00156   if (aWebProgress) {
00157     nsCOMPtr<nsIDOMWindow> domWindow;
00158     nsCOMPtr<nsIDOMWindow> topDomWindow;
00159 
00160     aWebProgress->GetDOMWindow(getter_AddRefs(domWindow));
00161 
00162     // get the root dom window
00163     if (domWindow)
00164       domWindow->GetTop(getter_AddRefs(topDomWindow));
00165 
00166     if (domWindow != topDomWindow)
00167       isSubFrameLoad = PR_TRUE;
00168   }
00169 
00170   if (!isSubFrameLoad) {
00171     mOwner->SetURI(newURI.get());
00172     gtk_signal_emit(GTK_OBJECT(mOwner->mOwningWidget),
00173                   moz_embed_signals[LOCATION]);
00174   }
00175 
00176   return NS_OK;
00177 }
00178 
00179 NS_IMETHODIMP
00180 EmbedProgress::OnStatusChange(nsIWebProgress  *aWebProgress,
00181                            nsIRequest      *aRequest,
00182                            nsresult         aStatus,
00183                            const PRUnichar *aMessage)
00184 {
00185   // need to make a copy so we can safely cast to a void *
00186   PRUnichar *tmpString = nsCRT::strdup(aMessage);
00187 
00188   gtk_signal_emit(GTK_OBJECT(mOwner->mOwningWidget),
00189                 moz_embed_signals[STATUS_CHANGE],
00190                 NS_STATIC_CAST(void *, aRequest),
00191                 NS_STATIC_CAST(int, aStatus),
00192                 NS_STATIC_CAST(void *, tmpString));
00193 
00194   nsMemory::Free(tmpString);
00195 
00196   return NS_OK;
00197 }
00198 
00199 NS_IMETHODIMP
00200 EmbedProgress::OnSecurityChange(nsIWebProgress *aWebProgress,
00201                             nsIRequest     *aRequest,
00202                             PRUint32         aState)
00203 {
00204   gtk_signal_emit(GTK_OBJECT(mOwner->mOwningWidget),
00205                 moz_embed_signals[SECURITY_CHANGE],
00206                 NS_STATIC_CAST(void *, aRequest),
00207                 aState);
00208   return NS_OK;
00209 }
00210 
00211 /* static */
00212 void
00213 EmbedProgress::RequestToURIString(nsIRequest *aRequest, char **aString)
00214 {
00215   // is it a channel
00216   nsCOMPtr<nsIChannel> channel;
00217   channel = do_QueryInterface(aRequest);
00218   if (!channel)
00219     return;
00220   
00221   nsCOMPtr<nsIURI> uri;
00222   channel->GetURI(getter_AddRefs(uri));
00223   if (!uri)
00224     return;
00225   
00226   nsCAutoString uriString;
00227   uri->GetSpec(uriString);
00228 
00229   *aString = strdup(uriString.get());
00230 }