1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla code.
18 * The Initial Developer of the Original Code is the Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
23 * Chris Double <chris.double@double.co.nz>
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 ***** */
38 #include "nsAString.h"
39 #include "nsNetUtil.h"
40 #include "nsMediaDecoder.h"
41 #include "nsIScriptSecurityManager.h"
42 #include "nsChannelToPipeListener.h"
43 #include "nsICachingChannel.h"
45 #define HTTP_OK_CODE 200
46 #define HTTP_PARTIAL_RESPONSE_CODE 206
48 nsChannelToPipeListener::nsChannelToPipeListener(
49 nsMediaDecoder
* aDecoder
,
59 nsresult
nsChannelToPipeListener::Init()
61 nsresult rv
= NS_NewPipe(getter_AddRefs(mInput
),
62 getter_AddRefs(mOutput
),
65 NS_ENSURE_SUCCESS(rv
, rv
);
70 void nsChannelToPipeListener::Stop()
77 void nsChannelToPipeListener::Cancel()
86 double nsChannelToPipeListener::BytesPerSecond() const
88 return mOutput
? mTotalBytes
/ ((PR_IntervalToMilliseconds(mIntervalEnd
-mIntervalStart
)) / 1000.0) : NS_MEDIA_UNKNOWN_RATE
;
91 nsresult
nsChannelToPipeListener::GetInputStream(nsIInputStream
** aStream
)
93 NS_IF_ADDREF(*aStream
= mInput
);
97 nsresult
nsChannelToPipeListener::OnStartRequest(nsIRequest
* aRequest
, nsISupports
* aContext
)
99 mIntervalStart
= PR_IntervalNow();
100 mIntervalEnd
= mIntervalStart
;
102 mDecoder
->UpdateBytesDownloaded(mTotalBytes
);
103 nsCOMPtr
<nsIHttpChannel
> hc
= do_QueryInterface(aRequest
);
105 PRUint32 responseStatus
= 0;
106 hc
->GetResponseStatus(&responseStatus
);
107 if (mSeeking
&& responseStatus
== HTTP_OK_CODE
) {
108 // If we get an OK response but we were seeking,
109 // and therefore expecting a partial response of
110 // HTTP_PARTIAL_RESPONSE_CODE, tell the decoder
111 // we don't support seeking.
112 mDecoder
->SetSeekable(PR_FALSE
);
114 else if (!mSeeking
&&
115 (responseStatus
== HTTP_OK_CODE
||
116 responseStatus
== HTTP_PARTIAL_RESPONSE_CODE
)) {
117 // We weren't seeking and got a valid response status,
118 // set the length of the content.
120 hc
->GetContentLength(&cl
);
121 mDecoder
->SetTotalBytes(cl
);
123 // If we get an HTTP_OK_CODE response to our byte range
124 // request, then we don't support seeking.
125 mDecoder
->SetSeekable(responseStatus
== HTTP_PARTIAL_RESPONSE_CODE
);
129 nsCOMPtr
<nsICachingChannel
> cc
= do_QueryInterface(aRequest
);
131 PRBool fromCache
= PR_FALSE
;
132 nsresult rv
= cc
->IsFromCache(&fromCache
);
134 if (NS_SUCCEEDED(rv
) && !fromCache
) {
135 cc
->SetCacheAsFile(PR_TRUE
);
139 /* Get our principal */
140 nsCOMPtr
<nsIChannel
> chan(do_QueryInterface(aRequest
));
142 nsCOMPtr
<nsIScriptSecurityManager
> secMan
=
143 do_GetService("@mozilla.org/scriptsecuritymanager;1");
145 nsresult rv
= secMan
->GetChannelPrincipal(chan
,
146 getter_AddRefs(mPrincipal
));
156 nsresult
nsChannelToPipeListener::OnStopRequest(nsIRequest
* aRequest
, nsISupports
* aContext
, nsresult aStatus
)
159 if (aStatus
!= NS_BINDING_ABORTED
&& mDecoder
) {
160 if (NS_SUCCEEDED(aStatus
)) {
161 mDecoder
->ResourceLoaded();
163 mDecoder
->NetworkError();
169 nsresult
nsChannelToPipeListener::OnDataAvailable(nsIRequest
* aRequest
,
170 nsISupports
* aContext
,
171 nsIInputStream
* aStream
,
176 return NS_ERROR_FAILURE
;
181 nsresult rv
= mOutput
->WriteFrom(aStream
, aCount
, &bytes
);
182 NS_ENSURE_SUCCESS(rv
, rv
);
185 mTotalBytes
+= bytes
;
186 mDecoder
->UpdateBytesDownloaded(mTotalBytes
);
189 nsresult rv
= mOutput
->Flush();
190 NS_ENSURE_SUCCESS(rv
, rv
);
191 mIntervalEnd
= PR_IntervalNow();
196 nsChannelToPipeListener::GetCurrentPrincipal()
201 NS_IMPL_THREADSAFE_ISUPPORTS2(nsChannelToPipeListener
, nsIRequestObserver
, nsIStreamListener
)