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
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.
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"
45 #include "nsIIPCSerializable.h"
46 #include "nsIClassInfo.h"
47 #include "nsComponentManagerUtils.h"
48 #include "nsNetUtil.h"
49 #include "nsStringStream.h"
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.
59 URI() : mURI(nsnull
) {}
60 URI(nsIURI
* aURI
) : mURI(aURI
) {}
61 operator nsIURI
*() const { return mURI
.get(); }
63 friend struct ParamTraits
<URI
>;
69 nsCOMPtr
<nsIURI
> mURI
;
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
);
84 nsCOMPtr
<nsIIPCSerializable
> serializable
= do_QueryInterface(aParam
.mURI
);
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
);
97 nsCString spec
, charset
;
98 aParam
.mURI
->GetSpec(spec
);
99 aParam
.mURI
->GetOriginCharset(charset
);
100 WriteParam(aMsg
, spec
);
101 WriteParam(aMsg
, charset
);
105 nsCOMPtr
<nsIClassInfo
> classInfo
= do_QueryInterface(aParam
.mURI
);
106 char cidStr
[NSID_LENGTH
];
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
)
122 if (!ReadParam(aMsg
, aIter
, &isNull
))
125 aResult
->mURI
= nsnull
;
130 if (!ReadParam(aMsg
, aIter
, &isSerialized
))
133 nsCString spec
, charset
;
134 if (!ReadParam(aMsg
, aIter
, &spec
) ||
135 !ReadParam(aMsg
, aIter
, &charset
))
138 nsCOMPtr
<nsIURI
> uri
;
139 nsresult rv
= NS_NewURI(getter_AddRefs(uri
), spec
, charset
.get());
143 uri
.swap(aResult
->mURI
);
147 nsCAutoString cidStr
;
149 if (!ReadParam(aMsg
, aIter
, &cidStr
) ||
150 !cid
.Parse(cidStr
.get()))
153 nsCOMPtr
<nsIURI
> uri
= do_CreateInstance(cid
);
156 nsCOMPtr
<nsIIPCSerializable
> serializable
= do_QueryInterface(uri
);
157 if (!serializable
|| !serializable
->Read(aMsg
, aIter
))
160 uri
.swap(aResult
->mURI
);
164 static void Log(const paramType
& aParam
, std::wstring
* aLog
)
166 nsIURI
* uri
= aParam
.mURI
;
170 aLog
->append(StringPrintf(L
"[%s]", spec
.get()));
180 InputStream() : mStream(nsnull
) {}
181 InputStream(nsIInputStream
* aStream
) : mStream(aStream
) {}
182 operator nsIInputStream
*() const { return mStream
.get(); }
184 friend struct ParamTraits
<InputStream
>;
188 InputStream
& operator=(InputStream
&);
190 nsCOMPtr
<nsIInputStream
> mStream
;
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
);
206 nsCOMPtr
<nsIIPCSerializable
> serializable
= do_QueryInterface(aParam
.mStream
);
207 bool isSerializable
= !!serializable
;
208 WriteParam(aMsg
, isSerializable
);
211 NS_WARNING("nsIInputStream implementation doesn't support nsIIPCSerializable; falling back to copying data");
213 nsCString streamString
;
216 aParam
.mStream
->Available(&bytes
);
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
);
226 nsCOMPtr
<nsIClassInfo
> classInfo
= do_QueryInterface(aParam
.mStream
);
227 char cidStr
[NSID_LENGTH
];
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
)
240 if (!ReadParam(aMsg
, aIter
, &isNull
))
244 aResult
->mStream
= nsnull
;
249 if (!ReadParam(aMsg
, aIter
, &isSerializable
))
252 nsCOMPtr
<nsIInputStream
> stream
;
253 if (!isSerializable
) {
254 nsCString streamString
;
255 if (!ReadParam(aMsg
, aIter
, &streamString
))
258 nsresult rv
= NS_NewCStringInputStream(getter_AddRefs(stream
), streamString
);
262 nsCAutoString cidStr
;
264 if (!ReadParam(aMsg
, aIter
, &cidStr
) ||
265 !cid
.Parse(cidStr
.get()))
268 stream
= do_CreateInstance(cid
);
271 nsCOMPtr
<nsIIPCSerializable
> serializable
= do_QueryInterface(stream
);
272 if (!serializable
|| !serializable
->Read(aMsg
, aIter
))
276 stream
.swap(aResult
->mStream
);
281 // nsIPermissionManager utilities
285 nsCString host
, type
;
286 PRUint32 capability
, expireType
;
290 Permission(const nsCString
& aHost
,
291 const nsCString
& aType
,
292 const PRUint32 aCapability
,
293 const PRUint32 aExpireType
,
294 const PRInt64 aExpireTime
) : host(aHost
),
296 capability(aCapability
),
297 expireType(aExpireType
),
298 expireTime(aExpireTime
) { }
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