File: mozilla-embed-persist.cpp

package info (click to toggle)
galeon 2.0.2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 19,212 kB
  • ctags: 9,891
  • sloc: ansic: 77,793; cpp: 15,301; sh: 8,961; xml: 5,761; makefile: 888
file content (411 lines) | stat: -rw-r--r-- 10,719 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 *  Copyright (C) 2000, 2001, 2002 Marco Pesenti Gritti
 *
 *  This program 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, or (at your option)
 *  any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <nscore.h>
#define MOZILLA_INTERNAL_API
#include <nsIServiceManager.h>
#undef MOZILLA_INTERNAL_API

#include "ProgressListener.h"
#include "mozilla-embed-persist.h"
#include "mozilla-embed.h"
#include "GaleonWrapper.h"
#include "gul-general.h"
#include "HeaderSniffer.h"
#include "galeon-debug.h"
#include "GaleonUtils.h"
#include "GulString.h"

#include <stddef.h>
#include <nsIURIChecker.h>
#include <nsIHistoryEntry.h>
#include <nsISHEntry.h>
#include <nsISupportsPrimitives.h>
#include <nsIDOMDocument.h>
#include <nsIDOMNSDocument.h>
#include <nsIWebBrowser.h>
#include <nsIWebNavigation.h>
#include <nsISHistory.h>
#include <nsNetCID.h>
#include <nsILocalFile.h>
#include <nsIChannel.h>
#include <nsIIOService.h>
#include <nsIStreamListener.h>
#include <nsIURI.h>
#include <nsIInputStream.h>

struct MozillaEmbedPersistPrivate
{
	gpointer dummy;
};

#define MOZILLA_EMBED_PERSIST_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), \
					MOZILLA_TYPE_EMBED_PERSIST, MozillaEmbedPersistPrivate))

G_DEFINE_TYPE (MozillaEmbedPersist, mozilla_embed_persist, GALEON_TYPE_EMBED_PERSIST);

