Bug 454558 Null check missing in nsGNOMEShellService::GetShouldCheckDefaultBrowser...
[wine-gecko.git] / embedding / browser / photon / src / EmbedProgress.cpp
blobcae140b05c287c3ce8a58c7e3a07e714b9bfd2c6
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is mozilla.org code.
16 * The Initial Developer of the Original Code is
17 * Christopher Blizzard.
18 * Portions created by the Initial Developer are Copyright (C) 2001
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Christopher Blizzard <blizzard@mozilla.org>
23 * Brian Edmond <briane@qnx.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "EmbedProgress.h"
41 #include <nsXPIDLString.h>
42 #include <nsIChannel.h>
44 #include <nsCWebBrowser.h>
45 #include <nsIComponentManager.h>
46 #include <nsIDocShellTreeItem.h>
47 #include "nsIWidget.h"
48 #include <nsString.h>
49 #include <nsError.h>
50 #include "nsReadableUtils.h"
51 #include <nsILocalFile.h>
52 #include <nsIRequestObserver.h>
53 #include <nsISocketTransportService.h>
54 #include <netCore.h>
55 #include "nsGfxCIID.h"
57 #include "nsIDOMWindow.h"
59 #include "EmbedWindow.h"
60 #include "EmbedPrivate.h"
62 #include "nsIURI.h"
63 #include "PtMozilla.h"
65 EmbedProgress::EmbedProgress(void)
67 NS_INIT_ISUPPORTS();
68 mOwner = nsnull;
69 mSkipOnState = mDownloadDocument = 0;
72 EmbedProgress::~EmbedProgress()
76 NS_IMPL_ISUPPORTS2(EmbedProgress,
77 nsIWebProgressListener,
78 nsISupportsWeakReference)
80 nsresult
81 EmbedProgress::Init(EmbedPrivate *aOwner)
83 mOwner = aOwner;
84 return NS_OK;
87 NS_IMETHODIMP
88 EmbedProgress::OnStateChange(nsIWebProgress *aWebProgress,
89 nsIRequest *aRequest,
90 PRUint32 aStateFlags,
91 nsresult aStatus)
93 PtMozillaWidget_t *moz = (PtMozillaWidget_t *) mOwner->mOwningWidget;
94 PtCallbackList_t *cb = NULL;
95 PtCallbackInfo_t cbinfo;
96 PtMozillaNetStateCb_t state;
98 // get the uri for this request
99 nsXPIDLCString uriString;
100 RequestToURIString(aRequest, getter_Copies(uriString));
101 nsString tmpString;
102 tmpString.AssignWithConversion(uriString);
104 if( ( aStateFlags & STATE_IS_REQUEST ) && NS_FAILED( aStatus ) )
106 PtWebErrorCallback_t cbw;
108 /* invoke the Pt_CB_WEB_ERROR in the client */
109 cb = moz->web_error_cb;
110 memset(&cbinfo, 0, sizeof(cbinfo));
111 cbinfo.reason = Pt_CB_MOZ_ERROR;
112 cbinfo.cbdata = &cbw;
114 memset( &cbw, 0, sizeof( PtWebErrorCallback_t ) );
115 cbw.url = (char*)((const char *)uriString);
116 cbw.description = "";
118 cbw.type = Pt_WEB_ERROR_TOPVIEW;
119 switch( aStatus )
121 case NS_ERROR_UNKNOWN_HOST:
122 cbw.reason = -12;
123 break;
124 case NS_ERROR_NET_TIMEOUT:
125 cbw.reason = -408;
126 break;
127 case NS_ERROR_NO_CONTENT:
128 cbw.reason = -8;
129 break;
130 case NS_ERROR_FILE_NOT_FOUND:
131 cbw.reason = -404;
132 break;
133 case NS_ERROR_CONNECTION_REFUSED:
134 cbw.reason = -13;
135 break;
136 default:
137 cbw.reason = -1;
138 break;
140 if( cbw.reason != -1)
141 PtInvokeCallbackList(cb, (PtWidget_t *)moz, &cbinfo);
142 /* let it check for STATE_STOP */
145 memset(&cbinfo, 0, sizeof(cbinfo));
147 if( aStateFlags & STATE_IS_NETWORK /* STATE_IS_REQUEST STATE_IS_DOCUMENT */ )
149 if( aStateFlags & STATE_START )
151 cbinfo.reason = Pt_CB_MOZ_START;
152 if( ( cb = moz->start_cb ) )
153 PtInvokeCallbackList(cb, (PtWidget_t *) moz, &cbinfo);
155 else if( aStateFlags & STATE_STOP )
157 PtWebCompleteCallback_t cbcomplete;
159 cbinfo.reason = Pt_CB_MOZ_COMPLETE;
160 cbinfo.cbdata = &cbcomplete;
161 memset( &cbcomplete, 0, sizeof( PtWebCompleteCallback_t ) );
162 cbcomplete.url = strdup( uriString );
164 if( ( cb = moz->complete_cb ) )
165 PtInvokeCallbackList(cb, (PtWidget_t *) moz, &cbinfo);
167 free( cbcomplete.url );
169 if( moz->text_zoom != moz->actual_text_zoom ) {
170 nsCOMPtr<nsIDOMWindow> domWindow;
171 moz->EmbedRef->mWindow->mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
172 if(domWindow) {
173 domWindow->SetTextZoom( moz->text_zoom/100. );
174 float vv;
175 domWindow->GetTextZoom( &vv );
176 moz->actual_text_zoom = (int) ( vv * 100 );
182 // invoke the raw status callbacks for page load status
183 cbinfo.reason = Pt_CB_MOZ_NET_STATE;
184 cbinfo.cbdata = &state;
185 state.flags = aStateFlags;
186 state.status = aStatus;
187 //state.url = (const char *)uriString;
188 state.url = NULL;
189 char *statusMessage = "";
190 PRInt32 flags = aStateFlags;
192 if (flags & STATE_IS_REQUEST)
194 if (flags & STATE_REDIRECTING)
195 statusMessage = "Redirecting to site:";
196 else if (flags & STATE_TRANSFERRING)
197 statusMessage = "Receiving Data:";
198 else if (flags & STATE_NEGOTIATING)
199 statusMessage = "Waiting for authorization:";
202 if (flags & STATE_IS_DOCUMENT)
204 if (flags & STATE_START)
205 statusMessage = "Loading site:";
206 else if (flags & STATE_STOP)
207 statusMessage = "Finishing:";
209 state.message = statusMessage;
210 if( ( cb = moz->net_state_cb ) && statusMessage[0])
211 PtInvokeCallbackList(cb, (PtWidget_t *) moz, &cbinfo);
212 return NS_OK;
215 NS_IMETHODIMP
216 EmbedProgress::OnProgressChange(nsIWebProgress *aWebProgress,
217 nsIRequest *aRequest,
218 PRInt32 aCurSelfProgress,
219 PRInt32 aMaxSelfProgress,
220 PRInt32 aCurTotalProgress,
221 PRInt32 aMaxTotalProgress)
223 PtMozillaWidget_t *moz = (PtMozillaWidget_t *) mOwner->mOwningWidget;
224 PtCallbackList_t *cb;
225 PtCallbackInfo_t cbinfo;
226 PtMozillaProgressCb_t prog;
227 PRInt32 cur, max;
228 nsXPIDLCString uriString;
229 if (!moz->progress_cb)
230 return NS_OK;
232 cur = aCurTotalProgress;
233 max = aMaxTotalProgress;
235 if (cur > max && max != -1 && max != 0)
236 cur = max; // Progress complete
238 cb = moz->progress_cb;
239 memset(&cbinfo, 0, sizeof(cbinfo));
240 cbinfo.reason = Pt_CB_MOZ_PROGRESS;
241 cbinfo.cbdata = &prog;
243 prog.cur = cur;
244 prog.max = max;
245 PtInvokeCallbackList(cb, (PtWidget_t *)moz, &cbinfo);
247 return NS_OK;
250 NS_IMETHODIMP
251 EmbedProgress::OnLocationChange(nsIWebProgress *aWebProgress,
252 nsIRequest *aRequest,
253 nsIURI *aLocation)
255 NS_ENSURE_ARG_POINTER(aLocation);
257 PtMozillaWidget_t *moz = (PtMozillaWidget_t *) mOwner->mOwningWidget;
258 PtCallbackList_t *cb = NULL;
259 PtCallbackInfo_t cbinfo;
260 PtMozillaUrlCb_t url;
261 nsCAutoString newURI;
263 aLocation->GetSpec(newURI);
264 mOwner->SetURI(newURI.get());
266 if (!moz->url_cb)
267 return NS_OK;
269 memset(&cbinfo, 0, sizeof(cbinfo));
270 cbinfo.cbdata = &url;
271 cbinfo.reason = Pt_CB_MOZ_URL;
272 url.url = (char *) newURI.get();
273 // aLocation->GetSpec(&(url.url));
274 cb = moz->url_cb;
275 PtInvokeCallbackList(cb, (PtWidget_t *) moz, &cbinfo);
277 return NS_OK;
280 NS_IMETHODIMP
281 EmbedProgress::OnStatusChange(nsIWebProgress *aWebProgress,
282 nsIRequest *aRequest,
283 nsresult aStatus,
284 const PRUnichar *aMessage)
286 PtMozillaWidget_t *moz = (PtMozillaWidget_t *) mOwner->mOwningWidget;
287 PtCallbackList_t *cb;
288 PtCallbackInfo_t cbinfo;
289 PtMozillaInfoCb_t info;
290 nsAutoString Text ( aMessage );
292 if (!moz->info_cb)
293 return NS_OK;
295 memset(&cbinfo, 0, sizeof(cbinfo));
296 cbinfo.cbdata = &info;
297 cbinfo.reason = Pt_CB_MOZ_INFO;
298 cb = moz->info_cb;
300 const char* msg = ToNewCString(Text);
301 info.type = Pt_MOZ_INFO_CONNECT;
302 info.status = 0;
303 info.data = (char *)msg;
304 PtInvokeCallbackList(cb, (PtWidget_t *) moz, &cbinfo);
306 nsMemory::Free( (void*)msg );
307 return NS_OK;
310 NS_IMETHODIMP
311 EmbedProgress::OnSecurityChange(nsIWebProgress *aWebProgress,
312 nsIRequest *aRequest,
313 PRUint32 aState)
315 return NS_OK;
318 /* static */
319 void
320 EmbedProgress::RequestToURIString(nsIRequest *aRequest, char **aString)
322 // is it a channel
323 nsCOMPtr<nsIChannel> channel;
324 channel = do_QueryInterface(aRequest);
325 if (!channel)
326 return;
328 nsCOMPtr<nsIURI> uri;
329 channel->GetURI(getter_AddRefs(uri));
330 if (!uri)
331 return;
333 nsCAutoString uriString;
334 uri->GetSpec(uriString);
336 *aString = strdup(uriString.get());