Bug 1732032 [wpt PR 30868] - fix: canShare() return false if not allowed to use,...
[gecko.git] / storage / mozStorageStatementRow.cpp
blob33c85430fecbb56cfe8b95eacbb1a282731663b6
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsMemory.h"
8 #include "nsString.h"
10 #include "mozilla/ErrorResult.h"
11 #include "mozilla/dom/MozStorageStatementRowBinding.h"
12 #include "mozStorageStatementRow.h"
13 #include "mozStorageStatement.h"
15 #include "jsapi.h"
16 #include "js/Array.h" // JS::NewArrayObject
17 #include "js/PropertyAndElement.h" // JS_DefineElement
18 #include "js/Value.h"
20 #include "xpc_make_class.h"
22 namespace mozilla {
23 namespace storage {
25 ////////////////////////////////////////////////////////////////////////////////
26 //// StatementRow
28 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StatementRow, mWindow)
30 NS_INTERFACE_TABLE_HEAD(StatementRow)
31 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
32 NS_INTERFACE_TABLE(StatementRow, nsISupports)
33 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(StatementRow)
34 NS_INTERFACE_MAP_END
36 NS_IMPL_CYCLE_COLLECTING_ADDREF(StatementRow)
37 NS_IMPL_CYCLE_COLLECTING_RELEASE(StatementRow)
39 StatementRow::StatementRow(nsPIDOMWindowInner* aWindow, Statement* aStatement)
40 : mWindow(aWindow), mStatement(aStatement) {}
42 JSObject* StatementRow::WrapObject(JSContext* aCx,
43 JS::Handle<JSObject*> aGivenProto) {
44 return dom::MozStorageStatementRow_Binding::Wrap(aCx, this, aGivenProto);
47 void StatementRow::NamedGetter(JSContext* aCx, const nsAString& aName,
48 bool& aFound,
49 JS::MutableHandle<JS::Value> aResult,
50 mozilla::ErrorResult& aRv) {
51 if (!mStatement) {
52 aRv.Throw(NS_ERROR_NOT_INITIALIZED);
53 return;
56 nsCString name = NS_ConvertUTF16toUTF8(aName);
58 uint32_t idx;
60 nsresult rv = mStatement->GetColumnIndex(name, &idx);
61 if (NS_FAILED(rv)) {
62 // It's highly likely that the name doesn't exist, so let the JS engine
63 // check the prototype chain and throw if that doesn't have the property
64 // either.
65 aFound = false;
66 return;
70 int32_t type;
71 aRv = mStatement->GetTypeOfIndex(idx, &type);
72 if (aRv.Failed()) {
73 return;
76 switch (type) {
77 case mozIStorageValueArray::VALUE_TYPE_INTEGER:
78 case mozIStorageValueArray::VALUE_TYPE_FLOAT: {
79 double dval;
80 aRv = mStatement->GetDouble(idx, &dval);
81 if (aRv.Failed()) {
82 return;
84 aResult.set(::JS_NumberValue(dval));
85 break;
87 case mozIStorageValueArray::VALUE_TYPE_TEXT: {
88 uint32_t bytes;
89 const char16_t* sval = reinterpret_cast<const char16_t*>(
90 static_cast<mozIStorageStatement*>(mStatement)
91 ->AsSharedWString(idx, &bytes));
92 JSString* str =
93 ::JS_NewUCStringCopyN(aCx, sval, bytes / sizeof(char16_t));
94 if (!str) {
95 aRv.Throw(NS_ERROR_UNEXPECTED);
96 return;
98 aResult.setString(str);
99 break;
101 case mozIStorageValueArray::VALUE_TYPE_BLOB: {
102 uint32_t length;
103 const uint8_t* blob = static_cast<mozIStorageStatement*>(mStatement)
104 ->AsSharedBlob(idx, &length);
105 JS::Rooted<JSObject*> obj(aCx, JS::NewArrayObject(aCx, length));
106 if (!obj) {
107 aRv.Throw(NS_ERROR_UNEXPECTED);
108 return;
110 aResult.setObject(*obj);
112 // Copy the blob over to the JS array.
113 for (uint32_t i = 0; i < length; i++) {
114 if (!::JS_DefineElement(aCx, obj, i, blob[i], JSPROP_ENUMERATE)) {
115 aRv.Throw(NS_ERROR_UNEXPECTED);
116 return;
119 break;
121 case mozIStorageValueArray::VALUE_TYPE_NULL:
122 aResult.setNull();
123 break;
124 default:
125 NS_ERROR("unknown column type returned, what's going on?");
126 break;
128 aFound = true;
131 void StatementRow::GetSupportedNames(nsTArray<nsString>& aNames) {
132 if (!mStatement) {
133 return;
136 uint32_t columnCount;
137 nsresult rv = mStatement->GetColumnCount(&columnCount);
138 if (NS_WARN_IF(NS_FAILED(rv))) {
139 return;
142 for (uint32_t i = 0; i < columnCount; i++) {
143 nsAutoCString name;
144 nsresult rv = mStatement->GetColumnName(i, name);
145 if (NS_WARN_IF(NS_FAILED(rv))) {
146 return;
148 aNames.AppendElement(NS_ConvertUTF8toUTF16(name));
152 } // namespace storage
153 } // namespace mozilla