aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/EphyHeaderSniffer.cpp
blob: d65e1390195a383507064c4d81b6103db4a6549b (plain) (blame)
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
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Chimera code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2002
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   David Hyatt  <hyatt@netscape.com>
 *   Simon Fraser <sfraser@netscape.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK *****
 *
 * $Id$
 */

#include "mozilla-config.h"

#include "config.h"

#include "MozDownload.h"
#include "EphyHeaderSniffer.h"

#include "ephy-file-chooser.h"
#include "ephy-prefs.h"
#include "ephy-gui.h"
#include "eel-gconf-extensions.h"
#include "ephy-debug.h"

#include <glib/gi18n.h>
#include <libgnomevfs/gnome-vfs-utils.h>

#include <nsIChannel.h>
#include <nsIHttpChannel.h>
#include <nsIURL.h>
#include <nsIPrefService.h>
#include <nsIMIMEService.h>
#include <nsIMIMEInfo.h>
#include <nsIDOMHTMLDocument.h>
#include <nsIDownload.h>
#include <nsIMIMEHeaderParam.h>
#include <nsIWindowWatcher.h>

EphyHeaderSniffer::EphyHeaderSniffer (nsIWebBrowserPersist* aPersist, MozillaEmbedPersist *aEmbedPersist,
        nsIFile* aFile, nsIURI* aURL, nsIDOMDocument* aDocument, nsIInputStream* aPostData,
        EphyEmbedSingle *single)
: mPersist(aPersist)
, mEmbedPersist(aEmbedPersist)
, mTmpFile(aFile)
, mURL(aURL)
, mOriginalURI(nsnull)
, mDocument(aDocument)
, mPostData(aPostData)
{
        LOG ("EphyHeaderSniffer ctor (%p)", this)

        nsCOMPtr<nsIWindowWatcher> watcher
                (do_GetService("@mozilla.org/embedcomp/window-watcher;1"));
    if (!watcher) return;

    watcher->GetNewAuthPrompter (nsnull, getter_AddRefs (mAuthPrompt));

    mSingle = single;
    g_object_add_weak_pointer (G_OBJECT (mSingle),
                   (gpointer *)&mSingle);
}

EphyHeaderSniffer::~EphyHeaderSniffer()
{
    LOG ("EphyHeaderSniffer dtor (%p)", this)

    if (mSingle)
    {
        g_object_remove_weak_pointer (G_OBJECT (mSingle),
                          (gpointer *)&mSingle);
    }
}

NS_IMPL_ISUPPORTS2(EphyHeaderSniffer, nsIWebProgressListener, nsIAuthPrompt)

NS_IMETHODIMP
EphyHeaderSniffer::HandleContent ()
{
    gboolean handled = FALSE;
    nsEmbedCString uriSpec;

    if (mPostData || !mSingle) return NS_ERROR_FAILURE;

    mURL->GetSpec (uriSpec);
    
    g_signal_emit_by_name (mSingle, "handle_content", mContentType.get(),
                   uriSpec.get(), &handled);

    return handled ? NS_OK : NS_ERROR_FAILURE;
}

NS_IMETHODIMP 
EphyHeaderSniffer::OnStateChange (nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, 
                                  PRUint32 aStatus)
{  
    if (aStateFlags & nsIWebProgressListener::STATE_START)
    {
        /* be sure to keep it alive while we save since it owns
           us as a listener and keep ourselves alive */
        nsCOMPtr<nsIWebBrowserPersist> kungFuDeathGrip(mPersist);
        nsCOMPtr<nsIWebProgressListener> kungFuSuicideGrip(this);
    
        nsresult rv;
        nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest, &rv);
        if (!channel) return rv;
        channel->GetContentType(mContentType);
    
        nsCOMPtr<nsIURI> origURI;
        channel->GetOriginalURI(getter_AddRefs(origURI));
    
        nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
        if (httpChannel)
        {
            httpChannel->GetResponseHeader(nsEmbedCString("content-disposition"),
                               mContentDisposition);
        }
    
        mPersist->CancelSave();

        PRBool exists;
        mTmpFile->Exists(&exists);
        if (exists)
        {
            mTmpFile->Remove(PR_FALSE);
        }

        rv = HandleContent ();
        if (NS_SUCCEEDED (rv)) return NS_OK;

        rv = PerformSave(origURI);
        if (NS_FAILED(rv))
        {
            /* FIXME put up some UI */
      
        }
    }

    return NS_OK;
}

