Backed out changeset 36e95068e103 (bug 1848160) for bc failures on browser_preference...
[gecko.git] / storage / mozStoragePrivateHelpers.cpp
blobffda8d51e8fb66e30960ad5ddbc18b1a4fee6011
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 bool isErrorCode(int aSQLiteResultCode) {
31 // Drop off the extended result bits of the result code.
32 int rc = aSQLiteResultCode & 0xFF;
34 return rc != SQLITE_OK && rc != SQLITE_ROW && rc != SQLITE_DONE;
37 nsresult convertResultCode(int aSQLiteResultCode) {
38 // Drop off the extended result bits of the result code.
39 int rc = aSQLiteResultCode & 0xFF;
41 switch (rc) {
42 case SQLITE_OK:
43 case SQLITE_ROW:
44 case SQLITE_DONE:
45 return NS_OK;
46 case SQLITE_CORRUPT:
47 case SQLITE_NOTADB:
48 return NS_ERROR_FILE_CORRUPTED;
49 case SQLITE_PERM:
50 case SQLITE_CANTOPEN:
51 return NS_ERROR_FILE_ACCESS_DENIED;
52 case SQLITE_BUSY:
53 return NS_ERROR_STORAGE_BUSY;
54 case SQLITE_LOCKED:
55 return NS_ERROR_FILE_IS_LOCKED;
56 case SQLITE_READONLY:
57 return NS_ERROR_FILE_READ_ONLY;
58 case SQLITE_IOERR:
59 return NS_ERROR_STORAGE_IOERR;
60 case SQLITE_FULL:
61 case SQLITE_TOOBIG:
62 return NS_ERROR_FILE_NO_DEVICE_SPACE;
63 case SQLITE_NOMEM:
64 return NS_ERROR_OUT_OF_MEMORY;
65 case SQLITE_MISUSE:
66 return NS_ERROR_UNEXPECTED;
67 case SQLITE_ABORT:
68 case SQLITE_INTERRUPT:
69 return NS_ERROR_ABORT;
70 case SQLITE_CONSTRAINT:
71 return NS_ERROR_STORAGE_CONSTRAINT;
74 // generic error
75 #ifdef DEBUG
76 nsAutoCString message;
77 message.AppendLiteral("SQLite returned error code ");
78 message.AppendInt(rc);
79 message.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
80 NS_WARNING_ASSERTION(rc == SQLITE_ERROR, message.get());
81 #endif
82 return NS_ERROR_FAILURE;
85 void checkAndLogStatementPerformance(sqlite3_stmt* aStatement) {
86 // Check to see if the query performed sorting operations or not. If it
87 // did, it may need to be optimized!
88 int count = ::sqlite3_stmt_status(aStatement, SQLITE_STMTSTATUS_SORT, 1);
89 if (count <= 0) return;
91 const char* sql = ::sqlite3_sql(aStatement);
93 // Check to see if this is marked to not warn
94 if (::strstr(sql, "/* do not warn (bug ")) return;
96 // CREATE INDEX always sorts (sorting is a necessary step in creating
97 // an index). So ignore the warning there.
98 if (::strstr(sql, "CREATE INDEX") || ::strstr(sql, "CREATE UNIQUE INDEX"))
99 return;
101 nsAutoCString message("Suboptimal indexes for the SQL statement ");
102 #ifdef MOZ_STORAGE_SORTWARNING_SQL_DUMP
103 message.Append('`');
104 message.Append(sql);
105 message.AppendLiteral("` [");
106 message.AppendInt(count);
107 message.AppendLiteral(" sort operation(s)]");
108 #else
109 nsPrintfCString address("0x%p", aStatement);
110 message.Append(address);
111 #endif
112 message.AppendLiteral(" (http://mzl.la/1FuID0j).");
113 NS_WARNING(message.get());
116 nsIVariant* convertJSValToVariant(JSContext* aCtx, const JS::Value& aValue) {
117 if (aValue.isInt32()) return new IntegerVariant(aValue.toInt32());
119 if (aValue.isDouble()) return new FloatVariant(aValue.toDouble());
121 if (aValue.isString()) {
122 nsAutoJSString value;
123 if (!value.init(aCtx, aValue.toString())) return nullptr;
124 return new TextVariant(value);
127 if (aValue.isBoolean()) return new IntegerVariant(aValue.isTrue() ? 1 : 0);
129 if (aValue.isNull()) return new NullVariant();
131 if (aValue.isObject()) {
132 JS::Rooted<JSObject*> obj(aCtx, &aValue.toObject());
133 // We only support Date instances, all others fail.
134 bool valid;
135 if (!js::DateIsValid(aCtx, obj, &valid) || !valid) return nullptr;
137 double msecd;
138 if (!js::DateGetMsecSinceEpoch(aCtx, obj, &msecd)) return nullptr;
140 msecd *= 1000.0;
141 int64_t msec = msecd;
143 return new IntegerVariant(msec);
146 return nullptr;
149 Variant_base* convertVariantToStorageVariant(nsIVariant* aVariant) {
150 RefPtr<Variant_base> variant = do_QueryObject(aVariant);
151 if (variant) {
152 // JS helpers already convert the JS representation to a Storage Variant,
153 // in such a case there's nothing left to do here, so just pass-through.
154 return variant;
157 if (!aVariant) return new NullVariant();
159 uint16_t dataType = aVariant->GetDataType();
161 switch (dataType) {
162 case nsIDataType::VTYPE_BOOL:
163 case nsIDataType::VTYPE_INT8:
164 case nsIDataType::VTYPE_INT16:
165 case nsIDataType::VTYPE_INT32:
166 case nsIDataType::VTYPE_UINT8:
167 case nsIDataType::VTYPE_UINT16:
168 case nsIDataType::VTYPE_UINT32:
169 case nsIDataType::VTYPE_INT64:
170 case nsIDataType::VTYPE_UINT64: {
171 int64_t v;
172 nsresult rv = aVariant->GetAsInt64(&v);
173 NS_ENSURE_SUCCESS(rv, nullptr);
174 return new IntegerVariant(v);
176 case nsIDataType::VTYPE_FLOAT:
177 case nsIDataType::VTYPE_DOUBLE: {
178 double v;
179 nsresult rv = aVariant->GetAsDouble(&v);
180 NS_ENSURE_SUCCESS(rv, nullptr);
181 return new FloatVariant(v);
183 case nsIDataType::VTYPE_CHAR:
184 case nsIDataType::VTYPE_CHAR_STR:
185 case nsIDataType::VTYPE_STRING_SIZE_IS:
186 case nsIDataType::VTYPE_UTF8STRING:
187 case nsIDataType::VTYPE_CSTRING: {
188 nsCString v;
189 nsresult rv = aVariant->GetAsAUTF8String(v);
190 NS_ENSURE_SUCCESS(rv, nullptr);
191 return new UTF8TextVariant(v);
193 case nsIDataType::VTYPE_WCHAR:
194 case nsIDataType::VTYPE_WCHAR_STR:
195 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
196 case nsIDataType::VTYPE_ASTRING: {
197 nsString v;
198 nsresult rv = aVariant->GetAsAString(v);
199 NS_ENSURE_SUCCESS(rv, nullptr);
200 return new TextVariant(v);
202 case nsIDataType::VTYPE_ARRAY: {
203 uint16_t type;
204 nsIID iid;
205 uint32_t len;
206 void* rawArray;
207 // Note this copies the array data.
208 nsresult rv = aVariant->GetAsArray(&type, &iid, &len, &rawArray);
209 NS_ENSURE_SUCCESS(rv, nullptr);
210 if (type == nsIDataType::VTYPE_UINT8) {
211 std::pair<uint8_t*, int> v(static_cast<uint8_t*>(rawArray), len);
212 // Take ownership of the data avoiding a further copy.
213 return new AdoptedBlobVariant(v);
215 [[fallthrough]];
217 case nsIDataType::VTYPE_EMPTY:
218 case nsIDataType::VTYPE_EMPTY_ARRAY:
219 case nsIDataType::VTYPE_VOID:
220 return new NullVariant();
221 case nsIDataType::VTYPE_ID:
222 case nsIDataType::VTYPE_INTERFACE:
223 case nsIDataType::VTYPE_INTERFACE_IS:
224 default:
225 NS_WARNING("Unsupported variant type");
226 return nullptr;
230 namespace {
231 class CallbackEvent : public Runnable {
232 public:
233 explicit CallbackEvent(mozIStorageCompletionCallback* aCallback)
234 : Runnable("storage::CallbackEvent"), mCallback(aCallback) {}
236 NS_IMETHOD Run() override {
237 (void)mCallback->Complete(NS_OK, nullptr);
238 return NS_OK;
241 private:
242 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
244 } // namespace
245 already_AddRefed<nsIRunnable> newCompletionEvent(
246 mozIStorageCompletionCallback* aCallback) {
247 NS_ASSERTION(aCallback, "Passing a null callback is a no-no!");
248 nsCOMPtr<nsIRunnable> event = new CallbackEvent(aCallback);
249 return event.forget();
252 } // namespace storage
253 } // namespace mozilla