Bug 1611596 [wpt PR 21420] - Fix buggy testcase font-variant-position-01.html, a...
[gecko.git] / storage / mozStoragePrivateHelpers.cpp
blob1a5d478f1a37bc3fd1a4f7cda03c19a40e426e32
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 "sqlite3.h"
9 #include "jsfriendapi.h"
11 #include "nsPrintfCString.h"
12 #include "nsString.h"
13 #include "nsError.h"
14 #include "mozilla/Mutex.h"
15 #include "mozilla/CondVar.h"
16 #include "nsQueryObject.h"
17 #include "nsThreadUtils.h"
18 #include "nsJSUtils.h"
20 #include "Variant.h"
21 #include "mozStoragePrivateHelpers.h"
22 #include "mozIStorageCompletionCallback.h"
24 #include "mozilla/Logging.h"
25 extern mozilla::LazyLogModule gStorageLog;
27 namespace mozilla {
28 namespace storage {
30 nsresult convertResultCode(int aSQLiteResultCode) {
31 // Drop off the extended result bits of the result code.
32 int rc = aSQLiteResultCode & 0xFF;
34 switch (rc) {
35 case SQLITE_OK:
36 case SQLITE_ROW:
37 case SQLITE_DONE:
38 return NS_OK;
39 case SQLITE_CORRUPT:
40 case SQLITE_NOTADB:
41 return NS_ERROR_FILE_CORRUPTED;
42 case SQLITE_PERM:
43 case SQLITE_CANTOPEN:
44 return NS_ERROR_FILE_ACCESS_DENIED;
45 case SQLITE_BUSY:
46 return NS_ERROR_STORAGE_BUSY;
47 case SQLITE_LOCKED:
48 return NS_ERROR_FILE_IS_LOCKED;
49 case SQLITE_READONLY:
50 return NS_ERROR_FILE_READ_ONLY;
51 case SQLITE_IOERR:
52 return NS_ERROR_STORAGE_IOERR;
53 case SQLITE_FULL:
54 case SQLITE_TOOBIG:
55 return NS_ERROR_FILE_NO_DEVICE_SPACE;
56 case SQLITE_NOMEM:
57 return NS_ERROR_OUT_OF_MEMORY;
58 case SQLITE_MISUSE:
59 return NS_ERROR_UNEXPECTED;
60 case SQLITE_ABORT:
61 case SQLITE_INTERRUPT:
62 return NS_ERROR_ABORT;
63 case SQLITE_CONSTRAINT:
64 return NS_ERROR_STORAGE_CONSTRAINT;
67 // generic error
68 #ifdef DEBUG
69 nsAutoCString message;
70 message.AppendLiteral("SQLite returned error code ");
71 message.AppendInt(rc);
72 message.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
73 NS_WARNING_ASSERTION(rc == SQLITE_ERROR, message.get());
74 #endif
75 return NS_ERROR_FAILURE;
78 void checkAndLogStatementPerformance(sqlite3_stmt* aStatement) {
79 // Check to see if the query performed sorting operations or not. If it
80 // did, it may need to be optimized!
81 int count = ::sqlite3_stmt_status(aStatement, SQLITE_STMTSTATUS_SORT, 1);
82 if (count <= 0) return;
84 const char* sql = ::sqlite3_sql(aStatement);
86 // Check to see if this is marked to not warn
87 if (::strstr(sql, "/* do not warn (bug ")) return;
89 // CREATE INDEX always sorts (sorting is a necessary step in creating
90 // an index). So ignore the warning there.
91 if (::strstr(sql, "CREATE INDEX") || ::strstr(sql, "CREATE UNIQUE INDEX"))
92 return;
94 nsAutoCString message("Suboptimal indexes for the SQL statement ");
95 #ifdef MOZ_STORAGE_SORTWARNING_SQL_DUMP
96 message.Append('`');
97 message.Append(sql);
98 message.AppendLiteral("` [");
99 message.AppendInt(count);
100 message.AppendLiteral(" sort operation(s)]");
101 #else
102 nsPrintfCString address("0x%p", aStatement);
103 message.Append(address);
104 #endif
105 message.AppendLiteral(" (http://mzl.la/1FuID0j).");
106 NS_WARNING(message.get());
109 nsIVariant* convertJSValToVariant(JSContext* aCtx, const JS::Value& aValue) {
110 if (aValue.isInt32()) return new IntegerVariant(aValue.toInt32());
112 if (aValue.isDouble()) return new FloatVariant(aValue.toDouble());
114 if (aValue.isString()) {
115 nsAutoJSString value;
116 if (!value.init(aCtx, aValue.toString())) return nullptr;
117 return new TextVariant(value);
120 if (aValue.isBoolean()) return new IntegerVariant(aValue.isTrue() ? 1 : 0);
122 if (aValue.isNull()) return new NullVariant();
124 if (aValue.isObject()) {
125 JS::Rooted<JSObject*> obj(aCtx, &aValue.toObject());
126 // We only support Date instances, all others fail.
127 bool valid;
128 if (!js::DateIsValid(aCtx, obj, &valid) || !valid) return nullptr;
130 double msecd;
131 if (!js::DateGetMsecSinceEpoch(aCtx, obj, &msecd)) return nullptr;
133 msecd *= 1000.0;
134 int64_t msec = msecd;
136 return new IntegerVariant(msec);
139 return nullptr;
142 Variant_base* convertVariantToStorageVariant(nsIVariant* aVariant) {
143 RefPtr<Variant_base> variant = do_QueryObject(aVariant);
144 if (variant) {
145 // JS helpers already convert the JS representation to a Storage Variant,
146 // in such a case there's nothing left to do here, so just pass-through.
147 return variant;
150 if (!aVariant) return new NullVariant();
152 uint16_t dataType = aVariant->GetDataType();
154 switch (dataType) {
155 case nsIDataType::VTYPE_BOOL:
156 case nsIDataType::VTYPE_INT8:
157 case nsIDataType::VTYPE_INT16:
158 case nsIDataType::VTYPE_INT32:
159 case nsIDataType::VTYPE_UINT8:
160 case nsIDataType::VTYPE_UINT16:
161 case nsIDataType::VTYPE_UINT32:
162 case nsIDataType::VTYPE_INT64:
163 case nsIDataType::VTYPE_UINT64: {
164 int64_t v;
165 nsresult rv = aVariant->GetAsInt64(&v);
166 NS_ENSURE_SUCCESS(rv, nullptr);
167 return new IntegerVariant(v);
169 case nsIDataType::VTYPE_FLOAT:
170 case nsIDataType::VTYPE_DOUBLE: {
171 double v;
172 nsresult rv = aVariant->GetAsDouble(&v);
173 NS_ENSURE_SUCCESS(rv, nullptr);
174 return new FloatVariant(v);
176 case nsIDataType::VTYPE_CHAR:
177 case nsIDataType::VTYPE_CHAR_STR:
178 case nsIDataType::VTYPE_STRING_SIZE_IS:
179 case nsIDataType::VTYPE_UTF8STRING:
180 case nsIDataType::VTYPE_CSTRING: {
181 nsCString v;
182 nsresult rv = aVariant->GetAsAUTF8String(v);
183 NS_ENSURE_SUCCESS(rv, nullptr);
184 return new UTF8TextVariant(v);
186 case nsIDataType::VTYPE_WCHAR:
187 case nsIDataType::VTYPE_WCHAR_STR:
188 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
189 case nsIDataType::VTYPE_ASTRING: {
190 nsString v;
191 nsresult rv = aVariant->GetAsAString(v);
192 NS_ENSURE_SUCCESS(rv, nullptr);
193 return new TextVariant(v);
195 case nsIDataType::VTYPE_ARRAY: {
196 uint16_t type;
197 nsIID iid;
198 uint32_t len;
199 void* rawArray;
200 // Note this copies the array data.
201 nsresult rv = aVariant->GetAsArray(&type, &iid, &len, &rawArray);
202 NS_ENSURE_SUCCESS(rv, nullptr);
203 if (type == nsIDataType::VTYPE_UINT8) {
204 std::pair<uint8_t*, int> v(static_cast<uint8_t*>(rawArray), len);
205 // Take ownership of the data avoiding a further copy.
206 return new AdoptedBlobVariant(v);
208 [[fallthrough]];
210 case nsIDataType::VTYPE_EMPTY:
211 case nsIDataType::VTYPE_EMPTY_ARRAY:
212 case nsIDataType::VTYPE_VOID:
213 return new NullVariant();
214 case nsIDataType::VTYPE_ID:
215 case nsIDataType::VTYPE_INTERFACE:
216 case nsIDataType::VTYPE_INTERFACE_IS:
217 default:
218 NS_WARNING("Unsupported variant type");
219 return nullptr;
222 return nullptr;
225 namespace {
226 class CallbackEvent : public Runnable {
227 public:
228 explicit CallbackEvent(mozIStorageCompletionCallback* aCallback)
229 : Runnable("storage::CallbackEvent"), mCallback(aCallback) {}
231 NS_IMETHOD Run() override {
232 (void)mCallback->Complete(NS_OK, nullptr);
233 return NS_OK;
236 private:
237 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
239 } // namespace
240 already_AddRefed<nsIRunnable> newCompletionEvent(
241 mozIStorageCompletionCallback* aCallback) {
242 NS_ASSERTION(aCallback, "Passing a null callback is a no-no!");
243 nsCOMPtr<nsIRunnable> event = new CallbackEvent(aCallback);
244 return event.forget();
247 } // namespace storage
248 } // namespace mozilla