storage/src/mozStorageStatementParams.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 /* -*- 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
michael@0 10 #include "jsapi.h"
michael@0 11
michael@0 12 #include "mozStoragePrivateHelpers.h"
michael@0 13 #include "mozStorageStatementParams.h"
michael@0 14 #include "mozIStorageStatement.h"
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17 namespace storage {
michael@0 18
michael@0 19 ////////////////////////////////////////////////////////////////////////////////
michael@0 20 //// StatementParams
michael@0 21
michael@0 22 StatementParams::StatementParams(mozIStorageStatement *aStatement) :
michael@0 23 mStatement(aStatement)
michael@0 24 {
michael@0 25 NS_ASSERTION(mStatement != nullptr, "mStatement is null");
michael@0 26 (void)mStatement->GetParameterCount(&mParamCount);
michael@0 27 }
michael@0 28
michael@0 29 NS_IMPL_ISUPPORTS(
michael@0 30 StatementParams,
michael@0 31 mozIStorageStatementParams,
michael@0 32 nsIXPCScriptable
michael@0 33 )
michael@0 34
michael@0 35 ////////////////////////////////////////////////////////////////////////////////
michael@0 36 //// nsIXPCScriptable
michael@0 37
michael@0 38 #define XPC_MAP_CLASSNAME StatementParams
michael@0 39 #define XPC_MAP_QUOTED_CLASSNAME "StatementParams"
michael@0 40 #define XPC_MAP_WANT_SETPROPERTY
michael@0 41 #define XPC_MAP_WANT_NEWENUMERATE
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 StatementParams::SetProperty(nsIXPConnectWrappedNative *aWrapper,
michael@0 48 JSContext *aCtx,
michael@0 49 JSObject *aScopeObj,
michael@0 50 jsid aId,
michael@0 51 JS::Value *_vp,
michael@0 52 bool *_retval)
michael@0 53 {
michael@0 54 NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
michael@0 55
michael@0 56 if (JSID_IS_INT(aId)) {
michael@0 57 int idx = JSID_TO_INT(aId);
michael@0 58
michael@0 59 nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp));
michael@0 60 NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED);
michael@0 61 nsresult rv = mStatement->BindByIndex(idx, variant);
michael@0 62 NS_ENSURE_SUCCESS(rv, rv);
michael@0 63 }
michael@0 64 else if (JSID_IS_STRING(aId)) {
michael@0 65 JSString *str = JSID_TO_STRING(aId);
michael@0 66 size_t length;
michael@0 67 const jschar *chars = JS_GetStringCharsAndLength(aCtx, str, &length);
michael@0 68 NS_ENSURE_TRUE(chars, NS_ERROR_UNEXPECTED);
michael@0 69 NS_ConvertUTF16toUTF8 name(chars, length);
michael@0 70
michael@0 71 // check to see if there's a parameter with this name
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 StatementParams::NewEnumerate(nsIXPConnectWrappedNative *aWrapper,
michael@0 87 JSContext *aCtx,
michael@0 88 JSObject *aScopeObj,
michael@0 89 uint32_t aEnumOp,
michael@0 90 jsval *_statep,
michael@0 91 jsid *_idp,
michael@0 92 bool *_retval)
michael@0 93 {
michael@0 94 NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
michael@0 95
michael@0 96 switch (aEnumOp) {
michael@0 97 case JSENUMERATE_INIT:
michael@0 98 case JSENUMERATE_INIT_ALL:
michael@0 99 {
michael@0 100 // Start our internal index at zero.
michael@0 101 *_statep = JSVAL_ZERO;
michael@0 102
michael@0 103 // And set our length, if needed.
michael@0 104 if (_idp)
michael@0 105 *_idp = INT_TO_JSID(mParamCount);
michael@0 106
michael@0 107 break;
michael@0 108 }
michael@0 109 case JSENUMERATE_NEXT:
michael@0 110 {
michael@0 111 NS_ASSERTION(*_statep != JSVAL_NULL, "Internal state is null!");
michael@0 112
michael@0 113 // Make sure we are in range first.
michael@0 114 uint32_t index = static_cast<uint32_t>(JSVAL_TO_INT(*_statep));
michael@0 115 if (index >= mParamCount) {
michael@0 116 *_statep = JSVAL_NULL;
michael@0 117 return NS_OK;
michael@0 118 }
michael@0 119
michael@0 120 // Get the name of our parameter.
michael@0 121 nsAutoCString name;
michael@0 122 nsresult rv = mStatement->GetParameterName(index, name);
michael@0 123 NS_ENSURE_SUCCESS(rv, rv);
michael@0 124
michael@0 125 // But drop the first character, which is going to be a ':'.
michael@0 126 JS::RootedString jsname(aCtx, ::JS_NewStringCopyN(aCtx, &(name.get()[1]),
michael@0 127 name.Length() - 1));
michael@0 128 NS_ENSURE_TRUE(jsname, NS_ERROR_OUT_OF_MEMORY);
michael@0 129
michael@0 130 // Set our name.
michael@0 131 JS::Rooted<jsid> id(aCtx);
michael@0 132 if (!::JS_StringToId(aCtx, jsname, &id)) {
michael@0 133 *_retval = false;
michael@0 134 return NS_OK;
michael@0 135 }
michael@0 136 *_idp = id;
michael@0 137
michael@0 138 // And increment our index.
michael@0 139 *_statep = INT_TO_JSVAL(++index);
michael@0 140
michael@0 141 break;
michael@0 142 }
michael@0 143 case JSENUMERATE_DESTROY:
michael@0 144 {
michael@0 145 // Clear our state.
michael@0 146 *_statep = JSVAL_NULL;
michael@0 147
michael@0 148 break;
michael@0 149 }
michael@0 150 }
michael@0 151
michael@0 152 return NS_OK;
michael@0 153 }
michael@0 154
michael@0 155 NS_IMETHODIMP
michael@0 156 StatementParams::NewResolve(nsIXPConnectWrappedNative *aWrapper,
michael@0 157 JSContext *aCtx,
michael@0 158 JSObject *aScopeObj,
michael@0 159 jsid aId,
michael@0 160 JSObject **_objp,
michael@0 161 bool *_retval)
michael@0 162 {
michael@0 163 NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
michael@0 164 // We do not throw at any point after this unless our index is out of range
michael@0 165 // because we want to allow the prototype chain to be checked for the
michael@0 166 // property.
michael@0 167
michael@0 168 JS::RootedObject scope(aCtx, aScopeObj);
michael@0 169 JS::RootedId id(aCtx, aId);
michael@0 170 bool resolved = false;
michael@0 171 bool ok = true;
michael@0 172 if (JSID_IS_INT(id)) {
michael@0 173 uint32_t idx = JSID_TO_INT(id);
michael@0 174
michael@0 175 // Ensure that our index is within range. We do not care about the
michael@0 176 // prototype chain being checked here.
michael@0 177 if (idx >= mParamCount)
michael@0 178 return NS_ERROR_INVALID_ARG;
michael@0 179
michael@0 180 ok = ::JS_DefineElement(aCtx, scope, idx, JSVAL_VOID, nullptr,
michael@0 181 nullptr, JSPROP_ENUMERATE);
michael@0 182 resolved = true;
michael@0 183 }
michael@0 184 else if (JSID_IS_STRING(id)) {
michael@0 185 JSString *str = JSID_TO_STRING(id);
michael@0 186 size_t nameLength;
michael@0 187 const jschar *nameChars = JS_GetStringCharsAndLength(aCtx, str, &nameLength);
michael@0 188 NS_ENSURE_TRUE(nameChars, NS_ERROR_UNEXPECTED);
michael@0 189
michael@0 190 // Check to see if there's a parameter with this name, and if not, let
michael@0 191 // the rest of the prototype chain be checked.
michael@0 192 NS_ConvertUTF16toUTF8 name(nameChars, nameLength);
michael@0 193 uint32_t idx;
michael@0 194 nsresult rv = mStatement->GetParameterIndex(name, &idx);
michael@0 195 if (NS_SUCCEEDED(rv)) {
michael@0 196 ok = ::JS_DefinePropertyById(aCtx, scope, id, JSVAL_VOID, nullptr,
michael@0 197 nullptr, JSPROP_ENUMERATE);
michael@0 198 resolved = true;
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 *_retval = ok;
michael@0 203 *_objp = resolved && ok ? scope.get() : nullptr;
michael@0 204 return NS_OK;
michael@0 205 }
michael@0 206
michael@0 207 } // namespace storage
michael@0 208 } // namespace mozilla

mercurial