Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/browser/GeckoDownload.cpp
blob: 6f099bdb9db57918b67b4005a38579de06338197 (plain)
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
#include "config.h"

#include <nsIFactory.h>
#include <nsIFile.h>
#include <nsIFileURL.h>
#include <nsServiceManagerUtils.h>
#include <nsIMIMEService.h>

#include "sugar-download-manager.h"

#include "GeckoDownload.h"

#define APPLICATION_OCTET_STREAM "application/octet-stream"

class GeckoDownload : public nsITransfer
{
public:
    GeckoDownload();
	virtual ~GeckoDownload();

	NS_DECL_ISUPPORTS
	NS_DECL_NSIWEBPROGRESSLISTENER
	NS_DECL_NSIWEBPROGRESSLISTENER2
	NS_DECL_NSITRANSFER

protected:
	nsIURI			*mSource;
	nsCString		mTargetFileName;
	nsIMIMEInfo		*mMIMEInfo;
	nsILocalFile	*mTempFile;
};

GeckoDownload::GeckoDownload ()
{
}

GeckoDownload::~GeckoDownload ()
{
}

NS_IMPL_ISUPPORTS3 (GeckoDownload,
					nsIWebProgressListener,
					nsIWebProgressListener2,
					nsITransfer)

NS_IMETHODIMP
GeckoDownload::Init (nsIURI *aSource,
					 nsIURI *aTarget,
					 const nsAString &aDisplayName,
					 nsIMIMEInfo *aMIMEInfo,
					 PRTime aStartTime,
					 nsILocalFile *aTempFile,
					 nsICancelable *aCancelable)
{
    mSource = aSource;
    mMIMEInfo = aMIMEInfo;
    mTempFile = aTempFile;

    nsresult rv;

    nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aTarget);
    NS_ENSURE_TRUE(fileURL, NS_ERROR_FAILURE);

    nsCOMPtr<nsIFile> file;
    rv = fileURL->GetFile(getter_AddRefs(file));
    NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);

    file->GetNativePath (mTargetFileName);

    return NS_OK; 
}

NS_IMETHODIMP 
GeckoDownload::OnStateChange(nsIWebProgress *aWebProgress,
	                          nsIRequest *aRequest,
                              PRUint32 aStateFlags,
                              nsresult aStatus)
{
    SugarDownloadManager *download_manager = sugar_get_download_manager();

    if(aStateFlags == STATE_START) {

        nsCString url;
        nsCString mimeType;

        mMIMEInfo->GetMIMEType(mimeType);
        mSource->GetSpec(url);

#ifdef HAVE_GECKO_1_9
        /* If the file is application/octet-stream, look up a better mime type
           from the extension. */
        if(mimeType.Equals(APPLICATION_OCTET_STREAM)) {
            nsresult rv;
            nsCOMPtr<nsIMIMEService> mimeService(
                do_GetService("@mozilla.org/mime;1", &rv));
            NS_ENSURE_SUCCESS(rv, rv);
            
            const char *fileExt = strrchr(mTargetFileName.get(), '.');
            if(fileExt) {
                nsCString contentType;

                mimeService->GetTypeFromExtension(nsCString(fileExt),
                                                   contentType);
                if(!contentType.IsEmpty()) {
                    mimeType = contentType;
                }
            }
        }
#endif

        sugar_download_manager_download_started(download_manager,
                                                 url.get(),
                                                 mimeType.get(),
                                                 mTargetFileName.get());

    } else if(aStateFlags == STATE_STOP) {
        
        if(NS_SUCCEEDED(aStatus)) {
            sugar_download_manager_download_completed(download_manager,
                                                       mTargetFileName.get());
        } else {
            sugar_download_manager_download_cancelled(download_manager,
                                                       mTargetFileName.get());
        }
    }

    return NS_OK; 
}

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

NS_IMETHODIMP
GeckoDownload::OnProgressChange64 (nsIWebProgress *aWebProgress,
								   nsIRequest *aRequest,
								   PRInt64 aCurSelfProgress,
								   PRInt64 aMaxSelfProgress,
								   PRInt64 aCurTotalProgress,
								   PRInt64 aMaxTotalProgress)
{	
	SugarDownloadManager *download_manager = sugar_get_download_manager ();
	PRInt32 percentComplete =
		(PRInt32)(((float)aCurSelfProgress / (float)aMaxSelfProgress) * 100.0);

	sugar_download_manager_update_progress (download_manager,
											mTargetFileName.get (),
											percentComplete);

	return NS_OK;
}

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

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

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

#ifdef HAVE_GECKO_1_9

NS_IMETHODIMP
GeckoDownload::OnRefreshAttempted (nsIWebProgress *aWebProgress,
                                   nsIURI *aRefreshURI,
                                   PRInt32 aMillis,
                                   PRBool aSameURI,
                                   PRBool *_retval)
{
	return NS_OK;
}

#endif

//*****************************************************************************
// GeckoDownloadFactory
//*****************************************************************************

class GeckoDownloadFactory : public nsIFactory {
public:
    NS_DECL_ISUPPORTS
    NS_DECL_NSIFACTORY

    GeckoDownloadFactory();
    virtual ~GeckoDownloadFactory();
};

//*****************************************************************************

NS_IMPL_ISUPPORTS1(GeckoDownloadFactory, nsIFactory)

GeckoDownloadFactory::GeckoDownloadFactory() {
}

GeckoDownloadFactory::~GeckoDownloadFactory() {
}

NS_IMETHODIMP
GeckoDownloadFactory::CreateInstance(nsISupports *aOuter, const nsIID & aIID, void **aResult)
{
    NS_ENSURE_ARG_POINTER(aResult);

    *aResult = NULL;
    GeckoDownload *inst = new GeckoDownload;
    if (!inst)
        return NS_ERROR_OUT_OF_MEMORY;

    nsresult rv = inst->QueryInterface(aIID, aResult);
    if (rv != NS_OK) {
        // We didn't get the right interface, so clean up
        delete inst;
    }

    return rv;
}

NS_IMETHODIMP
GeckoDownloadFactory::LockFactory(PRBool lock)
{
    return NS_OK;
}

//*****************************************************************************

nsresult
NS_NewGeckoDownloadFactory(nsIFactory** aFactory)
{
    NS_ENSURE_ARG_POINTER(aFactory);
    *aFactory = nsnull;

    GeckoDownloadFactory *result = new GeckoDownloadFactory;
    if (!result)
        return NS_ERROR_OUT_OF_MEMORY;

    NS_ADDREF(result);
    *aFactory = result;

    return NS_OK;
}