static gboolean
impl_save (GaleonEmbedPersist *persist)
{
	nsresult rv;
	char *filename;
	char *uri;
	long max_size;
	GaleonEmbed *embed;
	EmbedPersistFlags flags;

	/* FIXME implement max size */

	g_object_get (persist,
		      "source", &uri,        
		      "dest", &filename,
		      "flags", &flags,
		      "embed", &embed,
		      "max_size", &max_size,
		      NULL);

	g_return_val_if_fail (uri || embed, FALSE);

	GaleonWrapper *wrapper = NULL;
	if (embed)
	{
		/* Don't bother holding the ref to the embed, its not about
		 * to go away */
		g_object_unref (embed);

		wrapper = (GaleonWrapper *) mozilla_embed_get_galeon_wrapper (MOZILLA_EMBED(embed));
		NS_ENSURE_TRUE (wrapper, FALSE);
	}

	/* Get the uri to save from */
	nsCOMPtr<nsIURI> inURI;
	GulCString sURI;
	if (uri)
	{
		sURI.Assign (uri);
	}
	else if (flags & EMBED_PERSIST_MAINDOC)
	{
		rv = wrapper->GetMainDocumentUrl (sURI);
		NS_ENSURE_SUCCESS (rv, FALSE);
	}
	else
	{
		rv = wrapper->GetDocumentUrl (sURI);
		NS_ENSURE_SUCCESS (rv, FALSE);
	}
      	GaleonUtils::NewURI(getter_AddRefs(inURI), sURI);
	if (!inURI) return FALSE;

	/* If the URI wasn't specified, we are using the embed, so
	 * get the post data, DOM document and pageDescriptor */
	nsCOMPtr<nsIInputStream> postData;
	nsCOMPtr<nsIDOMDocument> DOMDocument;
	nsCOMPtr<nsISupports> pageDescriptor;
	if (!uri)
	{
		/* Post data */
		nsCOMPtr<nsISHistory> sessionHistory;
		nsCOMPtr<nsIHistoryEntry> entry;
		nsCOMPtr<nsIWebNavigation> webNav;
		PRInt32 sindex;

		webNav = do_QueryInterface(wrapper->mWebBrowser);
		webNav->GetSessionHistory(getter_AddRefs(sessionHistory));
		sessionHistory->GetIndex(&sindex);
		sessionHistory->GetEntryAtIndex(sindex, PR_FALSE, getter_AddRefs(entry));
		nsCOMPtr<nsISHEntry> shEntry(do_QueryInterface(entry));
		if (shEntry)
		{
			shEntry->GetPostData(getter_AddRefs(postData));
		}

		/* get the DOMDocument */
		if (flags & EMBED_PERSIST_MAINDOC)
		{
                	wrapper->GetMainDOMDocument (getter_AddRefs(DOMDocument));
		}
        	else
		{
                	wrapper->GetDOMDocument (getter_AddRefs(DOMDocument));
		}
        	NS_ENSURE_TRUE (DOMDocument, FALSE);

		/* and get the page descriptor */
	        wrapper->GetPageDescriptor(getter_AddRefs(pageDescriptor));
		/* Its not the end of the world if we don't get
		 * a pageDescriptor, mozilla doesn't even appear to use
		 * it */
	}

	/* Only use the save document code if it an appropriate mime type,
	 * we turn off the document save code, by just setting the
	 * DOMDocument to null */
	if (DOMDocument)
	{
		nsCOMPtr<nsIDOMNSDocument> doc = do_QueryInterface(DOMDocument);
		GulString contentType;
		if (doc)
		{
			doc->GetContentType (contentType);
		}

		if (!contentType.Equals (NS_LITERAL_STRING ("text/html")) &&
		    !contentType.Equals (NS_LITERAL_STRING ("text/xml"))  &&
		    !contentType.Equals (NS_LITERAL_STRING ("application/xhtml+xml")))
		{
			DOMDocument = nsnull;
		}
	}

	if (filename == NULL)
	{
		/* Create an header sniffer and do the save */
		HeaderSniffer* sniffer = new HeaderSniffer (persist, inURI,
							    DOMDocument, 
							    postData,
							    pageDescriptor);

		nsCOMPtr<nsIURIChecker> uri_checker = do_CreateInstance
			(NS_URICHECKER_CONTRACT_ID);

		rv = uri_checker->Init (inURI);
		if (NS_FAILED (rv)) return FALSE;

		PRInt32 checker_flags = 0;
		if (flags & EMBED_PERSIST_BYPASSCACHE)
		{
			checker_flags |= nsIRequest::LOAD_BYPASS_CACHE;
		}
		else
		{
			checker_flags |= nsIRequest::LOAD_FROM_CACHE;
		}
		
		uri_checker->SetLoadFlags (checker_flags);

		/* Start the checker, the sniffer will inspect
		 * the headers when it finishes */
		uri_checker->AsyncCheck (sniffer, nsnull);
 	}
	else
	{
		/* Filename to save to */
		nsCOMPtr<nsILocalFile> destFile;
		NS_NewNativeLocalFile (GulDependentCString (filename),
				       PR_TRUE, getter_AddRefs(destFile));
	        NS_ENSURE_TRUE (destFile, FALSE);

		rv =  InitiateMozillaDownload (inURI, destFile,
					       persist, DOMDocument,
					       pageDescriptor, postData,
					       PR_TRUE, inURI);
		if (NS_FAILED (rv)) return FALSE;
	}

	g_free (uri);
	g_free (filename);

	return TRUE;
}

// ------------------------------------------------------------

class GStreamListener : public nsIStreamListener
{
public:
	NS_DECL_ISUPPORTS
	NS_DECL_NSIREQUESTOBSERVER
	NS_DECL_NSISTREAMLISTENER

	GStreamListener (GaleonEmbedPersist *aEmbedPersist,
			 GaleonEmbedPersistStreamFunc aFunc,
			 gpointer aData) : 
		mEmbedPersist (aEmbedPersist), mFunc (aFunc), mData (aData)
	{
		LOG("[%p] GStreamListener()", this);

		g_object_ref (mEmbedPersist);
	}

private:
	~GStreamListener()
	{
		LOG("[%p] ~GStreamListener()", this);

		g_object_unref (mEmbedPersist);	
	}
	
	static NS_METHOD StreamReaderFunc (nsIInputStream* in, void* closure,
					   const char* fromRawSegment,
					   PRUint32 toOffset,  PRUint32 count,
					   PRUint32 *writeCount);

	GaleonEmbedPersist *mEmbedPersist;
	GaleonEmbedPersistStreamFunc mFunc;
	gpointer mData;
};

NS_IMPL_ISUPPORTS1(GStreamListener, nsIStreamListener)

NS_METHOD
GStreamListener::StreamReaderFunc (nsIInputStream* in, void* closure,
				   const char* fromRawSegment,
				   PRUint32 toOffset,  PRUint32 count,
				   PRUint32 *writeCount)
{
	GStreamListener *listener = (GStreamListener *)closure;

	if (! listener->mFunc (listener->mEmbedPersist, fromRawSegment, count,
			       listener->mData))
	{
		/* Caller is not interested any more */
		listener->mFunc = NULL;

		*writeCount = 0;
		return NS_ERROR_FAILURE;
	}

	*writeCount = count;
	return NS_OK;
}

