transport: sqlite channel owner attribute
[abstract.git] / transport / sqlite / src / aaSqliteChannel.cpp
bloba40c38b7325392e70854fd62345fc37069207795
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) 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"
24 #include "nsCOMPtr.h"
25 #include "nsAutoPtr.h"
26 #include "nsServiceManagerUtils.h"
27 #include "nsStringAPI.h"
28 #include "nsNetCID.h"
29 #include "nsIStreamListener.h"
30 #include "nsIURL.h"
31 #include "nsIFileURL.h"
32 #include "nsINetUtil.h"
33 #include "nsComponentManagerUtils.h"
35 /* Unfrozen API */
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)
46 : mURI(aURI)
49 NS_IMPL_THREADSAFE_ISUPPORTS2(aaSqliteChannel,
50 nsIRequest,
51 nsIChannel)
53 /* nsIRequest */
54 NS_IMETHODIMP
55 aaSqliteChannel::GetName(nsACString & aName)
57 return NS_ERROR_NOT_IMPLEMENTED;
60 NS_IMETHODIMP
61 aaSqliteChannel::IsPending(PRBool *_retval NS_OUTPARAM)
63 return NS_ERROR_NOT_IMPLEMENTED;
66 NS_IMETHODIMP
67 aaSqliteChannel::GetStatus(nsresult *aStatus)
69 return NS_ERROR_NOT_IMPLEMENTED;
72 NS_IMETHODIMP
73 aaSqliteChannel::Cancel(nsresult aStatus)
75 return NS_ERROR_NOT_IMPLEMENTED;
78 NS_IMETHODIMP
79 aaSqliteChannel::Suspend()
81 return NS_ERROR_NOT_IMPLEMENTED;
84 NS_IMETHODIMP
85 aaSqliteChannel::Resume()
87 return NS_ERROR_NOT_IMPLEMENTED;
90 NS_IMETHODIMP
91 aaSqliteChannel::GetLoadGroup(nsILoadGroup * *aLoadGroup)
93 return NS_ERROR_NOT_IMPLEMENTED;
95 NS_IMETHODIMP
96 aaSqliteChannel::SetLoadGroup(nsILoadGroup * aLoadGroup)
98 return NS_ERROR_NOT_IMPLEMENTED;
101 NS_IMETHODIMP
102 aaSqliteChannel::GetLoadFlags(nsLoadFlags *aLoadFlags)
104 return NS_ERROR_NOT_IMPLEMENTED;
106 NS_IMETHODIMP
107 aaSqliteChannel::SetLoadFlags(nsLoadFlags aLoadFlags)
109 return NS_ERROR_NOT_IMPLEMENTED;
112 /* nsIChannel */
113 NS_IMETHODIMP
114 aaSqliteChannel::GetOriginalURI(nsIURI * *aOriginalURI)
116 NS_ENSURE_ARG_POINTER(aOriginalURI);
117 NS_IF_ADDREF(*aOriginalURI = mURI);
118 return NS_OK;
120 NS_IMETHODIMP
121 aaSqliteChannel::SetOriginalURI(nsIURI * aOriginalURI)
123 return NS_ERROR_NOT_IMPLEMENTED;
126 NS_IMETHODIMP
127 aaSqliteChannel::GetURI(nsIURI * *aURI)
129 NS_ENSURE_ARG_POINTER(aURI);
130 NS_IF_ADDREF(*aURI = mURI);
131 return NS_OK;
134 NS_IMETHODIMP
135 aaSqliteChannel::GetOwner(nsISupports * *aOwner)
137 NS_ENSURE_ARG_POINTER(aOwner);
138 NS_IF_ADDREF(*aOwner = mOwner);
139 return NS_OK;
141 NS_IMETHODIMP
142 aaSqliteChannel::SetOwner(nsISupports * aOwner)
144 mOwner = aOwner;
145 return NS_OK;
148 NS_IMETHODIMP
149 aaSqliteChannel::GetNotificationCallbacks(nsIInterfaceRequestor *
150 *aNotificationCallbacks)
152 return NS_ERROR_NOT_IMPLEMENTED;
154 NS_IMETHODIMP
155 aaSqliteChannel::SetNotificationCallbacks(nsIInterfaceRequestor *
156 aNotificationCallbacks)
158 return NS_ERROR_NOT_IMPLEMENTED;
161 NS_IMETHODIMP
162 aaSqliteChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
164 return NS_ERROR_NOT_IMPLEMENTED;
167 NS_IMETHODIMP
168 aaSqliteChannel::GetContentType(nsACString & aContentType)
170 return NS_ERROR_NOT_IMPLEMENTED;
172 NS_IMETHODIMP
173 aaSqliteChannel::SetContentType(const nsACString & aContentType)
175 return NS_ERROR_NOT_IMPLEMENTED;
178 NS_IMETHODIMP
179 aaSqliteChannel::GetContentCharset(nsACString & aContentCharset)
181 return NS_ERROR_NOT_IMPLEMENTED;
183 NS_IMETHODIMP
184 aaSqliteChannel::SetContentCharset(const nsACString & aContentCharset)
186 return NS_ERROR_NOT_IMPLEMENTED;
189 NS_IMETHODIMP
190 aaSqliteChannel::GetContentLength(PRInt32 *aContentLength)
192 return NS_ERROR_NOT_IMPLEMENTED;
194 NS_IMETHODIMP
195 aaSqliteChannel::SetContentLength(PRInt32 aContentLength)
197 return NS_ERROR_NOT_IMPLEMENTED;
200 NS_IMETHODIMP
201 aaSqliteChannel::Open(nsIInputStream **_retval NS_OUTPARAM)
203 return NS_ERROR_NOT_IMPLEMENTED;
206 NS_IMETHODIMP
207 aaSqliteChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports *aContext)
209 NS_ENSURE_ARG_POINTER(aListener);
210 NS_ENSURE_STATE(mURI);
211 nsresult rv;
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);
222 nsCAutoString query;
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);
233 nsCAutoString sql;
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);
250 return NS_OK;