Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsMemory.h" |
michael@0 | 8 | #include "nsString.h" |
michael@0 | 9 | #include "nsCOMPtr.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "jsapi.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "mozStoragePrivateHelpers.h" |
michael@0 | 14 | #include "mozStorageAsyncStatement.h" |
michael@0 | 15 | #include "mozStorageAsyncStatementParams.h" |
michael@0 | 16 | #include "mozIStorageStatement.h" |
michael@0 | 17 | |
michael@0 | 18 | namespace mozilla { |
michael@0 | 19 | namespace storage { |
michael@0 | 20 | |
michael@0 | 21 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 22 | //// AsyncStatementParams |
michael@0 | 23 | |
michael@0 | 24 | AsyncStatementParams::AsyncStatementParams(AsyncStatement *aStatement) |
michael@0 | 25 | : mStatement(aStatement) |
michael@0 | 26 | { |
michael@0 | 27 | NS_ASSERTION(mStatement != nullptr, "mStatement is null"); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | NS_IMPL_ISUPPORTS( |
michael@0 | 31 | AsyncStatementParams |
michael@0 | 32 | , mozIStorageStatementParams |
michael@0 | 33 | , nsIXPCScriptable |
michael@0 | 34 | ) |
michael@0 | 35 | |
michael@0 | 36 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 37 | //// nsIXPCScriptable |
michael@0 | 38 | |
michael@0 | 39 | #define XPC_MAP_CLASSNAME AsyncStatementParams |
michael@0 | 40 | #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementParams" |
michael@0 | 41 | #define XPC_MAP_WANT_SETPROPERTY |
michael@0 | 42 | #define XPC_MAP_WANT_NEWRESOLVE |
michael@0 | 43 | #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE |
michael@0 | 44 | #include "xpc_map_end.h" |
michael@0 | 45 | |
michael@0 | 46 | NS_IMETHODIMP |
michael@0 | 47 | AsyncStatementParams::SetProperty( |
michael@0 | 48 | nsIXPConnectWrappedNative *aWrapper, |
michael@0 | 49 | JSContext *aCtx, |
michael@0 | 50 | JSObject *aScopeObj, |
michael@0 | 51 | jsid aId, |
michael@0 | 52 | JS::Value *_vp, |
michael@0 | 53 | bool *_retval |
michael@0 | 54 | ) |
michael@0 | 55 | { |
michael@0 | 56 | NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 57 | |
michael@0 | 58 | if (JSID_IS_INT(aId)) { |
michael@0 | 59 | int idx = JSID_TO_INT(aId); |
michael@0 | 60 | |
michael@0 | 61 | nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp)); |
michael@0 | 62 | NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED); |
michael@0 | 63 | nsresult rv = mStatement->BindByIndex(idx, variant); |
michael@0 | 64 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 65 | } |
michael@0 | 66 | else if (JSID_IS_STRING(aId)) { |
michael@0 | 67 | JSString *str = JSID_TO_STRING(aId); |
michael@0 | 68 | size_t length; |
michael@0 | 69 | const jschar *chars = JS_GetInternedStringCharsAndLength(str, &length); |
michael@0 | 70 | NS_ConvertUTF16toUTF8 name(chars, length); |
michael@0 | 71 | |
michael@0 | 72 | nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp)); |
michael@0 | 73 | NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED); |
michael@0 | 74 | nsresult rv = mStatement->BindByName(name, variant); |
michael@0 | 75 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 76 | } |
michael@0 | 77 | else { |
michael@0 | 78 | return NS_ERROR_INVALID_ARG; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | *_retval = true; |
michael@0 | 82 | return NS_OK; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | NS_IMETHODIMP |
michael@0 | 86 | AsyncStatementParams::NewResolve( |
michael@0 | 87 | nsIXPConnectWrappedNative *aWrapper, |
michael@0 | 88 | JSContext *aCtx, |
michael@0 | 89 | JSObject *aScopeObj, |
michael@0 | 90 | jsid aId, |
michael@0 | 91 | JSObject **_objp, |
michael@0 | 92 | bool *_retval |
michael@0 | 93 | ) |
michael@0 | 94 | { |
michael@0 | 95 | JS::Rooted<JSObject*> scopeObj(aCtx, aScopeObj); |
michael@0 | 96 | |
michael@0 | 97 | NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 98 | // We do not throw at any point after this because we want to allow the |
michael@0 | 99 | // prototype chain to be checked for the property. |
michael@0 | 100 | |
michael@0 | 101 | bool resolved = false; |
michael@0 | 102 | bool ok = true; |
michael@0 | 103 | if (JSID_IS_INT(aId)) { |
michael@0 | 104 | uint32_t idx = JSID_TO_INT(aId); |
michael@0 | 105 | // All indexes are good because we don't know how many parameters there |
michael@0 | 106 | // really are. |
michael@0 | 107 | ok = ::JS_DefineElement(aCtx, scopeObj, idx, JSVAL_VOID, nullptr, |
michael@0 | 108 | nullptr, 0); |
michael@0 | 109 | resolved = true; |
michael@0 | 110 | } |
michael@0 | 111 | else if (JSID_IS_STRING(aId)) { |
michael@0 | 112 | // We are unable to tell if there's a parameter with this name and so |
michael@0 | 113 | // we must assume that there is. This screws the rest of the prototype |
michael@0 | 114 | // chain, but people really shouldn't be depending on this anyways. |
michael@0 | 115 | ok = ::JS_DefinePropertyById(aCtx, scopeObj, aId, JSVAL_VOID, nullptr, |
michael@0 | 116 | nullptr, 0); |
michael@0 | 117 | resolved = true; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | *_retval = ok; |
michael@0 | 121 | *_objp = resolved && ok ? scopeObj.get() : nullptr; |
michael@0 | 122 | return NS_OK; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | } // namespace storage |
michael@0 | 126 | } // namespace mozilla |