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 tw=79 ft=cpp: */
4 * Copyright (C) 2010 Sergey Yanovich <ynvich@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include "xpcom-config.h"
24 #include "nsComponentManagerUtils.h"
25 #include "nsStringAPI.h"
26 #include "nsISimpleEnumerator.h"
27 #include "nsIWebNavigation.h"
28 #include "nsIInterfaceRequestor.h"
29 #include "nsIInterfaceRequestorUtils.h"
30 #include "nsIRequest.h"
31 #include "nsILoadGroup.h"
33 #include "nsIInputStream.h"
34 #include "nsIRunnable.h"
37 #include "nsThreadUtils.h"
39 /* Project includes */
40 #include "aaLoadGroup.h"
42 class aaLoadEvent
: public nsIRunnable
45 aaLoadEvent(nsIWeakReference
*weakDocShell
, const PRUnichar
*aURI
,
46 PRUint32 aLoadFlags
, nsIURI
*aReferrer
, nsIInputStream
*aPostData
,
47 nsIInputStream
*aHeaders
)
48 :mDocShell(weakDocShell
), mURI(aURI
), mLoadFlags(aLoadFlags
)
49 ,mReferrer(aReferrer
), mPostData(aPostData
), mHeaders(aHeaders
)
55 const PRUnichar
*mURI
;
57 nsCOMPtr
<nsIURI
> mReferrer
;
58 nsCOMPtr
<nsIInputStream
> mPostData
;
59 nsCOMPtr
<nsIInputStream
> mHeaders
;
65 NS_IMPL_ISUPPORTS1(aaLoadEvent
,
72 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryReferent(mDocShell
, &rv
);
73 NS_ENSURE_SUCCESS(rv
, rv
);
75 rv
= webNav
->LoadURI(mURI
, mLoadFlags
, mReferrer
, mPostData
, mHeaders
);
76 NS_ENSURE_SUCCESS(rv
, rv
);
81 aaLoadGroup::aaLoadGroup(nsIDocShell
*docShell
)
84 mDocShell
= do_GetWeakReference(docShell
, &mStatus
);
85 if (NS_FAILED(mStatus
)) return;
87 checkPresentation(docShell
);
90 NS_IMPL_ISUPPORTS3(aaLoadGroup
,
93 nsISupportsWeakReference
)
96 aaLoadGroup::AddRequest(nsIRequest
*aRequest
, nsISupports
*aContext
)
99 return mContained
->AddRequest(aRequest
, aContext
);
103 aaLoadGroup::RemoveRequest(nsIRequest
*aRequest
, nsISupports
*aContext
,
106 NS_ENSURE_TRUE(mCount
, NS_ERROR_UNEXPECTED
);
109 rv
= mContained
->RemoveRequest(aRequest
, aContext
, aStatus
);
110 NS_ENSURE_SUCCESS(rv
, rv
);
115 rv
= NS_DispatchToCurrentThread(mTask
);
116 NS_ENSURE_SUCCESS(rv
, rv
);
121 /* Private methods */
123 aaLoadGroup::Init(const PRUnichar
*aURI
, PRUint32 aLoadFlags
,
124 nsIURI
*aReferrer
, nsIInputStream
*aPostData
, nsIInputStream
*aHeaders
)
126 NS_ENSURE_SUCCESS(mStatus
, mStatus
);
129 nsCOMPtr
<nsIRunnable
> task
= new aaLoadEvent(mDocShell
, aURI
, aLoadFlags
,
130 aReferrer
, aPostData
, aHeaders
);
131 NS_ENSURE_TRUE(task
, NS_ERROR_OUT_OF_MEMORY
);
139 NS_ENSURE_SUCCESS(rv
, rv
);
145 aaLoadGroup::checkPresentation(nsIDocShell
*docShell
)
152 nsCOMPtr
<nsIInterfaceRequestor
> rq
= do_QueryInterface(docShell
, &rv
);
153 if (!rq
|| NS_FAILED(rv
))
156 nsCOMPtr
<nsILoadGroup
> loadGroup
= do_GetInterface(rq
, &rv
);
157 if (!loadGroup
|| NS_FAILED(rv
))
160 nsCOMPtr
<nsISimpleEnumerator
> requests
;
161 rv
= loadGroup
->GetRequests(getter_AddRefs(requests
));
162 if (!requests
|| NS_FAILED(rv
))
165 PRBool hasMore
= PR_FALSE
;
167 while (NS_SUCCEEDED(requests
->HasMoreElements(&hasMore
)) && hasMore
) {
168 nsCOMPtr
<nsISupports
> elem
;
169 requests
->GetNext(getter_AddRefs(elem
));
171 nsCOMPtr
<nsIRequest
> request
= do_QueryInterface(elem
);
173 mStatus
= request
->SetLoadGroup(this);
174 if (NS_FAILED(mStatus
))
181 mContained
= loadGroup
;