Bug 601299: Find RegExpStatics in cx->globalObject if necessary. (r=mrbkap)
[mozilla-central.git] / netwerk / ipc / NeckoMessageUtils.h
blob400645e857bdb8915849c1783eba6ad8cf933fbc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * the Mozilla Foundation.
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Josh Matthews <josh@joshmatthews.net> (Initial Developer)
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 ***** */
39 #ifndef mozilla_net_NeckoMessageUtils_h
40 #define mozilla_net_NeckoMessageUtils_h
42 #include "IPC/IPCMessageUtils.h"
43 #include "nsStringGlue.h"
44 #include "nsIURI.h"
45 #include "nsIIPCSerializable.h"
46 #include "nsIClassInfo.h"
47 #include "nsComponentManagerUtils.h"
48 #include "nsNetUtil.h"
49 #include "nsStringStream.h"
51 namespace IPC {
53 // Since IPDL doesn't have any knowledge of pointers, there's no easy way to
54 // pass around nsIURI pointers. This is a very thin wrapper that IPDL can
55 // easily work with, allowing for conversion to and from an nsIURI pointer.
57 class URI {
58 public:
59 URI() : mURI(nsnull) {}
60 URI(nsIURI* aURI) : mURI(aURI) {}
61 operator nsIURI*() const { return mURI.get(); }
63 friend struct ParamTraits<URI>;
65 private:
66 // Unimplemented
67 URI& operator=(URI&);
69 nsCOMPtr<nsIURI> mURI;
72 template<>
73 struct ParamTraits<URI>
75 typedef URI paramType;
77 static void Write(Message* aMsg, const paramType& aParam)
79 bool isNull = !aParam.mURI;
80 WriteParam(aMsg, isNull);
81 if (isNull)
82 return;
84 nsCOMPtr<nsIIPCSerializable> serializable = do_QueryInterface(aParam.mURI);
85 if (!serializable) {
86 nsCString scheme;
87 aParam.mURI->GetScheme(scheme);
88 if (!scheme.EqualsASCII("about:") &&
89 !scheme.EqualsASCII("javascript:") &&
90 !scheme.EqualsASCII("javascript"))
91 NS_WARNING("All IPDL URIs must be serializable or an allowed scheme");
94 bool isSerialized = !!serializable;
95 WriteParam(aMsg, isSerialized);
96 if (!isSerialized) {
97 nsCString spec, charset;
98 aParam.mURI->GetSpec(spec);
99 aParam.mURI->GetOriginCharset(charset);
100 WriteParam(aMsg, spec);
101 WriteParam(aMsg, charset);
102 return;
105 nsCOMPtr<nsIClassInfo> classInfo = do_QueryInterface(aParam.mURI);
106 char cidStr[NSID_LENGTH];
107 nsCID cid;
108 #ifdef DEBUG
109 nsresult rv =
110 #endif
111 classInfo->GetClassIDNoAlloc(&cid);
112 NS_ABORT_IF_FALSE(NS_SUCCEEDED(rv), "All IPDL URIs must report a valid class ID");
114 cid.ToProvidedString(cidStr);
115 WriteParam(aMsg, nsCAutoString(cidStr));
116 serializable->Write(aMsg);
119 static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
121 bool isNull;
122 if (!ReadParam(aMsg, aIter, &isNull))
123 return false;
124 if (isNull) {
125 aResult->mURI = nsnull;
126 return true;
129 bool isSerialized;
130 if (!ReadParam(aMsg, aIter, &isSerialized))
131 return false;
132 if (!isSerialized) {
133 nsCString spec, charset;
134 if (!ReadParam(aMsg, aIter, &spec) ||
135 !ReadParam(aMsg, aIter, &charset))
136 return false;
138 nsCOMPtr<nsIURI> uri;
139 nsresult rv = NS_NewURI(getter_AddRefs(uri), spec, charset.get());
140 if (NS_FAILED(rv))
141 return false;
143 uri.swap(aResult->mURI);
144 return true;
147 nsCAutoString cidStr;
148 nsCID cid;
149 if (!ReadParam(aMsg, aIter, &cidStr) ||
150 !cid.Parse(cidStr.get()))
151 return false;
153 nsCOMPtr<nsIURI> uri = do_CreateInstance(cid);
154 if (!uri)
155 return false;
156 nsCOMPtr<nsIIPCSerializable> serializable = do_QueryInterface(uri);
157 if (!serializable || !serializable->Read(aMsg, aIter))
158 return false;
160 uri.swap(aResult->mURI);
161 return true;
164 static void Log(const paramType& aParam, std::wstring* aLog)
166 nsIURI* uri = aParam.mURI;
167 if (uri) {
168 nsCString spec;
169 uri->GetSpec(spec);
170 aLog->append(StringPrintf(L"[%s]", spec.get()));
172 else {
173 aLog->append(L"[]");
178 class InputStream {
179 public:
180 InputStream() : mStream(nsnull) {}
181 InputStream(nsIInputStream* aStream) : mStream(aStream) {}
182 operator nsIInputStream*() const { return mStream.get(); }
184 friend struct ParamTraits<InputStream>;
186 private:
187 // Unimplemented
188 InputStream& operator=(InputStream&);
190 nsCOMPtr<nsIInputStream> mStream;
193 template<>
194 struct ParamTraits<InputStream>
196 typedef InputStream paramType;
198 static void Write(Message* aMsg, const paramType& aParam)
200 bool isNull = !aParam.mStream;
201 aMsg->WriteBool(isNull);
203 if (isNull)
204 return;
206 nsCOMPtr<nsIIPCSerializable> serializable = do_QueryInterface(aParam.mStream);
207 bool isSerializable = !!serializable;
208 WriteParam(aMsg, isSerializable);
210 if (!serializable) {
211 NS_WARNING("nsIInputStream implementation doesn't support nsIIPCSerializable; falling back to copying data");
213 nsCString streamString;
214 PRUint32 bytes;
216 aParam.mStream->Available(&bytes);
217 if (bytes > 0) {
218 nsresult rv = NS_ReadInputStreamToString(aParam.mStream, streamString, bytes);
219 NS_ABORT_IF_FALSE(NS_SUCCEEDED(rv), "Can't read input stream into a string!");
222 WriteParam(aMsg, streamString);
223 return;
226 nsCOMPtr<nsIClassInfo> classInfo = do_QueryInterface(aParam.mStream);
227 char cidStr[NSID_LENGTH];
228 nsCID cid;
229 nsresult rv = classInfo->GetClassIDNoAlloc(&cid);
230 NS_ABORT_IF_FALSE(NS_SUCCEEDED(rv), "All IPDL streams must report a valid class ID");
232 cid.ToProvidedString(cidStr);
233 WriteParam(aMsg, nsCAutoString(cidStr));
234 serializable->Write(aMsg);
237 static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
239 bool isNull;
240 if (!ReadParam(aMsg, aIter, &isNull))
241 return false;
243 if (isNull) {
244 aResult->mStream = nsnull;
245 return true;
248 bool isSerializable;
249 if (!ReadParam(aMsg, aIter, &isSerializable))
250 return false;
252 nsCOMPtr<nsIInputStream> stream;
253 if (!isSerializable) {
254 nsCString streamString;
255 if (!ReadParam(aMsg, aIter, &streamString))
256 return false;
258 nsresult rv = NS_NewCStringInputStream(getter_AddRefs(stream), streamString);
259 if (NS_FAILED(rv))
260 return false;
261 } else {
262 nsCAutoString cidStr;
263 nsCID cid;
264 if (!ReadParam(aMsg, aIter, &cidStr) ||
265 !cid.Parse(cidStr.get()))
266 return false;
268 stream = do_CreateInstance(cid);
269 if (!stream)
270 return false;
271 nsCOMPtr<nsIIPCSerializable> serializable = do_QueryInterface(stream);
272 if (!serializable || !serializable->Read(aMsg, aIter))
273 return false;
276 stream.swap(aResult->mStream);
277 return true;
281 // nsIPermissionManager utilities
283 struct Permission
285 nsCString host, type;
286 PRUint32 capability, expireType;
287 PRInt64 expireTime;
289 Permission() { }
290 Permission(const nsCString& aHost,
291 const nsCString& aType,
292 const PRUint32 aCapability,
293 const PRUint32 aExpireType,
294 const PRInt64 aExpireTime) : host(aHost),
295 type(aType),
296 capability(aCapability),
297 expireType(aExpireType),
298 expireTime(aExpireTime) { }
301 template<>
302 struct ParamTraits<Permission>
304 static void Write(Message* aMsg, const Permission& aParam)
306 WriteParam(aMsg, aParam.host);
307 WriteParam(aMsg, aParam.type);
308 WriteParam(aMsg, aParam.capability);
309 WriteParam(aMsg, aParam.expireType);
310 WriteParam(aMsg, aParam.expireTime);
313 static bool Read(const Message* aMsg, void** aIter, Permission* aResult)
315 return ReadParam(aMsg, aIter, &aResult->host) &&
316 ReadParam(aMsg, aIter, &aResult->type) &&
317 ReadParam(aMsg, aIter, &aResult->capability) &&
318 ReadParam(aMsg, aIter, &aResult->expireType) &&
319 ReadParam(aMsg, aIter, &aResult->expireTime);
322 static void Log(const Permission& aParam, std::wstring* aLog)
324 aLog->append(StringPrintf(L"[%s]", aParam.host.get()));
330 #endif // mozilla_net_NeckoMessageUtils_h