storage/src/mozStorageStatementRow.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 "mozStorageStatementRow.h"
michael@0 11 #include "mozStorageStatement.h"
michael@0 12
michael@0 13 #include "jsapi.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16 namespace storage {
michael@0 17
michael@0 18 ////////////////////////////////////////////////////////////////////////////////
michael@0 19 //// StatementRow
michael@0 20
michael@0 21 StatementRow::StatementRow(Statement *aStatement)
michael@0 22 : mStatement(aStatement)
michael@0 23 {
michael@0 24 }
michael@0 25
michael@0 26 NS_IMPL_ISUPPORTS(
michael@0 27 StatementRow,
michael@0 28 mozIStorageStatementRow,
michael@0 29 nsIXPCScriptable
michael@0 30 )
michael@0 31
michael@0 32 ////////////////////////////////////////////////////////////////////////////////
michael@0 33 //// nsIXPCScriptable
michael@0 34
michael@0 35 #define XPC_MAP_CLASSNAME StatementRow
michael@0 36 #define XPC_MAP_QUOTED_CLASSNAME "StatementRow"
michael@0 37 #define XPC_MAP_WANT_GETPROPERTY
michael@0 38 #define XPC_MAP_WANT_NEWRESOLVE
michael@0 39 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
michael@0 40 #include "xpc_map_end.h"
michael@0 41
michael@0 42 NS_IMETHODIMP
michael@0 43 StatementRow::GetProperty(nsIXPConnectWrappedNative *aWrapper,
michael@0 44 JSContext *aCtx,
michael@0 45 JSObject *aScopeObj,
michael@0 46 jsid aId,
michael@0 47 jsval *_vp,
michael@0 48 bool *_retval)
michael@0 49 {
michael@0 50 NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
michael@0 51
michael@0 52 JS::RootedObject scope(aCtx, aScopeObj);
michael@0 53 if (JSID_IS_STRING(aId)) {
michael@0 54 ::JSAutoByteString idBytes(aCtx, JSID_TO_STRING(aId));
michael@0 55 NS_ENSURE_TRUE(!!idBytes, NS_ERROR_OUT_OF_MEMORY);
michael@0 56 nsDependentCString jsid(idBytes.ptr());
michael@0 57
michael@0 58 uint32_t idx;
michael@0 59 nsresult rv = mStatement->GetColumnIndex(jsid, &idx);
michael@0 60 NS_ENSURE_SUCCESS(rv, rv);
michael@0 61 int32_t type;
michael@0 62 rv = mStatement->GetTypeOfIndex(idx, &type);
michael@0 63 NS_ENSURE_SUCCESS(rv, rv);
michael@0 64
michael@0 65 if (type == mozIStorageValueArray::VALUE_TYPE_INTEGER ||
michael@0 66 type == mozIStorageValueArray::VALUE_TYPE_FLOAT) {
michael@0 67 double dval;
michael@0 68 rv = mStatement->GetDouble(idx, &dval);
michael@0 69 NS_ENSURE_SUCCESS(rv, rv);
michael@0 70 *_vp = ::JS_NumberValue(dval);
michael@0 71 }
michael@0 72 else if (type == mozIStorageValueArray::VALUE_TYPE_TEXT) {
michael@0 73 uint32_t bytes;
michael@0 74 const jschar *sval = reinterpret_cast<const jschar *>(
michael@0 75 static_cast<mozIStorageStatement *>(mStatement)->
michael@0 76 AsSharedWString(idx, &bytes)
michael@0 77 );
michael@0 78 JSString *str = ::JS_NewUCStringCopyN(aCtx, sval, bytes / 2);
michael@0 79 if (!str) {
michael@0 80 *_retval = false;
michael@0 81 return NS_OK;
michael@0 82 }
michael@0 83 *_vp = STRING_TO_JSVAL(str);
michael@0 84 }
michael@0 85 else if (type == mozIStorageValueArray::VALUE_TYPE_BLOB) {
michael@0 86 uint32_t length;
michael@0 87 const uint8_t *blob = static_cast<mozIStorageStatement *>(mStatement)->
michael@0 88 AsSharedBlob(idx, &length);
michael@0 89 JSObject *obj = ::JS_NewArrayObject(aCtx, length);
michael@0 90 if (!obj) {
michael@0 91 *_retval = false;
michael@0 92 return NS_OK;
michael@0 93 }
michael@0 94 *_vp = OBJECT_TO_JSVAL(obj);
michael@0 95
michael@0 96 // Copy the blob over to the JS array.
michael@0 97 for (uint32_t i = 0; i < length; i++) {
michael@0 98 if (!::JS_SetElement(aCtx, scope, i, blob[i])) {
michael@0 99 *_retval = false;
michael@0 100 return NS_OK;
michael@0 101 }
michael@0 102 }
michael@0 103 }
michael@0 104 else if (type == mozIStorageValueArray::VALUE_TYPE_NULL) {
michael@0 105 *_vp = JSVAL_NULL;
michael@0 106 }
michael@0 107 else {
michael@0 108 NS_ERROR("unknown column type returned, what's going on?");
michael@0 109 }
michael@0 110 }
michael@0 111
michael@0 112 return NS_OK;
michael@0 113 }
michael@0 114
michael@0 115 NS_IMETHODIMP
michael@0 116 StatementRow::NewResolve(nsIXPConnectWrappedNative *aWrapper,
michael@0 117 JSContext *aCtx,
michael@0 118 JSObject *aScopeObj,
michael@0 119 jsid aId,
michael@0 120 JSObject **_objp,
michael@0 121 bool *_retval)
michael@0 122 {
michael@0 123 JS::Rooted<JSObject*> scopeObj(aCtx, aScopeObj);
michael@0 124
michael@0 125 NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
michael@0 126 // We do not throw at any point after this because we want to allow the
michael@0 127 // prototype chain to be checked for the property.
michael@0 128
michael@0 129 if (JSID_IS_STRING(aId)) {
michael@0 130 ::JSAutoByteString idBytes(aCtx, JSID_TO_STRING(aId));
michael@0 131 NS_ENSURE_TRUE(!!idBytes, NS_ERROR_OUT_OF_MEMORY);
michael@0 132 nsDependentCString name(idBytes.ptr());
michael@0 133
michael@0 134 uint32_t idx;
michael@0 135 nsresult rv = mStatement->GetColumnIndex(name, &idx);
michael@0 136 if (NS_FAILED(rv)) {
michael@0 137 // It's highly likely that the name doesn't exist, so let the JS engine
michael@0 138 // check the prototype chain and throw if that doesn't have the property
michael@0 139 // either.
michael@0 140 *_objp = nullptr;
michael@0 141 return NS_OK;
michael@0 142 }
michael@0 143
michael@0 144 *_retval = ::JS_DefinePropertyById(aCtx, scopeObj, aId, JSVAL_VOID,
michael@0 145 nullptr, nullptr, 0);
michael@0 146 *_objp = scopeObj;
michael@0 147 return NS_OK;
michael@0 148 }
michael@0 149
michael@0 150 return NS_OK;
michael@0 151 }
michael@0 152
michael@0 153 } // namespace storage
michael@0 154 } // namescape mozilla

mercurial