1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsXBLSerialize.h"
39 : #include "nsDOMScriptObjectHolder.h"
40 : #include "nsContentUtils.h"
41 : #include "jsxdrapi.h"
42 :
43 : nsresult
44 0 : XBL_SerializeFunction(nsIScriptContext* aContext,
45 : nsIObjectOutputStream* aStream,
46 : JSObject* aFunctionObject)
47 : {
48 0 : JSContext* cx = aContext->GetNativeContext();
49 0 : JSXDRState *xdr = ::JS_XDRNewMem(cx, JSXDR_ENCODE);
50 0 : if (!xdr)
51 0 : return NS_ERROR_OUT_OF_MEMORY;
52 0 : xdr->userdata = static_cast<void*>(aStream);
53 :
54 0 : JSAutoRequest ar(cx);
55 : nsresult rv;
56 0 : if (!JS_XDRFunctionObject(xdr, &aFunctionObject)) {
57 0 : rv = NS_ERROR_FAILURE;
58 : } else {
59 : uint32_t size;
60 : const char* data = reinterpret_cast<const char*>
61 0 : (JS_XDRMemGetData(xdr, &size));
62 0 : NS_ASSERTION(data, "no decoded JSXDRState data!");
63 :
64 0 : rv = aStream->Write32(size);
65 0 : if (NS_SUCCEEDED(rv))
66 0 : rv = aStream->WriteBytes(data, size);
67 : }
68 :
69 0 : JS_XDRDestroy(xdr);
70 :
71 0 : return rv;
72 : }
73 :
74 : // static
75 : nsresult
76 0 : XBL_DeserializeFunction(nsIScriptContext* aContext,
77 : nsIObjectInputStream* aStream,
78 : JSObject** aFunctionObject)
79 : {
80 0 : *aFunctionObject = nsnull;
81 :
82 : PRUint32 size;
83 0 : nsresult rv = aStream->Read32(&size);
84 0 : if (NS_FAILED(rv))
85 0 : return rv;
86 :
87 : char* data;
88 0 : rv = aStream->ReadBytes(size, &data);
89 0 : if (NS_FAILED(rv))
90 0 : return rv;
91 :
92 0 : JSContext* cx = aContext->GetNativeContext();
93 0 : JSXDRState *xdr = JS_XDRNewMem(cx, JSXDR_DECODE);
94 0 : if (!xdr) {
95 0 : rv = NS_ERROR_OUT_OF_MEMORY;
96 : } else {
97 0 : xdr->userdata = static_cast<void*>(aStream);
98 0 : JSAutoRequest ar(cx);
99 0 : JS_XDRMemSetData(xdr, data, size);
100 :
101 0 : if (!JS_XDRFunctionObject(xdr, aFunctionObject)) {
102 0 : rv = NS_ERROR_FAILURE;
103 : }
104 :
105 : uint32_t junk;
106 0 : data = static_cast<char*>(JS_XDRMemGetData(xdr, &junk));
107 0 : JS_XDRMemSetData(xdr, NULL, 0);
108 0 : JS_XDRDestroy(xdr);
109 : }
110 :
111 0 : nsMemory::Free(data);
112 0 : NS_ENSURE_SUCCESS(rv, rv);
113 0 : return rv;
114 : }
|