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
14 * The Original Code is Mozilla.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2003
19 * the Initial Developer. All Rights Reserved.
22 * Darin Fisher <darin@netscape.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsDownloader.h"
39 #include "nsICachingChannel.h"
40 #include "nsIInputStream.h"
41 #include "nsDirectoryServiceUtils.h"
42 #include "nsDirectoryServiceDefs.h"
43 #include "nsNetUtil.h"
45 // XXX this code is ripped from profile/src/nsProfile.cpp and is further
46 // duplicated in uriloader/exthandler. this should probably be moved
47 // into xpcom or some other shared library.
50 static const char table
[] =
51 { 'a','b','c','d','e','f','g','h','i','j',
52 'k','l','m','n','o','p','q','r','s','t',
53 'u','v','w','x','y','z','0','1','2','3',
54 '4','5','6','7','8','9' };
56 MakeRandomString(char *buf
, PRInt32 bufLen
)
58 // turn PR_Now() into milliseconds since epoch
59 // and salt rand with that.
61 LL_L2D(fpTime
, PR_Now());
62 srand((uint
)(fpTime
* 1e-6 + 0.5)); // use 1e-6, granularity of PR_Now() on the mac is seconds
65 for (i
=0;i
<bufLen
;i
++) {
66 *buf
++ = table
[rand()%TABLE_SIZE
];
72 nsDownloader::~nsDownloader()
74 if (mLocation
&& mLocationIsTemp
) {
75 // release the sink first since it may still hold an open file
76 // descriptor to mLocation. this needs to happen before the
77 // file can be removed otherwise the Remove call will fail.
83 nsresult rv
= mLocation
->Remove(PR_FALSE
);
85 NS_ERROR("unable to remove temp file");
89 NS_IMPL_ISUPPORTS3(nsDownloader
,
95 nsDownloader::Init(nsIDownloadObserver
*observer
, nsIFile
*location
)
103 nsDownloader::OnStartRequest(nsIRequest
*request
, nsISupports
*ctxt
)
105 nsresult rv
= NS_ERROR_FAILURE
;
107 nsCOMPtr
<nsICachingChannel
> caching
= do_QueryInterface(request
, &rv
);
108 if (NS_SUCCEEDED(rv
))
109 rv
= caching
->SetCacheAsFile(PR_TRUE
);
112 // OK, we will need to stream the data to disk ourselves. Make
113 // sure mLocation exists.
115 rv
= NS_GetSpecialDirectory(NS_OS_TEMP_DIR
, getter_AddRefs(mLocation
));
116 if (NS_FAILED(rv
)) return rv
;
119 MakeRandomString(buf
, 8);
120 memcpy(buf
+8, ".tmp", 5);
121 rv
= mLocation
->AppendNative(nsDependentCString(buf
, 12));
122 if (NS_FAILED(rv
)) return rv
;
124 rv
= mLocation
->CreateUnique(nsIFile::NORMAL_FILE_TYPE
, 0600);
125 if (NS_FAILED(rv
)) return rv
;
127 mLocationIsTemp
= PR_TRUE
;
130 rv
= NS_NewLocalFileOutputStream(getter_AddRefs(mSink
), mLocation
);
131 if (NS_FAILED(rv
)) return rv
;
133 // we could wrap this output stream with a buffered output stream,
134 // but it shouldn't be necessary since we will be writing large
135 // chunks given to us via OnDataAvailable.
141 nsDownloader::OnStopRequest(nsIRequest
*request
,
145 if (!mSink
&& NS_SUCCEEDED(status
)) {
146 nsCOMPtr
<nsICachingChannel
> caching
= do_QueryInterface(request
, &status
);
147 if (NS_SUCCEEDED(status
)) {
148 status
= caching
->GetCacheFile(getter_AddRefs(mLocation
));
149 if (NS_SUCCEEDED(status
)) {
150 NS_ASSERTION(mLocation
, "success without a cache file");
151 // ok, then we need to hold a reference to the cache token in
152 // order to ensure that the cache file remains valid until we
154 caching
->GetCacheToken(getter_AddRefs(mCacheToken
));
163 mObserver
->OnDownloadComplete(this, request
, ctxt
, status
, mLocation
);
170 nsDownloader::ConsumeData(nsIInputStream
* in
,
172 const char* fromRawSegment
,
175 PRUint32
*writeCount
)
177 nsDownloader
*self
= (nsDownloader
*) closure
;
179 return self
->mSink
->Write(fromRawSegment
, count
, writeCount
);
186 nsDownloader::OnDataAvailable(nsIRequest
*request
, nsISupports
*ctxt
,
187 nsIInputStream
*inStr
,
188 PRUint32 sourceOffset
, PRUint32 count
)
191 return inStr
->ReadSegments(ConsumeData
, this, count
, &n
);