NS_IMETHODIMP 
EphyHeaderSniffer::OnProgressChange (nsIWebProgress *aWebProgress, 
                                     nsIRequest *aRequest, 
                                     PRInt32 aCurSelfProgress, 
                                     PRInt32 aMaxSelfProgress, 
                                     PRInt32 aCurTotalProgress, 
                                     PRInt32 aMaxTotalProgress)
{
    return NS_OK;
}

NS_IMETHODIMP 
EphyHeaderSniffer::OnLocationChange (nsIWebProgress *aWebProgress, 
                     nsIRequest *aRequest, 
                     nsIURI *location)
{
    return NS_OK;
}

NS_IMETHODIMP 
EphyHeaderSniffer::OnStatusChange (nsIWebProgress *aWebProgress, 
                   nsIRequest *aRequest, 
                   nsresult aStatus, 
                   const PRUnichar *aMessage)
{
    return NS_OK;
}

NS_IMETHODIMP 
EphyHeaderSniffer::OnSecurityChange (nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
{
    return NS_OK;
}

static void
filechooser_response_cb (EphyFileChooser *dialog, gint response, EphyHeaderSniffer* sniffer)
{
    if (response == GTK_RESPONSE_ACCEPT)
    {
        char *filename;
        GtkWidget *parent = NULL; // FIXME!

        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));

        if (ephy_gui_confirm_overwrite_file (parent, filename) == FALSE)
        {
            g_free (filename);
            return;
        }

        nsCOMPtr<nsILocalFile> destFile = do_CreateInstance (NS_LOCAL_FILE_CONTRACTID);
        if (destFile)
        {
            destFile->InitWithNativePath (nsEmbedCString (filename));

            sniffer->InitiateDownload (destFile);
        }

        g_free (filename);
    }

    // FIXME how to inform user of failed save ?

    gtk_widget_destroy (GTK_WIDGET (dialog));
}

