transport: pass correct channel
[abstract.git] / view / aaLoadGroup.cpp
blob74bd72065a156fb988af932486b319e3c0cd0241
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: */
3 /*
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"
32 #include "nsIURI.h"
33 #include "nsIInputStream.h"
34 #include "nsIRunnable.h"
36 /* Unfrozen API */
37 #include "nsThreadUtils.h"
39 /* Project includes */
40 #include "aaLoadGroup.h"
42 class aaLoadEvent : public nsIRunnable
44 public:
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)
51 NS_DECL_ISUPPORTS
52 NS_DECL_NSIRUNNABLE
53 private:
54 nsWeakPtr mDocShell;
55 const PRUnichar *mURI;
56 PRUint32 mLoadFlags;
57 nsCOMPtr<nsIURI> mReferrer;
58 nsCOMPtr<nsIInputStream> mPostData;
59 nsCOMPtr<nsIInputStream> mHeaders;
60 private:
61 ~aaLoadEvent() {}
65 NS_IMPL_ISUPPORTS1(aaLoadEvent,
66 nsIRunnable)
68 NS_IMETHODIMP
69 aaLoadEvent::Run()
71 nsresult rv;
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);
78 return NS_OK;
81 aaLoadGroup::aaLoadGroup(nsIDocShell *docShell)
82 :mCount(0)
84 mDocShell = do_GetWeakReference(docShell, &mStatus);
85 if (NS_FAILED(mStatus)) return;
87 checkPresentation(docShell);
90 NS_IMPL_ISUPPORTS3(aaLoadGroup,
91 nsILoadGroup,
92 nsIRequest,
93 nsISupportsWeakReference)
94 /* nsILoadGroup */
95 NS_IMETHODIMP
96 aaLoadGroup::AddRequest(nsIRequest *aRequest, nsISupports *aContext)
98 mCount++;
99 return mContained->AddRequest(aRequest, aContext);
102 NS_IMETHODIMP
103 aaLoadGroup::RemoveRequest(nsIRequest *aRequest, nsISupports *aContext,
104 nsresult aStatus)
106 NS_ENSURE_TRUE(mCount, NS_ERROR_UNEXPECTED);
107 nsresult rv;
109 rv = mContained->RemoveRequest(aRequest, aContext, aStatus);
110 NS_ENSURE_SUCCESS(rv, rv);
112 if (--mCount > 0)
113 return NS_OK;
115 rv = NS_DispatchToCurrentThread(mTask);
116 NS_ENSURE_SUCCESS(rv, rv);
118 return NS_OK;
121 /* Private methods */
122 nsresult
123 aaLoadGroup::Init(const PRUnichar *aURI, PRUint32 aLoadFlags,
124 nsIURI *aReferrer, nsIInputStream *aPostData, nsIInputStream *aHeaders)
126 NS_ENSURE_SUCCESS(mStatus, mStatus);
127 nsresult rv;
129 nsCOMPtr<nsIRunnable> task = new aaLoadEvent(mDocShell, aURI, aLoadFlags,
130 aReferrer, aPostData, aHeaders);
131 NS_ENSURE_TRUE(task, NS_ERROR_OUT_OF_MEMORY);
133 if (mContained) {
134 mTask = task;
135 return NS_OK;
138 rv = task->Run();
139 NS_ENSURE_SUCCESS(rv, rv);
141 return NS_OK;
144 void
145 aaLoadGroup::checkPresentation(nsIDocShell *docShell)
147 nsresult rv;
149 if (!docShell)
150 return;
152 nsCOMPtr<nsIInterfaceRequestor> rq = do_QueryInterface(docShell, &rv);
153 if (!rq || NS_FAILED(rv))
154 return;
156 nsCOMPtr<nsILoadGroup> loadGroup = do_GetInterface(rq, &rv);
157 if (!loadGroup || NS_FAILED(rv))
158 return;
160 nsCOMPtr<nsISimpleEnumerator> requests;
161 rv = loadGroup->GetRequests(getter_AddRefs(requests));
162 if (!requests || NS_FAILED(rv))
163 return;
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);
172 if (request) {
173 mStatus = request->SetLoadGroup(this);
174 if (NS_FAILED(mStatus))
175 return;
177 mCount++;
180 if (mCount)
181 mContained = loadGroup;