js/xpconnect/tests/components/native/xpctest_params.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "xpctest_private.h"
michael@0 6 #include "xpctest_interfaces.h"
michael@0 7 #include "js/Value.h"
michael@0 8
michael@0 9 NS_IMPL_ISUPPORTS(nsXPCTestParams, nsIXPCTestParams)
michael@0 10
michael@0 11 nsXPCTestParams::nsXPCTestParams()
michael@0 12 {
michael@0 13 }
michael@0 14
michael@0 15 nsXPCTestParams::~nsXPCTestParams()
michael@0 16 {
michael@0 17 }
michael@0 18
michael@0 19 #define GENERIC_METHOD_IMPL { \
michael@0 20 *_retval = *b; \
michael@0 21 *b = a; \
michael@0 22 return NS_OK; \
michael@0 23 }
michael@0 24
michael@0 25 #define STRING_METHOD_IMPL { \
michael@0 26 _retval.Assign(b); \
michael@0 27 b.Assign(a); \
michael@0 28 return NS_OK; \
michael@0 29 }
michael@0 30
michael@0 31 #define TAKE_OWNERSHIP_NOOP(val) {}
michael@0 32 #define TAKE_OWNERSHIP_INTERFACE(val) {static_cast<nsISupports*>(val)->AddRef();}
michael@0 33 #define TAKE_OWNERSHIP_STRING(val) { \
michael@0 34 nsDependentCString vprime(val); \
michael@0 35 val = ToNewCString(vprime); \
michael@0 36 }
michael@0 37 #define TAKE_OWNERSHIP_WSTRING(val) { \
michael@0 38 nsDependentString vprime(val); \
michael@0 39 val = ToNewUnicode(vprime); \
michael@0 40 }
michael@0 41
michael@0 42 // Macro for our buffer-oriented types:
michael@0 43 // 'type' is the type of element that the buffer contains.
michael@0 44 // 'padding' is an offset added to length, allowing us to handle
michael@0 45 // null-terminated strings.
michael@0 46 // 'TAKE_OWNERSHIP' is one of the macros above.
michael@0 47 #define BUFFER_METHOD_IMPL(type, padding, TAKE_OWNERSHIP) { \
michael@0 48 uint32_t elemSize = sizeof(type); \
michael@0 49 \
michael@0 50 /* Copy b into rv. */ \
michael@0 51 *rvLength = *bLength; \
michael@0 52 *rv = static_cast<type*>(NS_Alloc(elemSize * (*bLength + padding))); \
michael@0 53 if (!*rv) \
michael@0 54 return NS_ERROR_OUT_OF_MEMORY; \
michael@0 55 memcpy(*rv, *b, elemSize * (*bLength + padding)); \
michael@0 56 \
michael@0 57 /* Copy a into b. */ \
michael@0 58 *bLength = aLength; \
michael@0 59 NS_Free(*b); \
michael@0 60 *b = static_cast<type*>(NS_Alloc(elemSize * (aLength + padding))); \
michael@0 61 if (!*b) \
michael@0 62 return NS_ERROR_OUT_OF_MEMORY; \
michael@0 63 memcpy(*b, a, elemSize * (aLength + padding)); \
michael@0 64 \
michael@0 65 /* We need to take ownership of the data we got from a, \
michael@0 66 since the caller owns it. */ \
michael@0 67 for (unsigned i = 0; i < *bLength + padding; ++i) \
michael@0 68 TAKE_OWNERSHIP((*b)[i]); \
michael@0 69 \
michael@0 70 return NS_OK; \
michael@0 71 }
michael@0 72
michael@0 73 /* boolean testBoolean (in boolean a, inout boolean b); */
michael@0 74 NS_IMETHODIMP nsXPCTestParams::TestBoolean(bool a, bool *b, bool *_retval)
michael@0 75 {
michael@0 76 GENERIC_METHOD_IMPL;
michael@0 77 }
michael@0 78
michael@0 79 /* octet testOctet (in octet a, inout octet b); */
michael@0 80 NS_IMETHODIMP nsXPCTestParams::TestOctet(uint8_t a, uint8_t *b, uint8_t *_retval)
michael@0 81 {
michael@0 82 GENERIC_METHOD_IMPL;
michael@0 83 }
michael@0 84
michael@0 85 /* short testShort (in short a, inout short b); */
michael@0 86 NS_IMETHODIMP nsXPCTestParams::TestShort(int16_t a, int16_t *b, int16_t *_retval)
michael@0 87 {
michael@0 88 GENERIC_METHOD_IMPL;
michael@0 89 }
michael@0 90
michael@0 91 /* long testLong (in long a, inout long b); */
michael@0 92 NS_IMETHODIMP nsXPCTestParams::TestLong(int32_t a, int32_t *b, int32_t *_retval)
michael@0 93 {
michael@0 94 GENERIC_METHOD_IMPL;
michael@0 95 }
michael@0 96
michael@0 97 /* long long testLongLong (in long long a, inout long long b); */
michael@0 98 NS_IMETHODIMP nsXPCTestParams::TestLongLong(int64_t a, int64_t *b, int64_t *_retval)
michael@0 99 {
michael@0 100 GENERIC_METHOD_IMPL;
michael@0 101 }
michael@0 102
michael@0 103 /* unsigned short testUnsignedShort (in unsigned short a, inout unsigned short b); */
michael@0 104 NS_IMETHODIMP nsXPCTestParams::TestUnsignedShort(uint16_t a, uint16_t *b, uint16_t *_retval)
michael@0 105 {
michael@0 106 GENERIC_METHOD_IMPL;
michael@0 107 }
michael@0 108
michael@0 109 /* unsigned long testUnsignedLong (in unsigned long a, inout unsigned long b); */
michael@0 110 NS_IMETHODIMP nsXPCTestParams::TestUnsignedLong(uint32_t a, uint32_t *b, uint32_t *_retval)
michael@0 111 {
michael@0 112 GENERIC_METHOD_IMPL;
michael@0 113 }
michael@0 114
michael@0 115 /* unsigned long long testUnsignedLongLong (in unsigned long long a, inout unsigned long long b); */
michael@0 116 NS_IMETHODIMP nsXPCTestParams::TestUnsignedLongLong(uint64_t a, uint64_t *b, uint64_t *_retval)
michael@0 117 {
michael@0 118 GENERIC_METHOD_IMPL;
michael@0 119 }
michael@0 120
michael@0 121 /* float testFloat (in float a, inout float b); */
michael@0 122 NS_IMETHODIMP nsXPCTestParams::TestFloat(float a, float *b, float *_retval)
michael@0 123 {
michael@0 124 GENERIC_METHOD_IMPL;
michael@0 125 }
michael@0 126
michael@0 127 /* double testDouble (in double a, inout float b); */
michael@0 128 NS_IMETHODIMP nsXPCTestParams::TestDouble(double a, float *b, double *_retval)
michael@0 129 {
michael@0 130 GENERIC_METHOD_IMPL;
michael@0 131 }
michael@0 132
michael@0 133 /* char testChar (in char a, inout char b); */
michael@0 134 NS_IMETHODIMP nsXPCTestParams::TestChar(char a, char *b, char *_retval)
michael@0 135 {
michael@0 136 GENERIC_METHOD_IMPL;
michael@0 137 }
michael@0 138
michael@0 139 /* string testString (in string a, inout string b); */
michael@0 140 NS_IMETHODIMP nsXPCTestParams::TestString(const char * a, char * *b, char * *_retval)
michael@0 141 {
michael@0 142 nsDependentCString aprime(a);
michael@0 143 nsDependentCString bprime(*b);
michael@0 144 *_retval = ToNewCString(bprime);
michael@0 145 *b = ToNewCString(aprime);
michael@0 146
michael@0 147 // XPCOM ownership rules dictate that overwritten inout params must be callee-freed.
michael@0 148 // See https://developer.mozilla.org/en/XPIDL
michael@0 149 NS_Free(const_cast<char*>(bprime.get()));
michael@0 150
michael@0 151 return NS_OK;
michael@0 152 }
michael@0 153
michael@0 154 /* wchar testWchar (in wchar a, inout wchar b); */
michael@0 155 NS_IMETHODIMP nsXPCTestParams::TestWchar(char16_t a, char16_t *b, char16_t *_retval)
michael@0 156 {
michael@0 157 GENERIC_METHOD_IMPL;
michael@0 158 }
michael@0 159
michael@0 160 /* wstring testWstring (in wstring a, inout wstring b); */
michael@0 161 NS_IMETHODIMP nsXPCTestParams::TestWstring(const char16_t * a, char16_t * *b, char16_t * *_retval)
michael@0 162 {
michael@0 163 nsDependentString aprime(a);
michael@0 164 nsDependentString bprime(*b);
michael@0 165 *_retval = ToNewUnicode(bprime);
michael@0 166 *b = ToNewUnicode(aprime);
michael@0 167
michael@0 168 // XPCOM ownership rules dictate that overwritten inout params must be callee-freed.
michael@0 169 // See https://developer.mozilla.org/en/XPIDL
michael@0 170 NS_Free((void*)bprime.get());
michael@0 171
michael@0 172 return NS_OK;
michael@0 173 }
michael@0 174
michael@0 175 /* DOMString testDOMString (in DOMString a, inout DOMString b); */
michael@0 176 NS_IMETHODIMP nsXPCTestParams::TestDOMString(const nsAString & a, nsAString & b, nsAString & _retval)
michael@0 177 {
michael@0 178 STRING_METHOD_IMPL;
michael@0 179 }
michael@0 180
michael@0 181
michael@0 182 /* AString testAString (in AString a, inout AString b); */
michael@0 183 NS_IMETHODIMP nsXPCTestParams::TestAString(const nsAString & a, nsAString & b, nsAString & _retval)
michael@0 184 {
michael@0 185 STRING_METHOD_IMPL;
michael@0 186 }
michael@0 187
michael@0 188 /* AUTF8String testAUTF8String (in AUTF8String a, inout AUTF8String b); */
michael@0 189 NS_IMETHODIMP nsXPCTestParams::TestAUTF8String(const nsACString & a, nsACString & b, nsACString & _retval)
michael@0 190 {
michael@0 191 STRING_METHOD_IMPL;
michael@0 192 }
michael@0 193
michael@0 194 /* ACString testACString (in ACString a, inout ACString b); */
michael@0 195 NS_IMETHODIMP nsXPCTestParams::TestACString(const nsACString & a, nsACString & b, nsACString & _retval)
michael@0 196 {
michael@0 197 STRING_METHOD_IMPL;
michael@0 198 }
michael@0 199
michael@0 200 /* jsval testJsval (in jsval a, in jsval b); */
michael@0 201 NS_IMETHODIMP nsXPCTestParams::TestJsval(JS::Handle<JS::Value> a,
michael@0 202 JS::MutableHandle<JS::Value> b,
michael@0 203 JS::MutableHandle<JS::Value> _retval)
michael@0 204 {
michael@0 205 _retval.set(b);
michael@0 206 b.set(a);
michael@0 207 return NS_OK;
michael@0 208 }
michael@0 209
michael@0 210 /* void testShortArray (in unsigned long aLength, [array, size_is (aLength)] in short a,
michael@0 211 * inout unsigned long bLength, [array, size_is (bLength)] inout short b,
michael@0 212 * out unsigned long rvLength, [array, size_is (rvLength), retval] out short rv); */
michael@0 213 NS_IMETHODIMP nsXPCTestParams::TestShortArray(uint32_t aLength, int16_t *a,
michael@0 214 uint32_t *bLength, int16_t **b,
michael@0 215 uint32_t *rvLength, int16_t **rv)
michael@0 216 {
michael@0 217 BUFFER_METHOD_IMPL(int16_t, 0, TAKE_OWNERSHIP_NOOP);
michael@0 218 }
michael@0 219
michael@0 220 /* void testDoubleArray (in unsigned long aLength, [array, size_is (aLength)] in double a,
michael@0 221 * inout unsigned long bLength, [array, size_is (bLength)] inout double b,
michael@0 222 * out unsigned long rvLength, [array, size_is (rvLength), retval] out double rv); */
michael@0 223 NS_IMETHODIMP nsXPCTestParams::TestDoubleArray(uint32_t aLength, double *a,
michael@0 224 uint32_t *bLength, double **b,
michael@0 225 uint32_t *rvLength, double **rv)
michael@0 226 {
michael@0 227 BUFFER_METHOD_IMPL(double, 0, TAKE_OWNERSHIP_NOOP);
michael@0 228 }
michael@0 229
michael@0 230 /* void testStringArray (in unsigned long aLength, [array, size_is (aLength)] in string a,
michael@0 231 * inout unsigned long bLength, [array, size_is (bLength)] inout string b,
michael@0 232 * out unsigned long rvLength, [array, size_is (rvLength), retval] out string rv); */
michael@0 233 NS_IMETHODIMP nsXPCTestParams::TestStringArray(uint32_t aLength, const char * *a,
michael@0 234 uint32_t *bLength, char * **b,
michael@0 235 uint32_t *rvLength, char * **rv)
michael@0 236 {
michael@0 237 BUFFER_METHOD_IMPL(char*, 0, TAKE_OWNERSHIP_STRING);
michael@0 238 }
michael@0 239
michael@0 240 /* void testWstringArray (in unsigned long aLength, [array, size_is (aLength)] in wstring a,
michael@0 241 * inout unsigned long bLength, [array, size_is (bLength)] inout wstring b,
michael@0 242 * out unsigned long rvLength, [array, size_is (rvLength), retval] out wstring rv); */
michael@0 243 NS_IMETHODIMP nsXPCTestParams::TestWstringArray(uint32_t aLength, const char16_t * *a,
michael@0 244 uint32_t *bLength, char16_t * **b,
michael@0 245 uint32_t *rvLength, char16_t * **rv)
michael@0 246 {
michael@0 247 BUFFER_METHOD_IMPL(char16_t*, 0, TAKE_OWNERSHIP_WSTRING);
michael@0 248 }
michael@0 249
michael@0 250 /* void testInterfaceArray (in unsigned long aLength, [array, size_is (aLength)] in nsIXPCTestInterfaceA a,
michael@0 251 * inout unsigned long bLength, [array, size_is (bLength)] inout nsIXPCTestInterfaceA b,
michael@0 252 * out unsigned long rvLength, [array, size_is (rvLength), retval] out nsIXPCTestInterfaceA rv); */
michael@0 253 NS_IMETHODIMP nsXPCTestParams::TestInterfaceArray(uint32_t aLength, nsIXPCTestInterfaceA **a,
michael@0 254 uint32_t *bLength, nsIXPCTestInterfaceA * **b,
michael@0 255 uint32_t *rvLength, nsIXPCTestInterfaceA * **rv)
michael@0 256 {
michael@0 257 BUFFER_METHOD_IMPL(nsIXPCTestInterfaceA*, 0, TAKE_OWNERSHIP_INTERFACE);
michael@0 258 }
michael@0 259
michael@0 260 /* void testSizedString (in unsigned long aLength, [size_is (aLength)] in string a,
michael@0 261 * inout unsigned long bLength, [size_is (bLength)] inout string b,
michael@0 262 * out unsigned long rvLength, [size_is (rvLength), retval] out string rv); */
michael@0 263 NS_IMETHODIMP nsXPCTestParams::TestSizedString(uint32_t aLength, const char * a,
michael@0 264 uint32_t *bLength, char * *b,
michael@0 265 uint32_t *rvLength, char * *rv)
michael@0 266 {
michael@0 267 BUFFER_METHOD_IMPL(char, 1, TAKE_OWNERSHIP_NOOP);
michael@0 268 }
michael@0 269
michael@0 270 /* void testSizedWstring (in unsigned long aLength, [size_is (aLength)] in wstring a,
michael@0 271 * inout unsigned long bLength, [size_is (bLength)] inout wstring b,
michael@0 272 * out unsigned long rvLength, [size_is (rvLength), retval] out wstring rv); */
michael@0 273 NS_IMETHODIMP nsXPCTestParams::TestSizedWstring(uint32_t aLength, const char16_t * a,
michael@0 274 uint32_t *bLength, char16_t * *b,
michael@0 275 uint32_t *rvLength, char16_t * *rv)
michael@0 276 {
michael@0 277 BUFFER_METHOD_IMPL(char16_t, 1, TAKE_OWNERSHIP_NOOP);
michael@0 278 }
michael@0 279
michael@0 280 /* void testInterfaceIs (in nsIIDPtr aIID, [iid_is (aIID)] in nsQIResult a,
michael@0 281 * inout nsIIDPtr bIID, [iid_is (bIID)] inout nsQIResult b,
michael@0 282 * out nsIIDPtr rvIID, [iid_is (rvIID), retval] out nsQIResult rv); */
michael@0 283 NS_IMETHODIMP nsXPCTestParams::TestInterfaceIs(const nsIID *aIID, void *a,
michael@0 284 nsIID **bIID, void **b,
michael@0 285 nsIID **rvIID, void **rv)
michael@0 286 {
michael@0 287 //
michael@0 288 // Getting the buffers and ownership right here can be a little tricky.
michael@0 289 //
michael@0 290
michael@0 291 // The interface pointers are heap-allocated, and b has been AddRef'd
michael@0 292 // by XPConnect for the duration of the call. If we snatch it away from b
michael@0 293 // and leave no trace, XPConnect won't Release it. Since we also need to
michael@0 294 // return an already-AddRef'd pointer in rv, we don't need to do anything
michael@0 295 // special here.
michael@0 296 *rv = *b;
michael@0 297
michael@0 298 // rvIID is out-only, so nobody allocated an IID buffer for us. Do that now,
michael@0 299 // and store b's IID in the new buffer.
michael@0 300 *rvIID = static_cast<nsIID*>(NS_Alloc(sizeof(nsID)));
michael@0 301 if (!*rvIID)
michael@0 302 return NS_ERROR_OUT_OF_MEMORY;
michael@0 303 **rvIID = **bIID;
michael@0 304
michael@0 305 // Copy the interface pointer from a to b. Since a is in-only, XPConnect will
michael@0 306 // release it upon completion of the call. AddRef it for b.
michael@0 307 *b = a;
michael@0 308 static_cast<nsISupports*>(*b)->AddRef();
michael@0 309
michael@0 310 // We already had a buffer allocated for b's IID, so we can re-use it.
michael@0 311 **bIID = *aIID;
michael@0 312
michael@0 313 return NS_OK;
michael@0 314 }
michael@0 315
michael@0 316 /* void testInterfaceIsArray (in unsigned long aLength, in nsIIDPtr aIID,
michael@0 317 * [array, size_is (aLength), iid_is (aIID)] in nsQIResult a,
michael@0 318 * inout unsigned long bLength, inout nsIIDPtr bIID,
michael@0 319 * [array, size_is (bLength), iid_is (bIID)] inout nsQIResult b,
michael@0 320 * out unsigned long rvLength, out nsIIDPtr rvIID,
michael@0 321 * [retval, array, size_is (rvLength), iid_is (rvIID)] out nsQIResult rv); */
michael@0 322 NS_IMETHODIMP nsXPCTestParams::TestInterfaceIsArray(uint32_t aLength, const nsIID *aIID,
michael@0 323 void **a,
michael@0 324 uint32_t *bLength, nsIID **bIID,
michael@0 325 void ***b,
michael@0 326 uint32_t *rvLength, nsIID **rvIID,
michael@0 327 void ***rv)
michael@0 328 {
michael@0 329 // Transfer the IIDs. See the comments in TestInterfaceIs (above) for an
michael@0 330 // explanation of what we're doing.
michael@0 331 *rvIID = static_cast<nsIID*>(NS_Alloc(sizeof(nsID)));
michael@0 332 if (!*rvIID)
michael@0 333 return NS_ERROR_OUT_OF_MEMORY;
michael@0 334 **rvIID = **bIID;
michael@0 335 **bIID = *aIID;
michael@0 336
michael@0 337 // The macro is agnostic to the actual interface types, so we can re-use code here.
michael@0 338 //
michael@0 339 // Do this second, since the macro returns.
michael@0 340 BUFFER_METHOD_IMPL(void*, 0, TAKE_OWNERSHIP_INTERFACE);
michael@0 341 }

mercurial