nsresult EphyHeaderSniffer::PerformSave (nsIURI* inOriginalURI)
{
    nsresult rv;
    EmbedPersistFlags flags;
    PRBool askDownloadDest;

    mOriginalURI = inOriginalURI;

    flags = ephy_embed_persist_get_flags (EPHY_EMBED_PERSIST (mEmbedPersist));
    askDownloadDest = flags & EMBED_PERSIST_ASK_DESTINATION;

    nsEmbedString defaultFileName;

    if (!defaultFileName.Length() && mContentDisposition.Length())
    {
        /* 1 Use the HTTP header suggestion. */
        nsCOMPtr<nsIMIMEHeaderParam> mimehdrpar =
            do_GetService("@mozilla.org/network/mime-hdrparam;1");     
        
        if (mimehdrpar)
        {
            nsEmbedCString fallbackCharset;
            if (mURL)
            {
                mURL->GetOriginCharset(fallbackCharset);
            }
            
            nsEmbedString fileName;
            
            rv = mimehdrpar->GetParameter (mContentDisposition, "filename",
                               fallbackCharset, PR_TRUE, nsnull,
                               fileName);
            if (NS_FAILED(rv) || !fileName.Length())
            {
                rv = mimehdrpar->GetParameter (mContentDisposition, "name",
                                   fallbackCharset, PR_TRUE, nsnull,
                                   fileName);
            }

            if (NS_SUCCEEDED(rv) && fileName.Length())
            {
                defaultFileName = fileName;
            }
        }
    }
    
    if (!defaultFileName.Length())
    {
        /* 2 For file URLs, use the file name. */

        nsCOMPtr<nsIURL> url(do_QueryInterface(mURL));
        if (url)
        {
            nsEmbedCString fileNameCString;
            url->GetFileName(fileNameCString);
            NS_CStringToUTF16 (fileNameCString, NS_CSTRING_ENCODING_UTF8,
                       defaultFileName);
        }
    }
    
    if (!defaultFileName.Length() && mDocument)
    {
        /* 3 Use the title of the document. */

        nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(mDocument));
        if (htmlDoc)
        {
            htmlDoc->GetTitle(defaultFileName);
        }
    }
    
    if (!defaultFileName.Length() && mURL)
    {
        /* 4 Use the host. */
        nsEmbedCString hostName;
        mURL->GetHost(hostName);
        NS_CStringToUTF16 (hostName, NS_CSTRING_ENCODING_UTF8,
                   defaultFileName);
    }
    
    /* 5 One last case to handle about:blank and other untitled pages. */
    if (!defaultFileName.Length())
    {
        NS_CStringToUTF16 (nsEmbedCString(_("Untitled")),
                   NS_CSTRING_ENCODING_UTF8, defaultFileName);
    }
        
    /* Validate the file name to ensure legality. */
    nsEmbedCString cDefaultFileName;
    NS_UTF16ToCString (defaultFileName, NS_CSTRING_ENCODING_UTF8,
               cDefaultFileName);
    char *default_name = g_strdup (cDefaultFileName.get());
    default_name = g_strdelimit (default_name, "/\\:", ' ');

    const char *key;
    key = ephy_embed_persist_get_persist_key (EPHY_EMBED_PERSIST (mEmbedPersist));

    /* FIXME: do better here by using nsITextToSubURI service, like in
     * http://lxr.mozilla.org/seamonkey/source/xpfe/communicator/resources/content/contentAreaUtils.js#763
     */
        char *filename;
        filename = gnome_vfs_unescape_string (default_name, NULL);

        if (!g_utf8_validate (filename, -1, NULL))
        {
                g_free (filename);
                filename = g_strdup (default_name);
        }

    g_free (default_name);

    if (askDownloadDest)
    {
        EphyFileChooser *dialog;
        GtkWindow *window;
        const char *title;

        title = ephy_embed_persist_get_fc_title (EPHY_EMBED_PERSIST (mEmbedPersist));
        window = ephy_embed_persist_get_fc_parent (EPHY_EMBED_PERSIST (mEmbedPersist));

        dialog = ephy_file_chooser_new (title ? title: _("Save"),
                        GTK_WIDGET (window),
                        GTK_FILE_CHOOSER_ACTION_SAVE,
                        key ? key : CONF_STATE_SAVE_DIR,
                        EPHY_FILE_FILTER_ALL_SUPPORTED);

        gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog),
                                                   filename);

        g_signal_connect (dialog, "response",
                  G_CALLBACK (filechooser_response_cb), this);

        gtk_widget_show (GTK_WIDGET (dialog));

                g_free (filename);
        return NS_OK;
    }
    /* FIXME: how to inform user of failed save ? */

    nsCOMPtr<nsILocalFile> destFile;
    BuildDownloadPath (filename, getter_AddRefs (destFile));
        g_free (filename);
    NS_ENSURE_TRUE (destFile, NS_ERROR_FAILURE);

    return InitiateDownload (destFile);
}

nsresult EphyHeaderSniffer::InitiateDownload (nsILocalFile *aDestFile)
{
    LOG ("Initiating download")

    return InitiateMozillaDownload (mDocument, mURL, aDestFile,
                    mContentType.get(), mOriginalURI, mEmbedPersist,
                    mPostData, nsnull, -1);
}