1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "xpctest_private.h"
#include "xpctest_interfaces.h"
#include "mozilla/Casting.h"
#include "js/Value.h"
#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsIURI.h"
using namespace mozilla;
NS_IMPL_ISUPPORTS(nsXPCTestParams, nsIXPCTestParams)
#define GENERIC_METHOD_IMPL \
{ \
*_retval = *b; \
*b = a; \
return NS_OK; \
}
#define STRING_METHOD_IMPL \
{ \
_retval.Assign(b); \
b.Assign(a); \
return NS_OK; \
}
#define SEQUENCE_METHOD_IMPL(TAKE_OWNERSHIP) \
{ \
_retval = std::move(b); \
b = a.Clone(); \
for (uint32_t i = 0; i < b.Length(); ++i) TAKE_OWNERSHIP(b[i]); \
return NS_OK; \
}
#define TAKE_OWNERSHIP_NOOP(val) \
{}
#define TAKE_OWNERSHIP_INTERFACE(val) \
{ static_cast<nsISupports*>(val)->AddRef(); }
#define TAKE_OWNERSHIP_STRING(val) \
{ \
nsDependentCString vprime(val); \
val = ToNewCString(vprime); \
}
#define TAKE_OWNERSHIP_WSTRING(val) \
{ \
nsDependentString vprime(val); \
val = ToNewUnicode(vprime); \
}
// Macro for our buffer-oriented types:
// 'type' is the type of element that the buffer contains.
// 'padding' is an offset added to length, allowing us to handle
// null-terminated strings.
// 'TAKE_OWNERSHIP' is one of the macros above.
#define BUFFER_METHOD_IMPL(type, padding, TAKE_OWNERSHIP) \
{ \
uint32_t elemSize = sizeof(type); \
\
/* Copy b into rv. */ \
*rvLength = *bLength; \
*rv = static_cast<type*>(moz_xmalloc(elemSize * (*bLength + padding))); \
memcpy(*rv, *b, elemSize*(*bLength + padding)); \
\
/* Copy a into b. */ \
*bLength = aLength; \
free(*b); \
*b = static_cast<type*>(moz_xmalloc(elemSize * (aLength + padding))); \
memcpy(*b, a, elemSize*(aLength + padding)); \
\
/* We need to take ownership of the data we got from a, \
since the caller owns it. */ \
for (unsigned i = 0; i < *bLength + padding; ++i) TAKE_OWNERSHIP((*b)[i]); \
\
return NS_OK; \
}
NS_IMETHODIMP nsXPCTestParams::TestBoolean(bool a, bool* b, bool* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestOctet(uint8_t a, uint8_t* b,
uint8_t* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestShort(int16_t a, int16_t* b,
int16_t* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestLong(int32_t a, int32_t* b,
int32_t* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestLongLong(int64_t a, int64_t* b,
int64_t* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestUnsignedShort(uint16_t a, uint16_t* b,
uint16_t* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestUnsignedLong(uint32_t a, uint32_t* b,
uint32_t* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestUnsignedLongLong(uint64_t a, uint64_t* b,
uint64_t* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestFloat(float a, float* b, float* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestDouble(double a, float* b, double* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestChar(char a, char* b, char* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestString(const char* a, char** b,
char** _retval) {
nsDependentCString aprime(a);
nsDependentCString bprime(*b);
*_retval = ToNewCString(bprime);
*b = ToNewCString(aprime);
// XPCOM ownership rules dictate that overwritten inout params must be
// callee-freed. See https://developer.mozilla.org/en/XPIDL
free(const_cast<char*>(bprime.get()));
return NS_OK;
}
NS_IMETHODIMP nsXPCTestParams::TestWchar(char16_t a, char16_t* b,
char16_t* _retval) {
GENERIC_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestWstring(const char16_t* a, char16_t** b,
char16_t** _retval) {
nsDependentString aprime(a);
nsDependentString bprime(*b);
*_retval = ToNewUnicode(bprime);
*b = ToNewUnicode(aprime);
// XPCOM ownership rules dictate that overwritten inout params must be
// callee-freed. See https://developer.mozilla.org/en/XPIDL
free((void*)bprime.get());
return NS_OK;
}
NS_IMETHODIMP nsXPCTestParams::TestAString(const nsAString& a, nsAString& b,
nsAString& _retval) {
STRING_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestAUTF8String(const nsACString& a,
nsACString& b,
nsACString& _retval) {
STRING_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestACString(const nsACString& a, nsACString& b,
nsACString& _retval) {
STRING_METHOD_IMPL;
}
NS_IMETHODIMP nsXPCTestParams::TestJsval(JS::Handle<JS::Value> a,
JS::MutableHandle<JS::Value> b,
JS::MutableHandle<JS::Value> _retval) {
_retval.set(b);
b.set(a);
return NS_OK;
}
NS_IMETHODIMP nsXPCTestParams::TestShortArray(uint32_t aLength, int16_t* a,
uint32_t* bLength, int16_t** b,
uint32_t* rvLength,
int16_t** rv) {
BUFFER_METHOD_IMPL(int16_t, 0, TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP nsXPCTestParams::TestDoubleArray(uint32_t aLength, double* a,
uint32_t* bLength, double** b,
uint32_t* rvLength,
double** rv) {
BUFFER_METHOD_IMPL(double, 0, TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP nsXPCTestParams::TestByteArrayOptionalLength(uint8_t* a,
uint32_t aLength,
uint32_t* rv) {
*rv = aLength;
return NS_OK;
}
NS_IMETHODIMP nsXPCTestParams::TestStringArray(uint32_t aLength, const char** a,
uint32_t* bLength, char*** b,
uint32_t* rvLength, char*** rv) {
BUFFER_METHOD_IMPL(char*, 0, TAKE_OWNERSHIP_STRING);
}
NS_IMETHODIMP nsXPCTestParams::TestWstringArray(
uint32_t aLength, const char16_t** a, uint32_t* bLength, char16_t*** b,
uint32_t* rvLength, char16_t*** rv) {
BUFFER_METHOD_IMPL(char16_t*, 0, TAKE_OWNERSHIP_WSTRING);
}
NS_IMETHODIMP nsXPCTestParams::TestInterfaceArray(
uint32_t aLength, nsIXPCTestInterfaceA** a, uint32_t* bLength,
nsIXPCTestInterfaceA*** b, uint32_t* rvLength, nsIXPCTestInterfaceA*** rv) {
BUFFER_METHOD_IMPL(nsIXPCTestInterfaceA*, 0, TAKE_OWNERSHIP_INTERFACE);
}
NS_IMETHODIMP nsXPCTestParams::TestJsvalArray(uint32_t aLength, JS::Value* a,
uint32_t* bLength, JS::Value** b,
uint32_t* rvLength,
JS::Value** rv) {
BUFFER_METHOD_IMPL(JS::Value, 0, TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP nsXPCTestParams::TestSizedString(uint32_t aLength, const char* a,
uint32_t* bLength, char** b,
uint32_t* rvLength, char** rv) {
BUFFER_METHOD_IMPL(char, 1, TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP nsXPCTestParams::TestSizedWstring(uint32_t aLength,
const char16_t* a,
uint32_t* bLength, char16_t** b,
uint32_t* rvLength,
char16_t** rv) {
BUFFER_METHOD_IMPL(char16_t, 1, TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP nsXPCTestParams::TestInterfaceIs(const nsIID* aIID, void* a,
nsIID** bIID, void** b,
nsIID** rvIID, void** rv) {
//
// Getting the buffers and ownership right here can be a little tricky.
//
// The interface pointers are heap-allocated, and b has been AddRef'd
// by XPConnect for the duration of the call. If we snatch it away from b
// and leave no trace, XPConnect won't Release it. Since we also need to
// return an already-AddRef'd pointer in rv, we don't need to do anything
// special here.
*rv = *b;
// rvIID is out-only, so nobody allocated an IID buffer for us. Do that now,
// and store b's IID in the new buffer.
*rvIID = static_cast<nsIID*>(moz_xmalloc(sizeof(nsID)));
**rvIID = **bIID;
// Copy the interface pointer from a to b. Since a is in-only, XPConnect will
// release it upon completion of the call. AddRef it for b.
*b = a;
static_cast<nsISupports*>(*b)->AddRef();
// We already had a buffer allocated for b's IID, so we can re-use it.
**bIID = *aIID;
return NS_OK;
}
NS_IMETHODIMP nsXPCTestParams::TestInterfaceIsArray(
uint32_t aLength, const nsIID* aIID, void** a, uint32_t* bLength,
nsIID** bIID, void*** b, uint32_t* rvLength, nsIID** rvIID, void*** rv) {
// Transfer the IIDs. See the comments in TestInterfaceIs (above) for an
// explanation of what we're doing.
*rvIID = static_cast<nsIID*>(moz_xmalloc(sizeof(nsID)));
**rvIID = **bIID;
**bIID = *aIID;
// The macro is agnostic to the actual interface types, so we can re-use code
// here.
//
// Do this second, since the macro returns.
BUFFER_METHOD_IMPL(void*, 0, TAKE_OWNERSHIP_INTERFACE);
}
NS_IMETHODIMP nsXPCTestParams::TestOutAString(nsAString& o) {
o.AssignLiteral("out");
return NS_OK;
}
NS_IMETHODIMP nsXPCTestParams::TestStringArrayOptionalSize(const char** a,
uint32_t length,
nsACString& out) {
out.Truncate();
for (uint32_t i = 0; i < length; ++i) {
out.Append(a[i]);
}
return NS_OK;
}
NS_IMETHODIMP
nsXPCTestParams::TestShortSequence(const nsTArray<short>& a, nsTArray<short>& b,
nsTArray<short>& _retval) {
SEQUENCE_METHOD_IMPL(TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP
nsXPCTestParams::TestDoubleSequence(const nsTArray<double>& a,
nsTArray<double>& b,
nsTArray<double>& _retval) {
SEQUENCE_METHOD_IMPL(TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP
nsXPCTestParams::TestInterfaceSequence(
const nsTArray<RefPtr<nsIXPCTestInterfaceA>>& a,
nsTArray<RefPtr<nsIXPCTestInterfaceA>>& b,
nsTArray<RefPtr<nsIXPCTestInterfaceA>>& _retval) {
SEQUENCE_METHOD_IMPL(TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP
nsXPCTestParams::TestAStringSequence(const nsTArray<nsString>& a,
nsTArray<nsString>& b,
nsTArray<nsString>& _retval) {
SEQUENCE_METHOD_IMPL(TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP
nsXPCTestParams::TestACStringSequence(const nsTArray<nsCString>& a,
nsTArray<nsCString>& b,
nsTArray<nsCString>& _retval) {
SEQUENCE_METHOD_IMPL(TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP
nsXPCTestParams::TestJsvalSequence(const nsTArray<JS::Value>& a,
nsTArray<JS::Value>& b,
nsTArray<JS::Value>& _retval) {
SEQUENCE_METHOD_IMPL(TAKE_OWNERSHIP_NOOP);
}
NS_IMETHODIMP
nsXPCTestParams::TestSequenceSequence(const nsTArray<nsTArray<short>>& a,
nsTArray<nsTArray<short>>& b,
nsTArray<nsTArray<short>>& _retval) {
_retval = std::move(b);
for (const auto& element : a) {
b.AppendElement(element.Clone());
}
return NS_OK;
}
NS_IMETHODIMP
nsXPCTestParams::TestInterfaceIsSequence(const nsIID* aIID,
const nsTArray<void*>& a, nsIID** bIID,
nsTArray<void*>& b, nsIID** rvIID,
nsTArray<void*>& _retval) {
// Shuffle around our nsIIDs
*rvIID = (*bIID)->Clone();
*bIID = aIID->Clone();
// Perform the generic sequence shuffle.
SEQUENCE_METHOD_IMPL(TAKE_OWNERSHIP_INTERFACE);
}
NS_IMETHODIMP
nsXPCTestParams::TestOptionalSequence(const nsTArray<uint8_t>& aInArr,
nsTArray<uint8_t>& aReturnArr) {
aReturnArr = aInArr.Clone();
return NS_OK;
}
NS_IMETHODIMP
nsXPCTestParams::TestOmittedOptionalOut(nsIXPCTestParams* aJSObj,
nsIURI** aOut) {
MOZ_ASSERT(!(*aOut), "Unexpected value received");
// Call the js component, to check XPConnect won't crash when passing nullptr
// as the optional out parameter, and that the out object is built regardless.
nsresult rv;
// Invoke it directly passing nullptr.
rv = aJSObj->TestOmittedOptionalOut(nullptr, nullptr);
NS_ENSURE_SUCCESS(rv, rv);
// Also invoke it with a ref pointer.
nsCOMPtr<nsIURI> someURI;
rv = aJSObj->TestOmittedOptionalOut(nullptr, getter_AddRefs(someURI));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString spec;
rv = someURI->GetSpec(spec);
if (!spec.EqualsLiteral("http://example.com/")) {
return NS_ERROR_UNEXPECTED;
}
someURI.forget(aOut);
return NS_OK;
}
NS_IMETHODIMP
nsXPCTestParams::GetTestNaN(double* aResult) {
*aResult =
BitwiseCast<double>((uint64_t(JSVAL_TAG_OBJECT) << JSVAL_TAG_SHIFT) + 1);
return NS_OK;
}
|