NS_IMETHODIMP
GStreamListener::OnDataAvailable(nsIRequest *aRequest, nsISupports *aContext,
				 nsIInputStream *aInputStream, 
				 PRUint32 aOffset, PRUint32 aCount)
{
	LOG("[%p] OnDataAvailable(%d, %d)", this, aOffset, aCount);

	// OnDataAvailable is not supposed to be called again after returning
	// and error from a callback.  But it sometimes is...
	// https://bugzilla.mozilla.org/show_bug.cgi?id=290371
	NS_ENSURE_TRUE (mFunc, NS_ERROR_FAILURE);

	PRUint32 totalRead;
	nsresult rv;
	rv = aInputStream->ReadSegments (GStreamListener::StreamReaderFunc,
					 (void*)this, aCount, &totalRead);
	NS_ENSURE_SUCCESS (rv, rv);

	return mFunc ? NS_OK : NS_ERROR_FAILURE;
}

/* void onStartRequest (in nsIRequest aRequest, in nsISupports aContext); */
NS_IMETHODIMP
GStreamListener::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
{
	LOG("[%p] OnStartRequest()", this);
	return NS_OK;
}

/* void onStopRequest (in nsIRequest aRequest, in nsISupports aContext, in nsresult aStatusCode); */
NS_IMETHODIMP
GStreamListener::OnStopRequest(nsIRequest *aRequest, nsISupports *aContext, nsresult aStatusCode)
{
	LOG("[%p] OnStopRequest()", this);

	if (NS_SUCCEEDED(aStatusCode) && mFunc)
	{
		g_signal_emit_by_name (mEmbedPersist, "completed");
	}

	return NS_OK;
}


static gboolean
impl_stream (GaleonEmbedPersist *persist, GaleonEmbedPersistStreamFunc func,
	     gpointer data)
{
	nsresult rv;
	char *source;
	GaleonEmbed *embed;
	EmbedPersistFlags flags;

	g_return_val_if_fail (GALEON_IS_EMBED_PERSIST (persist), FALSE);
	g_return_val_if_fail (func, FALSE);

	/* FIXME implement flags */

	g_object_get (persist,
		      "source", &source,
		      "flags", &flags,
		      "embed", &embed,
		      NULL);

	nsCOMPtr<nsIIOService> ioService;
	rv = GaleonUtils::GetIOService (getter_AddRefs (ioService));
	NS_ENSURE_SUCCESS (rv, rv);

	nsCOMPtr<nsIChannel> channel;
	rv = ioService->NewChannel (GulDependentCString (source), nsnull, nsnull, 
				    getter_AddRefs (channel));
	NS_ENSURE_SUCCESS (rv, rv);
	if (NS_FAILED (rv)) return FALSE;

	// referrer?
	nsCOMPtr<nsIStreamListener> listener = new GStreamListener (persist, func, data);
	rv = channel->AsyncOpen(listener, nsnull);
	if (NS_FAILED (rv)) return FALSE;

	return TRUE;
}

static void
mozilla_embed_persist_init (MozillaEmbedPersist *persist)
{
	persist->priv = MOZILLA_EMBED_PERSIST_GET_PRIVATE (persist);
}

static void
mozilla_embed_persist_finalize (GObject *object)
{
	G_OBJECT_CLASS (mozilla_embed_persist_parent_class)->finalize (object);
}

static void
mozilla_embed_persist_class_init (MozillaEmbedPersistClass *klass)
{
	GObjectClass *object_class;
	GaleonEmbedPersistClass *persist_class;
	
	persist_class = GALEON_EMBED_PERSIST_CLASS (klass);
	object_class  = G_OBJECT_CLASS (klass);

	object_class->finalize = mozilla_embed_persist_finalize;
	persist_class->save = impl_save;
	persist_class->stream = impl_stream;

	g_type_class_add_private (object_class, sizeof (MozillaEmbedPersistPrivate));
}

GaleonEmbedPersist *
galeon_embed_persist_new (GaleonEmbed *embed)
{
	GaleonEmbedPersist *persist;
	
	persist = GALEON_EMBED_PERSIST (g_object_new (MOZILLA_TYPE_EMBED_PERSIST,
						      "embed", embed, NULL));

	return persist;
}