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) 2008 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"
25 #include "nsAutoPtr.h"
26 #include "nsServiceManagerUtils.h"
27 #include "nsStringAPI.h"
29 #include "nsIStreamListener.h"
31 #include "nsIFileURL.h"
32 #include "nsINetUtil.h"
33 #include "nsComponentManagerUtils.h"
36 #include "nsThreadUtils.h"
37 #include "nsIRunnable.h"
38 #include "nsIThread.h"
40 /* Project includes */
42 #include "aaSqliteChannel.h"
43 #include "aaSqlitePump.h"
45 aaSqliteChannel::aaSqliteChannel(nsIURI
*aURI
)
49 NS_IMPL_THREADSAFE_ISUPPORTS2(aaSqliteChannel
,
55 aaSqliteChannel::GetName(nsACString
& aName
)
57 return NS_ERROR_NOT_IMPLEMENTED
;
61 aaSqliteChannel::IsPending(PRBool
*_retval NS_OUTPARAM
)
63 return NS_ERROR_NOT_IMPLEMENTED
;
67 aaSqliteChannel::GetStatus(nsresult
*aStatus
)
69 return NS_ERROR_NOT_IMPLEMENTED
;
73 aaSqliteChannel::Cancel(nsresult aStatus
)
75 return NS_ERROR_NOT_IMPLEMENTED
;
79 aaSqliteChannel::Suspend()
81 return NS_ERROR_NOT_IMPLEMENTED
;
85 aaSqliteChannel::Resume()
87 return NS_ERROR_NOT_IMPLEMENTED
;
91 aaSqliteChannel::GetLoadGroup(nsILoadGroup
* *aLoadGroup
)
93 return NS_ERROR_NOT_IMPLEMENTED
;
96 aaSqliteChannel::SetLoadGroup(nsILoadGroup
* aLoadGroup
)
98 return NS_ERROR_NOT_IMPLEMENTED
;
102 aaSqliteChannel::GetLoadFlags(nsLoadFlags
*aLoadFlags
)
104 return NS_ERROR_NOT_IMPLEMENTED
;
107 aaSqliteChannel::SetLoadFlags(nsLoadFlags aLoadFlags
)
109 return NS_ERROR_NOT_IMPLEMENTED
;
114 aaSqliteChannel::GetOriginalURI(nsIURI
* *aOriginalURI
)
116 NS_ENSURE_ARG_POINTER(aOriginalURI
);
117 NS_IF_ADDREF(*aOriginalURI
= mURI
);
121 aaSqliteChannel::SetOriginalURI(nsIURI
* aOriginalURI
)
123 return NS_ERROR_NOT_IMPLEMENTED
;
127 aaSqliteChannel::GetURI(nsIURI
* *aURI
)
129 NS_ENSURE_ARG_POINTER(aURI
);
130 NS_IF_ADDREF(*aURI
= mURI
);
135 aaSqliteChannel::GetOwner(nsISupports
* *aOwner
)
137 NS_ENSURE_ARG_POINTER(aOwner
);
138 NS_IF_ADDREF(*aOwner
= mOwner
);
142 aaSqliteChannel::SetOwner(nsISupports
* aOwner
)
149 aaSqliteChannel::GetNotificationCallbacks(nsIInterfaceRequestor
*
150 *aNotificationCallbacks
)
152 return NS_ERROR_NOT_IMPLEMENTED
;
155 aaSqliteChannel::SetNotificationCallbacks(nsIInterfaceRequestor
*
156 aNotificationCallbacks
)
158 return NS_ERROR_NOT_IMPLEMENTED
;
162 aaSqliteChannel::GetSecurityInfo(nsISupports
* *aSecurityInfo
)
164 return NS_ERROR_NOT_IMPLEMENTED
;
168 aaSqliteChannel::GetContentType(nsACString
& aContentType
)
170 return NS_ERROR_NOT_IMPLEMENTED
;
173 aaSqliteChannel::SetContentType(const nsACString
& aContentType
)
175 return NS_ERROR_NOT_IMPLEMENTED
;
179 aaSqliteChannel::GetContentCharset(nsACString
& aContentCharset
)
181 return NS_ERROR_NOT_IMPLEMENTED
;
184 aaSqliteChannel::SetContentCharset(const nsACString
& aContentCharset
)
186 return NS_ERROR_NOT_IMPLEMENTED
;
190 aaSqliteChannel::GetContentLength(PRInt32
*aContentLength
)
192 return NS_ERROR_NOT_IMPLEMENTED
;
195 aaSqliteChannel::SetContentLength(PRInt32 aContentLength
)
197 return NS_ERROR_NOT_IMPLEMENTED
;
201 aaSqliteChannel::Open(nsIInputStream
**_retval NS_OUTPARAM
)
203 return NS_ERROR_NOT_IMPLEMENTED
;
207 aaSqliteChannel::AsyncOpen(nsIStreamListener
*aListener
, nsISupports
*aContext
)
209 NS_ENSURE_ARG_POINTER(aListener
);
210 NS_ENSURE_STATE(mURI
);
213 nsCOMPtr
<nsIEventTarget
> worker
=
214 do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID
, &rv
);
215 NS_ENSURE_SUCCESS(rv
, rv
);
217 mObserver
= aListener
;
219 nsCOMPtr
<nsIURL
> url
= do_QueryInterface(mURI
, &rv
);
220 NS_ENSURE_SUCCESS(rv
, rv
);
223 rv
= url
->GetQuery(query
);
224 NS_ENSURE_SUCCESS(rv
, rv
);
226 NS_ENSURE_TRUE(StringBeginsWith(query
, NS_LITERAL_CSTRING("sql=")),
227 NS_ERROR_INVALID_ARG
);
228 const nsACString
& sqlQuery
= Substring(query
, 4);
230 nsCOMPtr
<nsINetUtil
> netUtil
= do_CreateInstance(NS_NETUTIL_CONTRACTID
, &rv
);
231 NS_ENSURE_SUCCESS(rv
, rv
);
234 rv
= netUtil
->UnescapeString(sqlQuery
, nsINetUtil::ESCAPE_XALPHAS
, sql
);
235 NS_ENSURE_SUCCESS(rv
, rv
);
237 nsCOMPtr
<nsIFileURL
> fileURL
= do_QueryInterface(mURI
, &rv
);
238 NS_ENSURE_SUCCESS(rv
, rv
);
240 nsCOMPtr
<nsIFile
> db
;
241 rv
= fileURL
->GetFile(getter_AddRefs(db
));
242 NS_ENSURE_SUCCESS(rv
, rv
);
244 nsRefPtr
<aaSqlitePump
> pump
= new aaSqlitePump(db
, sql
.get(), this);
245 NS_ENSURE_TRUE(pump
, NS_ERROR_OUT_OF_MEMORY
);
247 rv
= worker
->Dispatch(pump
, NS_DISPATCH_NORMAL
);
248 NS_ENSURE_SUCCESS(rv
, rv
);