Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / content / media / video / src / nsChannelToPipeListener.cpp
blob4eb2afaeca25ad4f3d57696404fd9c744b6e4960
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
14 * License.
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.
22 * Contributor(s):
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,
50 PRBool aSeeking) :
51 mDecoder(aDecoder),
52 mIntervalStart(0),
53 mIntervalEnd(0),
54 mTotalBytes(0),
55 mSeeking(aSeeking)
59 nsresult nsChannelToPipeListener::Init()
61 nsresult rv = NS_NewPipe(getter_AddRefs(mInput),
62 getter_AddRefs(mOutput),
63 0,
64 PR_UINT32_MAX);
65 NS_ENSURE_SUCCESS(rv, rv);
67 return NS_OK;
70 void nsChannelToPipeListener::Stop()
72 mDecoder = nsnull;
73 mInput = nsnull;
74 mOutput = nsnull;
77 void nsChannelToPipeListener::Cancel()
79 if (mOutput)
80 mOutput->Close();
82 if (mInput)
83 mInput->Close();
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);
94 return NS_OK;
97 nsresult nsChannelToPipeListener::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
99 mIntervalStart = PR_IntervalNow();
100 mIntervalEnd = mIntervalStart;
101 mTotalBytes = 0;
102 mDecoder->UpdateBytesDownloaded(mTotalBytes);
103 nsCOMPtr<nsIHttpChannel> hc = do_QueryInterface(aRequest);
104 if (hc) {
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.
119 PRInt32 cl = 0;
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);
130 if (cc) {
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));
141 if (chan) {
142 nsCOMPtr<nsIScriptSecurityManager> secMan =
143 do_GetService("@mozilla.org/scriptsecuritymanager;1");
144 if (secMan) {
145 nsresult rv = secMan->GetChannelPrincipal(chan,
146 getter_AddRefs(mPrincipal));
147 if (NS_FAILED(rv)) {
148 return rv;
153 return NS_OK;
156 nsresult nsChannelToPipeListener::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext, nsresult aStatus)
158 mOutput = nsnull;
159 if (aStatus != NS_BINDING_ABORTED && mDecoder) {
160 if (NS_SUCCEEDED(aStatus)) {
161 mDecoder->ResourceLoaded();
162 } else {
163 mDecoder->NetworkError();
166 return NS_OK;
169 nsresult nsChannelToPipeListener::OnDataAvailable(nsIRequest* aRequest,
170 nsISupports* aContext,
171 nsIInputStream* aStream,
172 PRUint32 aOffset,
173 PRUint32 aCount)
175 if (!mOutput)
176 return NS_ERROR_FAILURE;
178 PRUint32 bytes = 0;
180 do {
181 nsresult rv = mOutput->WriteFrom(aStream, aCount, &bytes);
182 NS_ENSURE_SUCCESS(rv, rv);
184 aCount -= bytes;
185 mTotalBytes += bytes;
186 mDecoder->UpdateBytesDownloaded(mTotalBytes);
187 } while (aCount) ;
189 nsresult rv = mOutput->Flush();
190 NS_ENSURE_SUCCESS(rv, rv);
191 mIntervalEnd = PR_IntervalNow();
192 return NS_OK;
195 nsIPrincipal*
196 nsChannelToPipeListener::GetCurrentPrincipal()
198 return mPrincipal;
201 NS_IMPL_THREADSAFE_ISUPPORTS2(nsChannelToPipeListener, nsIRequestObserver, nsIStreamListener)